0

我正在 Tasm 中编程并想输入一个 32 位数字。

我知道我必须逐个数字地输入它(我希望没有一个用于数字输入的呼叫功能)

这是我的代码

    .        .486
    .model small
    .code
    start:

    mov ebx, 0

    ; enter again and again untill enter is hit
    again:
    mov ah,01h
    int 21h
    cmp al, 13
    jz next
    mov dl, al
    mov eax, ebx
    mov ebx, 10
    mul ebx
    mov ebx, eax
    mov eax, 0
    mov al, dl
    add ebx, eax
    jmp again

    ; now find the digits back

    next:
    ; just testing to see if i got number
    mov edx, 13
    mov ah, 02h
    int 21h

    mov edx, 10
    mov ah,02h
    int 21h

    mov edx, ebx
    mov ah, 02h
    int 21h

    mov eax, ebx

    mov ebx, eax

    xor edx, edx
    xor cl, cl

    ; find digits and push into stack from last to first so when i pop i get digits back
    finddigit:
    xor edx,edx
    mov ch , 10
    div ch
    push dx ;taking risk dx dl
    inc cl
    cmp eax, 0
    jz print
    jmp finddigit


    ; stored into cl the number of digits

    print:
    cmp cl,0
    jz exit
    dec cl
    pop dx
    mov ah,02h
    int 21h
    jmp print


    exit:
    end start

我在 enter 停止输入。

我收到错误 NTVDM 遇到了一个硬错误。

谢谢

这是我新修改的代码。它对 2 和 123 等数字运行良好,但对 333、4444、555 则失败;(我希望推送和弹出不会修改指定以外的任何寄存器):

.486
.model small
.code
start:

mov ebx, 0

; enter again and again untill enter is hit
again:
mov ah,01h
int 21h
cmp al, 13
jz next
mov cl, al
sub cl, 30h
mov eax, ebx
mov ebx, 10
mul ebx
mov ebx, eax
mov eax, 0
mov al, cl
add ebx, eax
jmp again

; now find the digits back

next:
; just testing to see if i got number
mov edx, 13
mov ah, 02h
int 21h

mov edx, 10
mov ah,02h
int 21h


mov eax, ebx

mov ebx, eax

xor ecx, ecx

mov ebx, ebp
; find digits and push into stack from last to first so when i pop i get digits back
finddigit:
xor edx,edx
mov ebp , 10
div ebp
push edx
inc cl
cmp eax, 0
jz print
jmp finddigit

; stored into cl the number of digits

print:
cmp cl,0
jz exit
dec cl
xor edx,edx
pop edx
add dl,30h
mov ah,02h
int 21h
jmp print


exit:
mov ah,4ch
int 21h               
end start

我正在运行这是 MS-DOS CMD.exe 窗口 弹出错误来了:

错误

4

1 回答 1

2

假设这是针对 DOS 环境(由于int 21h):

您的代码中有一些错误。

1.读取字符函数在 中返回其输出al,没关系。但是随后您立即使用以下顺序销毁读取字符的值:

    mov dl, al    ; the character read now in dl
    mov eax, ebx  ; eax is now 0 in the first loop
    mov ebx, 10   ; ebx is now 10
    mul ebx       ; the result of mul ebx is now in edx:eax,
                  ; the character read (in dl) is lost.

dl因此,如果要执行此操作,则无法将字符存储在其中mul ebx,因为mul reg32将结果输出到edx:eax. 你可以存储它,例如。在clch代替。

2.我注意到的其他错误是您试图将 ASCII 值乘以 10(在前面的代码中)。在乘法之前,您应该首先减去每个读取字符的“0”值,即sub al, 30hor sub al, '0'

3.第三个错误按以下顺序排列:

   xor edx,edx
   mov ch , 10
   div ch
   push dx ;taking risk dx dl
   inc cl
   cmp eax, 0
   jz print
   jmp finddigit

编辑:在这里你除以axch这显然不适用于 32 位除法。看起来你想要你的股息,用(就像你做的那样)eax清除,然后用 32 位寄存器除以,例如。,或(到目前为止,这些似乎在您的代码中未使用),您将获得商数和余数。edxxor edx, edxedx:eaxebpesiedieaxedx

于 2012-08-24T06:34:36.677 回答