Professional Documents
Culture Documents
Arithmetic
1
Notes
⬛ Lectures will become more and more challenging
⬛ Low level textbook details are “good to know” but not required
⬛ In the next lecture we will have some practice problems
3
Assembly is Important for Security
⬛ System attacks
⬛ Reverse engineering
Other Processors
⬛ Which CPU company dominates
mobile/smartphones?
7
Types of Instruction Sets: CISC and RISC
⬛ Intel CPUs: Complex instruction set computer (CISC)
▪ Complex, specialized instructions
▪ Too many of these complex instructions
▪ But, only small subset encountered with Linux programs
⬛ Desktop Model
▪ 10 cores ▪ 3.6-5.3 GHz
▪ Integrated graphics ▪ 125 W
10
11
Our Coverage
⬛ x86-64
▪ The standard now
12
13
Definitions
⬛ Architecture (ISA: Instruction Set Architecture)
⬛ Abstract CPU design that one needs to understand to write assembly
⬛ Examples: instruction set specification, registers
⬛ Microarchitecture: Implementation of the architecture
▪ Examples: cache sizes and core frequency
⬛ Code Forms:
▪ Machine Code: The byte-level programs that a processor executes
▪ Assembly Code: A text representation of machine code
⬛ Example ISAs:
▪ Intel: x86, IA32, Itanium, x86-64
▪ ARM: Used in almost all mobile phones
14
Programmer-Visible State
▪ PC: Program counter ▪ Memory
▪ Address of next instruction ▪ Byte addressable array
▪ Called “RIP” (x86-64)
▪ Code and user data
▪ Register file ▪ Stack to support procedures
▪ Heavily used program data
▪ Condition codes
▪ Store status information about most recent
arithmetic or logical operation
▪ Used for conditional branching
15
16
18
19
Object Code
Code for sumstore
⬛ Assembler
0x0400595:
0x53 ▪ Translates .s into .o
0x48 ▪ Binary encoding of each instruction
0x89
0xd3 ▪ Nearly-complete image of executable code
0xe8 ▪ Missing linkages between code in different
0xf2 files
0xff
0xff ⬛ Linker
0xff
0x48 • Total of 14 bytes ▪ Resolves references between files
0x89 ▪ Combines with static run-time libraries
0x03 • Starts at address
0x5b 0x0400595 E.g., code for malloc, printf
▪
0xc3 ▪ Some libraries are dynamically linked
▪ Linking occurs when program begins
execution
20
21
⬛ Disassembler
objdump –d sum
▪ Useful tool for examining object code
▪ Analyzes bit pattern of series of instructions
▪ Produces approximate rendition of assembly code
▪ Can be run on either a.out (complete executable) or .o file
22
Demo
23
Alternate Disassembly
Disassembled
Object
0x0400595:
0x53 Dump of assembler code for function sumstore:
0x48 0x0000000000400595 <+0>: push %rbx
0x89 0x0000000000400596 <+1>: mov %rdx,%rbx
0xd3 0x0000000000400599 <+4>: callq 0x400590 <plus>
0xe8 0x000000000040059e <+9>: mov %rax,(%rbx)
0xf2 0x00000000004005a1 <+12>:pop %rbx
0xff 0x00000000004005a2 <+13>:retq
0xff
0xff
0x48
0x89 ⬛ Within gdb Debugger
0x03 gdb sum
0x5b
0xc3 disassemble sumstore
▪ Disassemble procedure
24
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b Reverse
ec mov
engineering %esp,%ebp by
forbidden
30001003: 6a ff push $0xffffffff
Microsoft
30001005: 68 End
90 10 00 30User
pushLicense Agreement
$0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
26
source
%esi %si index
destination
%edi %di index
stack
%esp %sp pointer
base
%ebp %bp
pointer
29
31
Practice
⬛ Which ones are correct?
⬛ Answers
movq %rax, $0x010
—-> Incorrect: immediate can’t be a destination
32
movq (%rcx),%rax
movq 2(%rcx),%rdx
33
void swap
(long *xp, long *yp)
{
long t0 = *xp;
long t1 = *yp;
*xp = t1;
*yp = t0;
}
34
Understanding Swap()
Memory
void swap Registers
(long *xp, long *yp)
{ %rdi
long t0 = *xp;
long t1 = *yp; %rsi
*xp = t1;
%rax
*yp = t0;
} %rdx
Register Value
%rdi xp
%rsi yp swap:
%rax t0 movq (%rdi), %rax # t0 = *xp
%rdx t1 movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
35
Understanding Swap()
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 0x108
%rdx 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
36
Understanding Swap()
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
37
Understanding Swap()
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
38
Understanding Swap()
Memory
Registers Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
39
Understanding Swap()
Memory
Registers Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 123 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
40
⬛ Special Cases
(Rb,Ri) (Rb+Ri)
D(Rb,Ri) (D+Rb+Ri)
(Rb,Ri,S) (Rb+Ri*S)
41
⬛ Special Cases
(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]
D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D]
(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]
42
%rdx 0xf000
%rcx 0x0100
44
45
46
long m12(long x)
{
Converted to ASM by compiler:
return x*12; leaq (%rdi,%rdi,2), %rax
} salq $2, %rax
47
48
long m12(long x)
{
Converted to ASM by compiler:
return x*12; leaq (%rdi,%rdi,2), %rax # t <- x+x*2
} salq $2, %rax # return t<<2
49
50
Practice
⬛ Calculate the address
%rdx 0xff00
%rcx 0x0110
s exp frac
1 8-bits 8-bits
53
Practice v = (–1)s M 2E
E = Exp - Bias
⬛ 7.125 10 = 111.0012
s exp frac
1 8-bits 8-bits
⬛ 111.001 = 1.11001 x 22
⬛ M = 1.11001 ===> frac = 11001
⬛ E=2
⬛ Exp = E + Bias
⬛ Bias = 2k-1 - 1 = 28-1 - 1 = 27 - 1 = 127
⬛ ===> Exp = 2 + 127 = 12910 = 100000012
1 8-bits 8-bits
54
Xor properties
⬛ A⊕B=?
⬛ A⊕B=A.B+A.B
⬛ Commutativity
⬛ A⊕B=B⊕A
⬛ Associativity
⬛ A⊕(B⊕C)=(A⊕B)⊕C
⬛ Identity element
⬛ A⊕0=A
⬛ Self-inverse
⬛ A⊕A=0
55
Practice
⬛ What does the following function do?
void s(int& a, int& b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
56
Practice
⬛ Write the C analog for the following assembly instructions
Assembly C Analog
57