Lecture 6
Lecture 6
Announcement
• Programming Assignment 1 is due today
• NetID is not your 8-digit URID
• If you submitted your assignment before Sep. 11th, please
submit one more time
• For using slip days, email the two TAs listed in the
assignment description
• Programming Assignment 2 is out today
• Details:
https://www.cs.rochester.edu/courses/252/fall2024/labs/
assignment2.html
• Due on Sep. 30th, 11:59 PM
• You (may still) have 3 slip days
2
Carnegie Mellon
Announcement
• Programming assignment 2 is in x86 assembly language.
• Read the instructions before getting started!!!
• You get 1/4 point off for every wrong answer
• Maxed out at 10
• TAs are best positioned to answer your questions about
programming assignments!!!
• Programming assignments do NOT repeat the lecture
materials. They ask you to synthesize what you have
learned from the lectures and work out something new.
3
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
Fetch Instruction
(According to PC)
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
Fetch Instruction
(According to PC)
0x4801d8
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
addq %rax,(%rbx)
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
Update
Condition
Codes
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
Update
Condition
Codes
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
Update
Condition
Codes Adjust
PC
4
Instruction Processing Sequence
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes
Update
Condition
Codes Adjust
PC
4
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
5
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
5
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
5
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
6
Data Movement Instruction Example
• Semantics:
• Move (really, copy) data in register %rdx to memory location
whose address is the value stored in %rdi
• Pointer dereferencing
8
Data Movement Instruction Example
address
• Semantics:
• Move (really, copy) data in register %rdx to memory location
whose address is the value stored in %rdi
• Pointer dereferencing
8
Data Movement Instruction Example
address
• Semantics:
• Move (really, copy) data in register %rdx to memory location
whose address is the value stored in %rdi
• Pointer dereferencing
8
Data Movement Instruction Example
address
• Semantics:
• Move (really, copy) data in register %rdx to memory location
whose address is the value stored in %rdi
• Pointer dereferencing
8
Data Movement Instructions
movq Source, Dest
9
Data Movement Instructions
movq Source, Dest
Operator Operands
9
Data Movement Instructions
movq Source, Dest
Operator Operands
• Memory:
• Simplest example: (%rax)
• How to obtain the address is called “addressing mode”
9
Data Movement Instructions
movq Source, Dest
Operator Operands
• Memory:
• Simplest example: (%rax)
• How to obtain the address is called “addressing mode”
• Register:
• Example: %rax, %r13
• But %rsp reserved for special use
9
Data Movement Instructions
movq Source, Dest
Operator Operands
• Memory:
• Simplest example: (%rax)
• How to obtain the address is called “addressing mode”
• Register:
• Example: %rax, %r13
• But %rsp reserved for special use
• Immediate: Constant integer data
• Example: $0x400, $-533; like C constant, but prefixed with ‘$’
• Encoded with 1, 2, or 4 bytes; can only be source
9
movq Operand Combinations
Reg
Imm
Mem
Mem Reg
Mem Reg
Mem Reg
Mem Reg
Mem Reg
Mem Reg
Mem Reg
Mem Reg
Mem Reg
11
Example of Simple Addressing Modes
Registers Memory Addr
void swap
(long *xp, long *yp) %rdi xp *xp xp
{
long t0 = *xp; %rsi yp
long t1 = *yp;
%rax
*xp = t1;
*yp = t0; %rdx yp
} *yp
11
Example of Simple Addressing Modes
Registers Memory Addr
void swap
(long *xp, long *yp) %rdi xp *xp xp
{
long t0 = *xp; %rsi yp
long t1 = *yp;
%rax
*xp = t1;
*yp = t0; %rdx yp
} *yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
11
Understanding Swap()
Registers Memory Addr
123 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax
0x108
%rdx 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
123 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax
0x108
%rdx 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
123 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
123 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
123 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
123 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
456 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
456 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 456 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Understanding Swap()
Registers Memory Addr
456 0x120 xp
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123
0x108
%rdx 456 123 0x100 yp
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
12
Memory Addressing Modes
• An addressing mode specifies:
• how to calculate the effective memory address of an operand
• by using information held in registers and/or constants
13
Memory Addressing Modes
• An addressing mode specifies:
• how to calculate the effective memory address of an operand
• by using information held in registers and/or constants
• Normal: (R)
• Memory address: content of Register R (Reg[R])
• Pointer dereferencing in C
13
Memory Addressing Modes
• An addressing mode specifies:
• how to calculate the effective memory address of an operand
• by using information held in registers and/or constants
• Normal: (R)
• Memory address: content of Register R (Reg[R])
• Pointer dereferencing in C
14
Complete Memory Addressing Modes
• The General Form: D(Rb,Ri,S)
• Memory address: Reg[Rb] + S * Reg[Ri] + D
• E.g., 8(%eax, %ebx, 4); // address = %eax + 4 * %ebx + 8
• D: Constant “displacement”
• Rb: Base register: Any of 16 integer registers
• Ri: Index register: Any, except for %rsp
• S: Scale: 1, 2, 4, or 8
• What is 8(%eax, %ebx, 4)used for?
14
Complete Memory Addressing Modes
• The General Form: D(Rb,Ri,S)
• Memory address: Reg[Rb] + S * Reg[Ri] + D
• E.g., 8(%eax, %ebx, 4); // address = %eax + 4 * %ebx + 8
• D: Constant “displacement”
• Rb: Base register: Any of 16 integer registers
• Ri: Index register: Any, except for %rsp
• S: Scale: 1, 2, 4, or 8
• What is 8(%eax, %ebx, 4)used for?
• Special Cases
(Rb,Ri) address = Reg[Rb]+Reg[Ri]
D(Rb,Ri) address = Reg[Rb]+Reg[Ri]+D
(Rb,Ri,S) address = Reg[Rb]+S*Reg[Ri]
14
Carnegie Mellon
%rcx 0x0100
(%rdx,%rcx,4)
0x80(,%rdx,2)
15
Carnegie Mellon
%rcx 0x0100
(%rdx,%rcx,4)
0x80(,%rdx,2)
15
Carnegie Mellon
%rcx 0x0100
0x80(,%rdx,2)
15
Carnegie Mellon
%rcx 0x0100
15
Carnegie Mellon
%rcx 0x0100
15
Carnegie Mellon
16
Carnegie Mellon
16
Carnegie Mellon
16
Carnegie Mellon
• Uses
• Computing addresses without a memory reference
• E.g., translation of p = &x[i];
16
Carnegie Mellon
17
Carnegie Mellon
long m12(long x)
{
return x*12;
}
17
Carnegie Mellon
long m12(long x)
{
return x*12;
}
Converted to
assembly by compiler:
17
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
18
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
18
Assembly Program Instructions
CPU Addresses Memory
Assembly Register
Programmer’s PC File Code
Data Data
Perspective Condition Stack
of a Computer ALU Instructions
Codes Heap
19
Carnegie Mellon
20
Carnegie Mellon
u •••
+ v •••
u+v •••
TAddw(u , v) •••
u •••
+ v •••
u+v •••
TAddw(u , v) •••
21
Carnegie Mellon
22
Carnegie Mellon
long signed_add
(long x, long y)
{
long res = x + y;
return res;
}
#x in %rdx, y in %rax
addq %rdx, %rax
22
Carnegie Mellon
23
Carnegie Mellon
24
Carnegie Mellon
25
Carnegie Mellon
25
Carnegie Mellon
26
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
27
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
27
CF set when
yxxxxxxxxxxxx...
+ yxxxxxxxxxxxx...
1 zxxxxxxxxxxxx...
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
29
ZF set when
000000000000…00000000000
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
31
SF set when
1xxxxxxxxxxx…xxxxxxxxxxx
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
33
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Bit-level
111
+) 010
1001
33
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Bit-level Signed
111 -1
+) 010 +) 2
1001 1
33
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Bit-level
011
+) 001
100
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Bit-level Signed
011 3
+) 001 +) 1
100 -4
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
100
+) 111 0 0 0 0
c011 CF ZF SF OF
35
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
100
+) 111 10 0 0 0
c011 CF ZF SF OF
35
Carnegie Mellon
Condition Codes
addq %rax, %rbx
Arithmetic instructions implicitly set condition codes (think of it as
side effect)
CF set if %rax + %rbx generates a carry (i.e., unsigned overflow)
ZF set if %rax + %rbx == 0
SF set if %rax + %rbx < 0
OF set if %rax + %rbx overflows when %rax and %rbx are treated as signed
numbers
%rax > 0, %rbx > 0, and (%rax + %rbx) < 0), or
%rax < 0, %rbx < 0, and (%rax + %rbx) >= 0)
100
+) 111 10 0 0 01
c011 CF ZF SF OF
35
Compare Instruction
cmpq a, b
▪ Computes 𝑏 − 𝑎 (just like sub)
▪ Sets condition codes based on result, but…
▪ Does not change 𝒃
▪ All it does is setting condition codes!
Carnegie Mellon
37
Carnegie Mellon
37
Carnegie Mellon
37
Carnegie Mellon
37
Carnegie Mellon
37
Carnegie Mellon
37
Carnegie Mellon
Register Use(s)
int gt (long x, long y)
{ %rdi Argument x
return x > y; %rsi Argument y
}
%rax Return value
Register Use(s)
int gt (long x, long y)
{ %rdi Argument x
return x > y; %rsi Argument y
}
%rax Return value