It's supposed to be able to add two very large numbers... I believe my first function is correct, but my second one is so screwed up. I don't know what the heck to do... I'm so lost on where the values are going, if you could just explain what the heck I'm doing wrong and what I need to do, that would be great. :disgust:
int add2Digits( int a, int b )
{
int return_val;
__asm
{
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, a //int a is stored in eax
mov ebx, b //int b stored in ebx
add eax, ebx //sum stored in eax
add eax, edx //cary is added to sum
cmp al, 10 //checks to see if the sum is equal to 10
jle exitLoop //exits if 10 or less
xor edx, edx //clears out edx
mov edx, 1 //edx stores the carry
sub ax, 10 //subtracts 10 from ax so it stores only the last digit
exitLoop:
mov return_val, eax
}
return return_val; // return_val gets put into eax by the compiler
}
void add2BigNums( char* num1, char* num2, int len, char* result )
{
__asm
{
xor ebx, ebx
xor ecx, ecx
xor esi, esi
mov ebx, num1 //num1 is stored in ebx
mov ecx, num2 //num2 is stored in edx
mov edx, len
addNumbers:
mov bl, [ebx + esi]
mov cl, [ecx + esi]
sub bl, 48
sub cl, 48
push ebx //pushes the digit of num1 on the stack
push ecx //pushes the digit of num2 on the stack
call add2Digits
pop ecx
pop ebx
dec esi
push eax
cmp edx, 0
je carryCheck
dec esi
dec edx
cmp edx, 0
je carryCheck
jmp addNumbers
doneAdding:
mov eax, result
carryCheck:
cmp dx, 0
add dx, 48
jmp doneAdding
}
}
int add2Digits( int a, int b )
{
int return_val;
__asm
{
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, a //int a is stored in eax
mov ebx, b //int b stored in ebx
add eax, ebx //sum stored in eax
add eax, edx //cary is added to sum
cmp al, 10 //checks to see if the sum is equal to 10
jle exitLoop //exits if 10 or less
xor edx, edx //clears out edx
mov edx, 1 //edx stores the carry
sub ax, 10 //subtracts 10 from ax so it stores only the last digit
exitLoop:
mov return_val, eax
}
return return_val; // return_val gets put into eax by the compiler
}
void add2BigNums( char* num1, char* num2, int len, char* result )
{
__asm
{
xor ebx, ebx
xor ecx, ecx
xor esi, esi
mov ebx, num1 //num1 is stored in ebx
mov ecx, num2 //num2 is stored in edx
mov edx, len
addNumbers:
mov bl, [ebx + esi]
mov cl, [ecx + esi]
sub bl, 48
sub cl, 48
push ebx //pushes the digit of num1 on the stack
push ecx //pushes the digit of num2 on the stack
call add2Digits
pop ecx
pop ebx
dec esi
push eax
cmp edx, 0
je carryCheck
dec esi
dec edx
cmp edx, 0
je carryCheck
jmp addNumbers
doneAdding:
mov eax, result
carryCheck:
cmp dx, 0
add dx, 48
jmp doneAdding
}
}