Platinum Partner

Writeln In Assembler

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
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}