Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char* addTwoStringNumbers(char *num1, int posNum1, char *num2, int posNum2)
  2. {
  3.     int posNum3 = 1+(posNum1>=posNum2?posNum1:posNum2);
  4.     char *resNum = new char[posNum3];
  5.     resNum[posNum3] = '\0';
  6.     __asm
  7.     {
  8.         push esi;
  9.         push edi;
  10.         push eax;
  11.         push ebx;
  12.         push ecx;
  13.         push edx;
  14.         // START CODING HERE
  15.         MOV EDI, posNum3;           // posNum3
  16.         MOV BH, byte ptr[num1];     // *num1
  17.         MOV BL, byte ptr[num2];     // *num2
  18.         MOV ECX, 0;                 // carry = 0
  19.         MOV DH, byte ptr[resNum];   // *resNum
  20.         MOV DL, 0;                  // currNum3 = 0
  21. WHILE:
  22.         MOV AH, 0;                  // currNum1 = 0
  23.         MOV AL, 0;                  // currNum2 = 0
  24.         MOV ESI, posNum1;
  25.         PUSH ESI;
  26.         CMP ESI, 0;                 // if posNum1 > 0
  27.         JG IF_1;
  28.         MOV ESI, posNum2;           // dont need posNum1 if it didnt jump
  29.         CMP ESI, 0;                 // if posNum2 > 0
  30.         JG IF_2;
  31. IF_1:
  32.         DEC ESI;                    // posNum1--
  33.         MOV AH, byte ptr[BH+ESI];   // currNum1 = num1[posNum1] - '0';
  34.         SUB AH, '0';
  35.         JMP CONT_WHILE;
  36. IF_2:
  37.         DEC ESI;                    // posNum2--
  38.         MOV AL, byte ptr[BL+ESI];   // currNum2 = num2[posNum2] - '0';
  39.         SUB AL, '0';
  40.         JMP CONT_WHILE;
  41.  
  42. CONT_WHILE:
  43.         ADD AH, AL;             // currNum1 = currNum1 + currNum2
  44.         ADD AH, ECX;            // currNum1 = currNum1 + carry
  45.         MOV DL, AH;             // currNum3 = currNum1
  46.         MOV ECX, '0';           // carry = 0
  47.         CMP DL, 10;             // currNum3 >= 10
  48.         JGE IF_3;
  49.         JMP END_WHILE;
  50. IF_3:
  51.         SUB DL, 10;             // currNum3 = currNum3 - 10;
  52.         MOV ECX, 0x31;          // carry = 1;
  53.  
  54. END_WHILE:
  55.         DEC EDI;                // posNum3 --
  56.         ADD DL, '0';            // resNum[posNum3] = currNum3 +'0';
  57.         MOV byte ptr[DH+EDI], DL;
  58.         CMP EDI, 0;             // check if posNum3 is >0
  59.         JG WHILE;      
  60.         // END CODING HERE
  61.         pop edx;
  62.         pop ecx;
  63.         pop ebx;
  64.         pop eax;
  65.         pop edi;
  66.         pop esi;
  67.     }
  68.     return resNum;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement