2010-03-11 38 views

回答

2

最簡單的方法是將數字轉換爲十六進制。每組4位變成一個十六進制數字。

; this is pseudocode 
mov di, [addr_of_buffer] ; use the string instructions 
cld      ; always remember to clear the direction flag 

; convert bx 
mov cx, 4 ; 4 hexits per 16 bit register 
hexLoopTop: 
mov al, bl ; load the low 8 bits of bx 
and al, 0x0F ; clear the upper 4 bits of al 
cmp al, 10 
jl decimal 
add al, 65 ; ASCII A character 
jmp doneHexit: 
decimal: 
add al, 48 ; ASCII 0 character 
doneHexit: 
stosb  ; write al to [di] then increment di (since d flag is clear) 
ror bx  ; rotate bits of bx down, happens 4 times so bx is restored 
loop hexLoopTop 

; repeat for si, note that there is no need to reinit di 

現在如果你想要十進制輸出,你需要一個相當複雜的算法。明顯的做法是重複劃分。請注意,如果您有32位CPU(386或更高版本),則可以使用16位模式下的32位寄存器,這使得這更容易。

將si:bx移至eax開始。爲了得到下一個數字,你需要執行一條cdq指令來將eax擴展爲edx:eax,div 10,其餘的edx是下一個數字。在eax中的商數是0,在這種情況下,你完成了,或者是下一輪循環的輸入。這將首先給你最重要的數字,所以當你完成後你需要反轉數字。您的緩衝區至少需要10^32的10進制數,它是10的整數。調整此算法以使用帶符號整數並不難。