问题
need help, how to add two numbers and then print the result here is my code
.MODEL SMALL
.STACK 200H
.DATA
NUM1 DB 12
NUM2 DB 3
VAL DB ?
MSG1 DB "The sum is : $"
.CODE
BEGIN PROC
MOV AX, @DATA
MOV DS, AX
MOV AL, NUM1
ADD AL, NUM2
MOV VAL, AL
LEA DX, MSG1
MOV AH, 9
INT 21H
MOV AH, 2
MOV DL, VAL
INT 21H
MOV AX, 4C00H
INT 21H
BEGIN ENDP
END BEGIN
I got an output that says
The sum is 0
What is the error to my code?
回答1:
By dividing the value constantly by 10 you'll get the single digits in the remainder - but in the "wrong" order (last to first). To print it in the "right" order (first to last) you can reverse them by PUSH
ing and POP
ing (keyword: LIFO = last in first out):
.MODEL SMALL
.STACK 200H
.DATA
NUM1 DB 12
NUM2 DB 3
VAL DW ?
MSG1 DB "The sum is : "
DECIMAL DB "00000$"
.CODE
BEGIN PROC
MOV AX, @DATA
MOV DS, AX
XOR AX, AX
MOV AL, NUM1
ADD AL, NUM2
ADC AH, 0
MOV VAL, AX
MOV AX, VAL
CALL AX_to_DEC
LEA DX, MSG1
MOV AH, 9
INT 21H
MOV AX, 4C00H
INT 21H
BEGIN ENDP
AX_to_DEC PROC
mov bx, 10 ; divisor
xor cx, cx ; CX=0 (number of digits)
First_Loop:
xor dx, dx ; Attention: DIV applies also DX!
div bx ; DX:AX / BX = AX remainder: DX
push dx ; LIFO
inc cx ; increment number of digits
test ax, ax ; AX = 0?
jnz First_Loop ; no: once more
mov di, OFFSET DECIMAL ; target string DECIMAL
Second_Loop:
pop ax ; get back pushed digit
or ax, 00110000b ; to ASCII
mov byte ptr [di], al ; save AL
inc di ; DI points to next character in string DECIMAL
loop Second_Loop ; until there are no digits left
mov byte ptr [di], '$' ; End-of-string delimiter for INT 21 / FN 09h
ret
AX_to_DEC ENDP
END BEGIN
回答2:
You cannot do it like this, You will not see the number, You must get the ascii representating hexvalue s of the digits, which represent Your result. Very basic basics. Let me look for a link.
Here it is: How to convert from 4-bit hexadecimal to 7-bit ASCII?
来源:https://stackoverflow.com/questions/23679521/add-2-numbers-in-assembly-language-and-print-the-result