Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir...
-
Upload
cameron-franklin-morton -
Category
Documents
-
view
213 -
download
0
Transcript of Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir...
![Page 1: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/1.jpg)
Carnegie Mellon
1
Introduction to x86 Assemblyor “What does my laptop actually do?”
Ymir Vigfusson
Some slides gracefully borrowed from 18-213@CMU
![Page 2: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/2.jpg)
Carnegie Mellon
2
So what is assembly really?
![Page 3: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/3.jpg)
Carnegie Mellon
3
Why study assembly?
triton$
![Page 4: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/4.jpg)
Carnegie Mellon
4
One reason: Reverse engineering
People who figure out what viruses do today
$$$ !
![Page 5: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/5.jpg)
Carnegie Mellon
5
Motivation: The Turing Machine! http://www.youtube.com/watch?v=cYw2ewoO6c4
![Page 6: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/6.jpg)
Carnegie Mellon
6
Intel x86 Processors: Overview
X86-64 / EM64t
X86-32/IA32
X86-16 8086
286
386486PentiumPentium MMX
Pentium III
Pentium 4
Pentium 4E
Pentium 4F
Core 2 DuoCore i7
IA: often redefined as latest Intel architecture
time
Architectures Processors
MMX
SSE
SSE2
SSE3
SSE4
![Page 7: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/7.jpg)
Carnegie Mellon
7
Intel x86 Evolution: Milestones
Name Date Transistors MHz 8086 1978 29K 5-10
First 16-bit processor. Basis for IBM PC & DOS 1MB address space
386 1985 275K 16-33 First 32 bit processor , referred to as IA32 Added “flat addressing” Capable of running Unix 32-bit Linux/gcc uses no instructions introduced in later models
Pentium 4F 2004 125M 2800-3800 First 64-bit processor, referred to as x86-64
Core i7 2008 731M 2667-3333 Our shark machines
![Page 8: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/8.jpg)
Carnegie Mellon
8
Intel x86 Processors Machine Evolution
386 1985 0.3M Pentium 1993 3.1M Pentium/MMX 1997 4.5M PentiumPro 1995 6.5M Pentium III 1999 8.2M Pentium 4 2001 42M Core 2 Duo 2006 291M Core i7 2008 731M
Added Features Instructions to support multimedia operations
Parallel operations on 1, 2, and 4-byte data, both integer & FP Instructions to enable more efficient conditional operations
Linux/GCC Evolution Two major steps: 1) support 32-bit 386. 2) support 64-bit x86-64
![Page 9: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/9.jpg)
Carnegie Mellon
9
What is this? (gdb) disass checksum Dump of assembler code for function checksum: 0x08048400 <+0>: push %ebp 0x08048401 <+1>: xor %edx,%edx 0x08048403 <+3>: mov %esp,%ebp 0x08048405 <+5>: xor %eax,%eax 0x08048407 <+7>: push %esi 0x08048408 <+8>: mov 0x8(%ebp),%esi 0x0804840b <+11>: push %ebx 0x0804840c <+12>: mov 0xc(%ebp),%ebx 0x0804840f <+15>: test %ebx,%ebx 0x08048411 <+17>: jle 0x8048425 <checksum+37> 0x08048413 <+19>: nop 0x08048414 <+20>: lea 0x0(%esi,%eiz,1),%esi 0x08048418 <+24>: movsbl (%esi,%edx,1),%ecx 0x0804841c <+28>: add $0x1,%edx 0x0804841f <+31>: xor %ecx,%eax 0x08048421 <+33>: cmp %ebx,%edx 0x08048423 <+35>: jne 0x8048418 <checksum+24> 0x08048425 <+37>: pop %ebx 0x08048426 <+38>: pop %esi 0x08048427 <+39>: pop %ebp 0x08048428 <+40>: ret End of assembler dump.
![Page 10: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/10.jpg)
Carnegie Mellon
10
CPU
Assembly Programmer’s View
Programmer-Visible State PC: Program counter
Address of next instruction Called “EIP” (IA32) or “RIP” (x86-64)
Register file Heavily used program data
Condition codes Store status information about most
recent arithmetic operation Used for conditional branching
PCRegisters
Memory
CodeDataStack
Addresses
Data
InstructionsConditionCodes
Memory Byte addressable array Code and user data Stack to support procedures
![Page 11: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/11.jpg)
Carnegie Mellon
11
text
text
binary
binary
Compiler (gcc -S)
Assembler (gcc or as)
Linker (gcc or ld)
C program (p1.c p2.c)
Asm program (p1.s p2.s)
Object program (p1.o p2.o)
Executable program (p)
Static libraries (.a)
Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc –O1 p1.c p2.c -o p
Use basic optimizations (-O1) Put resulting binary in file p
![Page 12: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/12.jpg)
Carnegie Mellon
12
Compiling Into AssemblyC Codeint sum(int x, int y){ int t = x+y; return t;}
Generated IA32 Assemblysum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax popl %ebp ret
Obtain with command
/usr/local/bin/gcc –O1 –m32 -S code.c
Produces file code.s
Some compilers use instruction “leave”
![Page 13: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/13.jpg)
Carnegie Mellon
13
Assembly Characteristics: Data Types “Integer” data of 1, 2, or 4 bytes
Data values Addresses (untyped pointers)
Floating point data of 4, 8, or 10 bytes
No aggregate types such as arrays or structures Just contiguously allocated bytes in memory
![Page 14: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/14.jpg)
Carnegie Mellon
14
Assembly Characteristics: Operations Perform arithmetic function on register or memory data
Transfer data between memory and register Load data from memory into register Store register data into memory
Transfer control Unconditional jumps to/from procedures Conditional branches
![Page 15: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/15.jpg)
Carnegie Mellon
15
Code for sum0x401040 <sum>: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x5d 0xc3
Object Code Assembler
Translates .s into .o Binary encoding of each instruction Nearly-complete image of executable code Missing linkages between code in different
files Linker
Resolves references between files Combines with static run-time libraries
E.g., code for malloc, printf Some libraries are dynamically linked
Linking occurs when program begins execution
• Total of 11 bytes• Each instruction
1, 2, or 3 bytes• Starts at address 0x401040
![Page 16: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/16.jpg)
Carnegie Mellon
16
Machine Instruction Example C Code
Add two signed integers Assembly
Add 2 4-byte integers “Long” words in GCC parlance Same instruction whether signed
or unsigned Operands:
x: Register %eaxy: Memory M[%ebp+8]t: Register %eax
–Return function value in %eax Object Code
3-byte instruction Stored at address 0x80483ca
int t = x+y;
addl 8(%ebp),%eax
0x80483ca: 03 45 08
Similar to expression: x += y
More precisely:int eax;
int *ebp;
eax += ebp[2]
![Page 17: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/17.jpg)
Carnegie Mellon
17
Disassembled
Disassembling Object Code
Disassemblerobjdump -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 complete executable or .o file
080483c4 <sum>: 80483c4: 55 push %ebp 80483c5: 89 e5 mov %esp,%ebp 80483c7: 8b 45 0c mov 0xc(%ebp),%eax 80483ca: 03 45 08 add 0x8(%ebp),%eax 80483cd: 5d pop %ebp 80483ce: c3 ret
![Page 18: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/18.jpg)
Carnegie Mellon
18
Disassembled
Dump of assembler code for function sum:0x080483c4 <sum+0>: push %ebp0x080483c5 <sum+1>: mov %esp,%ebp0x080483c7 <sum+3>: mov 0xc(%ebp),%eax0x080483ca <sum+6>: add 0x8(%ebp),%eax0x080483cd <sum+9>: pop %ebp0x080483ce <sum+10>: ret
Alternate Disassembly
Within gdb Debuggergdb pdisassemble sum Disassemble procedurex/11xb sum Examine the 11 bytes starting at sum
Object0x401040: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x5d 0xc3
![Page 19: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/19.jpg)
Carnegie Mellon
19
What Can be Disassembled?
Anything that can be interpreted as executable code Disassembler examines bytes and reconstructs assembly source
% objdump -d WINWORD.EXE
WINWORD.EXE: file format pei-i386
No symbols in "WINWORD.EXE".Disassembly of section .text:
30001000 <.text>:30001000: 55 push %ebp30001001: 8b ec mov %esp,%ebp30001003: 6a ff push $0xffffffff30001005: 68 90 10 00 30 push $0x300010903000100a: 68 91 dc 4c 30 push $0x304cdc91
![Page 20: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/20.jpg)
Carnegie Mellon
20
Registers, operands, move operation
![Page 21: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/21.jpg)
Carnegie Mellon
21
Integer Registers (IA32)%eax
%ecx
%edx
%ebx
%esi
%edi
%esp
%ebp
%ax
%cx
%dx
%bx
%si
%di
%sp
%bp
%ah
%ch
%dh
%bh
%al
%cl
%dl
%bl
16-bit virtual registers(backwards compatibility)
gene
ral p
urpo
se
accumulate
counter
data
base
source index
destinationindex
stack pointer
basepointer
Origin(mostly obsolete)
![Page 22: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/22.jpg)
Carnegie Mellon
22
Moving Data: IA32 Moving Data
movl Source, Dest:
Operand Types Immediate: Constant integer data
Example: $0x400, $-533 Like C constant, but prefixed with ‘$’ Encoded with 1, 2, or 4 bytes
Register: One of 8 integer registers Example: %eax, %edx But %esp and %ebp reserved for special use Others have special uses for particular instructions
Memory: 4 consecutive bytes of memory at address given by register Simplest example: (%eax) Various other “address modes”
%eax
%ecx
%edx
%ebx
%esi
%edi
%esp
%ebp
![Page 23: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/23.jpg)
Carnegie Mellon
23
movl Operand Combinations
Cannot do memory-memory transfer with a single instruction
movl
Imm
Reg
Mem
RegMem
RegMem
Reg
Source Dest C Analog
movl $0x4,%eax temp = 0x4;
movl $-147,(%eax) *p = -147;
movl %eax,%edx temp2 = temp1;
movl %eax,(%edx) *p = temp;
movl (%eax),%edx temp = *p;
Src,Dest
![Page 24: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/24.jpg)
Carnegie Mellon
24
Simple Memory Addressing Modes Normal (R) Mem[Reg[R]]
Register R specifies memory address
movl (%ecx),%eax
Displacement D(R) Mem[Reg[R]+D] Register R specifies start of memory region Constant displacement D specifies offset
movl 8(%ebp),%edx
![Page 25: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/25.jpg)
Carnegie Mellon
25
Using Simple Addressing Modes
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;} Body
SetUp
Finish
swap: pushl %ebp movl %esp,%ebp pushl %ebx
movl 8(%ebp), %edx movl 12(%ebp), %ecx movl (%edx), %ebx movl (%ecx), %eax movl %eax, (%edx) movl %ebx, (%ecx)
popl %ebx popl %ebp ret
![Page 26: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/26.jpg)
Carnegie Mellon
26
Using Simple Addressing Modes
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
swap:pushl %ebpmovl %esp,%ebppushl %ebx
movl 8(%ebp), %edxmovl 12(%ebp), %ecxmovl (%edx), %ebxmovl (%ecx), %eaxmovl %eax, (%edx)movl %ebx, (%ecx)
popl %ebxpopl %ebpret
Body
SetUp
Finish
![Page 27: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/27.jpg)
Carnegie Mellon
27
Understanding Swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
Stack(in memory)
Register Value%edx xp%ecx yp%ebx t0%eax t1
yp
xp
Rtn adr
Old %ebp %ebp 0
4
8
12
Offset
•••
Old %ebx-4 %esp
movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 28: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/28.jpg)
Carnegie Mellon
28
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp 0x104movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 29: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/29.jpg)
Carnegie Mellon
29
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x124
0x104
0x120
movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 30: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/30.jpg)
Carnegie Mellon
30
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x120
0x104
0x124
0x124
movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 31: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/31.jpg)
Carnegie Mellon
31
456
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x124
0x120
123
0x104movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 32: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/32.jpg)
Carnegie Mellon
32
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
123
456
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
456
0x124
0x120
0x104
123
123
movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 33: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/33.jpg)
Carnegie Mellon
33
456
456
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
456456
0x124
0x120
123
0x104
123
movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 34: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/34.jpg)
Carnegie Mellon
34
Understanding Swap
0x120
0x124
Rtn adr
%ebp 0
4
8
12
Offset
-4
456
123
Address0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
456
0x124
0x120
0x104
123123
movl 8(%ebp), %edx # edx = xpmovl 12(%ebp), %ecx # ecx = ypmovl (%edx), %ebx # ebx = *xp (t0)movl (%ecx), %eax # eax = *yp (t1)movl %eax, (%edx) # *xp = t1movl %ebx, (%ecx) # *yp = t0
![Page 35: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/35.jpg)
Carnegie Mellon
35
Complete Memory Addressing Modes Most General Form
D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] D: Constant “displacement” 1, 2, or 4 bytes Rb: Base register: Any of 8 integer registers Ri: Index register: Any, except for %esp
Unlikely you’d use %ebp, either S: Scale: 1, 2, 4, or 8 (why these numbers?)
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]]
![Page 36: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/36.jpg)
Carnegie Mellon
36
So far! History of Intel processors and architectures
Evolutionary design leads to many quirks and artifacts
C, assembly, machine code Compiler must transform statements, expressions, procedures into
low-level instruction sequences
Assembly Basics: Registers, operands, move The x86 move instructions cover wide range of data movement
forms
![Page 37: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/37.jpg)
Carnegie Mellon
37
Complete addressing mode andaddress computation (leal)
![Page 38: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/38.jpg)
Carnegie Mellon
38
Data Representations: IA32 + x86-64 Sizes of C Objects (in Bytes) C Data Type Generic 32-bit Intel IA32 x86-64
unsigned 4 44
int 4 44
long int 4 48
char 1 11
short 2 22
float 4 44
double 8 88
long double 8 10/1216
char * 4 48– Or any other pointer
![Page 39: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/39.jpg)
Carnegie Mellon
39
Complete Memory Addressing Modes Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]
D: Constant “displacement” 1, 2, or 4 bytes Rb: Base register: Any of 8 integer registers Ri: Index register: Any, except for %esp
Unlikely you’d use %ebp, either S: Scale: 1, 2, 4, or 8 (why these numbers?)
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]]
![Page 40: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/40.jpg)
Carnegie Mellon
40
Address Computation Examples
Expression Address Computation Address
0x8(%edx) 0xf000 + 0x8 0xf008
(%edx,%ecx) 0xf000 + 0x100 0xf100
(%edx,%ecx,4) 0xf000 + 4*0x100 0xf400
0x80(,%edx,2) 2*0xf000 + 0x80 0x1e080
%edx 0x7000
%ecx 0x0200
Expression Address Computation Address
0x8(%edx)
(%edx,%ecx)
(%edx,%ecx,4)
0x80(,%edx,2)
![Page 41: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/41.jpg)
Carnegie Mellon
41
Address Computation Instruction leal Src,Dest
Src is address mode expression Set Dest to address denoted by expression
Uses Computing addresses without a memory reference
E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*y
k = 1, 2, 4, or 8 Example
int mul12(int x){ return x*12;}
int mul12(int x){ return x*12;}
leal (%eax,%eax,2), %eax ;t <- x+x*2sall $2, %eax ;return t<<2
leal (%eax,%eax,2), %eax ;t <- x+x*2sall $2, %eax ;return t<<2
Converted to ASM by compiler:
![Page 42: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/42.jpg)
Carnegie Mellon
42
Arithmetic operations
![Page 43: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/43.jpg)
Carnegie Mellon
43
Some Arithmetic Operations Two Operand Instructions:FormatComputationaddl Src,Dest Dest = Dest + Srcsubl Src,Dest Dest = Dest Srcimull Src,Dest Dest = Dest * Srcsall Src,Dest Dest = Dest << Src Also called shllsarl Src,Dest Dest = Dest >> Src Arithmeticshrl Src,Dest Dest = Dest >> Src Logicalxorl Src,Dest Dest = Dest ^ Srcandl Src,Dest Dest = Dest & Srcorl Src,Dest Dest = Dest | Src
Watch out for argument order! No distinction between signed and unsigned int (why?)
![Page 44: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/44.jpg)
Carnegie Mellon
44
Some Arithmetic Operations One Operand Instructionsincl Dest Dest = Dest + 1decl Dest Dest = Dest 1negl Dest Dest = Destnotl Dest Dest = ~Dest
See the chapter from CSAPP for more instructions
![Page 45: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/45.jpg)
Carnegie Mellon
45
Arithmetic Expression Example
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
arith:pushl %ebpmovl %esp, %ebp
movl 8(%ebp), %ecxmovl 12(%ebp), %edxleal (%edx,%edx,2), %eaxsall $4, %eaxleal 4(%ecx,%eax), %eaxaddl %ecx, %edxaddl 16(%ebp), %edximull %edx, %eax
popl %ebpret
Body
SetUp
Finish
![Page 46: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/46.jpg)
Carnegie Mellon
46
•
•
•
16 z
12 y
8 x
4 Rtn Addr
0 Old %ebp
Understanding arith
movl 8(%ebp), %ecxmovl 12(%ebp), %edxleal (%edx,%edx,2), %eaxsall $4, %eaxleal 4(%ecx,%eax), %eaxaddl %ecx, %edxaddl 16(%ebp), %edximull %edx, %eax
%ebp
Offsetint arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
![Page 47: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/47.jpg)
Carnegie Mellon
47
•
•
•
16 z
12 y
8 x
4 Rtn Addr
0 Old %ebp
Understanding arith
%ebp
Offset
Stack
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
movl 8(%ebp), %ecx # ecx = xmovl 12(%ebp), %edx # edx = yleal (%edx,%edx,2), %eax # eax = y*3sall $4, %eax # eax *= 16 (t4)leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)addl %ecx, %edx # edx = x+y (t1)addl 16(%ebp), %edx # edx += z (t2)imull %edx, %eax # eax = t2 * t5 (rval)
![Page 48: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/48.jpg)
Carnegie Mellon
48
Observations about arith Instructions in different
order from C code Some expressions require
multiple instructions Some instructions cover
multiple expressions Get exact same code when
compile: (x+y+z)*(x+4+48*y)
movl 8(%ebp), %ecx # ecx = xmovl 12(%ebp), %edx # edx = yleal (%edx,%edx,2), %eax # eax = y*3sall $4, %eax # eax *= 16 (t4)leal 4(%ecx,%eax), %eax # eax = t4 +x+4 (t5)addl %ecx, %edx # edx = x+y (t1)addl 16(%ebp), %edx # edx += z (t2)imull %edx, %eax # eax = t2 * t5 (rval)
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
int arith(int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}
![Page 49: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/49.jpg)
Carnegie Mellon
49
Another Example
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
logical:pushl %ebpmovl %esp,%ebp
movl 12(%ebp),%eaxxorl 8(%ebp),%eaxsarl $17,%eaxandl $8185,%eax
popl %ebpret
Body
SetUp
Finish
movl 12(%ebp),%eax # eax = yxorl 8(%ebp),%eax # eax = x^y (t1)sarl $17,%eax # eax = t1>>17 (t2)andl $8185,%eax # eax = t2 & mask (rval)
![Page 50: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/50.jpg)
Carnegie Mellon
50
Another Example
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
logical:pushl %ebpmovl %esp,%ebp
movl 12(%ebp),%eaxxorl 8(%ebp),%eaxsarl $17,%eaxandl $8185,%eax
popl %ebpret
Body
SetUp
Finish
movl 12(%ebp),%eax # eax = yxorl 8(%ebp),%eax # eax = x^y (t1)sarl $17,%eax # eax = t1>>17 (t2)andl $8185,%eax # eax = t2 & mask (rval)
![Page 51: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/51.jpg)
Carnegie Mellon
51
Another Example
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
logical:pushl %ebpmovl %esp,%ebp
movl 12(%ebp),%eaxxorl 8(%ebp),%eaxsarl $17,%eaxandl $8185,%eax
popl %ebpret
Body
SetUp
Finish
movl 12(%ebp),%eax # eax = yxorl 8(%ebp),%eax # eax = x^y (t1)sarl $17,%eax # eax = t1>>17 (t2)andl $8185,%eax # eax = t2 & mask (rval)
![Page 52: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/52.jpg)
Carnegie Mellon
52
Another Example
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}
logical:pushl %ebpmovl %esp,%ebp
movl 12(%ebp),%eaxxorl 8(%ebp),%eaxsarl $17,%eaxandl $8185,%eax
popl %ebpret
Body
SetUp
Finish
movl 12(%ebp),%eax # eax = yxorl 8(%ebp),%eax # eax = x^y (t1)sarl $17,%eax # eax = t1>>17 (t2)andl $8185,%eax # eax = t2 & mask (rval)
213 = 8192, 213 – 7 = 8185213 = 8192, 213 – 7 = 8185
![Page 53: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/53.jpg)
53
Control: Conditon codes
![Page 54: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/54.jpg)
54
Processor State (IA32, Partial) Information
about currently executing program Temporary data
( %eax, … ) Location of runtime stack
( %ebp,%esp ) Location of current code
control point( %eip, … )
Status of recent tests( CF, ZF, SF, OF )
%eip
General purposeregisters
Current stack top
Current stack frame
Instruction pointer
CF ZF SF OF Condition codes
%eax
%ecx
%edx
%ebx
%esi
%edi
%esp
%ebp
![Page 55: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/55.jpg)
55
Condition Codes (Implicit Setting)
Single bit registersCF Carry Flag (for unsigned) SF Sign Flag (for signed)ZF Zero Flag OF Overflow Flag (for signed)
Implicitly set (think of it as side effect) by arithmetic operationsExample: addl/addq Src,Dest ↔ t = a+bCF set if carry out from most significant bit (unsigned overflow)ZF set if t == 0SF set if t < 0 (as signed)OF set if two’s-complement (signed) overflow(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)
Not set by lea instruction
![Page 56: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/56.jpg)
56
Condition Codes (Explicit Setting: Compare)
Explicit Setting by Compare Instructioncmpl Src2, Src1cmpl b,a like computing a-b without setting destination
CF set if carry out from most significant bit (used for unsigned comparisons)ZF set if a == bSF set if (a-b) < 0 (as signed)OF set if two’s-complement (signed) overflow(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)
![Page 57: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/57.jpg)
57
Condition Codes (Explicit Setting: Test)
Explicit Setting by Test instructiontestl Src2, Src1testl b,a like computing a&b without setting destination
Sets condition codes based on value of Src1 & Src2Useful to have one of the operands be a mask
ZF set when a&b == 0SF set when a&b < 0
![Page 58: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/58.jpg)
58
Reading Condition Codes SetX Instructions
Set single byte based on combinations of condition codes
SetX Condition Descriptionsete ZF Equal / Zerosetne ~ZF Not Equal / Not Zerosets SF Negativesetns ~SF Nonnegativesetg ~(SF^OF)&~ZF Greater (Signed)
setge ~(SF^OF) Greater or Equal (Signed)
setl (SF^OF) Less (Signed)setle (SF^OF)|ZF Less or Equal (Signed)seta ~CF&~ZF Above (unsigned)setb CF Below (unsigned)
![Page 59: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/59.jpg)
59
movl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x : ysetg %al # al = x > ymovzbl %al,%eax # Zero rest of %eax
Reading Condition Codes (Cont.)
SetX Instructions: Set single byte based on combination of condition
codes One of 8 addressable byte
registers Does not alter remaining 3 bytes Typically use movzbl to finish jobint gt (int x, int y){ return x > y;}
int gt (int x, int y){ return x > y;}
Body
%eax %ah %al
%ecx %ch %cl
%edx %dh %dl
%ebx %bh %bl
%esi
%edi
%esp
%ebp
![Page 60: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/60.jpg)
60
Conditional branches and moves
![Page 61: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/61.jpg)
61
Carnegie Mellon
Jumping jX Instructions
Jump to different part of code depending on condition codes
jX Condition Descriptionjmp 1 Unconditional
je ZF Equal / Zero
jne ~ZF Not Equal / Not Zero
js SF Negative
jns ~SF Nonnegative
jg ~(SF^OF)&~ZF Greater (Signed)
jge ~(SF^OF) Greater or Equal (Signed)
jl (SF^OF) Less (Signed)
jle (SF^OF)|ZF Less or Equal (Signed)
ja ~CF&~ZF Above (unsigned)
jb CF Below (unsigned)
![Page 62: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/62.jpg)
62
Carnegie Mellon
Conditional Branch Example
int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}
int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
![Page 63: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/63.jpg)
63
Carnegie Mellon
Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
C allows “goto” as means of transferring control Closer to machine-level
programming style Generally
considered bad coding style
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
![Page 64: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/64.jpg)
64
Carnegie Mellon
Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
![Page 65: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/65.jpg)
65
Carnegie Mellon
Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
![Page 66: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/66.jpg)
66
Carnegie Mellon
Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
![Page 67: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/67.jpg)
67
Carnegie Mellon
Loops
![Page 68: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/68.jpg)
68
Carnegie Mellon
C Codeint pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}
int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}
Goto Versionint pcount_do(unsigned x){ int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
int pcount_do(unsigned x){ int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
“Do-While” Loop Example
Count number of 1’s in argument x (“popcount”)
Use conditional branch to either continue looping or to exit loop
![Page 69: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/69.jpg)
69
Carnegie Mellon
Goto Version“Do-While” Loop Compilation
Registers:%edx x%ecx result
movl $0, %ecx # result = 0.L2: # loop:
movl %edx, %eaxandl $1, %eax # t = x & 1addl %eax, %ecx # result += tshrl %edx # x >>= 1jne .L2 # If !0, goto loop
int pcount_do(unsigned x) { int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
int pcount_do(unsigned x) { int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
![Page 70: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/70.jpg)
70
Carnegie Mellon
C Code
do Body while (Test);
do Body while (Test);
Goto Version
loop: Body if (Test) goto loop
loop: Body if (Test) goto loop
General “Do-While” Translation
Body:
Test returns integer = 0 interpreted as false ≠ 0 interpreted as true
{ Statement1; Statement2; … Statementn;}
![Page 71: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/71.jpg)
71
Carnegie Mellon
C Code Goto Version
“While” Loop Example
Is this code equivalent to the do-while version? Must jump out of loop if test fails
int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;}
int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;}
int pcount_do(unsigned x) { int result = 0; if (!x) goto done;loop: result += x & 0x1; x >>= 1; if (x) goto loop;done: return result;}
int pcount_do(unsigned x) { int result = 0; if (!x) goto done;loop: result += x & 0x1; x >>= 1; if (x) goto loop;done: return result;}
![Page 72: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/72.jpg)
72
Carnegie Mellon
While version
while (Test) Bodywhile (Test) Body
Do-While Version
if (!Test) goto done; do Body while(Test);done:
if (!Test) goto done; do Body while(Test);done:
General “While” Translation
Goto Version
if (!Test) goto done;loop: Body if (Test) goto loop;done:
if (!Test) goto done;loop: Body if (Test) goto loop;done:
![Page 73: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/73.jpg)
73
Carnegie Mellon
C Code
“For” Loop Example
Is this code equivalent to other versions?
#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
![Page 74: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/74.jpg)
74
Carnegie Mellon
“For” Loop Form
for (Init; Test; Update )
Body
General Form
for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; }
for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; }
i = 0i = 0
i < WSIZEi < WSIZE
i++i++
{ unsigned mask = 1 << i; result += (x & mask) != 0;}
{ unsigned mask = 1 << i; result += (x & mask) != 0;}
Init
Test
Update
Body
![Page 75: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/75.jpg)
75
Carnegie Mellon
“For” Loop While Loop
for (Init; Test; Update )
Body
For Version
Init;
while (Test ) {
Body
Update;
}
While Version
![Page 76: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/76.jpg)
76
Carnegie Mellon
“For” Loop … Goto
for (Init; Test; Update )
Body
For Version
Init;
while (Test ) {
Body
Update;
}
While Version
Init; if (!Test) goto done; do Body Update while(Test);done:
Init; if (!Test) goto done; do Body Update while(Test);done:
Init; if (!Test) goto done;loop: Body Update if (Test) goto loop;done:
Init; if (!Test) goto done;loop: Body Update if (Test) goto loop;done:
![Page 77: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/77.jpg)
77
Carnegie Mellon
C Code
“For” Loop Conversion Example
Initial test can be optimized away
#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
#define WSIZE 8*sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
Goto Version
int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result;}
int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result;}
Init
!Test
Body
UpdateTest
![Page 78: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/78.jpg)
78
Carnegie Mellon
Summary So far
Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches & conditional moves Loops
Coming up! Switch statements Stack Call / return Procedure call discipline
![Page 79: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/79.jpg)
79
Carnegie Mellon
Today Switch statements IA 32 Procedures
Stack Structure Calling Conventions Illustrations of Recursion & Pointers
![Page 80: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/80.jpg)
80
Carnegie Mellon
IA32 Stack Region of memory
managed with stack discipline
Grows toward lower addresses
Register %esp contains
lowest stack address address of “top” elementStack Pointer: %esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
![Page 81: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/81.jpg)
81
Carnegie Mellon
IA32 Stack: Push pushl Src
Fetch operand at Src Decrement %esp by 4 Write operand at address given by %esp
-4
Stack GrowsDown
IncreasingAddresses
Stack “Bottom”
Stack Pointer: %esp
Stack “Top”
![Page 82: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/82.jpg)
82
Stack Pointer: %esp
Stack GrowsDown
IncreasingAddresses
Stack “Top”
Stack “Bottom”
Carnegie Mellon
IA32 Stack: Pop
+4
![Page 83: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/83.jpg)
83
Carnegie Mellon
Procedure Control Flow Use stack to support procedure call and
return Procedure call: call label
Push return address on stack Jump to label
Return address: Address of the next instruction right after call Example from disassembly804854e: e8 3d 06 00 00 call 8048b90 <main>
8048553: 50 pushl %eax Return address = 0x8048553
Procedure return: ret Pop address from stack Jump to address
![Page 84: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/84.jpg)
84
0x8048553
0x104
Carnegie Mellon
%esp
%eip
%esp
%eip 0x8048b90
0x108
0x10c
0x110
0x104
0x804854e
123
Procedure Call Example
0x108
0x10c
0x110
123
0x108
call 8048b90
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax
%eip: program counter
![Page 85: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/85.jpg)
85
Carnegie Mellon
%esp
%eip
0x104
%esp
%eip0x804859
1
0x104
0x108
0x10c
0x110
0x8048553
123
Procedure Return Example
0x108
0x10c
0x110
123
ret
8048591: c3 ret8048591: c3 ret
0x108
0x8048553
0x8048553
%eip: program counter
![Page 86: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/86.jpg)
86
Carnegie Mellon
Stack-Based Languages Languages that support recursion
e.g., C, Pascal, Java Code must be “Reentrant”
Multiple simultaneous instantiations of single procedure Need some place to store state of each instantiation
Arguments Local variables Return pointer
Stack discipline State for given procedure needed for limited time
From when called to when return Callee returns before caller does
Stack allocated in Frames state for single procedure instantiation
![Page 87: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/87.jpg)
87
Carnegie Mellon
Call Chain Example
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
yoo
who
amI
amI
amI
ExampleCall Chain
amI
Procedure amI() is recursive
![Page 88: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/88.jpg)
88
Carnegie Mellon
Frame Pointer: %ebp
Stack Frames Contents
Local variables Return information Temporary space
Management Space allocated when enter procedure
“Set-up” code Deallocated when return
“Finish” code
Stack Pointer: %esp
Stack “Top”
Previous Frame
Frame for
proc
![Page 89: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/89.jpg)
89
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo%ebp
%esp
Stack
yoo
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
![Page 90: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/90.jpg)
90
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
![Page 91: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/91.jpg)
91
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
![Page 92: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/92.jpg)
92
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
![Page 93: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/93.jpg)
93
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI
amI
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
![Page 94: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/94.jpg)
94
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
amI
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
![Page 95: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/95.jpg)
95
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
![Page 96: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/96.jpg)
96
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
![Page 97: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/97.jpg)
97
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
amI
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
amI(…){ • • amI(); • •}
amI(…){ • • amI(); • •}
![Page 98: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/98.jpg)
98
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo
%ebp
%esp
Stack
yoo
who
yoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
who(…){ • • • amI(); • • • amI(); • • •}
who(…){ • • • amI(); • • • amI(); • • •}
![Page 99: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/99.jpg)
99
Carnegie Mellon
Example
yoo
who
amI
amI
amI
amI
yoo%ebp
%esp
Stack
yooyoo(…){ • • who(); • •}
yoo(…){ • • who(); • •}
![Page 100: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/100.jpg)
100
Carnegie Mellon
IA32/Linux Stack Frame Current Stack Frame (“Top”
to Bottom) “Argument build:”
Parameters for function about to call Local variables
If can’t keep in registers Saved register context Old frame pointer
Caller Stack Frame Return address
Pushed by call instruction Arguments for this call
Return Addr
SavedRegisters
+Local
Variables
ArgumentBuild
Old %ebp
Arguments
CallerFrame
Frame pointer
%ebp
Stack pointer
%esp
![Page 101: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/101.jpg)
101
Carnegie Mellon
Revisiting swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
int course1 = 15213;int course2 = 18243;
void call_swap() { swap(&course1, &course2);}
int course1 = 15213;int course2 = 18243;
void call_swap() { swap(&course1, &course2);}
call_swap:• • •subl $8, %espmovl $course2, 4(%esp)movl $course1, (%esp)call swap• • •
call_swap:• • •subl $8, %espmovl $course2, 4(%esp)movl $course1, (%esp)call swap• • •
&course2
&course1
Rtn adr %esp
ResultingStack•
••
Calling swap from call_swap
%esp
%espsubl
call
![Page 102: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/102.jpg)
102
Carnegie Mellon
Revisiting swap
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}
swap:pushl %ebpmovl %esp, %ebppushl %ebx
movl 8(%ebp), %edxmovl 12(%ebp), %ecxmovl (%edx), %ebxmovl (%ecx), %eaxmovl %eax, (%edx)movl %ebx, (%ecx)
popl %ebxpopl %ebpret
Body
SetUp
Finish
![Page 103: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/103.jpg)
103
Carnegie Mellon
swap Setup #1
swap:
pushl %ebp
movl %esp,%ebp
pushl %ebx
Resulting Stack
&course2
&course1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp
%ebp•••
%esp
![Page 104: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/104.jpg)
104
Carnegie Mellon
swap Setup #2
swap:
pushl %ebp
movl %esp,%ebp
pushl %ebx
Resulting Stack
&course2
&course1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp%ebp
•••
%esp
![Page 105: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/105.jpg)
105
Carnegie Mellon
swap Setup #3
swap:
pushl %ebp
movl %esp,%ebp
pushl %ebx
Resulting Stack
&course2
&course1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp %ebp
•••
%espOld %ebx
![Page 106: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/106.jpg)
106
Carnegie Mellon
swap Body
movl 8(%ebp),%edx # get xpmovl 12(%ebp),%ecx # get yp. . .
Resulting Stack
&course2
&course1
Rtn adr %esp
Entering Stack
•••
%ebp
yp
xp
Rtn adr
Old %ebp %ebp
•••
%espOld %ebx
Offset relative to %ebp
12
8
4
![Page 107: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/107.jpg)
107
Carnegie Mellon
swap FinishStack Before Finish
popl %ebxpopl %ebp
yp
xp
Rtn adr
Old %ebp %ebp
•••
%espOld %ebx
Resulting Stack
yp
xp
Rtn adr
•••
%ebp
%esp
Observation Saved and restored register %ebx Not so for %eax, %ecx, %edx
![Page 108: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/108.jpg)
108
Carnegie Mellon
Disassembled swap08048384 <swap>: 8048384: 55 push %ebp 8048385: 89 e5 mov %esp,%ebp 8048387: 53 push %ebx 8048388: 8b 55 08 mov 0x8(%ebp),%edx 804838b: 8b 4d 0c mov 0xc(%ebp),%ecx 804838e: 8b 1a mov (%edx),%ebx 8048390: 8b 01 mov (%ecx),%eax 8048392: 89 02 mov %eax,(%edx) 8048394: 89 19 mov %ebx,(%ecx) 8048396: 5b pop %ebx 8048397: 5d pop %ebp 8048398: c3 ret
80483b4: movl $0x8049658,0x4(%esp) # Copy &course2 80483bc: movl $0x8049654,(%esp) # Copy &course1 80483c3: call 8048384 <swap> # Call swap 80483c8: leave # Prepare to return 80483c9: ret # Return
Calling Code
![Page 109: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/109.jpg)
109
Carnegie Mellon
IA32/Linux+Windows Register Usage
%eax, %edx, %ecx Caller saves prior to call if values
are used later
%eax also used to return integer value
%ebx, %esi, %edi Callee saves if wants to use them
%esp, %ebp special form of callee save Restored to original values upon
exit from procedure
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
Caller-SaveTemporaries
Callee-SaveTemporaries
Special
![Page 110: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/110.jpg)
110
Carnegie Mellon
So far IA 32 Procedures
Stack Structure Calling Conventions Illustrations of Recursion & Pointers
![Page 111: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/111.jpg)
111
Carnegie Mellon
%esp
Creating and Initializing Local Variableint add3(int x) { int localx = x; incrk(&localx, 3); return localx;}
int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}
Variable localx must be stored on stack Because: Need to create pointer to it
Compute pointer as -4(%ebp)
First part of add3
x
Rtn adrOld
%ebp%ebp 0
4
8
-4 localx = x
Unused-12
-8
-16
add3:pushl%ebpmovl %esp, %ebpsubl $24, %esp # Alloc. 24 bytesmovl 8(%ebp), %eaxmovl %eax, -4(%ebp)# Set localx to x
add3:pushl%ebpmovl %esp, %ebpsubl $24, %esp # Alloc. 24 bytesmovl 8(%ebp), %eaxmovl %eax, -4(%ebp)# Set localx to x -20
-24
![Page 112: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/112.jpg)
112
Carnegie Mellon
%esp
Creating Pointer as Argument
int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}
int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}
Use leal instruction to compute address of localx
Middle part of add3
x
Rtn adrOld
%ebp%ebp 0
4
8
-4 localx
Unused-12
-8
-16
movl $3, 4(%esp) # 2nd arg = 3leal -4(%ebp), %eax# &localxmovl %eax, (%esp) # 1st arg = &localxcall incrk
movl $3, 4(%esp) # 2nd arg = 3leal -4(%ebp), %eax# &localxmovl %eax, (%esp) # 1st arg = &localxcall incrk
-20
-24
3 %esp+4
![Page 113: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/113.jpg)
113
Carnegie Mellon
%esp
Retrieving local variable
int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}
int add3(int x) { int localx = x; incrk(&localx, 3); return localx;}
Retrieve localx from stack as return value
Final part of add3
x
Rtn adrOld
%ebp%ebp 0
4
8
-4 localx
Unused-12
-8
-16
movl -4(%ebp), %eax # Return val= localxleaveret
movl -4(%ebp), %eax # Return val= localxleaveret
-20
-24
![Page 114: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/114.jpg)
114
Carnegie Mellon
IA32/Linux+Windows Register Usage
%eax, %edx, %ecx Caller saves prior to call if values
are used later
%eax also used to return integer value
%ebx, %esi, %edi Callee saves if wants to use them
%esp, %ebp special form of callee save Restored to original values upon
exit from procedure
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
Caller-SaveTemporaries
Callee-SaveTemporaries
Special
![Page 115: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/115.jpg)
115
Carnegie Mellon
Today Switch statements IA 32 Procedures
Stack Structure Calling Conventions Illustrations of Recursion & Pointers
![Page 116: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/116.jpg)
Carnegie Mellon
116
Basic Data Types Integral
Stored & operated on in general (integer) registers Signed vs. unsigned depends on instructions used
Intel ASM Bytes Cbyte b 1 [unsigned] charword w 2 [unsigned] shortdouble word l 4 [unsigned] intquad word q 8 [unsigned] long int (x86-64)
Floating Point Stored & operated on in floating point registers
Intel ASM Bytes CSingle s 4 floatDouble l 8 doubleExtended t 10/12/16 long double
![Page 117: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/117.jpg)
Carnegie Mellon
117
Array Allocation Basic Principle
T A[L]; Array of data type T and length L Contiguously allocated region of L * sizeof(T) bytes
char string[12];
x x + 12
int val[5];
x x + 4 x + 8 x + 12 x + 16 x + 20
double a[3];
x + 24x x + 8 x + 16
char *p[3];
x x + 8 x + 16 x + 24
x x + 4 x + 8 x + 12
IA32
x86-64
![Page 118: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/118.jpg)
Carnegie Mellon
118
Array Access Basic Principle
T A[L]; Array of data type T and length L Identifier A can be used as a pointer to array element 0: Type T*
Reference Type Valueval[4] int 3val int * xval+1 int * x + 4&val[2] int * x + 8val[5] int ??*(val+1)int 5val + i int * x + 4 i
int val[5]; 1 5 2 1 3
x x + 4 x + 8 x + 12 x + 16 x + 20
![Page 119: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/119.jpg)
Carnegie Mellon
119
Array Example
Declaration “zip_dig cmu” equivalent to “int cmu[5]” Example arrays were allocated in successive 20 byte blocks
Not guaranteed to happen in general
#define ZLEN 5typedef int zip_dig[ZLEN];
zip_dig cmu = { 1, 5, 2, 1, 3 };zip_dig mit = { 0, 2, 1, 3, 9 };zip_dig ucb = { 9, 4, 7, 2, 0 };
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
zip_dig mit; 0 2 1 3 9
36 40 44 48 52 56
zip_dig ucb; 9 4 7 2 0
56 60 64 68 72 76
![Page 120: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/120.jpg)
Carnegie Mellon
120
Array Accessing Example
Register %edx contains starting address of array
Register %eax contains array index
Desired digit at 4*%eax + %edx
Use memory reference (%edx,%eax,4)
int get_digit (zip_dig z, int dig){ return z[dig];}
# %edx = z # %eax = dig
movl (%edx,%eax,4),%eax # z[dig]
IA32
zip_dig cmu; 1 5 2 1 3
16 20 24 28 32 36
![Page 121: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/121.jpg)
Carnegie Mellon
121
# edx = zmovl $0, %eax # %eax = i
.L4: # loop:addl $1, (%edx,%eax,4) # z[i]++addl $1, %eax # i++cmpl $5, %eax # i:5jne .L4 # if !=, goto loop
Array Loop Example (IA32)
void zincr(zip_dig z) { int i; for (i = 0; i < ZLEN; i++) z[i]++;}
![Page 122: Carnegie Mellon 1 Introduction to x86 Assembly or “What does my laptop actually do?” Ymir Vigfusson Some slides gracefully borrowed from 18-213@CMU.](https://reader030.fdocuments.us/reader030/viewer/2022013101/56649ef25503460f94c04577/html5/thumbnails/122.jpg)
Carnegie Mellon
122
Pointer Loop Example (IA32)void zincr_p(zip_dig z) { int *zend = z+ZLEN; do { (*z)++; z++; } while (z != zend); }
void zincr_v(zip_dig z) { void *vz = z; int i = 0; do { (*((int *) (vz+i)))++; i += ISIZE; } while (i != ISIZE*ZLEN);}
# edx = z = vzmovl $0, %eax # i = 0
.L8: # loop:addl $1, (%edx,%eax) # Increment vz+iaddl $4, %eax # i += 4cmpl $20, %eax # Compare i:20jne .L8 # if !=, goto loop