3 Assembly Basics
3 Assembly Basics
PROGRAM REPRESENTATION
ASSEMBLY: BASICS
JIALIANG TAN SPRING 2025
CSE202
OUTLINE
✦ History of Intel family processors
✦ C, Assembly, and Machine code
✦ Assembly Language Basics
✦ Registers and Operands
✦ Data movement instructions
✦ Arithmetic operation instructions
✦ Logical operation instructions
Lehigh University Spring 2025 2
CSE202
LEARNING OUTCOMES
✦ Analyze simple assembly programs
✦ Reverse engineer assembly code into its C code
equivalent
✦ Generate assembly code for a given C code
✦ Write simple assembly programs
✦ Identify security issues related to mixing data
and code
prog1.i/prog2.i
Compiler - cc1
prog1.s/prog2.s
Assembler - as
prog1.o/prog2.o
Disassembler - objdump
Linker - ld
prog (a.out)
Lehigh University Spring 2025 7
CSE202 ASSEMBLY BASICS
C - Assembly - Machine code
✦ Disassembling object code back to assembly
> objdump -d prog.o
✦ Useful to examine object les
✦ Analyze bit patterns of sequence of
instructions
✦ Produces approximate assembly code
✦ Can be run on .o or executable les (a.out)
.string "%d"
.text
.globl main
main:
.LFB0:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movq $20, -4(%rbp)
movq $25, -8(%rbp)
movq -8(%rbp), %rdx
movq -4(%rbp), %rax
movq %rdx, %rsi
> gcc -S test.c movq %rax, %rdi
call sum
movq %rax, %rsi
movq $.LC0, %rdi
movq $0, %rax
#include <stdio.h> call printf
movq $0, %rax
int sum(long, long);
ret
int main(){ sum:
int x = 20; .LFB1:
int y = 25; pushq %rbp
printf("%d",sum(x,y)); movq %rsp, %rbp
return 0; movq %rdi, -4(%rbp)
} movq %rsi, -8(%rbp)
movq -8(%rbp), %rax
int sum(long a, long b){ movq -4(%rbp), %rdx
return a+b; addq %rdx, %rax
} popq %rbp
Lehigh University Spring 2025
test.c ret 9
test.s
CSE202 ASSEMBLY BASICS
C - Assembly - Machine code
C language
int sum(int a, int b){
return a+b;
}
Assembly language
pushq %rbp
movq %rsp, %rbp
movq %rdi, -4(%rbp)
movq %rsi, -8(%rbp)
movq -8(%rbp), %rax
movq -4(%rbp), %rdx
addq %rdx, %rax
popq %rbp
ret
Object code
0 : A0 05
2 : 48 05 04
5 : . . .
16 : 60 02 00
Lehigh University Spring 2025 19 : C3 10
CSE202 ASSEMBLY BASICS
C - Assembly - Machine code
#include <stdio.h> test.c
#include <stdlib.h>
Data Instructions
Stack
REGISTER FILE PC(RIP) Heap
Address Bus
Data
IR Code
ARITHMETIC
AND LOGIC
UNIT Memory Frame of a
CONTROL program
UNIT
CPU
✦ Assembly code
✦ Instructions - Manipulation of
operands (data)
%rax
63 55 54 48 47 40 39 32 31 24 23 16 15 87 0
%al
%ax
%eax
%rax
Lehigh University Spring 2025 18
CSE202 ASSEMBLY BASICS
Registers
%rax %eax %ax %al
char Byte b 1
short Word w 2
int Double word l 4
long Quad word q 8
char* Quad word q 8
float Single precision s 4
double Double precision l 8
Lehigh University Spring 2025 23
fi
CSE202 ASSEMBLY BASICS
Operands
Instructions may manipulate different types
of operands
✦ Immediate values- constant integer data
($0x40)
✦ Register values - one of the 16 integer
registers (%rax to %r15)
✦ Memory values - up to 8 consecutive
bytes of memory at the address stored in
a register
Lehigh University Spring 2025 24
CSE202 ASSEMBLY BASICS
Operands s: 1, 2, 4, or 8
Type Source Form Operand value
Instruction Description
fi
CSE202 ASSEMBLY BASICS
Data Movement Operations
void swap(long *xp, long *yp){
long t0 = *xp;
long t1 = *yp;
*xp = t1;
*yp = t0
} xp in %rdi, yp in %rsi
swap:
movq (%rdi), %rax -- %rax=*xp
movq (%rsi), %rdx -- %rdx=*yp
movq %rdx, (%rdi) -- (%rdi)=*yp
movq %rax, (%rsi) -- (%rsi)=*xp
Lehigh University
ret Spring 2025 31
CSE202 ASSEMBLY BASICS
Data Movement Operations
void swap(long *xp,
long *yp){
0x100 25
long t0 = *xp;
long t1 = *yp; …
*xp = t1;
…
*yp = t0
} 0x200 75
%rsi = 0x200
%rax = 25 swap:
movq (%rdi), %rax -- %rax=*xp
%rdx = 75 movq (%rsi), %rdx -- %rdx=*yp
movq %rdx, (%rdi) --(%rdi)=*yp
movq %rax, (%rsi) --(%rsi)=*xp
Lehigh University ret
Spring 2025 32
CSE202 ASSEMBLY BASICS
Data Movement Operations
void swap(long *xp,
long *yp){
0x100 75
long t0 = *xp;
long t1 = *yp; …
*xp = t1;
…
*yp = t0
} 0x200 25
%rsi = 0x200
%rax = 25 swap:
movq (%rdi), %rax -- %rax=*xp
%rdx = 75 movq (%rsi), %rdx --%rdx=*yp
movq %rdx, (%rdi) --(%rdi)=*yp
movq %rax, (%rsi) --(%rsi)=*xp
Lehigh University ret
Spring 2025 33
CSE202 ASSEMBLY BASICS
Data Movement Operations
✦ Reverse engineer the assembly code into its
equivalent C code
void decode(long *xp, long*yp, long *zp)
xp in %rdi, yp in %rsi, zp in %rdx
decode:
movq (%rdi),%r8
movq (%rsi),%rcx
movq (%rdx),%rax
movq %r8,(%rsi)
movq %rcx,(%rdx)
movq %rax,(%rdi)
Lehigh University Spring 2025 34
CSE202 ASSEMBLY BASICS
Data Movement Operations
✦ Pushing and Popping (Stack Data)
0x100 25 0x100 25
0x108 17 0x108 17
0x110 42 0x110 42
0x118 31 0x118 31
0x120 22 0x120 22
0x108 17 0x108 17
0x110 42 0x110 42
0x118 31 0x118 31
0x120 22 0x120 22
pop()
%rsp = 0x100
%rsp = 0x108
scale:
leaq (%rdi,%rsi, 4), %rax -- %rax = 4*y+x
leaq (%rdx,%rdx, 2), %rdx -- %rdx = 2*z+z=3z
leaq (%rax,%rdx, 4), %rax -- %rax = 12z+4*y+x
ret
arith:
xorq %rsi,%rdi #%rdi=x^y(t1)
leaq (%rdx,%rdx, 2),%rax #%rax= z+2z= 3z
salq $4,%rax #%rax= 16*3z= 48z (t2)
andl $252645135, %rdi #%rdi = t1 & 0x0F0F0F0F(t3)
subq %rdi, %rax #%rax = t2 - t3 (t4)
ret #%rax = t4
Lehigh University Spring 2025 42
CSE202 ASSEMBLY BASICS
Arithmetic and Logical Operations
long arith2(long x, long y, long z){
long t1 = ???; long t2 = ???;
long t3 = ???; long t4 = ???;
return t4;
}
x in %rdi, y in %rsi, z in %rdx
arith2:
orq %rsi,%rdi
sarq $3,%rdi
notq %rdi
movq %rdx, %rax
subq %rdi, %rax
ret
Lehigh University Spring 2025 43
CSE202 ASSEMBLY BASICS
Arithmetic and Logical Operations
Type Instruction Effect Description
Signed full
imulq src R[%rdx]:R[%rax] = src * R[%rax]
multiply
decode:
subq %rdx,%rsi
movq %rsi,%rax
imulq %rdi
salq $63, %rax
sarq $63, %rax
xorq %rdi, %rax
ret
Lehigh University Spring 2025 45
CSE202 ASSEMBLY BASICS
Summary