On this page

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, and rsp expose esi/si/sil, edi/di/dil, ebp/bp/bpl, and esp/sp/spl; high-byte registers sih, dih, bph, sph do not exist;
  • r8–r15 expose r8d, r8w, and r8b.

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:

regs_lab.asm
; 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 progbits

Build 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'
GDB flags in a batch session

Three flags ensure a reproducible, non-interactive run:

  • -q — suppresses the startup version banner;
  • --nx — skips ~/.gdbinit and the system config so personal aliases don’t alter output;
  • -batch — runs every command passed via -ex and 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 = 0x2a

The 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, -1 yields 0xffffffffffffffff because negative numbers on x86-64 are stored in two’s complement;
  • xor edi, edi at the end zeros the exit code: edi is the 32-bit window of rdi, and XORing a register with itself always produces zero.

Let’s unpack the GDB syntax in this output:

  • p/x $rax is shorthand for print /x: $ denotes a register, and /x requests hexadecimal format;
  • $1, $2, $3 aren’t register names—they’re value history entries: GDB numbers every printed result so you can reference it later;
  • info registers rax prints two columns: 0x11228877 is the hex value, and 287475831 is 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          287475831

How 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                	syscall

Instruction 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:

rex_bad.asm
; 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 instruction

NASM 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:

c
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

  1. “Writing to eax only modifies the lower 32 bits.”
    GDB fact: after mov eax, 0x11223344, rax equals 0x11223344, not 0xffffffff11223344.
    Cause: zero-extension on 32-bit writes.
    Fix: remember the rule and don’t treat eax as an “independent variable”; if you need to preserve the upper bits, write to ax/al or use explicit bitmasks.

  2. "mov r8b, ah should work—they’re both byte registers."
    Fact: NASM fails with error: cannot use high byte register in this instruction.
    Cause: the REX prefix and high-byte registers are mutually exclusive.
    Fix: never mix ah/bh/ch/dh with r8–r15 or sil/dil; use low sub-registers that don’t require REX, or pick different registers.

  3. "mov rax, 42 always takes 10 bytes."
    Fact: objdump shows b8 2a 00 00 00—five bytes disassembled as mov eax,0x2a.
    Cause: the assembler selects the short zero-extending form whenever the immediate fits in 32 bits.
    Fix: verify actual machine bytes with objdump instead of guessing instruction size from source code.

  4. "ah and al are the same thing."
    Fact: after mov ah, 0x88, rax becomes 0x11228877; al stays 0x77, while ah occupies bits 8–15.
    Cause: they are two distinct bytes inside ax.
    Fix: sketch out the register windows and inspect $rax in 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:

  1. mov rax, -1
  2. mov eax, 0xA1B2C3D4
  3. mov ax, 0x5A5A
  4. mov al, 0x0F
  5. mov ah, 0xF0
  6. mov 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.

  1. The file assembles with nasm -f elf64 and links with ld.
  2. GDB shows the expected values after each partial write.
  3. After mov eax, ..., the upper 32 bits are zero.
  4. objdump -d -M intel shows the short form mov eax,0x1234 for mov rax, 0x1234.
  5. mov r8b, ah in a separate file triggers a NASM error.
Hints, expected output, and walkthrough

Expected values for the sequence in the contract:

text
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:        0x1234

Walkthrough:

  • mov eax zeros the upper 32 bits;
  • mov ax replaces the lower 16 bits;
  • mov al replaces the lower 8 bits;
  • mov ah replaces bits 8–15;
  • mov rax, 0x1234 overwrites 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

  1. x86-64 has 16 eight-byte general-purpose registers; rax exposes eax, ax, ah, and al windows.
  2. Writing to a 32-bit register clears the upper 32 bits of the 64-bit register (zero-extension).
  3. Writing to ax, al, or ah modifies only those bits and leaves the rest untouched.
  4. mov rax, 42 assembles into the shorter 32-bit form mov eax, 42 because the value fits in 32 bits.
  5. ah/bh/ch/dh cannot be combined with the REX prefix required by r8–r15.
Next Up
We can manipulate registers now, though our program still can’t exit cleanly or talk to the kernel. In lesson 04, I break down the _start entry point, the syscall instruction, exit codes, and how the kernel hands control to your first instruction.