在汇编语言中得到错误的结果
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 executable DOS 程序,省略第一个 org 100h
和最后一个 ret
。
或者使用更简单的 COM 可执行文件,它不使用段切换指令 .data
、.code
,并且您不必为段而烦恼寄存器。它的骨架看起来像
.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(而不是减去)来转换为个位数结果。
关于如何将无符号 16 位整数转换为十进制数字的方法搜索该站点,这里有很多示例。
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 executable DOS 程序,省略第一个 org 100h
和最后一个 ret
。
或者使用更简单的 COM 可执行文件,它不使用段切换指令 .data
、.code
,并且您不必为段而烦恼寄存器。它的骨架看起来像
.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(而不是减去)来转换为个位数结果。
关于如何将无符号 16 位整数转换为十进制数字的方法搜索该站点,这里有很多示例。