Arithmetic, Logic, and Condition Codes


1. Recap: What is Assembly?

  • Assembly is a low-level, human-readable language that closely reflects the binary machine code executed by the CPU.
  • Operates on registers—64-bit “scratchpad” storage inside the processor.
  • Assembly instructions correspond to operations on data inside registers or memory.

2. Data Sizes and Register Sizes

Terminology:

Size NameBytesSuffix
Byte1b
Word2w
Double4l
Quad8q

Register Naming by Size (example with %rax):

SizeRegister
64-bit%rax
32-bit%eax
16-bit%ax
8-bit%al

3. Register Responsibilities

  • %rax – Return value
  • %rdi – First argument
  • %rsi – Second argument
  • %rdx – Third argument
  • %rsp – Stack pointer (top of stack)
  • %rip – Instruction pointer

4. mov Variants and Data Transfers

Suffixes:

  • movb, movw, movl, movq

Special Instructions:

movabsq $0x1122334455667788, %rax
  • Use movabsq to load a full 64-bit immediate constant into a register.

5. Zero and Sign Extension

movz (zero-extend)

InstructionEffect
movzbwbyte → word
movzblbyte → double
movzwqword → quad

movs (sign-extend)

InstructionEffect
movsbwbyte → word
movsblbyte → double
movslqdouble → quad
cltq   ; Sign-extend %eax into %rax

6. lea: Load Effective Address

lea 6(%rax), %rdx
  • Not a memory access — computes the address expression and places the result into the destination.
  • Useful for address arithmetic.

lea vs mov Comparison:

Expressionmov Meaninglea Meaning
6(%rax), %rdxRead from memory at addressCompute 6 + %rax
(%rax,%rcx),%rdxRead from address %rax + %rcxCompute %rax + %rcx
(%rax,%rcx,4)Read from %rax + 4 * %rcxCompute %rax + 4 * %rcx
7(%rax,%rax,8)Read from 7 + %rax + 8 * %raxCompute 7 + %rax + 8 * %rax

7. Unary Arithmetic Instructions

incq 16(%rax)
dec %rdx
neg %rax
not %rcx
InstructionDescription
incIncrement
decDecrement
negNegate (2’s complement)
notBitwise NOT

8. Binary Arithmetic and Logic

addq %rcx, (%rax)
xorq $16, (%rax, %rdx, 8)
subq %rdx, 8(%rax)
InstructionEffect
addD ← D + S
subD ← D - S
imulD ← D * S
xorD ← D ^ S
or`D ← D
andD ← D & S

9. Multiplication and Division

64-bit x 64-bit = 128-bit product:

  • Result split across %rdx:%rax.
imulq %rbx        ; Signed multiply: %rax × %rbx → %rdx:%rax
mulq  %rbx        ; Unsigned multiply

10. Division and Remainder

Usage:

  • %rdx:%rax is the dividend
  • Operand is the divisor
  • %rax gets the quotient
  • %rdx gets the remainder
cqto            ; Sign-extend %rax into %rdx
idivq %rdi      ; Signed divide
divq  %rdi      ; Unsigned divide

11. Bit Shifts

shll $3, (%rax)           ; logical left shift
shr %cl, (%rax,%rdx,8)    ; logical right shift
sar $4, 8(%rax)           ; arithmetic right shift
InstructionMeaning
sal / shlLeft shift
shrLogical right shift (zero-fill)
sarArithmetic right shift (sign-extend)
  • Shift amount can be an immediate or stored in %cl.

Note on %cl as Shift Amount:

  • %cl = 0xFF
  • shlb uses only the lowest 3 bits → shift by 7
  • shlw uses 4 bits → shift by 15
  • Higher-width shifts use more bits.

12. Code Examples from Lecture

C Code:

int add_to_first(int x, int arr[]) {
    int sum = x;
    sum += arr[0];
    return sum;
}

Division Example:

long full_divide(long x, long y, long *remainder_ptr) {
    long quotient = x / y;
    long remainder = x % y;
    *remainder_ptr = remainder;
    return quotient;
}