2012-11-19 39 views
3

好吧我正在爲基於Intel的計算機作業進行彙編,爲了簡單起見,教授讓我們在16位工作。該任務是獲取用戶輸入的數字,並將其轉換爲ASCII中的整數,然後將該整數存儲在數組中。我有這部分工作正常。然後我們從數組中取出整數並將它們轉換回ASCII並將它們打印到屏幕上。在我印刷之前,這在大部分情況下也是行得通的。顯示ascii十六進制數值的字符串彙編語言

我使用tasm,當我用調試器跟蹤我的代碼時,它顯示它應該的數字,但它們被大量空白空間和一些奇怪字符包圍。那麼當我只是嘗試在命令提示符下運行代碼時,我所得到的是一些真正搞砸的字符,然後它似乎崩潰了。

編輯:打印現在工作正常,但是我意識到其餘的是比我想象的buggier。例如,如果我輸入任何低於2559的數字,它似乎工作得很好,但如果我輸入任何數字2560或以上,我得到一個除零錯誤。任何幫助,這將不勝感激。

代碼是如何工作
它會提示你
你鍵入一個號碼,然後按輸入
類型另一個號碼按回車
數字(數應該可以是任何無符號16位整數)輸入另一個數字按回車鍵(應該可以用這種方式輸入最多10個數字)
當您輸入數字時鍵入減號「 - 」
然後它應該重複輸入您輸入的數字

繼承人我的代碼:

.MODEL small 

.STACK 200h 
CR EQU 13 
LF EQU 10 

.DATA 
;variables used by InputNumber procedure 
BinaryValue dw 0 
CharacterCount dw 0 
Index dw 0 
Multiplier dw 1 
InputString db 7 dup (0) 
prompt2 db 'Enter A Number:', '$' 


;variables used by LoadArray prcedure 
prompt1 db 'You may enter up to 10 positive integers then enter a -1', CR, LF, '$' 
userFlag db 0 
numberArray dw 10 dup (0) 
numArraySize dw 0 

;variables for PrintArray 
prompt3 db 'Array data is:', '$' 
numSize dw 0 

.CODE 
Main proc 
    MOV ax, @data 
    MOV ds, ax 
    CALL LoadArray 
    CALL PrintArray 
    MOV ax, 4c00h 
    INT 21h 
Main endp 





LoadArray proc 
    ; informs the user to enter numbers then enter a -1 
    MOV ah, 09h 
    MOV dx, offset prompt1 
    INT 21h 

    Mov di, 0 
    While1: 
     Mov BinaryValue, 0 
     Mov CharacterCount, 0 
     Mov Index, 0 
     Mov Multiplier, 1 
     Call InputNumber 
     Cmp userFlag, 0 
     Jne endWhile1 
     Mov bx, BinaryValue 
     Mov numberArray[di], bx 
     Inc di 
     Inc di 
     jmp While1 
    endWhile1: 
    Mov numArraySize, di 
    RET 
LoadArray endp 





InputNumber proc 
    MOV ah, 09h 
    MOV dx, offset prompt2 
    INT 21h 

    ;Get character from keyboard 
    Mov ah, 01h 
    Int 21h 

    ;while that character is not the enter key keep getting keys from the keyboard 
    Mov si, CharacterCount 
    while2: 
     Cmp al, 0Dh 
     Je endWhile2 
     Cmp al, 2Dh 
     Je endOfProc 
     Mov InputString[si], al 
     Inc si 
     Mov ah, 01h 
     Int 21h 
     jmp while2 
    endWhile2: 
    Mov CharacterCount, si 

    ;initializing multiplier and index to the values needed 
    Mov Multiplier, 1 
    Dec si 
    Mov Index, si 

    ;loop through InputString array pulling out each character, convert it by removing its ascii bits and muliplying it by its place value 
    Mov cx, CharacterCount 
    Mov CharacterCount, 0 
    convertLoop: 
     Mov al, InputString[si] 
     And ax, 0Fh 
     Mul Multiplier 
     Add BinaryValue, ax 
     Mov ax, 10 
     Mul Multiplier 
     Mov Multiplier, ax 
     Dec si 
     Loop convertLoop 
    RET 
endOfProc: 
    Mov userFlag, 1 
    RET 
InputNumber endp 




PrintArray proc 
    MOV ah, 09h 
    MOV dx, offset prompt3 
    INT 21h 

    Mov si, 0 
    doWhile1: 
     Mov bx, numberArray[si] 
     Mov BinaryValue, bx 
     call DisplayNumber 
     Inc si 
     Inc si 
     Cmp si, numArraySize 
     jb doWhile1 
    RET 
PrintArray endp 




DisplayNumber proc 
    Mov ax, BinaryValue 
    ;determine size of the number in BinaryValue 
    loop1: 
     Mov bl, 10 
     Div bl 
     cmp al, 0 
     je end1 
     Inc numSize 
     Mov ah, 0 
     jmp loop1 
    end1: 
    Inc numSize 
    Mov di, numSize 
    Mov InputString[di], '$' 
    dec di 
    Mov ax, 0 
    Mov ax, BinaryValue 
    while3: 
     cmp ax, 10 
     jb endWhile3 
     Div bl 
     Or ah, 30h 
     Mov InputString[di], ah 
     Mov ah, 0 
     dec di 
     jmp While3 
    endWhile3: 
    Or al, 30h 
    Mov InputString[di], al 

    ;print the ascii characters stored in InputString to the screen 
    Mov dx, word ptr InputString[di] 
    Mov ah, 09h 
    Int 21h 
    Ret 
DisplayNumber endp 
end main 

我會在做這個,直到它的工作,因爲我需要它爲以後分配。任何幫助將非常感激。即使它建議如何清理代碼或使其更好或更易讀(包括樣式)。

+0

'Mov dx,word ptr InputString [di]'對我來說不正確。在那裏試試'lea'。 –

+0

謝謝,這對打印有很大的幫助。它現在正確地顯示了數字,但現在我意識到其餘部分並不像我想的那樣工作得很好。 – yahbo

+0

我的程序集有點生疏,但你確定它應該是「numberArray dw 10 dup(0)」,而不是「numberArray dw 0 dup(10)」? –

回答

0

我看着你的代碼,只有你的問題,這部分關注:

編輯:印刷現在工作正常,但香港專業教育學院認識到,剩下的就是比我想象buggier。例如,如果我輸入任何低於2559的數字,它似乎工作得很好,但如果我輸入任何數字2560或以上,我得到一個除零錯誤。任何幫助,這將不勝感激。

的問題是在DisplayNumber這裏:

Mov ax, BinaryValue 
;determine size of the number in BinaryValue 
loop1: 
    Mov bl, 10 
    Div bl 
    cmp al, 0 
    je end1 

Div bl是由寄存器的8位除法,所以源是ax。它執行除法,並將結果放在al中,其餘部分放在ah - 如下面的語句cmp al,0進行測試。

的問題是,如果除法運算的結果將不適合al - 這是當BinaryValue爲2560或更大時發生的事情:2560/10 = 256,這將不適合al。所以處理器引發了一個Divide異常 - 俗稱Division by zero,因爲這是獲取它的最簡單方法,但在其他情況下也會引發異常。

相關問題