Chapter 02 5
Chapter 02 5
Chapter 02 5
Static linking
81
Assembler
n Assembler (or compiler) translates program into
machine instructions
n To simplify translation and programming:
n pseudoinstructions
n li x9, 123 // load immediate value 123 into register x9
n converted to: addi x9, x0, 123
n j Label
n converted to: jal x0, Label
82
83
84
Dynamic Linking
n Static approach
n The library routines become part of the
executable code.
n It loads all routines in the library.
n Only link/load library procedure when it is
called
n Requires procedure code to be relocatable
n Avoids image bloat caused by static linking of
all (transitively) referenced libraries
n Automatically picks up new library versions
Chapter 2 — Instructions: Language of the Computer — 85
85
Loading a Program
n Load from image file on disk into memory
1. Read header to determine segment sizes
2. Create virtual address space
3. Copy text and initialized data into memory
n Or set page table entries so they can be faulted in
4. Set up arguments on stack
5. Initialize registers (including sp, fp, gp)
6. Jump to startup routine
n Copies arguments to x10, … and calls main
n When main returns, do exit syscall
86
87
1.5
0.5
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
1.5
0.5
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
2000
1500
1000
500
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
88
Lessons Learnt
n Instruction count and CPI are not good
performance indicators in isolation
n Compiler optimizations are sensitive to the
algorithm
n Java/JIT compiled code is significantly
faster than JVM interpreted
n Comparable to optimized C in some cases
n Nothing can fix a dumb algorithm!
89
Other ISAs
90
91
92
93
Fallacies
n Backward compatibility Þ instruction set
doesn’t change
n But they do accrete more instructions
94
Pitfalls
n Sequential words are not at sequential
addresses
n Increment by 4, not by 1!
n Keeping a pointer to an automatic variable
after procedure returns
n e.g., passing pointer back via an argument
n Pointer becomes invalid when stack popped
95
Concluding Remarks
n Design principles
1. Simplicity favors regularity
2. Smaller is faster
3. Good design demands good compromises
n Layers of software/hardware
n Compiler, assembler, hardware
96
97
Some statistics
RISC-V instruction classes, examples, correspondence to
high-level program language constructs, and percentage of
RISC-V instructions executed by category for the average
integer and floating point SPEC CPU2006 benchmarks.
98
Check yourself
Given this binary number:
00000001010010110010100000100011
99