Over a million developers have joined DZone.

Writeln In Assembler

DZone's Guide to

Writeln In Assembler

Free Resource
NASM code for writing the integer in eax to stdout.

segment .bss
        writeTemp       resb    10

segment .text
        MOV     ebx,    eax             ; Number is already in eax
        MOV     ecx,    10
        XOR     edi,    edi
        CMP     eax,    0
        JE      L1
        ADD     edi,    1
        XOR     edx,    edx
        IDIV    ecx
        JMP     L0
        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
        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
        MOV     eax,    4
        MOV     ebx,    1
        MOV     ecx,    writeTemp
        MOV     edx,    esi
        SUB     edx,    writeTemp
        ADD     edx,    1
        INT     80h

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}