2013-06-20 44 views
1

我對8086程序集非常陌生,請原諒那些草率的代碼和可能不必要的行,我是自學的。這段代碼是我正在製作的另一個程序的一部分,它需要用戶輸入數字。這些具體的行接受輸入,然後產生一個電腦實際可以使用的數字。例如,採取5,4和3,並「編譯」數字到543.從內存中將正確的值加載到8086程序集的寄存器中

問題出現在第59行,在那裏我嘗試從內存加載一個數字註冊bx,在這種情況下,而不是加載正確的數字,如40(從前543),它只是加載一個1.

59行後的一些代碼可能不工作,因爲我卡在那裏。

我可能沒有使用正確的寄存器,但是我自學成才並且很難找到關於在線語法的簡單易懂的信息。

org 100h 

mov si, 100d 

input1: 
    mov ah, 1h  ;input char 
    int 21h 
    push ax 
    sub al, 30h  ;convert ascii to integer 
    mov dl, al  ;put char into dl to be read 
    mov [si], al ;save char to ram for later 
    mov ah, 2h  ;output char 
    inc si   ;to save on next location in mem 
    pop ax 
    cmp al, 13  ;check if done 
    jne input1 

    dec si    ;insert terination char 
    dec si    ;decrement to save value of si for multilying by ten 
    push si    ;save current si value 
    inc si    ;then continue 
    mov al, 24h 
    mov [si], al 

    pop si 
    mov cx, 1 

    compileNum1: 
     mov ax, 0 
     mov bx, 0 
     mov dx, 0  
    .fixNum: 
     mov al, [si] ; load last num into ax to be multiplied by 10 
     mul cx 
     mov bp, ax 
     mov [si], bp 
     dec si 
     mov al, 10 
     mov bx, cx 
     mul bl 
     mov cx, ax   
     cmp si, 99d 
     jne .fixNum 

    mov si, 100d  ;starts number addition 

    mov ax, [si]  ;loads first number 
    inc si    ;prepares second 
    mov bx, [si]  ;loads second 

    cmp bx, 24h  ;checks if there was only 1 number 
    je .terminate1  ;if there was, goto terminate 

    add ax, bx   ;else add them together 

    .stloop1: 
     inc si   ;prepares for third, fourth etc 
     mov bx, [si] ;loads it 

     cmp bx, 24h ;checks if numbver is 3 digts ot more long (depends on loop) 
     je .terminate1 ;terminate if so 

     add ax, bx  ;add them together, store in ax 

    .terminate1: 
     mov [100d], ax 





mov ax, 0 ;clear screen 
int 10h 


mov ah, 2h ;print char 
int 21h 

mov ah, 0 
int 16h 
ret 

感謝您的幫助!

回答

0

算法我通常使用「擺脫鍵盤的數字」 - 這實際上是「從鍵盤獲取文本,並將其轉換爲數字」爲你實現,是這樣的:

preloop:

將「迄今爲止的結果」寄存器設置爲零。

設置一個16位的寄存器10作爲乘數使用

循環:

獲取文本字符。您可以一次完成一個,或者一次從用戶放置的輸入緩衝區中加載一個。

確保你有一個十進制數字 - 這些討厭的用戶會輸入任何補丁的東西!你可能想要接受'+'和/或' - '。我會在這裏注意13(CR)或其他終止字符,而不是像在做最後一樣(你並不是真的想將「convert to number」算法應用於CR)。如果有的話,你可以做你想要的「垃圾輸入」。

最簡單的事情就是假設一個行爲良好的用戶。我通常只是返回到目前爲止的數字 - 即使爲零。禮貌的錯誤信息,讓他們重試將是最​​好的。

一旦你得到一個有效的十進制數字,從字符中減去'0' - 注意這是字符'0',而不是數字0 ..如果你喜歡,可以稱它爲48或30h,但我認爲「0」更明顯「它的用途」。

然後乘以「結果迄今」由10

然後加入從當前位所獲得的數量。這可以是一個小小的三關鍵。我們不能add ax, bl。我們可以add ax, bx,但我們必須確定bh爲零! (更改寄存器以適合您的目的)

重複,直到我們發現CR(或其他終止字符)。就是這樣,這個數字是「迄今爲止的結果」。

希望這會有所幫助。你們走在正確的軌道上,但我認爲,讓它變得比需要的複雜。好評如潮,自己到目前爲止!

說到「數字到文本」的部分,我看到一個真正的甜蜜,簡單的例子,這裏的一位常客在這裏發佈。我的記憶很糟糕。 如果你需要它,你可以找到它。

+0

就是這樣!非常感謝! –