Chapter 02 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Dr.

Youssef Iraqi 20 March 2023

§2.12 Translating and Starting a Program


Translation and Startup

Many compilers produce


object modules directly

Static linking

Chapter 2 — Instructions: Language of the Computer — 81

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 mv x10, x11 // register x10 gets register x11


n converted to: addi x10, x11, 0

n j Label
n converted to: jal x0, Label

n and x9, x10, 15


n becomes: andi x9, x10, 15

Chapter 2 — Instructions: Language of the Computer — 82

82

Chapter 2 — Instructions: Language of the Computer 1


Dr. Youssef Iraqi 20 March 2023

Producing an Object Module


n Provides information for building a complete
program from the pieces
n Header: described contents of object module
n Text segment: translated instructions
n Static data segment: data allocated for the life of the
program
n Relocation info: for contents that depend on absolute
location of loaded program
n Symbol table: global definitions and external refs
n Debug info: for associating with source code

Chapter 2 — Instructions: Language of the Computer — 83

83

Linking Object Modules


n Produces an executable image
1. Merges segments
2. Resolve labels (determine their addresses)
3. Patch location-dependent and external refs
n Could leave location dependencies for
fixing by a relocating loader
n But with virtual memory, no need to do this
n Program can be loaded into absolute location
in virtual memory space

Chapter 2 — Instructions: Language of the Computer — 84

84

Chapter 2 — Instructions: Language of the Computer 2


Dr. Youssef Iraqi 20 March 2023

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

Chapter 2 — Instructions: Language of the Computer — 86

86

Chapter 2 — Instructions: Language of the Computer 3


Dr. Youssef Iraqi 20 March 2023

Effect of Compiler Optimization


Compiled with gcc for Pentium 4 under Linux

3 Relative Performance 140000 Instruction count


2.5 120000
100000
2
80000
1.5
60000
1
40000
0.5 20000
0 0
none O1 O2 O3 none O1 O2 O3

180000 Clock Cycles 2 CPI


160000
140000 1.5
120000
100000
1
80000
60000
40000 0.5
20000
0 0
none O1 O2 O3 none O1 O2 O3

Chapter 2 — Instructions: Language of the Computer — 87

87

Effect of Language and Algorithm


3 Bubblesort Relative Performance
2.5

1.5

0.5

0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT

2.5 Quicksort Relative Performance


2

1.5

0.5

0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT

3000 Quicksort vs. Bubblesort Speedup


2500

2000

1500

1000

500

0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT

Chapter 2 — Instructions: Language of the Computer — 88

88

Chapter 2 — Instructions: Language of the Computer 4


Dr. Youssef Iraqi 20 March 2023

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!

Chapter 2 — Instructions: Language of the Computer — 89

89

§2.16 Real Stuff: MIPS Instructions

Other ISAs

n MIPS, ARMv7/v8, x86

n Read sections 2.16, 2.17, 2.18, 2.19 of the


textbook.

Chapter 2 — Instructions: Language of the Computer — 90

90

Chapter 2 — Instructions: Language of the Computer 5


Dr. Youssef Iraqi 20 March 2023

§2.20 The Rest of the RISC-V Instruction Set


Other RISC-V Instructions
n Base integer instructions (RV32I)

n 64-bit variant: RV64I


n registers are 64-bits wide

Chapter 2 — Instructions: Language of the Computer — 91

91

RISC-V Base and Extensions

Chapter 2 — Instructions: Language of the Computer — 92

92

Chapter 2 — Instructions: Language of the Computer 6


Dr. Youssef Iraqi 20 March 2023

§2.22 Fallacies and Pitfalls


Fallacies
n Powerful instruction Þ higher performance
n Fewer instructions required
n But complex instructions are hard to implement
n May slow down all instructions, including simple ones
n Compilers are good at making fast code from simple
instructions
n Use assembly code for high performance
n But modern compilers are better at dealing with
modern processors
n For humans: more lines of code Þ more errors and
less productivity

Chapter 2 — Instructions: Language of the Computer — 93

93

Fallacies
n Backward compatibility Þ instruction set
doesn’t change
n But they do accrete more instructions

x86 instruction set

Chapter 2 — Instructions: Language of the Computer — 94

94

Chapter 2 — Instructions: Language of the Computer 7


Dr. Youssef Iraqi 20 March 2023

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

Chapter 2 — Instructions: Language of the Computer — 95

95

§2.23 Concluding Remarks

Concluding Remarks
n Design principles
1. Simplicity favors regularity
2. Smaller is faster
3. Good design demands good compromises

n Make the common case fast

n Layers of software/hardware
n Compiler, assembler, hardware

Chapter 2 — Instructions: Language of the Computer — 96

96

Chapter 2 — Instructions: Language of the Computer 8


Dr. Youssef Iraqi 20 March 2023

The RISC-V instruction set covered so far.

Chapter 2 — Instructions: Language of the Computer — 97

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.

Chapter 2 — Instructions: Language of the Computer — 98

98

Chapter 2 — Instructions: Language of the Computer 9


Dr. Youssef Iraqi 20 March 2023

Check yourself
Given this binary number:
00000001010010110010100000100011

n What is it in hexadecimal format?


n Assuming it is an unsigned number, what is it in
decimal?
n Does the value change if it is considered a
signed number?
n What assembly language program does it
represent?

Chapter 2 — Instructions: Language of the Computer — 99

99

Chapter 2 — Instructions: Language of the Computer 10

You might also like