05 Machine Basics
05 Machine Basics
Instructors:
Dave O’Hallaron, Greg Ganger, and Greg Kesden
1
Carnegie Mellon
2
Carnegie Mellon
Evolutionary design
Backwards compatible up until 8086, introduced in 1978
Added more features as time goes on
3
Carnegie Mellon
5
Carnegie Mellon
6
Carnegie Mellon
Intel’s 64-Bit
Intel Attempted Radical Shift from IA32 to IA64
Totally different architecture (Itanium)
Executes IA32 code only as legacy
Performance disappointing
AMD Stepped in with Evolutionary Solution
x86-64 (now called “AMD64”)
Intel Felt Obligated to Focus on IA64
Hard to admit mistake or that AMD is better
2004: Intel Announces EM64T extension to IA32
Extended Memory 64-bit Technology
Almost identical to x86-64!
All but low-end x86 processors support x86-64
But, lots of code still runs in 32-bit mode
7
Carnegie Mellon
Our Coverage
IA32
The traditional x86
shark> gcc –m32 hello.c
x86-64
The emerging standard
shark> gcc hello.c
shark> gcc –m64 hello.c
Presentation
Book presents IA32 in Sections 3.1—3.12
Covers x86-64 in 3.13
We will cover both simultaneously
Some labs will be based on x86-64, others on IA32
8
Carnegie Mellon
9
Carnegie Mellon
Definitions
Architecture: (also ISA: instruction set architecture) The
parts of a processor design that one needs to understand
to write assembly code.
Examples: instruction set specification, registers.
Microarchitecture: Implementation of the architecture.
Examples: cache sizes and core frequency.
10
Carnegie Mellon
Programmer-Visible State
PC: Program counter Memory
Byte addressable array
Address of next instruction
Called “EIP” (IA32) or “RIP” (x86- Code and user data
Register file
Heavily used program data
Condition codes
Store status information about
most recent arithmetic operation
11
Carnegie Mellon
12
Carnegie Mellon
13
Carnegie Mellon
14
Carnegie Mellon
Transfer control
Unconditional jumps to/from procedures
Conditional branches
15
Carnegie Mellon
Object Code
Code for sum Assembler
0x401040 <sum>: Translates .s into .o
0x55 Binary encoding of each instruction
0x89 Nearly-complete image of executable code
0xe5 Missing linkages between code in different
0x8b
files
0x45
0x0c Linker
0x03 Resolves references between files
0x45 • Total of 11 bytes
0x08
Combines with static run-time libraries
• Each instruction
E.g., code for malloc, printf
0x5d
1, 2, or 3 bytes
0xc3 Some libraries are dynamically linked
• Starts at address
Linking occurs when program begins
0x401040
execution
16
Carnegie Mellon
x += y or unsigned
More precisely: Operands:
int eax; x: Register %eax
int *ebp; y: Memory M[%ebp+8]
eax += ebp[2] t: Register %eax
– Return function value in %eax
Object Code
0x80483ca: 03 45 08
3-byte instruction
Stored at address 0x80483ca
17
Carnegie Mellon
Disassembler
objdump -d p
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
18
Carnegie Mellon
Alternate Disassembly
Object Disassembled
0x401040:
0x55 Dump of assembler code for function sum:
0x89 0x080483c4 <sum+0>: push %ebp
0xe5 0x080483c5 <sum+1>: mov %esp,%ebp
0x8b 0x080483c7 <sum+3>: mov 0xc(%ebp),%eax
0x45 0x080483ca <sum+6>: add 0x8(%ebp),%eax
0x0c 0x080483cd <sum+9>: pop %ebp
0x03 0x080483ce <sum+10>: ret
0x45
0x08
0x5d Within gdb Debugger
0xc3 gdb p
disassemble sum
Disassemble procedure
x/11xb sum
Examine the 11 bytes starting at sum
19
Carnegie Mellon
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ff push $0xffffffff
30001005: 68 90 10 00 30 push $0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
21
Carnegie Mellon
source
%esi %si index
destination
%edi %di index
stack
%esp %sp
pointer
base
%ebp %bp
pointer
23
Carnegie Mellon
movl (%ecx),%eax
movl 8(%ebp),%edx
25
Carnegie Mellon
popl %ebx
popl %ebp Finish
ret
26
Carnegie Mellon
popl %ebx
popl %ebp Finish
ret
27
Carnegie Mellon
Understanding Swap
void swap(int *xp, int *yp) •
{ • Stack
int t0 = *xp; • (in memory)
Offset
int t1 = *yp;
*xp = t1; 12 yp
*yp = t0; 8 xp
}
4 Rtn adr
0 Old %ebp %ebp
-4 Old %ebx %esp
Register Value
%edx xp
%ecx yp
%ebx t0 movl 8(%ebp), %edx # edx = xp
%eax t1 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
28
Carnegie Mellon
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx Offset
0x114
%ecx yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
29
Carnegie Mellon
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx 0x124 Offset
0x114
%ecx yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
30
Carnegie Mellon
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
31
Carnegie Mellon
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
32
Carnegie Mellon
Address
Understanding Swap 123 0x124
456 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
33
Carnegie Mellon
Address
Understanding Swap 456 0x124
456 0x120
0x11c
%eax 456
456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
34
Carnegie Mellon
Address
Understanding Swap 456 0x124
123 0x120
0x11c
%eax 456 0x118
%edx 0x124 Offset
0x114
%ecx 0x120 yp 12 0x120 0x110
xp 8 0x124 0x10c
%ebx 123
4 Rtn adr 0x108
%esi
%ebp 0 0x104
%edi -4
0x100
%esp
movl 8(%ebp), %edx # edx = xp
%ebp 0x104 movl 12(%ebp), %ecx # ecx = yp
movl (%edx), %ebx # ebx = *xp (t0)
movl (%ecx), %eax # eax = *yp (t1)
movl %eax, (%edx) # *xp = t1
movl %ebx, (%ecx) # *yp = t0
35
Carnegie Mellon
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]]
36
Carnegie Mellon
37
Carnegie Mellon
Instructions
Long word l (4 Bytes) ↔ Quad word q (8 Bytes)
New instructions:
movl ➙ movq
addl ➙ addq
sall ➙ salq
etc.
40
Carnegie Mellon
popl %ebx
popl %ebp Finish
ret
41
Carnegie Mellon
43
Carnegie Mellon
44