Masm 16bit 将 Ascii 转换为字节

Masm 16bit converting Ascii to byte

我是汇编语言的初学者,我必须快速找到解决方案。
问题是我必须从这个人那里读取一个数字(3 位数字),将其转换为整数并将其与两个值进行比较。
问题是转换后比较关闭,有时结果正确有时不正确。

pile segment para stack 'pile'
    db 256 dup (0)
pile ends

data segment
ageper db 4,5 dup(0)
bigg db 13,10,"bigger than 146 ",13,10,"$"
lesss db 13,10,"less than 0 ",13,10,"$"
right db 13,10,"correct number 123 ",13,10,"$"
theint db 0


exacnumber db 123
data ends

code segment
main proc far

    assume cs:code
    assume ds:data
    assume ss:pile
    mov ax,data
    mov ds,ax

    mov ah,0ah
    lea dx,ageper
    int 21h

    mov ch,0
    cmp ageper[4],0
    jz phase2
    mov ah,ageper[4]
    sub ah,48
    add theint,ah
    phase2:
    mov cl,10
    cmp ageper[3],0
    jz phase3
    mov ah,ageper[3]
    sub ah,48
    mov al,ah
    mul cl
    add theint,al
    phase3:
    mov cl,100
    cmp ageper[2],0
    jz phase4
    mov ah,ageper[2]
    sub ah,48
    mov al,ah
    mul cl
    add theint,al

    phase4:
    cmp theint,123
    je yes
    cmp theint,130
    jg big
    cmp theint,0
    jl less
    jmp ending


    big:
    mov ah,09h
    lea dx,bigg
    int 21h
    jmp ending

    yes:
    mov ah,09h
    lea dx,right
    int 21h
    jmp ending

    less:
    mov ah,09h
    lea dx,lesss
    int 21h


    ending:
    mov ageper,20
    mov ageper[1],20

    mov ah,02
    lea dx,theint
    int 21h

    mov ah,4ch
    int 21h
main endp
code ends
    end main
cmp ageper[4],0
jz phase2
...
cmp ageper[3],0
jz phase3
...
cmp ageper[2],0
jz phase4

您的程序运行不稳定,因为您没有正确解释输入!
从 DOS 收到的简单 3 位数字输入不需要像您那样检查数字零。只需删除这 3 个 cmpjz


另一个逻辑上的小错误是,当数字大于130时,你报告它大于146


mov ageper,20
mov ageper[1],20

这些指令毫无意义!


mov ah,02
lea dx,theint
int 21h

在这里您对正确的DOS功能的使用感到困惑。函数 02h 使用 DL 寄存器,函数 09h 使用 DX 寄存器。请在您的手册中查找。


为了解决@Michael 报告的问题(处理从 256 到 999 的 3 位数字),将 theint 变量定义为 word 并添加到其中,如下所示:

theint dw 0

mov ch, 0
mov cl, ageper[4]
sub cl, 48
mov theint, cx       <<< Use MOV the first time!
phase2:
mov cl, 10
mov al, ageper[3]
sub al, 48
mul cl
add theint, ax       <<< Add AX in stead of AH
phase3:
mov cl, 100
mov al, ageper[2]
sub al, 48
mul cl
add theint, ax       <<< Add AX in stead of AH