问题
This is my printing function, it should output a 3digit result. I store my result in RES which is a dw. The push and pop fixed my problems with printing before now I don't know where it goes wrong.
XOR AX, AX
XOR BX, BX
;this divides my 3digit number by 100 giving me my, hundredth digit
MOV AX, RES
MOV BX, 100
DIV BX
;prints the hundredth digit
ADD AL, '0'
MOV DL, AL
PUSH AX ; save AX on the stack
MOV AH, 02h
INT 21h
POP AX ; restore ax
;divides the remainder by 10 giving me my tens digit
MOV BX, 10
DIV BX
;prints my tens digit
ADD AL, '0'
MOV DL, AL
PUSH AX ; save AX on the stack
MOV AH, 02h
INT 21h
POP AX ; restore ax
;print my last remainder which is my ones
ADD AH, '0'
MOV DL, AH
MOV AH, 02h
INT 21h
回答1:
"div bx" divides dx:ax by bx. So, what is your dx?
回答2:
I believe you are taking the address of "RES" instead of its value, which needs a parenthesis or bracket.
回答3:
At the tens stage your remainder is in AH, not AX.
And NEVER put comments like "Save AX on the stack" or "Restore AX". Anyone with any fluency in assembler (and it's been 20 years since I've written anything in it, yet that includes me) will know what those instructions are doing.
Comments should be explaining WHY you are doing it, not what you are doing.
I'm rusty enough on this that I won't swear that there aren't more bugs, I'm just pointing out one I see.
来源:https://stackoverflow.com/questions/17664108/printing-3-digits-in-assembly