Help Me Correct My Assembly Code for 80x86 PLEASE!!!

Dragnov

Diamond Member
Apr 24, 2001
6,878
0
0
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

}
}

 

m0ti

Senior member
Jul 6, 2001
975
0
0
It's been a while but I'll try.

I'm not sure what compiler your working on, but are you sure that you have access to a and b STRAIGHT in assembly? This should usually be [bp+6] and that kind of stuff.

The stack:


OLDBP <----BP
Return Address
a
b


a is at [bp+4] and b is at [bp+6].

Further more, there's no reason do be doing digit by digit addition. You can check the size of the numbers you're working with by using sizeof(), and use that as your base for adding the numbers watching overflow/carry values as you go. It doesn't really matter though.

Have you tried testing your first function by itself?

Are you sure that you're starting at the right point in the summation in the second function (i.e. the proper digits)? Try it out with 1 digit numbers, and then 2 digits and so on.