linux nasm assembly print all numbers from zero to 100

后端 未结 3 1123
深忆病人
深忆病人 2021-01-28 15:23

I am writing a program to print out all the numbers from zero to 100. The only reason I am doing this is to test out printing out multiple digit numbers.

The problem th

3条回答
  •  野性不改
    2021-01-28 15:36

    You need to convert the number to an ASCII digit(s) in order to print to terminal. Now I won't give you my dwtoa, that will take the fun out of learning, but you could do something like this:

    sys_exit        equ     1
    sys_write       equ     4
    stdout          equ     1
    
    SECTION .bss
    lpBuffer    resb    4
    
    SECTION .text
    GLOBAL _start
    _start:
        xor     esi, esi
    
    .NextNum:
        call    PrintNum
        inc     esi
        cmp     esi, 100
        jna     .NextNum
    
    .Exit:                                 
        mov     eax, sys_exit                
        xor     ebx, ebx                      
        int     80h                           
    
    ;~ #####################################################################
    PrintNum:   
        push    lpBuffer
        push    esi 
        call    dwtoa
    
        mov     edi, lpBuffer
        call    GetStrlen
        inc     edx
        mov     ecx, lpBuffer
    
        mov     eax, sys_write
        mov     ebx, stdout
        int     80H     
        ret     
    
    ;~ #####################################################################    
    GetStrlen:
        push    ebx
        xor     ecx, ecx
        not     ecx
        xor     eax, eax
        cld
        repne   scasb
        mov     byte [edi - 1], 10
        not     ecx
        pop     ebx
        lea     edx, [ecx - 1]
        ret
    

    Notice, I use things like sys_exit, sys_write, stdout, instead of hard coded numbers. Makes code a bit more self documenting.

提交回复
热议问题