Binary, Hex & Two's Complement
Registers and memory hold bits. To read assembly comfortably you need to switch fluently between decimal, binary and hexadecimal, and you need to know how negative numbers are stored.
Number bases in NASM
NASM accepts numbers in several notations. They all produce the same bits:
| Notation | Example | Value |
|---|---|---|
| Decimal | 200 |
200 |
| Hexadecimal | 0xC8 or 0C8h |
200 |
| Binary | 0b11001000 or 11001000b |
200 |
| Octal | 0o310 or 310q |
200 |
| Character | 'A' |
65 (its ASCII code) |
Hexadecimal is the favourite of assembly programmers because each hex digit is exactly 4 bits, so a byte is always two hex digits: 0xC8 = 1100 1000.
section .text
global _start
_start:
mov rdi, 200
call print_uint
mov rdi, 0xC8
call print_uint
mov rdi, 0b1100_1000 ; underscores are allowed for readability
call print_uint
mov rdi, 'A'
call print_uint
mov rdi, 0xFFFF_FFFF ; the largest 32-bit unsigned value
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
ret200
200
200
65
4294967295
Ranges
With n bits you can represent 2^n different values:
| Size | Unsigned range | Signed range |
|---|---|---|
| 8 bits | 0 to 255 | −128 to 127 |
| 16 bits | 0 to 65,535 | −32,768 to 32,767 |
| 32 bits | 0 to 4,294,967,295 | −2,147,483,648 to 2,147,483,647 |
| 64 bits | 0 to 18,446,744,073,709,551,615 | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Two's complement
The CPU does not store a separate minus sign. Instead negative integers use two's complement: to negate a number, flip all its bits and add 1. In 8 bits:
5 = 0000 0101
flip 1111 1010
+1 1111 1011 = -5 (also 251 if read as unsigned)The beautiful property: addition works the same for signed and unsigned numbers. -5 + 7 in 8 bits is 1111 1011 + 0000 0111 = 1 0000 0010; the ninth bit falls off and the result is 2. That is why x86 has a single add instruction for both.
Whether a bit pattern is "negative" is only a matter of interpretation. In the next example the register holds -1, which is 64 one-bits. Printed as an unsigned number it is the largest 64-bit value; the print_int helper interprets the very same bits as signed:
section .text
global _start
_start:
mov rdi, -1
call print_uint ; read as unsigned
mov rdi, -1
call print_int ; read as signed
mov rdi, 5
neg rdi ; two's complement negation: rdi = -5
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
ret18446744073709551615
-1
-5
The highest bit is called the sign bit: for signed interpretation, if it is 1 the number is negative.
Overflow and wrap-around
Registers have a fixed width, so arithmetic wraps around. Adding 1 to the largest 8-bit value gives 0. The CPU records what happened in the flags (a later lesson), but it never raises an error on its own.
section .text
global _start
_start:
mov al, 255 ; al is the lowest 8 bits of rax
add al, 1 ; 255 + 1 does not fit in 8 bits
movzx edi, al
call print_uint ; wrapped around to 0
mov al, 127 ; largest positive signed 8-bit value
add al, 1
movsx rdi, al ; read al as a SIGNED byte
call print_int ; 127 + 1 "became" -128
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
ret0
-128
movsx (move with sign-extension) copies the sign bit into all the new upper bits, so a negative byte stays negative in the 64-bit register. Compare with movzx, which fills with zeros.
Quick conversion tips
- Hex to binary: replace each hex digit by 4 bits (
0x3F→0011 1111). - Powers of two:
0x100= 256,0x1000= 4096,0x10000= 65536. -1is all ones in any size:0xFF,0xFFFF,0xFFFFFFFF, …- The smallest negative number has only the sign bit set:
0x80= −128 in 8 bits.
Test yourself
What is 0x2A in decimal?
- 32
- 42
- 52
- 210
In 8-bit two's complement, what does the bit pattern 1111 1111 mean as a signed number?
- 255
- -127
- -1
- -128
What is the difference between movzx and movsx?
movzxis fastermovzxfills the new upper bits with zeros,movsxcopies the sign bit into themmovsxonly works with memory- There is none