Masm 16bit将Ascii转换为字节

时间:2016-11-19 10:47:18

标签: assembly ascii masm x86-16

我是汇编语言的初学者,我必须快速找到解决方案 问题是我必须从人中读取一个数字(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

1 个答案:

答案 0 :(得分:1)

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