Assembly Language, Addressing Modes, and Reverse Engineering Review

Quiz Overview & Structure

  • Date and format: October 27; consists of 4 questions totaling 4040 marks.

  • Question breakdown:

    • Question 1: True or false addressing theoretical concepts.

    • Questions 2–4: Reverse engineering assembly to high-level C code (2525 out of 4040 marks), writing assembly code from C (55 marks), and core conceptual problems.

  • Reference sheet: Register names (88-bit, 1616-bit, 3232-bit, and 6464-bit) and utilizations (caller-saved, callee-saved, argument passing, return locations) are provided on the final page of the exam.

  • Register conventions: First argument in RDI, second in RSI, third in RDX, and function return values in RAX.

Operands & Addressing Modes

  • Register operand: A bare register name (e.g., RAX) accesses the register's direct value.

  • Absolute memory address: A numeric value without prefixes (e.g., 104) specifies a memory location whose contents are dereferenced.

  • Immediate operand: A numeric value prefixed with $ (e.g., 108108) denotes a constant immediate value rather than a memory reference.

  • Indirect addressing: A register in parentheses (RAX) dereferences the memory address contained in the register.

  • Displacement addressing: D(RAX)D(\text{RAX}) adds displacement DD to the register value to compute the target memory address.

  • Scaled index addressing: Follows the format D(Rb,Ri,S)D(\text{Rb}, \text{Ri}, S) with the effective address formula Rb+S×Ri+D\text{Rb} + S \times \text{Ri} + D.

    • If the scale factor SS is omitted, it defaults to 11.

    • Allowable scale values for SS are strictly 11, 22, 44, or 88.

    • Address calculations require proper base-16 (hexadecimal) arithmetic.

Instruction Suffixes & Move Constraints

  • Operand size suffixes:

    • b: Byte (11 byte / 88 bits).

    • w: Word (22 bytes / 1616 bits).

    • l: Long word / Double word (44 bytes / 3232 bits).

    • q: Quadword (88 bytes / 6464 bits).

  • Suffix matching: The instruction suffix must match the register size (e.g., movl with 3232-bit registers like EAX, movq with 6464-bit registers like RAX).

  • Move constraints:

    • Direct memory-to-memory moves (e.g., movw (%rax), (%rsp)) are illegal; operations must transfer through an intermediate register.

    • An immediate value cannot be a destination operand.

    • Register memory pointers must use the complete 6464-bit register name (e.g., (%rbx) rather than partial register portions).

Casting, Extension, and Truncation

  • Truncation: Transitioning from a larger data type to a smaller type (e.g., int to char); achieved by selecting only the needed low-order register bytes.

  • Expansion: Transitioning from a smaller data type to a larger type (e.g., char to int).

    • Signed expansion: Replicates the sign bit across higher-order bits (e.g., movsb...); applied by default to signed types.

    • Unsigned expansion: Pads higher-order bits with zeros (e.g., movzb...); applied when explicitly declared unsigned.

    • Zero-padding a 3232-bit destination (EAX) automatically zero-extends across the entire 6464-bit register (RAX).

Load Effective Address (LEA) & Arithmetic Optimization

  • Key characteristics of lea:

    • Computes memory addresses using the form Rb+S×Ri+D\text{Rb} + S \times \text{Ri} + D without reading or writing to memory.

    • Does not alter condition codes.

    • Commonly utilized for high-speed linear arithmetic.

  • Constant scaling optimization:

    • Multiplying by a compile-time constant is implemented via shift and addition instructions (lea or bitwise shifts) rather than explicit multiplication circuits to conserve processor cycles and power.

  • Register clearing:

    • Performing an exclusive OR on a register with itself (e.g., xorq %rdx, %rdx) clears the register to 00.

    • Compilers prefer xor over mov $0, %rdx because it produces a shorter machine-code instruction encoding.

Control Flow: Branching & Loops

  • Variable-based tracing: Disassemble and annotate assembly instructions using symbolic variable names (x, y, z) rather than register names to maintain clarity during reverse engineering.

  • Conditional branching (if-else):

    • Signaled by a comparison (cmp) or test instruction immediately followed by a conditional jump (jge, jle, etc.) jumping forward past alternative execution blocks.

    • Conditional moves (cmov) can implement selections without branching.

  • Loops (while):

    • Characterized by an initial deterministic jump forward to the loop test, followed by condition evaluation and a conditional jump backward to the loop body.

Questions & Discussion

  • Question regarding which lecture covered sign and zero extension padding:

    • Covered in lecture 12 alongside condition codes and explicit set instructions (sete, setg), where single-byte results are expanded into full integer returns.

  • Question regarding how to implement multiplication by non-power-of-two constants (e.g., 12b12b):

    • Addressing scales are restricted to 11, 22, 44, or 88. Multiplications by arbitrary constants must be decomposed into legal scale factors (e.g., using lea to compute 3b3b, then shifting or scaling by 44 and adding the base variable).

  • Question regarding the output of xor %rdx, %rdx:

    • XORing any register against itself evaluates to zero, effectively clearing the register.