Hi everyone. I'm having a little trouble trying to debug this code I made. What I am trying to do is add two numbers each up to 255 decimals.
What I did was put the numbers (input by the user) into a string, convert it to BCD, add them, convert it back to ASCII, and print the result. When I try to run my program it either crashes or gives me a random number.
Any help would be appreciated. Thanks.
This is my main function:
#include <stdio.h>
#include <stdlib.h>
extern void addLarge(char *number1, char *number2, char *result);
int main(int argc, char *argv[])
{
unsigned char number1[256];
unsigned char number2[256];
unsigned char result[256];
puts("number 1: ");
gets(number1);
puts("number 2: ");
gets(number2);
addLarge(number1, number2, result);
printf("The result is : %d\n", result);
system("PAUSE");
return 0;
}
This is the assembly function
.global _addLarge
_addLarge:
pushl %ebp
movl %esp, %ebp
call _rightalignfirst
call _rightalignsecond
call _convascii
call _addnumbers
call _convbcd
movl %ebp, %esp
popl %ebp
ret
.global _rightalignfirst
_rightalignfirst:
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx
xor %esi, %esi
xor %eax, %eax
movl $255, %edi
loop0:
cmpb $0, (%ebx, %esi, 1)
je loop1
inc %esi
movl %esi, %edx
cmpl $255, %esi
jge loop1
jmp loop0
loop1:
cmpl $-1, %esi
jle loop2
movb (%ebx, %esi, 1), %al
movb %al, (%ebx, %edi, 1)
dec %esi
dec %edi
jmp loop1
loop2:
movl $255, %edi
subl %edx, %edi
subl $2, %edi
loop3:
cmpl $-1, %edi
jle faldone
movb $0, (%ebx, %edi, 1)
dec %edi
jmp loop3
faldone:
popl %ebp
ret
.global _rightalignsecond
_rightalignsecond:
pushl %ebp
movl %esp, %ebp
movl 12(%ebp), %ecx
xor %esi, %esi
xor %eax, %eax
movl $255, %edi
back0:
cmpb $0, (%ecx, %edi, 1)
je back1
inc %esi
movl %esi, %edx
cmpl $255, %esi
jge back1
jmp back0
back1:
cmpl $-1, %esi
jle back2
movb (%ecx, %esi, 1), %al
movl %eax, (%ecx, %edi, 1)
dec %esi
dec %edi
jmp back1
back2:
movl $255, %edi
subl %edx, %edi
subl $2, %edi
back3:
cmpl $-1, %edi
jle saldone
movb $0, (%ecx, %edi, 1)
dec %edi
jmp back3
saldone:
popl %ebp
ret
.global _convascii
_convascii:
pushl %ebp
movl %esp, %ebp
xor %esi, %esi
convone:
cmpl $255, %esi
jge parttwo
subb $30, (%ebx, %esi, 1)
inc %esi
jmp convone
parttwo:
xor %esi, %esi
convtwo:
cmpl $255, %esi
jge convasciidone
subb $30, (%ecx, %esi, 1)
inc %esi
jmp convtwo
convasciidone:
popl %ebp
ret
.global _addnumbers
_addnumbers:
pushl %ebp
movl %esp, %ebp
movl $255, %esi
xor %edx, %edx
xor %edi, %edi
addthem:
cmpl $-1, %esi
jle adddone
movb (%ebx, %esi, 1), %dl
addb (%ecx, %esi, 1), %dl
addl %edi, %edx
movb %dl, (%eax, %esi, 1)
cmpb $9, (%eax, %esi, 1)
jle notover
subb $10, (%eax, %esi, 1)
movl $1, %edi
dec %esi
jmp addthem
notover:
xor %edi, %edi
dec %esi
jmp addthem
adddone:
popl %ebp
ret
.global _convbcd
_convbcd:
pushl %ebp
movl %esp, %ebp
movl $255, %esi
dec %esi
xor %edi, %edi
conv:
cmpl %esi, %edi
jge convbcddone
addb $30, (%eax, %edi, 1)
inc %edi
jmp conv
convbcddone:
movl %eax, 16(%ebp)
pop %ebp
ret