2012-02-02 51 views
2

我正在使用intel上的& t語法在程序集中使用程序。程序集編號爲ascii

我迷路了,如何將寄存器中的整數轉換爲ASCII碼?

可以說我想轉換數字10,我會把數字10放在寄存器%eax中。 如果我只是添加的號碼48%eax中,則ASCII符號將是:在10號

我要添加48到1,然後48到0我該怎麼辦呢?

示例代碼:

mov $10, %eax 
#Cut the number in some way. 
add $48, %eax 
+0

相關:http://stackoverflow.com/questions/4953506/why-does-my-code-display-rubbish/4954659#4954659 – 2012-02-02 14:27:42

+0

另: http://stackoverflow.com/questions/9113060/print-decimal-in-8086-emulator – 2012-02-02 14:29:57

回答

5

要將數字轉換爲ASCII,您需要將數字除以10,並將餘數作爲結果。然後添加ASCII'0'並存儲結果數字。然後重複相同的商,直到它達到零。

但是,這會從最低有效位開始以相反順序給出數字。您可以通過使用堆棧來反轉訂單。將每個數字推入堆棧,然後彈出它們並存儲到字符串緩衝區中。

像這樣(未測試):

.DATA 
    strResult db 16 dup (0) ; string buffer to store results 

.CODE 
    mov eax, number  ; number to be converted 
    mov ecx, 10   ; divisor 
    xor bx, bx   ; count digits 

divide: 
    xor edx, edx  ; high part = 0 
    div ecx    ; eax = edx:eax/ecx, edx = remainder 
    push dx    ; DL is a digit in range [0..9] 
    inc bx    ; count digits 
    test eax, eax  ; EAX is 0? 
    jnz divide   ; no, continue 

    ; POP digits from stack in reverse order 
    mov cx, bx   ; number of digits 
    lea si, strResult ; DS:SI points to string buffer 
next_digit: 
    pop ax 
    add al, '0'   ; convert to ASCII 
    mov [si], al  ; write it to the buffer 
    inc si 
    loop next_digit 
+0

At行'推動DX',你的意思是推EDX?我不熟悉英特爾語法,所以我很困惑。將餘數edx推到堆棧是合乎邏輯的。 – Creator13 2016-10-21 15:17:18

4

一般來說,你可以這樣來做:

repeat 
    d = x MOD 10 
    x = x DIV 10 
    stringd = d + 48; 
    store character somewhere 
until x == 0 
print characters in reverse order 

但數字將被從後到前...轉換這個集會......

+1

+1爲僞代碼解決方案 – 2012-02-02 14:29:01

+0

C實現此僞代碼:http://stackoverflow.com/a/9113669/1155000 – 2012-02-02 15:31:51