NASM整数到ascii转换算法不起作用

时间:2012-03-14 22:32:15

标签: ascii nasm division

我的NASM程序的一个子部分是尝试取一个整数并将其转换为ASCII以便在屏幕上打印,但它无法正常工作。我不知道为什么不。忽略关于减半/加倍的东西 - 这是我遗漏的程序的其他部分。

这是我的代码:

segment .data                                   ;data segment
    return          db      0xA                     ;return character

segment .bss                                    ;uninitialized data
    numc            resb    4               ;reserve 4 bytes for the converted number (int)
    numh            resb    5               ;reserve 5 bytes for halved input number
    numha           resb    5               ;reserve 5 bytes for the halved number in     ascii form
    numd            resb    5               ;reserve 5 bytes for doubled input number
    numda           resb    5               ;reserve 5 bytes for the doubled number in ascii form

segment .text                                   ;code segment
    global  _start                          ;global program name

_start:  
    mov     ebx, 1234               ;number to be converted
    mov     [numc], ebx

_inttoascii:                            ;set registers up
    mov     eax, [numc]
    xor     ebx, ebx
    xor     edx, edx
    mov     ecx, 32

_loop2:
    xor     edx, edx                ;clear edx
    mov     ebx, 10                 ;divide eax by ten
    div     ebx
    add     edx, 48                 ;add 48 to the remainder (ascii of 0 is 48)
    mov     [numha+ecx], edx        ;move result to the correct spot in memory
    sub     ecx, 8                  ;switch to another byte for next iteration

    cmp     eax, 0                  ;if eax register is exhausted, we're done.
    jg      _loop2


_inttoascii2:                            ;set registers up
    mov     eax, [numc]
    xor     ebx, ebx
    xor     edx, edx
    mov     ecx, 32

_loop3:
    xor     edx, edx                ;clear edx
    mov     ebx, 10                 ;divide eax by ten
    div     ebx
    add     edx, 48                 ;add 48 to the remainder (ascii of 0 is 48)
    mov     [numda+ecx], edx        ;move result to the correct spot in memory
    sub     ecx, 8                  ;switch to another byte for next iteration

    cmp     eax, 0                  ;if eax register is exhausted, we're done.
    jg      _loop3

    mov     eax, 4                          ;select kernel call #4 (write number storing half)
    mov     ebx, 1                          ;default output device
    mov     ecx, numha                      ;pointer to numh
    mov     edx, 5                          ;length of numh
    int     0x80                            ;kernel call to write

    mov     eax, 4                          ;select kernel call #4 (new line)
    mov     ebx, 1                          ;default output device
    mov     ecx, return                     ;pointer to return character
    mov     edx, 1                          ;length of return character
    int     0x80

    mov     eax, 4                          ;select kernel call #4 (write number storing double)
    mov     ebx, 1                          ;default output device
    mov     ecx, numda                      ;pointer to numd
    mov     edx, 5                          ;length of numd
    int     0x80                            ;kernel call to write

    mov     eax,4                           ;select kernel call #4 (new line)
    mov     ebx,1                           ;default output device
    mov     ecx, return                     ;pointer to return character
    mov     edx, 1                          ;length of return character
    int     0x80                            ;kernel call to write

exit:   mov     eax, 1                          ;select system call #1
    int     0x80                            ;kernel call to exit

输出atm只是空行。

1 个答案:

答案 0 :(得分:0)

我正在逐位而不是按字节移动内存地址:

mov     ecx, 32
mov     [numha+ecx], edx        ;move result to the correct spot in memory
sub     ecx, 8                  ;switch to another byte for next iteration

应该是

mov     ecx, 4
mov     [numha+ecx], edx        ;move result to the correct spot in memory
sub     ecx, 1                  ;switch to another byte for next iteration