2012-03-14 24 views
0

我的NASM程序的一个子部分正在尝试取整数并将其转换为ASCII码以便在屏幕上打印,并且它不工作。我说不出为什么。忽略关于减半/加倍的东西 - 这是我忽略的程序的其他部分。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只是空行。

+0

胡说,我是按字节移动通过位的内存地址,而不是: mov ecx,32 mov [numha + ecx],edx;将结果移动到内存中的正确位置 sub ecx,8;切换到另一个字节进行下一次迭代 d一直 MOV ECX,4 MOV [numha + ECX],EDX;移动结果在存储器 子ECX正确的位置,1;开关为下一次迭代 – mavix 2012-03-14 22:53:48

+0

所以...另一个字节已经解决了吗? – 2012-03-15 02:12:42

回答

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