在汇编语言中得到错误的结果 [英] 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
推荐答案
您错误地将两个程序模型混合在一起.对于MZ 可执行 DOS 程序,省略第一个org 100h
和最后一个ret
.或者使用更简单的 COM 可执行文件,它不使用段切换指令 .data
、.code
,而您没有打扰段寄存器.它的骨架看起来像
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
当您将两个数字与 mul var
相乘时,乘积在寄存器 AX
中,它可能在 0..65535 范围内.只有在特殊情况下,例如 2 乘 3,您会得到结果 AX=6,可以通过加 48(而不是减法)将其转换为个位数结果.
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).
关于如何将无符号16位整数转换为十进制数字的方法搜索本站,这里有很多例子.
For methods how to convert unsigned 16bit integer to decimal digits search this site, there are lots of examples here.
这篇关于在汇编语言中得到错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!