Digits to text in Assembly
A number becomes printable by adding '0'.
; A number becomes text by adding '0' -- one digit at a time.
global _start
section .bss
buf: resb 2
section .text
_start:
mov rax, 7 ; the digit to print
add rax, '0' ; 7 -> '7' (ASCII 55)
mov [buf], al ; store the low byte
mov byte [buf + 1], 10 ; newline
mov rax, 1
mov rdi, 1
mov rsi, buf
mov rdx, 2
syscall
mov rax, 60
xor rdi, rdi
syscall
How it works
add rax, '0'turns 7 into ASCII 55.mov [buf], alstores just the low byte.- One digit and a newline make a two-byte write.
Keywords and builtins used here
addalbyteglobalmovraxrdirdxresbrsisectionsyscallxor
The run, in numbers
- Lines
- 22
- Characters to type
- 386
- Tokens
- 62
- Three-star pace
- 55 tpm
At the three-star pace of 55 tokens a minute, this run takes about 68 seconds.
Step 1 of 3 in Talking to Linux, step 26 of 31 in Language basics.