A register isn’t a C variable. It’s a physical slot inside the CPU with a fixed width, overlapping sub-registers, and strict encoding rules.
In C99, a uint64_t variable lives wherever the compiler decides to put it: in a register, on the stack, or nowhere at all if its value can be computed at compile time. In assembly, you pick the register yourself—and that choice affects not only the result, but also the byte size of the machine instruction. In this lesson, I break down the x86-64 register file, the overlapping views of a single register, and the encoding trap that stops mov r8b, ah from assembling.
Mental Model: C99 Types vs. Register Views
| What it looks like in C99 | What happens in hardware |
|---|---|
uint64_t x is a single 8-byte variable |
rax is a 64-bit register; its lower slices are exposed as eax, ax, ah, and al |
x = 0x11223344 just changes the value |
Writing to eax clears the upper 32 bits of rax; writing to ax or al leaves the remaining bits intact |
uint8_t and uint16_t are independent types |
ah and al are two bytes inside ax; ax is the lower 16 bits of eax |
| A variable name uniquely identifies a location | The same register can be reused by different instructions and change roles throughout a function |
x86-64 provides 16 GPRs. Together, they form the register file.
Sub-Register Windows of rax
The lower slices of rax overlap:
Bits: 63 32 31 16 15 8 7 0
┌─────────────────┬─────────────────────────────────────────────────────┐
rax │ upper 32 bits │ eax (31..0) │
└─────────────────┴─────────────────────────────────────────────────────┘
┌──────────┬──────────────────────────────────────────┐
eax │ 31..16 │ ax (15..0) │
└──────────┴──────────────────────────────────────────┘
┌─────────────────────┬────────────────────┐
ax │ ah (15..8 / Byte 1) │ al (7..0 / Byte 0) │
└─────────────────────┴────────────────────┘Each row of the diagram adds a finer slice: the upper 32 bits and eax, then the upper 16 bits and ax, then the two bytes ah and al. The vertical borders in every row align on the exact same bit boundaries, making it obvious these aren’t separate registers—just halves of ax.
| 64-bit | 32-bit | 16-bit | High byte | Low byte |
|---|---|---|---|---|
rax |
eax |
ax |
ah |
al |
rbx |
ebx |
bx |
bh |
bl |
rcx |
ecx |
cx |
ch |
cl |
rdx |
edx |
dx |
dh |
dl |
The remaining registers are structured like this:
rsi,rdi,rbp, andrspexposeesi/si/sil,edi/di/dil,ebp/bp/bpl, andesp/sp/spl; high-byte registerssih,dih,bph,sphdo not exist;r8–r15exposer8d,r8w, andr8b.
rip and rflags are not part of the 16 GPRs: rip holds the address of the next instruction, while rflags holds status and control bits. They are dedicated registers with their own roles, not general-purpose storage slots.
Mechanics: mov and Register Windows
Our sample program regs_lab.asm runs a sequence of writes into different windows of rax:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; regs_lab.asm — rax/eax/ax/ah/al windows and writing to eax.
default rel
%define SYS_EXIT 60
section .text
global _start
_start:
mov rax, -1
mov eax, 0x11223344
mov ax, 0x5566
mov al, 0x77
mov ah, 0x88
mov rax, 42
; syscall: exit(0)
mov eax, SYS_EXIT
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbitsBuild the binary, stop at _start in GDB, and single-step through the instructions:
$ cd lessons/03-registers
$ make build
$ gdb -q --nx -batch ./build/regs_lab \
-ex 'set debuginfod enabled off' \
-ex 'set pagination off' \
-ex 'break _start' -ex 'run' \
-ex 'printf "start: "' -ex 'p/x $rax' \
-ex 'stepi' -ex 'printf "mov rax, -1: "' -ex 'p/x $rax' \
-ex 'stepi' -ex 'printf "mov eax, 0x11223344: "' -ex 'p/x $rax' \
-ex 'stepi' -ex 'printf "mov ax, 0x5566: "' -ex 'p/x $rax' \
-ex 'stepi' -ex 'printf "mov al, 0x77: "' -ex 'p/x $rax' \
-ex 'stepi' -ex 'printf "mov ah, 0x88: "' -ex 'p/x $rax' \
-ex 'stepi' -ex 'printf "mov rax, 42: "' -ex 'p/x $rax'Three flags ensure a reproducible, non-interactive run:
-q— suppresses the startup version banner;--nx— skips~/.gdbinitand the system config so personal aliases don’t alter output;-batch— runs every command passed via-exand exits without entering an interactive prompt.
The set debuginfod enabled off command disables network queries to distro debug-symbol servers (otherwise GDB can hang waiting on debuginfod), and set pagination off disables paged output prompts. Each -ex passes one debugger command, letting the entire script fit on a single command line. In lesson 02, -q --nx was wrapped into the gdb-clean alias; stepping commands like break, run, stepi, and interactive GDB usage are covered in detail in lesson 05.
Abbreviated output (GDB status lines and repeated source echo omitted; values are real):
start: $1 = 0x0
mov rax, -1: $2 = 0xffffffffffffffff
mov eax, 0x11223344: $3 = 0x11223344
mov ax, 0x5566: $4 = 0x11225566
mov al, 0x77: $5 = 0x11225577
mov ah, 0x88: $6 = 0x11228877
mov rax, 42: $7 = 0x2aThe 0x0 at start just reflects rax in this specific environment, not a specification guarantee: the OS isn’t required to zero registers before jumping to _start.
mov rax, -1yields0xffffffffffffffffbecause negative numbers on x86-64 are stored in two’s complement;xor edi, ediat the end zeros the exit code:ediis the 32-bit window ofrdi, and XORing a register with itself always produces zero.
Let’s unpack the GDB syntax in this output:
p/x $raxis shorthand forprint /x:$denotes a register, and/xrequests hexadecimal format;$1,$2,$3aren’t register names—they’re value history entries: GDB numbers every printed result so you can reference it later;info registers raxprints two columns:0x11228877is the hex value, and287475831is the same number in decimal.
The key takeaway is clear: writing to eax cleared the upper 32 bits, so GDB shows 0x11223344 (0x0000000011223344). This behavior is called zero-extension. Writing to ax, al, or ah has no such effect: all other bits remain untouched.
The same register in GDB’s full info registers output:
$ gdb -q --nx -batch ./build/regs_lab \
-ex 'set debuginfod enabled off' \
-ex 'set pagination off' -ex 'break _start' -ex 'run' \
-ex 'stepi' -ex 'stepi' -ex 'stepi' -ex 'stepi' -ex 'stepi' \
-ex 'info registers rax'
rax 0x11228877 287475831How It’s Encoded
Here are the machine bytes. mov rax, 42 doesn’t turn into a 10-byte instruction carrying a full 64-bit immediate: the assembler sees that 42 fits in 32 bits and picks the shorter zero-extending form.
$ objdump -d -M intel build/regs_lab | sed -n '/<_start>:/,/syscall/p'
0000000000401000 <_start>:
401000: 48 c7 c0 ff ff ff ff mov rax,0xffffffffffffffff
401007: b8 44 33 22 11 mov eax,0x11223344
40100c: 66 b8 66 55 mov ax,0x5566
401010: b0 77 mov al,0x77
401012: b4 88 mov ah,0x88
401014: b8 2a 00 00 00 mov eax,0x2a
401019: b8 3c 00 00 00 mov eax,0x3c
40101e: 31 ff xor edi,edi
401020: 0f 05 syscallInstruction size and encoding depend on the value and write width:
| Instruction | Bytes | Encoding form |
|---|---|---|
mov rax, 42 |
b8 2a 00 00 00 |
5 bytes: b8 plus a 32-bit immediate, upper 32 bits cleared by zero-extension; used for any unsigned constant up to 0xFFFFFFFF |
mov rax, -1 |
48 c7 c0 ff ff ff ff |
7 bytes: 64-bit operand prefix (48; see REX below), opcode c7, the c0 byte encoding the destination register, and a 32-bit signed immediate sign-extended to 64 bits |
mov rax, 0x1122334455667788 (example only, not in regs_lab.asm) |
48 b8 88 77 66 55 44 33 22 11 |
10 bytes: general case for values that don’t fit in 32 bits; 64-bit operand prefix plus b8 and a full 64-bit immediate (objdump calls this movabs) |
mov ax, 0x5566 |
66 b8 66 55 |
the 66 prefix switches operand width to 16 bits, followed by b8 and a 16-bit immediate |
Without the 64-bit prefix, b8 writes to eax and zeros the upper 32 bits of rax, so the short form can carry at most 32 significant bits. For -1, it would produce 0x00000000ffffffff, so the assembler picks the 7-byte 48 c7 c0 sign-extending form instead.
The REX Prefix and ah
To address r8–r15 or sil/dil/bpl/spl, the CPU needs a REX. That’s why this instruction fails to assemble:
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
; rex_bad.asm — intentional error: ah cannot be combined with a REX prefix.
default rel
section .text
global _start
_start:
mov r8b, ah
; syscall: exit(0)
mov eax, 60
xor edi, edi
syscall
section .note.GNU-stack noalloc noexec nowrite progbits$ nasm -f elf64 -o build/rex_bad.o rex_bad.asm
rex_bad.asm:11: error: cannot use high byte register in this instructionNASM 3.02 reports that exact error. It’s not that “ah is banned outright”—it’s an encoding conflict: r8b requires REX, and ah cannot be encoded alongside REX. For the low byte of r8, pair r8b with, say, al, or switch to r8w/r8d.
In the ModR/M byte, the 3-bit codes 100–111 change meaning depending on whether a REX prefix is present:
ModR/M code |
Without REX |
With REX (0x40..0x4F) |
With REX + extension bit |
|---|---|---|---|
100 (4) |
ah |
spl |
r12b |
101 (5) |
ch |
bpl |
r13b |
110 (6) |
dh |
sil |
r14b |
111 (7) |
bh |
dil |
r15b |
That extension bit is REX.B for the r/m field and REX.R for the reg field—it’s what turns spl into r12b. The moment an instruction requires REX (mandatory for r8b), the lookup table switches to spl–dil, and encodings for ah–bh cease to exist in that instruction.
C99 Parallel: The Same Windows as Bitmasks
In C99, you would have to emulate those same register windows using bitmasks:
uint64_t value = UINT64_MAX; /* 0xffffffffffffffff */
value = UINT32_C(0x11223344); /* rax = 0x0000000011223344 */
value = (value & ~UINT64_C(0xFFFF)) | UINT64_C(0x5566);
value = (value & ~UINT64_C(0xFF)) | UINT64_C(0x77);
value = (value & ~UINT64_C(0xFF00)) | UINT64_C(0x8800);Assembly accomplishes the same thing in one instruction, with one key difference: writing to eax isn’t a “mask”—it’s the architectural zero-extension rule. The C99 compiler knows this and likewise emits 32-bit instructions whenever the upper bits aren’t needed.
Common Pitfalls
-
“Writing to
eaxonly modifies the lower 32 bits.”
GDB fact: aftermov eax, 0x11223344,raxequals0x11223344, not0xffffffff11223344.
Cause: zero-extension on 32-bit writes.
Fix: remember the rule and don’t treateaxas an “independent variable”; if you need to preserve the upper bits, write toax/alor use explicit bitmasks. -
"
mov r8b, ahshould work—they’re both byte registers."
Fact: NASM fails witherror: cannot use high byte register in this instruction.
Cause: the REX prefix and high-byte registers are mutually exclusive.
Fix: never mixah/bh/ch/dhwithr8–r15orsil/dil; use low sub-registers that don’t require REX, or pick different registers. -
"
mov rax, 42always takes 10 bytes."
Fact:objdumpshowsb8 2a 00 00 00—five bytes disassembled asmov eax,0x2a.
Cause: the assembler selects the short zero-extending form whenever the immediate fits in 32 bits.
Fix: verify actual machine bytes withobjdumpinstead of guessing instruction size from source code. -
"
ahandalare the same thing."
Fact: aftermov ah, 0x88,raxbecomes0x11228877;alstays0x77, whileahoccupies bits 8–15.
Cause: they are two distinct bytes insideax.
Fix: sketch out the register windows and inspect$raxin GDB after each partial write.
Lab
Goal. Assemble a sequence of writes to rax, predict the value after each instruction, and verify your predictions in GDB.
Contract. Write regs_predict.asm with the following sequence:
mov rax, -1mov eax, 0xA1B2C3D4mov ax, 0x5A5Amov al, 0x0Fmov ah, 0xF0mov rax, 0x1234
Before running GDB, write down the expected $rax values after each instruction. Use gdb -q --nx, break _start, stepi, and p/x $rax. A ready-made testbed lives in lessons/03-registers/regs_predict.asm in the asm-course repository.
Verification criteria.
- The file assembles with
nasm -f elf64and links withld. - GDB shows the expected values after each partial write.
- After
mov eax, ..., the upper 32 bits are zero. objdump -d -M intelshows the short formmov eax,0x1234formov rax, 0x1234.mov r8b, ahin a separate file triggers a NASM error.
Hints, expected output, and walkthrough
Expected values for the sequence in the contract:
start: 0x0
mov rax, -1: 0xffffffffffffffff
mov eax, 0xA1B2C3D4: 0xa1b2c3d4
mov ax, 0x5A5A: 0xa1b25a5a
mov al, 0x0F: 0xa1b25a0f
mov ah, 0xF0: 0xa1b2f00f
mov rax, 0x1234: 0x1234Walkthrough:
mov eaxzeros the upper 32 bits;mov axreplaces the lower 16 bits;mov alreplaces the lower 8 bits;mov ahreplaces bits 8–15;mov rax, 0x1234overwrites the entire value, though it assembles into the shorter 32-bit form.
If your values differ, make sure you didn’t swap ah and al, and check info registers rax in GDB. Keep in mind that 0x0 upon entering _start is just this environment’s state, not a standard requirement.
Lab source code, Makefile, and automated tests for this lesson: lessons/03-registers.
Summary
- x86-64 has 16 eight-byte general-purpose registers;
raxexposeseax,ax,ah, andalwindows. - Writing to a 32-bit register clears the upper 32 bits of the 64-bit register (zero-extension).
- Writing to
ax,al, orahmodifies only those bits and leaves the rest untouched. mov rax, 42assembles into the shorter 32-bit formmov eax, 42because the value fits in 32 bits.ah/bh/ch/dhcannot be combined with the REX prefix required byr8–r15.
_start entry point, the syscall instruction, exit codes, and how the kernel hands control to your first instruction.