← Runtimes
Runtimes

Reading a little assembly

Arguments, selects, and divisions in an optimized ARM64 listing.

Follow values through a short optimized listing.

Outcome

Match a short optimized listing back to its source: argument and result registers, shifted operands, branch-free selects, and the rounding correction in signed division.

Terminology (7)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Listing
The assembly text a compiler emits for compiled code, one instruction per line.
Calling convention
The rules for which registers carry a function’s arguments and result.
Shifted operand
An AArch64 operand such as w1, lsl #2, shifted before the instruction uses it.
Conditional select
An instruction such as csel that chooses between two registers using flags set by an earlier comparison.
Arithmetic shift right
A right shift that copies the sign bit, so it divides by a power of two rounding down.
Inlining
Replacing a call with the called function’s body, so the function may have no listing of its own.
Plan 9 syntax
The assembler notation Go’s tools print: destination last, R0… for registers, $ for constants.

Read instructions as changes to values

Assembly is a textual notation for a processor’s instructions. Its operation names identify actions, and its operands name registers, constants or memory locations. A calling convention specifies how a function receives arguments and returns a result. To understand a short function, start with those incoming values and follow each instruction’s reads and writes to the returned value.

An optimized listing can combine or rearrange source operations while preserving their required behavior. Its syntax and register conventions depend on the target and toolchain. Here the recorded ARM64 listings sit beside their Go, Rust or C++ source; the native notes supply the operand conventions needed to read each one.

01Arguments and results

Where do the arguments go?

A checkout service is replacing a platform-specific carrier-rate helper. Its compatibility review must preserve the calculations for parcel dimensions, billed weight and shipment adjustments. A reviewer sees no multiplication instruction in the girth listing and suspects missing work. Trace that result before deciding whether the replacement agrees with the reference.

The review first checks girth, twice the sum of width and height. The source and its reference listing are both available here, so you can establish how to follow a value before reconstructing other helpers. On AArch64 a function receives its first integer arguments in registers and returns its result in the first one. Each line of a listing is one instruction: an operation, then its operands. A shifted operand such as w1, lsl #2 means the value shifted left by 2 bits, which is a multiplication by 4.

Go’s compiler prints listings in the Plan 9 assembler syntax. Its internal calling convention passes arguments in R0, R1, … and returns in R0, and it writes the destination register last: ADD R0, R1, R2 means R2 = R1 + R0.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// A parcel's girth, 2 × (width + height), and its optimized listing.
package main

import "fmt"

func girth(width, height int32) int32 {
	return 2 * (width + height)
}

// go build -gcflags=-S, go1.27.3, GOARCH=arm64:
//
//	TEXT  main.girth(SB), ABIInternal
//	ADD   R0, R1, R1
//	ADD   R1, R1, R0
//	RET

func main() {
	fmt.Println(girth(3, 4))
}
Which instruction performs the multiplication by 2?
Go · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
GOARCH=arm64 go build -gcflags=-S . 2>&1 | less

The carrier integration is an authored scenario. Its reference listings are real compiler output recorded from this lesson’s source functions on an ARM64 Mac with the toolchains named in each comment. Go cross-compiles with GOARCH=arm64 on any machine; the Rust and C++ commands target the host by default and can print a different instruction set on another machine. Rust and C++ listings come from the same functions compiled as exported library functions, because in a whole program the optimizer inlines these small functions into main. Compiler Explorer (godbolt.org) shows the same view in a browser. The examples use small inputs whose arithmetic fits the 32-bit operands of the recorded target; they do not establish behavior after signed overflow. TypeScript is not offered because this lesson compares ahead-of-time native release artifacts; a JavaScript engine’s generated code needs a separate JIT-specific investigation.

02Decisions

Where did the if go?

The review next checks light parcels, where item plus packaging weighs less than the carrier’s 10-ounce minimum. The replacement must preserve that minimum even when optimized control flow looks different. An if does not have to become a branch. A compiler can compute both possible results and pick one with a conditional select: csel wd, wa, wb, cond writes wa to wd when the condition held at the last comparison, and wb otherwise. The comparison sets flags; the select reads them. A select expresses this choice without a conditional jump. That observation alone does not establish the speed of the complete helper.

This recorded Go build keeps a branch for the minimum-weight check. CMPW $10, R0 compares R0 with 10, and BGE 20 jumps to byte offset 0x14 (20) when R0 is greater than or equal to 10.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// Billed weight: item plus packaging, never less than 10 ounces, and its optimized listing.
package main

import "fmt"

func billedOunces(item, packaging int32) int32 {
	sum := item + packaging

	if sum < 10 {
		return 10
	}

	return sum
}

// go build -gcflags=-S, go1.27.3, GOARCH=arm64 (byte offsets on the left):
//
//	TEXT  main.billedOunces(SB), ABIInternal
//	0x00  ADD   R1, R0, R0
//	0x04  CMPW  $10, R0
//	0x08  BGE   20
//	0x0c  MOVD  $10, R0
//	0x10  RET
//	0x14  RET

func main() {
	fmt.Println(billedOunces(1, 3), billedOunces(8, 9))
}
Following the listing, what does billedOunces return when the item and its packaging weigh 4 ounces together?

03Signed division

Why does x / 4 take four instructions?

Positive adjustments match in the port, but a 5-cent shipment credit split across four parcels gives a different whole-cent amount. Inspect the signed and unsigned helpers together to find what the final shift alone leaves out. Dividing by a power of two can be a right shift, and for unsigned values it is exactly that. Signed division is different. The languages here all define integer / to round toward zero, so −5 / 4 is −1. An arithmetic shift right rounds down instead: −5 >> 2 is −2. To match /, the compiler adds divisor − 1 to negative values before shifting.

Go spells the correction with bitfield instructions. SBFX $31, R0, $1, R1 copies the sign bit across a register (all ones for a negative amount); the shifted ADD adds its top bits, 3 for a credit and 0 otherwise; the second SBFX is the arithmetic shift. UBFX is the unsigned shift.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// A shipment adjustment split over four parcels, signed (credits are negative) and
// unsigned, with their optimized listings.
package main

import "fmt"

func perParcel(cents int32) int32 {
	return cents / 4
}

func perParcelUnsigned(cents uint32) uint32 {
	return cents / 4
}

// go build -gcflags=-S, go1.27.3, GOARCH=arm64:
//
//	TEXT  main.perParcel(SB), ABIInternal
//	SBFX  $31, R0, $1, R1
//	MOVWU R1, R1
//	ADD   R1>>30, R0, R1
//	SBFX  $2, R1, $30, R0
//	RET
//
//	TEXT  main.perParcelUnsigned(SB), ABIInternal
//	UBFX  $2, R0, $30, R0
//	RET

func main() {
	fmt.Println(perParcel(9), perParcel(-5), perParcelUnsigned(9))
}
Use these listings for a new shipment adjustment: signed cents = −11, and unsigned cents = 11. What do the two per-parcel helpers return?

04Practice

Rebuild the shipping helpers

Continue the carrier integration’s port with three helpers whose reference release listings are supplied above their editable functions. Reconstruct the quote, then its flat-rate ceiling, then the signed count of whole eight-hour shifts before a pickup cutoff. These are fresh listings: the ceiling chooses the opposite side of a threshold from the reading’s billed-weight minimum, and the shift helper has its own divisor and negative-input cases. Command parsing and completed helper functions are supplied.

The shipping-rate library

Rebuild the quote, the flat-rate ceiling and the count of shifts before the pickup cutoff from their listings.

  1. easy · R-ASM-V4-1 Rebuild the quote
  2. medium · R-ASM-V4-2 Rebuild the flat-rate ceiling
  3. hard · R-ASM-V4-3 Rebuild the shift count
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.