1
Machine-Level Programming I: Basics
CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGİN
Slides adapted from Bryant & O’Hallaron’s slides
Machine-Level Programming I: Basics CSE 238/2038/2138: Systems - - PowerPoint PPT Presentation
Machine-Level Programming I: Basics CSE 238/2038/2138: Systems Programming Instructor: Fatma CORUT ERGN Slides adapted from Bryant & OHallarons slides 1 Today: Machine Programming I: Basics History of Intel processors and
1
Slides adapted from Bryant & O’Hallaron’s slides
2
History of Intel processors and architectures Assembly Basics: Registers, operands, move Arithmetic & logical operations C, assembly, machine code
3
Dominate laptop/desktop/server market Evolutionary design
Complex instruction set computer (CISC)
4
8086
386
Pentium 4E
Core 2
Core i7
5
Machine Evolution
Added Features
6
Mobile Model: Core i7
Desktop Model: Core i7
Server Model: Xeon
7
Historically
Then
Recent Years
8
2001: Intel Attempts Radical Shift from IA32 to IA64
2003: AMD Steps in with Evolutionary Solution
Intel Felt Obligated to Focus on IA64
2004: Intel Announces EM64T extension to IA32
All but low-end x86 processors support x86-64
9
x86-64
10
History of Intel processors and architectures Assembly Basics: Registers, operands, move Arithmetic & logical operations C, assembly, machine code
11
Architecture: (also ISA: instruction set architecture) The
Microarchitecture: Implementation of the architecture.
Code Forms:
Example ISAs:
12
recent arithmetic or logical operation
Condition Codes
13
14
15
.globl _sumstore .align 4, 0x90 _sumstore: .cfi_startproc ## BB#0: pushq %rbp Ltmp3: .cfi_def_cfa_offset 16 Ltmp4: .cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp5: .cfi_def_cfa_register %rbp addl %esi, %edi movl %edi, (%rdx) popq %rbp retq .cfi_endproc
16
“Integer” data of 1, 2, 4, or 8 bytes
Floating point data of 4, 8, or 10 bytes Code: Byte sequences encoding series of instructions No aggregate types such as arrays or structures
17
Transfer data between memory and register
Perform arithmetic function on register or memory data Transfer control
18
0x100000f20: 0x55 0x48 0x89 0xe5 0x01 0xf7 0x89 0x3a 0x5d 0xc3
Assembler
Linker
1, 2, or 3 bytes
19
C Code
Assembly
Object Code
*dest = t; movl %edi, (%rdx) 0x100000f26: 89 3a
20
Disassembler
21
Within gdb Debugger
0x100000f20: 0x55 0x48 0x89 0xe5 0x01 0xf7 0x89 0x3a 0x5d 0xc3
22
Anything that can be interpreted as executable code Disassembler examines bytes and reconstructs assembly source
23
History of Intel processors and architectures Assembly Basics: Registers, operands, move Arithmetic & logical operations C, assembly, machine code
24
25
%ax %cx %dx %bx %si %di %sp %bp %ah %ch %dh %bh %al %cl %dl %bl 16-bit virtual registers (backwards compatibility) general purpose
accumulate counter data base source index destination index
stack pointer base pointer Origin (mostly obsolete)
26
Moving Data
Operand Types
27
28
Normal
Displacement
29
30
%rdi %rsi %eax %ecx
void swap (int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; }
Register Value %rdi xp %rsi yp %eax t0 %ecx t1 swap: movl (%rdi), %eax # t0 = *xp movl (%rsi), %ecx # t1 = *yp movl %ecx, (%rdi) # *xp = t1 movl %eax, (%rsi) # *yp = t0 ret
31
Address
32
Address
33
Address
34
Address
35
Address
36
Normal
Displacement
37
Most General Form
Special Cases
38
Carnegie Mellon
39
History of Intel processors and architectures Assembly Basics: Registers, operands, move Arithmetic & logical operations C, assembly, machine code
40
Carnegie Mellon
leaq Src
Uses
Example
41
Carnegie Mellon
Two Operand Instructions:
Watch out for argument order!
No distinction between signed and unsigned int (why?)
42
Carnegie Mellon
One Operand Instructions
See book for more instructions
43
Carnegie Mellon
long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } arith: leaq (%rdi,%rsi), %rax addq %rdx, %rax leaq (%rsi,%rsi,2), %rdx salq $4, %rdx leaq 4(%rdi,%rdx), %rcx imulq %rcx, %rax ret
44
Carnegie Mellon
long arith (long x, long y, long z) { long t1 = x+y; long t2 = z+t1; long t3 = x+4; long t4 = y * 48; long t5 = t3 + t4; long rval = t2 * t5; return rval; } arith: leaq (%rdi,%rsi), %rax # t1 addq %rdx, %rax # t2 leaq (%rsi,%rsi,2), %rdx salq $4, %rdx # t4 leaq 4(%rdi,%rdx), %rcx # t5 imulq %rcx, %rax # rval ret Register Use(s) %rdi Argument x %rsi Argument y %rdx Argument z, t4 %rax t1, t2, rval %rcx t5
45
History of Intel processors and architectures
C, assembly, machine code
Assembly Basics: Registers, operands, move
Arithmetic