I have just started learning assembly language and i tried to write a program on my own with notepad++ and dosbox emulator for multiplication operations. However, the result is always random symbol that doesnt tally with my expected answer.
Here is the full code I've written
.model small
.stack
.data
msg1 DB "Quantity (unit): $"
msg2 DB 13,10,"Unit price (RM): $"
msg3 DB 13,10,"Total amount is RM$"
Quantity DB 0
Unit_price DB 0
Total DB 0
.code
main proc
mov AX,@data
mov DS, AX
mov AH, 09h ;display 1st msg
lea DX, msg1
int 21h
mov AH, 01h
mov Quantity, AL
int 21h
mov AH, 09h ;display 2nd msg
lea DX, msg2
int 21h
mov AH, 01h
mov Unit_price, AL
int 21h
mov AX, 0
sub Quantity, '0'
sub Unit_price, '0'
mov AL, Quantity
mul Unit_price
add Total, AL
add Total, '0'
mov AH, 09h
lea DX, msg3
int 21h
mov AH, 02h
mov DL, Total
int 21h
mov AX, 4C00H
int 21h
MAIN ENDP
END MAIN
The picture is the result i obtained from multiplication of 2 and 3 (should be 6 but idk why it's an arrow)
You wrote:
mov AH, 01h
mov Quantity, AL
int 21h
The character that was read is returned in AL
by the int 21h
service, so mov Quantity, AL
needs to come after int 21h
.
As noted by fuz, adding/subtracting '0'
to convert a number to/from ASCII only works for single-digit numbers. So your program should work for calculating 2*3
as in your test, but not for 3*5
; you'd have to do a proper decimal conversion for that (repeatedly multiplying/dividing by 10 to extract digits one by one).