TExcelTAD
Home › x86-64 Assembly › Printing Numbers

Printing Numbers

The write system call prints bytes, not numbers. To show the value 365 you must produce the three characters '3', '6', '5' (bytes 51, 54, 53) yourself. This lesson builds the print_uint helper you have been using since the first lesson, and its signed sibling print_int.

The idea: repeated division by 10

Dividing by 10 splits off the last decimal digit as the remainder:

text
365 / 10 = 36  remainder 5
 36 / 10 =  3  remainder 6
  3 / 10 =  0  remainder 3   <- quotient 0: done

The digits come out last digit first. Each remainder (0–9) becomes a character by adding the ASCII code of '0' (48). Let's do these steps by hand, printing each remainder as it comes out:

nasm · try it yourself
section .bss
char:   resb 1

section .text
global _start
_start:
    mov eax, 365
    mov ecx, 10
.next:
    xor edx, edx
    div rcx                     ; rax = rax / 10, rdx = last digit
    add dl, '0'                 ; 5 -> '5'
    mov [char], dl
    push rax                    ; save the quotient (syscall clobbers rax, rcx)
    mov eax, 1
    mov edi, 1
    mov rsi, char
    mov edx, 1
    syscall                     ; print one character
    pop rax
    mov ecx, 10
    test rax, rax               ; anything left?
    jnz .next

    mov eax, 60
    xor edi, edi
    syscall
563

The digits are right but in reverse order. The fix is to fill a buffer from its end towards its start, then print the whole buffer with a single write.

print_uint, line by line

nasm
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
  1. The input arrives in rdi, following the standard calling convention (first argument in rdi). It is copied to rax because div divides rdx:rax.
  2. A buffer on the stack. sub rsp, 32 reserves 32 bytes below the current stack top — the biggest 64-bit number has 20 digits, plus the newline. add rsp, 32 at the end gives them back.
  3. Fill backwards. rsi starts at the last byte of the buffer (rsp + 31), which gets the newline. Each new digit is stored one byte further to the left.
  4. The loop is a do … while: it runs at least once, so the number 0 prints as 0 instead of nothing.
  5. The length is "end of buffer minus first digit": (rsp + 32) - rsi.
  6. One write prints everything: rsi already points at the first digit and rdx holds the length.

The routine uses only rax, rcx, rdx, rsi, rdi and (through syscall) r11 — all registers that a caller must expect to lose — and leaves rbx, rbp and r12–r15 untouched. That's why examples keep important values in rbx or r12–r15 across call print_uint.

nasm · try it yourself
section .text
global _start
_start:
    xor edi, edi                ; 0: the loop still runs once
    call print_uint
    mov edi, 7
    call print_uint
    mov edi, 2026
    call print_uint
    mov rdi, -1                 ; the largest unsigned 64-bit value: 20 digits
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
0
7
2026
18446744073709551615

Signed numbers: print_int

For a signed number, print a - first when the value is negative, then print its absolute value with print_uint:

nasm
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
    test rdi, rdi
    jns print_uint          ; not negative: print it as it is
    neg rdi                 ; make it positive
    push rdi                ; keep it (write and syscall clobber registers)
    push '-'                ; the minus sign, as a byte on the stack
    mov eax, 1              ; write(1, rsp, 1)
    mov edi, 1
    mov rsi, rsp
    mov edx, 1
    syscall
    pop rdi                 ; drop the '-'
    pop rdi                 ; the positive value again
    jmp print_uint          ; print_uint's ret returns to our caller
  • test rdi, rdi sets the sign flag from bit 63; jns ("jump if not sign") jumps straight into print_uint for non-negative values.
  • push '-' stores the 8-byte value 45 on the stack. On a little-endian machine its first byte is 45, the '-' character, so rsp points right at it.
  • The final jmp is a tail call: instead of call print_uint followed by ret, we jump, and print_uint's own ret returns directly to whoever called print_int.
  • The smallest number, −2^63, has no positive counterpart: neg leaves it unchanged. Read as unsigned, though, it is exactly 2^63, so it still prints correctly.
nasm · try it yourself
section .text
global _start
_start:
    mov rdi, -42
    call print_int
    mov rdi, 42
    call print_int
    mov rdi, 0x8000000000000000 ; the most negative 64-bit number
    call print_int

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
    test rdi, rdi
    jns print_uint          ; not negative: print it as it is
    neg rdi                 ; make it positive
    push rdi                ; keep it (write and syscall clobber registers)
    push '-'                ; the minus sign, as a byte on the stack
    mov eax, 1              ; write(1, rsp, 1)
    mov edi, 1
    mov rsi, rsp
    mov edx, 1
    syscall
    pop rdi                 ; drop the '-'
    pop rdi                 ; the positive value again
    jmp print_uint          ; print_uint's ret returns to our caller

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
-42
42
-9223372036854775808

Other bases

Nothing about the algorithm is specific to 10. Divide by 2 and you get binary; divide by 16 and you get hexadecimal (with 'A'–'F' for 10–15). Here is a binary printer built from the same pattern:

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 10
    call print_bin
    mov edi, 255
    call print_bin

    mov eax, 60
    xor edi, edi
    syscall

; print_bin: print rdi in binary and a newline
print_bin:
    mov rax, rdi
    sub rsp, 72                 ; up to 64 digits + newline
    lea rsi, [rsp + 71]
    mov byte [rsi], 10
    mov ecx, 2                  ; the base
.next_bit:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    test rax, rax
    jnz .next_bit
    lea rdx, [rsp + 72]
    sub rdx, rsi
    mov eax, 1
    mov edi, 1
    syscall
    add rsp, 72
    ret
1010
11111111

Tip: Dividing by a power of two is much cheaper with shifts and masks (and eax, 1, shr rdi, 1); the print_hex routine in the Registers lesson works that way.

Test yourself

Why does print_uint fill its buffer from the end?

  1. Because the stack grows downwards
  2. Because repeated division produces the last digit first
  3. Because write prints backwards
  4. To save a register

What does add dl, '0' do to a remainder of 7?

  1. Nothing
  2. Turns it into the character code 55, i.e. '7'
  3. Turns it into 0
  4. Adds a newline

Why is the digit loop a do-while (test at the bottom) rather than a while loop?

  1. It is faster
  2. So that the number 0 still prints one digit
  3. Because div needs it
  4. To print the newline