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:
365 / 10 = 36 remainder 5
36 / 10 = 3 remainder 6
3 / 10 = 0 remainder 3 <- quotient 0: doneThe 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:
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
syscall563
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
; ---- 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- The input arrives in
rdi, following the standard calling convention (first argument inrdi). It is copied toraxbecausedivdividesrdx:rax. - A buffer on the stack.
sub rsp, 32reserves 32 bytes below the current stack top — the biggest 64-bit number has 20 digits, plus the newline.add rsp, 32at the end gives them back. - Fill backwards.
rsistarts at the last byte of the buffer (rsp + 31), which gets the newline. Each new digit is stored one byte further to the left. - The loop is a
do … while: it runs at least once, so the number 0 prints as0instead of nothing. - The length is "end of buffer minus first digit":
(rsp + 32) - rsi. - One
writeprints everything:rsialready points at the first digit andrdxholds 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.
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
ret0
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:
; ---- 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 callertest rdi, rdisets the sign flag from bit 63;jns("jump if not sign") jumps straight intoprint_uintfor 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, sorsppoints right at it.- The final
jmpis a tail call: instead ofcall print_uintfollowed byret, we jump, andprint_uint's ownretreturns directly to whoever calledprint_int. - The smallest number, −2^63, has no positive counterpart:
negleaves it unchanged. Read as unsigned, though, it is exactly 2^63, so it still prints correctly.
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:
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
ret1010
11111111
Tip: Dividing by a power of two is much cheaper with shifts and masks (
and eax, 1,shr rdi, 1); theprint_hexroutine in the Registers lesson works that way.
Test yourself
Why does print_uint fill its buffer from the end?
- Because the stack grows downwards
- Because repeated division produces the last digit first
- Because write prints backwards
- To save a register
What does add dl, '0' do to a remainder of 7?
- Nothing
- Turns it into the character code 55, i.e. '7'
- Turns it into 0
- Adds a newline
Why is the digit loop a do-while (test at the bottom) rather than a while loop?
- It is faster
- So that the number 0 still prints one digit
- Because div needs it
- To print the newline