DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Writeln In Assembler

09.10.2007
| 3002 views |
  • submit to reddit
        NASM code for writing the integer in eax to stdout.

segment .bss
        writeTemp       resb    10

segment .text
_writeln:                               
        MOV     ebx,    eax             ; Number is already in eax
        MOV     ecx,    10
        XOR     edi,    edi
L0:
        CMP     eax,    0
        JE      L1
        ADD     edi,    1
        XOR     edx,    edx
        IDIV    ecx
        JMP     L0
L1:
        ADD     edi,    writeTemp       ; Length of the number is in edi
        MOV     esi,    edi
        MOV byte        [edi],  10      ; Add a newline
        SUB     edi,    1
        XOR     eax,    eax
L2:                             
        CMP     ebx,    0
        JE      L3
        MOV     eax,    ebx
        XOR     edx,    edx
        IDIV    ecx
        ADD     dl,     48
        MOV     [edi],  dl
        SUB     edi,    1
        MOV     ebx,    eax
        JMP     L2                      ; Number has been written to writeTemp from tail to head
L3:                             
        MOV     eax,    4
        MOV     ebx,    1
        MOV     ecx,    writeTemp
        MOV     edx,    esi
        SUB     edx,    writeTemp
        ADD     edx,    1
        INT     80h