Number bases and operands
Number bases
NASM writes hex with a 0x prefix, binary with 0b, and plain decimal by default. Character constants sit in single or double quotes and hold the ASCII value:
mov al, 0xFF ; 255 mov bl, 0b1010 ; 10 mov cl, "A" ; 65 mov dl, 100 ; decimal 100
Operand sizes
When both operands are memory or a constant is involved, the size must be unambiguous. NASM resolves it from a register when it can, and from a size keyword otherwise:
mov byte [flag], 1 mov word [count], 1000 mov qword [big], 1000000
Bit masks
Hex is the natural base for masks, because one hex digit is exactly four bits. A mask of 0xF keeps the low nibble, and 0xFF00 keeps bits 8 through 15:
and eax, 0xFF ; keep only the low byte shr eax, 8 ; shift right one byte
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which number does
0b1010mean in decimal?Answer
10
The 0b prefix marks binary, and 1010 is 8 + 2 = 10.
-
What does the character constant
"A"hold?Answer
The value 65
A one-character constant is its ASCII code, and A is 65.
-
Which value does
0xFFhave?Answer
255
Two hex digits are eight bits, and 0xFF is all ones, which is 255.
-
Why is hex natural for bit masks?
Answer
Because one hex digit is four bits
Each hex digit maps to one nibble, so a mask can be written and read bit by bit.
-
What does
and eax, 0xFFkeep?Answer
The low byte only
The mask has ones in the low eight bits and zeros above, so only the low byte survives.
-
Why does
mov byte [flag], 1need thebytekeyword?Answer
Because the constant has no size
The constant 1 fits every size, so the size of the destination must be stated.
-
What does this program print?
default rel section .data msg db "A", 10 section .text global _start _start: mov al, 0x42 mov [msg], al mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 2 syscall mov rax, 60 xor rdi, rdi syscallAnswer
B
0x42 is 66, the code of B, and it replaces the first byte of the message.
-
What does
shr eax, 8do?Answer
It shifts eax right by eight bits
shr moves every bit toward the low end and fills the top with zeros, so the value is divided by 256.
-
What does this program print?
default rel section .data msg db "X", 10 section .text global _start _start: mov eax, 0x12345658 and eax, 0xFF mov [msg], al mov rax, 1 mov rdi, 1 mov rsi, msg mov rdx, 1 syscall mov rax, 60 xor rdi, rdi syscallAnswer
X
The low byte of 0x12345658 is 0x58, which is 88, the ASCII code of X.
-
Which prefix writes a hexadecimal number in NASM?
Answer
0x
NASM reads 0x as hex, 0b as binary and a bare number as decimal.
The snippets on this page are fragments, not whole programs: they show the number bases and masks. They were assembled with NASM 3.01 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev