02 Architecture
02 Architecture
L2 Cache L2 Cache
Core A BIGL3PILE
Cache
OF Core
HARDWARE
Memory Bus
REQUIRING A(DRAM)
Memory Unit LOT OF
HIGHLY SKILLED
SSD CARE AND FEEDING! 100G
storage Ethernet
PCIe Bus
CORNELL CS4414 - FALL 2021. 3
WHAT’S INSIDE? ARCHITECTURE = COMPONENTS
OF A COMPUTER + OPERATING SYSTEM
Registers Registers
CPU (L1 cache) CPU (L1 cache)
L2 Cache L2 Cache
Core L3 Cache Core
Memory Bus
Memory Unit (DRAM)
SSD 100G
storage Ethernet
PCIe Bus
CORNELL CS4414 - FALL 2021. 4
WHAT’S INSIDE? ARCHITECTURE = COMPONENTS
OF A COMPUTER + OPERATING SYSTEM
But the past decade has been disruptive! Suddenly even a single
computer might have the ability to do hundreds of parallel tasks!
CORNELL CS4414 - FALL 2021. 17
THE HARDWARE SHAPES THE
APPLICATION DESIGN PROCESS
Example ISAs:
Intel: x86, IA32, Itanium, x86-64
ARM: Used in almost all mobile phones
RISC V: New open-source ISA
CORNELL CS4414 - FALL 2021. 20
TODAY: MACHINE PROGRAMMING I: BASICS
History of Intel processors and architectures
Assembly Basics: Registers, operands, move
Arithmetic & logical operations
C/C++, assembly, machine code
Register file
Heavily used program data Puzzle:
Condition codes On a NUMA machine, a CPU is near a fast
Store status information about most recent memory but can access all memory.
arithmetic or logical operation How does this impact software design?
Used for conditional branching
Register file
Heavily used program data Puzzle: …
Linux doesn’t even tell you which parts of your address space are
mapped to which DRAM units.
CORNELL CS4414 - FALL 2021. 25
(We’ll cover what we
MACHINE LANGUAGE can but probably
won’t have time for
all of this)
CORNELL CS4414 - FALL 2021. 26
THE HARDWARE UNDERSTANDS “PRIMITIVE”
DATA TYPES
“Integer” data of 1, 2, 4, or 8 bytes (SIMD vector data types of 8, 16, 32
Data values or 64 bytes)
Addresses (untyped pointers)
Transfer control
Unconditional jumps to/from procedures
Conditional branches
Indirect branches
CORNELL CS4414 - FALL 2021. 31
Carnegie Mellon
movq (%rcx),%rax
movq 8(%rbp),%rdx
%rsi
%rdi
void swap
(long *xp, long *yp)
{ swap:
long t0 = *xp; movq (%rdi), %rax
long t1 = *yp; movq (%rsi), %rdx
*xp = t1; movq %rdx, (%rdi)
*yp = t0; movq %rax, (%rsi)
} ret
Understanding swap()
Memory Addr
void swap Registers
(long *xp, long *yp) xp
{ %rdi
long t0 = *xp;
%rsi
long t1 = *yp;
*xp = t1; %rax
*yp = t0; yp
} %rdx
Register Value
%rdi xp
%rsi yp swap:
%rax t0 movq (%rdi), %rax # t0 = *xp
%rdx t1 movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37
Carnegie Mellon
Understanding swap()
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 0x108
%rdx 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding swap()
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding swap()
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding swap()
Memory
Registers Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
Understanding swap()
Memory
Registers Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 123 0x100
swap:
movq (%rdi), %rax # t0 = *xp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1
movq %rax, (%rsi) # *yp = t0
ret
movq (%rcx),%rax
movq 8(%rbp),%rdx
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]]
%rcx 0x0100
%rcx 0x0100
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
long m12(long x)
{ Converted to ASM by compiler:
return x*12; leaq (%rdi,%rdi,2), %rax # t = x+2*x
} salq $2, %rax # return t<<2
Depending how you count, there are 2,034 total x86 instructions
(If you count all addr modes, op widths, flags, it’s actually 3,683)
Intel is a believer in the “CISC” model: complex instructions that are highly optimized
Modern example: vector parallel instructions (also called SIMD: Single instruction,
multiple data). Introduced to make the x86 more competitive with GPU accelerators
Such as “Multiply these two vectors and put the result in this third vector”, or “sum up the elements
in this vector, and put the result here.”
The underlying hardware uses parallel processing to do the job faster.
The C++ compiler can recognize many of these patterns and will emit vector parallel instructions (if
the target computer supports them). You can also provide “hints” to the compiler, to do so.
There are many more examples; we will see a few later in the semester
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 53
Carnegie Mellon
Compiler (c++)
Obtain with command This uses the “indirect” addressing mode: dest holds
a memory address and *dest is a long integer at that
C++ sum.c address. We are using that location as a variable here!
Transfer control
Unconditional jumps to/from procedures
Conditional branches
Object Code
Code for sumstore
Assembler
0x0400595:
0x53
Translates .s into .o
0x48 Binary encoding of each instruction
0x89 Nearly-complete image of executable code
0xd3
0xe8
Missing linkages between code in different
0xf2 files
0xff Linker
0xff
0xff Resolves references between files
• Total of 14 bytes
0x48 Combines with static run-time libraries
0x89 • Each instruction
e.g., code for malloc, printf
0x03 1, 3, or 5 bytes
0x5b • Starts at address
Some libraries are dynamically linked
0xc3 0x0400595 Linking occurs when program begins
execution
Disassembler
objdump –d sum
Useful tool for examining object code
Analyzes bit pattern of series of instructions
Produces approximate rendition of assembly code
Can be run on either a.out (complete executable) or .o file
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 63
Carnegie Mellon
Alternate Disassembly
Disassembled
Warning!
Hackers disassemble programs to look for coding errors that they can leverage to
steal passwords or even take control by sending malformed inputs.
This is why it is illegal to disassemble things like Microsoft Word.
Cornell has harsh penalties for people who engage in hacking activities
while enrolled in the university. A hacker could be suspended or expelled!