Carnegie Mellon
1
Basics of Machine-Level Programming
(continued)
Janne Lindqvist
(Slides adapted from Bryant, O’Hallaron, and Nath.)

Carnegie Mellon
2
Carnegie Mellon
Today
Simple addressing modes continued
Complete addressing mode, address computation (leal)
Arithmetic operations
Intro to x86-64

Carnegie Mellon
3
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

Carnegie Mellon
4
Using Simple Addressing Modes
void swap(int *xp, int *yp)
{
int t0 = *xp;
int t1 = *yp;
*xp = t1;
*yp = t0;
}
Body
Set
Up
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

Carnegie Mellon
5
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
PC
Registers
Memory
Object Code
Program Data
OS Data
Addresses
Data
Instructions
Stack
Condition
Codes
§
Memory
§
Byte addressable array
§
Code, user data, (some) OS data
§
Includes stack used to support
procedures

Carnegie Mellon
6
Using Simple Addressing Modes
void swap(int *xp, int *yp)
{
int t0 = *xp;
int t1 = *yp;
*xp = t1;
*yp = t0;
}
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
Body
Set
Up
Finish

Carnegie Mellon
7
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 = xp
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

Carnegie Mellon
8
Understanding Swap
0x120
0x124
Rtn adr
%ebp
0
4
8
12
Offset
-4
123
456
Address
0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x104
movl
8(%ebp), %edx
# edx = xp
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

Carnegie Mellon
9
Understanding Swap
0x120
0x124
Rtn adr
%ebp
0
4
8
12
Offset
-4
123
456
Address
0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x124
0x104
movl
8(%ebp), %edx
# edx = xp
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

Carnegie Mellon
10
Understanding Swap
0x120
0x124
Rtn adr
%ebp
0
4
8
12
Offset
-4
123
456
Address
0x124
0x120
0x11c
0x118
0x114
0x110
0x10c
0x108
0x104
0x100
yp
xp
%eax
%edx
%ecx
%ebx
%esi
%edi
%esp
%ebp
0x120
0x104
0x124
movl
8(%ebp), %edx
# edx = xp
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

Carnegie Mellon
11
456
