2012-08-24 41 views
0

我在Tasm中編程,想要輸入一個32位的數字。如何在程序集中輸入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 

我來控制輸入的輸入。

我收到錯誤NTVDM遇到了一個硬性錯誤。

感謝

這是我的新的修改後的代碼。對於像2和123 這樣的一些數字,它工作正常,但是對於333,4444,555失敗; (我希望推動和空間PoPing不修改超過規定的其他任何寄存器):

.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窗口彈出錯誤談到:

Error

回答

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, 30hsub 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中獲得您的股息,請使用xor edx, edx(與您一樣)清除edx,然後將edx:eax與32位寄存器(例如, ebp,esiedi(這些在您的代碼中似乎尚未使用),您將獲得eax中的商數和餘下的edx

+1

在你的「exit:」標籤處還需要一個「退出」(mov ah,4Ch/int 21h)。這可能是NTDVM所抱怨的。 –

+0

@nrz我已經更新了這個問題。新代碼粘貼。它對一些輸入工作正常,但對其他輸入失敗。 –

+0

如果我使用ebp,我必須在使用它之前存儲它,並在工作後將其替換。 –

相關問題