2011-04-03 52 views
6

首先,這是作業。將十進制轉換爲十六進制

我想讀取一個5位數字進入寄存器bx。該數字被假定爲不大於65535(16位)。以下是我如何嘗試這樣做。

但是,當我嘗試打印該號碼時,我只打印輸入的最後一位數字。這導致我猜測,當我向bx添加另一個數字時,它將覆蓋以前的數字,但我無法看到問題。任何幫助,將不勝感激,我幾乎可以肯定,這是小東西我俯瞰: -/

mov cx,0x05 ; loop 5 times 
    mov bx,0 ; clear the register we are going to store our result in 
    mov dx,10 ; set our divisor to 10 

read: 
    mov ah,0x01  ; read a character function 
    int 0x21  ; store the character in al 
    sub al,0x30  ; convert ascii number to its decimal equivalent 
    and ax,0x000F ; set higher bits of ax to 0, so we are left with the decimal 
    push ax   ; store the number on the stack, this is the single digit that was typed 
    ; at this point we have read the char, converted it to decimal, and pushed it onto the stack 
    mov ax,bx  ; move our total into ax 
    mul dx   ; multiply our total by 10, to shift it right 1 
    pop bx   ; pop our single digit into bx 
    add bx,ax  ; add our total to bx 
    loop read  ; read another char 
+0

如果你將十進制轉換爲十六進制,那麼怎麼樣,你除以10呢?當convertng爲十六進制時,你不需要乘以/除以10 – fazo 2011-04-03 23:09:48

+0

我沒有看到我在哪裏在我發佈的片段中除以10。但是我乘以10的原因是因爲我一次只能讀取1 *字符*,並且我需要將它乘以10,這樣當我添加另一個數字時,它就在適當的位置。 – Adam 2011-04-03 23:16:50

+0

+1用於發佈作業問題並顯示您已經嘗試過的內容。 – vcsjones 2011-04-03 23:18:10

回答

4

當使用MUL操作碼,有三種不同的結果:

  • 8位 - 結果存儲在斧
  • 16位 - 結果存儲在DX:斧
  • 32位 - 結果存儲在 EDX:EAX

所以當你執行你的乘法時,指令會在你的情況下用零覆蓋dx。這意味着多次操作碼的每次後續使用都乘以零。

+0

非常感謝,一行,它的工作完美。我沒有想到dx被寫入0,我認爲它會保持相同的值。 – Adam 2011-04-04 00:34:18

+1

請記住,只有在反向使用div操作碼時纔會發生同樣的情況。 – Sparafusile 2011-04-04 11:42:01

相關問題