CSSE 132 Mini-reading

Assembly Basics in 10 Minutes

Review how C becomes an executable, how ARM64 uses registers, and how ldr and str move data between registers and memory.

Why This Matters

Assembly exposes the small steps a processor performs to run a program. In CSSE 132, you will translate short instruction sequences into pseudocode, track values in registers and memory, and connect C code to the instructions that the computer executes.

Core Idea

The processor performs calculations in registers, which are small storage locations inside the processor. Most assembly code repeats the same basic pattern: load data from memory into registers, operate on that data, and store a result back to memory.

wN registers hold data

w0 through w30 name the lower 32 bits of the corresponding x registers. In this course, use a w register for a 32-bit data value.

xN registers hold addresses

x0 through x30 are 64-bit registers. An address is a 64-bit value in our ARM64 environment, so use an x register when it identifies a memory location.

The brackets in [x2] mean “the memory at the address stored in x2.” Without brackets, x2 means the value inside the register itself. Keeping those two meanings separate is the key to tracing loads and stores.

Worked Examples

Example 1: From C source to an executable

StepInputOutputTool
CompileC source (.c)Assembly source (.s)Compiler
AssembleAssembly source (.s)Object file (.o)Assembler
LinkObject file(s) and librariesExecutableLinker

Each step changes the program's representation. Linking also combines your object code with needed library code to produce a runnable program.

Example 2: Move and calculate in registers

mov w0, 11
mov w1, 7
add w2, w0, w1
sub w3, w2, 4

Trace one instruction at a time. The first two instructions put 11 in w0 and 7 in w1. Then w2 = 11 + 7 = 18, and finally w3 = 18 - 4 = 14. The destination register appears first, so the last instruction changes w3, not w2.

Example 3: Load, change, and store a value

Assume x2 contains the address 0x400, and memory at that address contains the 32-bit value 9.

ldr w0, [x2]
add w0, w0, 1
str w0, [x2]
  1. ldr reads 9 from memory at 0x400 into w0.
  2. add changes the register value to 10.
  3. str writes 10 from w0 to memory at 0x400.

The address in x2 remains 0x400. The instructions read and update the data at that address.

Example 4: Use an offset

An offset adds a byte distance to a base address:

str w1, [x2, 4]
ldr w3, [x2, 8]

If x2 = 0x400, the store writes the data from w1 to address 0x404. The load reads data from address 0x408 into w3. The offset changes the address used for that memory access; it does not change x2.

Example 5: Trace a swap through memory

sub sp, sp, #16

mov w3, 5
str w3, [sp]
mov w3, 12
str w3, [sp, #4]

ldr w3, [sp]
str w3, [sp, #8]
ldr w3, [sp, #4]
str w3, [sp]
ldr w3, [sp, #8]
str w3, [sp, #4]

add sp, sp, #16

The first and last instructions reserve and release 16 bytes while keeping sp aligned for ARM64.

The first two stores create values 5 and 12 at offsets 0 and 4. The next store saves 5 temporarily at offset 8. The final four instructions copy 12 to offset 0, then copy the saved 5 to offset 4. In pseudocode:

x = 5;
y = 12;
temp = x;
x = y;
y = temp;

Common Mistakes

Using a w register as an address

Addresses are 64-bit values in ARM64. In this course, use xN for an address and wN for 32-bit data.

Reversing ldr and str

ldr copies from memory to a register. str copies from a register to memory.

Forgetting that offsets count bytes

In [x2, 4], the effective address is the value in x2 plus 4 bytes. The offset is not a register number or an item count.

Changing the base register by accident

ldr w0, [x2, 4] uses the address x2 + 4, but this form leaves x2 unchanged.

Quick Reference

ARM64 formMeaning used in CSSE 132
mov w0, 11Put the data value 11 in w0
add w0, w1, 14w0 = w1 + 14
sub w0, w1, 14w0 = w1 - 14
ldr w0, [x2]Read data at the address in x2 into w0
str w1, [x2]Write data from w1 to the address in x2
str w0, [x2, 4]Write data from w0 to address x2 + 4

Register reminder: x2 and w2 are two views of the same register. Use the 64-bit x2 view for an address and the lower 32-bit w2 view for data.

Check Yourself

Try these before opening the answers. The values differ from Worksheet 04.

  1. What file does the assembler take as input, and what file does it produce?
  2. After mov w0, 8 and add w1, w0, 6, what values are in w0 and w1?
  3. Assume x4 = 0x500. Describe str w2, [x4, 12], including the source and destination.
  4. A student writes ldr x0, [w2] to load a 32-bit data value. Which register widths should change for the course convention?
  5. Memory at [sp] contains 20. What value is there after ldr w5, [sp], sub w5, w5, 3, and str w5, [sp]?

Answers

Show the answers
  1. It takes assembly source, normally a .s file, and produces an object file, normally a .o file.
  2. w0 remains 8, and w1 becomes 14. The destination of add is w1.
  3. It writes the 32-bit data from w2 to memory at 0x500 + 12 = 0x50C. The offset is measured in bytes.
  4. Use ldr w0, [x2]: w0 receives 32-bit data, and x2 holds the 64-bit address.
  5. 17. The load copies 20 into w5, the subtraction produces 17, and the store writes 17 back to memory.

Next Step

Complete Worksheet 04: ARM64 Assembly Basics. For every ldr or str, write the effective address and draw an arrow showing the direction of the data transfer.