Reputation: 1
So I have this code (x86 assembly language) for solving basic arithmetic equations but my problem is that I can only input 1-digit numbers. Also, the answers can only be within 1-9. I want a code that would be able to accept two or more digits. Here is working sample code for addition.
jmp start
msg1 db 0ah,0dh, "Enter first number: $"
msg2 db 0ah,0dh, "Enter second number: $"
msg3 db 0ah,0dh, "Sum: $"
ans db ?, " $"
num1 db ?
num2 db ?
start:
mov ah, 0
mov al, 3
int 10h
mov ah, 06
mov al,0
int 10h
mov ah, 09h
lea dx, msg1
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov num1, al
mov ah, 09h
lea dx, msg2
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov num2, al
mov al, num1
add al,num2
add al, 30h
mov ans, al
mov ah, 09h
lea dx, msg3
int 21h
mov ah, 09h
lea dx, ans
int 21h
int 20h
Upvotes: 0
Views: 1038
Reputation: 366093
Convert strings to integers, do your arithmetic, then convert integers back to strings (either on-the-fly printing one digit at a time, or into a buffer which you then print.)
None of this is specific to x86 asm or the API you're using for string / character input/output (in this case, DOS int 21h
system calls). It's exactly how you'd do it in C. I'd actually recommend using the C standard library for conversions, unless you want to write strtol and sprintf yourself.
Or you could keep your data in string form from start to finish, and do BCD add-carry and sub-borrow. Each decimal digit is stored in a separate byte. This is easier in ASM than in C, because C doesn't expose the carry flag. It's not a good programming technique, and will lead to slow code.
If you want to do arbitrary-precision arithmetic, work in chunks of 32b or 64b binary integers, instead of single decimal digits. Or just use gmp unless you want to re-invent the wheel as an exercise.
Upvotes: 1