Assembly Language, Addressing Modes, and Reverse Engineering Review
Quiz Overview & Structure
Date and format: October 27; consists of 4 questions totaling marks.
Question breakdown:
Question 1: True or false addressing theoretical concepts.
Questions 2–4: Reverse engineering assembly to high-level C code ( out of marks), writing assembly code from C ( marks), and core conceptual problems.
Reference sheet: Register names (-bit, -bit, -bit, and -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 inRSI, third inRDX, and function return values inRAX.
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.,) 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:
adds displacement to the register value to compute the target memory address.Scaled index addressing: Follows the format
with the effective address formula.If the scale factor is omitted, it defaults to .
Allowable scale values for are strictly , , , or .
Address calculations require proper base-16 (hexadecimal) arithmetic.
Instruction Suffixes & Move Constraints
Operand size suffixes:
b: Byte ( byte / bits).w: Word ( bytes / bits).l: Long word / Double word ( bytes / bits).q: Quadword ( bytes / bits).
Suffix matching: The instruction suffix must match the register size (e.g.,
movlwith -bit registers likeEAX,movqwith -bit registers likeRAX).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 -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.,
inttochar); achieved by selecting only the needed low-order register bytes.Expansion: Transitioning from a smaller data type to a larger type (e.g.,
chartoint).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 -bit destination (
EAX) automatically zero-extends across the entire -bit register (RAX).
Load Effective Address (LEA) & Arithmetic Optimization
Key characteristics of
lea:Computes memory addresses using the form
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 (
leaor 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 .Compilers prefer
xorovermov $0, %rdxbecause 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
setinstructions (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., ):
Addressing scales are restricted to , , , or . Multiplications by arbitrary constants must be decomposed into legal scale factors (e.g., using
leato compute , then shifting or scaling by 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.