問題描述
在彙編語言中得到錯誤的結果 (Getting wrong result in Assembly language)
org 100h
.model small
.data
var db ?
msg db 10,13,'$'
.code
; add your code here
main proc
mov ax,@data
mov ds,ax
mov ah,1 ;input 1st number
int 21h
sub al,48
mov var,al
mov ah,1 ;input 2nd number
int 21h
sub al,48
MUL var ; multiplying two numbers
sub al,48 ; going to ASCII value
mov dl,al
mov ah,2 ; printing result
int 21h
mov ah,4ch
int 21h
main endp
end main
ret
參考解法
方法 1:
You incorrectly mix two program models together. For MZ executable DOS program omit the first org 100h
and the last ret
. Or use the much simpler COM executable, which doesn't use segment‑switching directives .data
, .code
, and you don;t have to bother with segment registers. Its skeleton looks like
.model TINY
org 100h
main proc
; Program code must start here at offset 100h
; The first machine instruction.
; Put your code here.
ret ; A simple ret terminates the COM program.
var db ? ; Data variables follow the code section.
msg db 10,13,'$'
end main
</code></pre>
When you multiplicate both digits with mul var
, the product is in register AX
and it may be in the range 0..65535. Only in special case, such as multiplaying 2 by 3 you will get result AX=6, which can be converted to a single‑digit result by adding 48 to it (not by subtracting).
For methods how to convert unsigned 16bit integer to decimal digits search this site, there are lots of examples here.
(by AmirHassan、vitsoft)
參考文件