CPSC 313.203 – 2019W2
CPSC 313
Computer Hardware & Operating Systems
Unit 0 – Introduction
Please close or put away your laptops
CPSC 313.203 – 2019W2
Who am I?
‣ Donald Acton
‣ How should you address me: • Dr Acton
• Don
• Donald
‣ I am not
• Acton and my last name is not Action
‣ Don’t be afraid to say hi to me outside of class ‣ Pronouns: he, him
CPSC 313.203 – 2019W2
Areas of Interest
‣ Operating Systems
‣ Computer Networking ‣ Distributed Computing
CPSC 313.203 – 2019W2
Current Registration Situation
‣ Situation as of 10:30 today
• 203: 137 students, 0 on wait list
• 204: 180 students, 3 on wait list
• About 20 not in tutorials but there are about 60 seats untaken, more on tutorials in a moment
‣ We have opened 15 additional seats in section 203
• Tell your friends in section 204 that should be able to switch into 203 if they want ☺
CPSC 313.203 – 2019W2
ON TO THE COURSE
CPSC 313.203 – 2019W2
Abstraction Hides Complexity
‣ Hiding complexity is usually good
• All computation is mechanical
• At the machine-level all computation is extremely complex
‣ We work at a higher-level whenever we can • Design, describe, evaluate and debug
‣ We use abstractions to explain, understand, and help use reason about
what is going on
7
CPSC 313.203 – 2019W2
Complexity is … Complex
‣ But … abstractions sometimes hide too much
• Things like performance, correctness, security are not fully covered by one layer or
perspective
‣ Good programmers must be able to dig deeper
• by understanding computation at various levels of abstraction
• including the lower layers of abstraction describing how hardware works
‣ Extra reading on this topic on Canvas
8
CPSC 313.203 – 2019W2
Learning to think across the layers
‣ Good mental models of multiple layers
• CPU implementation
– what is concurrent and what is sequential – caches and the memory system
• Disks
– virtual memory – file systems
• The role, if any, of operating systems in all this
CPSC 313.203 – 2019W2
A big picture of computing
0x00000000:
0xffffffff:
0x00000000:
0xffffffff:
%eax: %ebx: …
movl 6(%ebp), %eax addl $10, %eax jne loop
WB
write back
0x00000000:
0xffffffff:
human composition
static compile and link
dynamic load and link
operating system process and virtual memory
execution instruction-set architecture physical memory
hardware implementation processor and memory hierarchy
gates transistors, capacitors and wires photolithography silicon, glass and metal
IF
inst fetch
ID
inst decode
EX ME
execute mem access
10
CPSC 313.203 – 2019W2
int foo(int j, int k) { int i, l;
l=j;
for (i=0; i
JMP 0x1000 JMP loop
7
0
7
2
7
3
7
4
7
5
7
6
10
00
00
00
00
00
00
PC <- Dest
CPSC 313.203 – 2019W2
Conditional Jumps
‣ Jump based on the state of the condition codes:
Condition
Test
Zero flag
Sign Flag
g
>0
0
and
0
!ZF & !SF
ge
>= 0
0
!SF
e
== 0
1
ZF
ne
!= 0
0
!ZF
le
<= 0
1
or
1
80
ZF | SF
l
<0
1
SF
CPSC 313
• Blanks mean that the flag can be either 0 or 1
CPSC 313.203 – 2019W2
Code Example
# Using the conditional jump instructions
#rax=rsi ==rdi?1:0
# to compare two numbers. irmovq 0xff, %rsi
irmovq 0x1, %rdi
# rax = rsi > rdi ? 1 : 0 irmovq 1, %rax
rrmovq %rsi, %r10
l1:
irmovq 1, %rax rrmovq %rsi, %r10 subq %rdi, %r10
CPSC 313
subq %rdi, %r10
# if result should be 1, we’re all set
jg l1
irmovq 0, %rax
#rax=rsi ==rdi?1:0 l1:
irmovq 1, %rax rrmovq %rsi, %r10 subq %rdi, %r10
# if result should be 1, we’re all set je l2
irmovq 0, %rax
# rax = rsi <= 0 l2:
irmovq 1, %rax
# if result should be 1, we're all set
l3:
81
andq %r10, %r10 jle l3
irmovq 0, %rax
halt
CPSC 313.203 – 2019W2
irmovq 0, %rax
The Nifty Conditional Move
‣ Recall that we had a fun field for RRMOVQ 2-byte instruction
RRMOVQ %r8, %r9
• The CMOVxx instructions use the condition codes to
2
0
rA
rB
2
0
8
9
CPSC 313
provide conditional moves.
#Compare rsi and rdi irmovq 1, %rax rrmovq %rsi, %r10 subq %rdi, %r10 jxx L1
#Compare rsi and rdi
irmovq 0, %r8a2x irmovq 1, %r11 rrmovq %rsi, %r10 subq %rdi, %r10 cmovxx %r11, %rax
irmovq 0, %rax
L1:
CPSC 313.203 – 2019W2
Calling Conventions
‣ Defines register, and stack usage when one function calls another
‣ This permits compiler to compile code for a function without knowing
where it will be called from
‣ Calling conventions are: • Compiler
• Architecture
• Operating system dependent
‣ For example:
• 32bit, x86, gcc based
• calling convention is called cdecl (after C ☺)
83
CPSC 313.203 – 2019W2
cdecl calling conventions
‣ All parameters passed on the stack
‣ Parameters are pushed on the stack in reverse order
‣ Registers eax, ecx, edx can be overwritten by the called function
‣ Function’s return value is in eax
‣ ebp (frame pointer) is pushed onto the stack upon function entry
‣ ebp is then set to point to the current top of stack. Parameters and local
variables are then addressed relative to ebp ‣ Additional rules for floating point registers
84
CPSC 313.203 – 2019W2
213 calling conventions (For Comparison)
‣ All parameters passed on the stack
‣ Parameters are pushed on the stack in reverse order
‣ All registers can be overwritten by the called function (except r5 and r6) ‣ Function’s return value is in r0
‣ Return address for function is passed through r6
CS61 Fall 2015
85
CPSC 313.203 – 2019W2
Y86-64 ISA: Function Calls and Stack
‣ Parameters are passed in registers unless there are too many ‣ Stack pointer (%rsp) represents the top of the stack
• %rbp is sometimes used for the base of the frame (where stack was when function started, %rbp called the frame or base pointer)
‣ Stack is used for:
• local variables in functions
• arguments (if too many)CS61 Fall 2015 86 • returning address
• register values before a function call that may change them
CPSC 313.203 – 2019W2
Y86-64 Calling Conventions (based on X86-64)
‣ Stack pointer: %rsp
‣ Arguments passed in registers, in this order: %rdi, %rsi, %rdx, %rcx, %r8,
%r9
• Additional arguments passed in stack
‣ Returning value passed in %rax
‣ Caller-save registers: %rax, %r10, %r11 + arguments • Callee can change their values at will
CS61 Fall 2015 87
• Callee responsible for saving before it calls other functions
‣ Callee-save registers: %rsp, %rbx, %rbp, %r12, %r13, %r14 • Caller assumes they have same value when function returns
• Callee must restore value before returning
CPSC 313.203 – 2019W2
Y86-64 Calling Conventions Quick summary
• Click to edit Master text styles • Second level
• Third level
– Fourth level
» Fifth level
88
CPSC 313.203 – 2019W2
Drawing stacks the book’s way
• Click to edit Master text styles • Second level
• Third level
– Fourth level
» Fifth level
→
89
stack grows towards lower addresses.
caller's local data Arg 10
Arg 9
Arg 8
return address saved caller %rbp local data
local data
0x1038
CPSC 313.203 – 2019W2
Jan 15 classes cancelled due to snow. The next set of slides Along with Margos videos were used to bridge to the Jan 17 material
CPSC 313.203 – 2019W2