











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












L08 – Assemblers and Compilers 9/19/
When I find my code in tons of trouble,Friends and colleagues come to me,Speaking words of wisdom:"Write in C."
Long, long, time ago, I can still rememberHow mnemonics used to make me smile...And I knew that with just the opcode namesthat I could play those assembly gamesand maybe hack some programs for a while.But Comp 411 made me shiver,With every new lecture that was delivered,There was bad news at the door step,I couldn’t handle another problem set.My whole life thus far must have flashed,the day the SPIM simulator crossed my path,All I know is that it made my hard disk crash,On the day the hardware died.And I was singing…
L08 – Assemblers and Compilers 9/19/
Path from Programs to Bits · Traditional Compilation^ C or C++ program^ Compiler^ Assembly Code^ Assembler^ “Object Code”
“Library Routines” Linker “Executable” Loader “Memory”
High-level, portable(architectureindependent) programdescription Architecture dependentmnemonic programdescription with symbolicmemory references Machine languagewith symbolic memoryreferences
A collection of precompiledobject code modulesMachine languagewith all memory referencesresolved Program and data bitsloaded into memory
L08 – Assemblers and Compilers 9/19/ Resolving Addresses- 1
st^ Pass
·^ “Old-style” 2-pass assembler approach
test:slti^ $t0,$t3, 0x2968000a 40
beq^ $0,$0,test Code 0x3c09^0000 0x3529^0000 0x000050210x000058210x1000^000016 0x000b4080 0x012840200xad0b00000x014b50200x216b0001 0x1500^0000 0x3c01^0000 0xac2a^0000 0x03e
j^ $ra 56
sw^ $t2,total 4852
bne^ $t0,$0,loop 44
add^ $t0,$t1,$t0sw^ $t0,($t0)add^ $t0,$t1,$t0addi^ $t3,$t3, 24283236
loop:sll^ $t0,$t3, 20
move^ $t2,$move^ $t3,$ (^812)
la $t1,array 0 4
Instruction Segmentoffset
(^40) data total
(^40) text test
(^20) text loop
textmain
LocationpointerSegmentSymboloffsetdataarray
L08 – Assemblers and Compilers 9/19/ Resolving Addresses – 2
nd^ Pass
·^ “Old-style” 2-pass assembler approach Pass 2
(^40) data total
(^40) text test
(^20) text loop
textmain
-^ In the second pass, theappropriate fields of thoseinstructions that referencememory are filled in with thecorrect values if possible.^ Symbol table after Pass 1LocationpointerSegmentSymboloffsetdataarray test:slti^ $t0,$t3, 0x2968000a 40
beq^ $0,$0,test Code 0x3c09^1001 0x3529^0000 0x000050210x000058210x1000^000616 0x000b4080 0x012840200xad0b00000x014b50200x216b0001 0x1500fffa0x3c01^1001 0xac2a^0028 0x03e
j^ $ra 56
sw^ $t2,total 4852
bne^ $t0,$0,loop 44
add^ $t0,$t1,$t0sw^ $t0,($t0)add^ $t0,$t1,$t0addi^ $t3,$t3, 24283236
loop:sll^ $t0,$t3, 20
move^ $t2,$move^ $t3,$ (^812)
la $t1,array 0 4
Instruction Segmentoffset
L08 – Assemblers and Compilers 9/19/ The Role of a Linker Some aspects of address resolution cannot be handled bythe assembler alone.1) References to data or routines in other object modules2)The layout of all segments in memory3) Support for REUSABLE code modules4) Support for RELOCATABLE code modulesThis final step of resolution is the job of a LINKER
Linker^
ExecutableFile
Sourcefile^
Assembler^
Objectfile Sourcefile^
Assembler^
Objectfile Sourcefile^
Assembler^
Objectfile
L08 – Assemblers and Compilers 9/19/
Static and Dynamic Libraries • LIBRARIES^ are commonly used routines stored as aconcatenation of “Object files”. A global symbol table ismaintained for the entire library with
entry points
for
each routine. • When routines in LIBRARIES are referenced by assemblymodules, the routine’s entry points are resolved by the LINKER, and the appropriate code is added to theexecutable. This sort of linking is called STATIC linking. • Many programs use common libraries. It is wasteful ofboth memory and disk space to include the same code inmultiple executables. The modern alternative to STATIClinking is to allow the
LOADER^ and
THE PROGRAM
ITSELF^ to resolve the addresses of libraries routines.This form of lining is called DYNAMIC linking (e.x. .dll).
L08 – Assemblers and Compilers 9/19/
Dynamically Linked Libraries
-^ Lazy address resolution:
L08 – Assemblers and Compilers 9/19/ Modern Languages ·^ Intermediate “object code language”
Java program^ Compiler JVM bytecodes^ Interpreter
High-level, portable(architectureindependent) programdescription PORTABLE mnemonicprogram description withsymbolic memoryreferencesAn application thatEMULATES a virtualmachine. Can be writtenfor any Instruction SetArchitecture. In the end,machine languageinstructions must beexecuted for each JVMbytecode
L08 – Assemblers and Compilers 9/19/ Compiler Optimizations ·^ Example “C” Code:
L08 – Assemblers and Compilers 9/19/
Unoptimized Assembly Output · With debug flags set: .globl main.textmain:addu $sp,$sp,-
sw $0,total^
sw $0,0($sp)
lw $8,0($sp)
b^ L.^
L.2:^
sll $24,$8,
make i a word offset sw $8,array($24)
#^ array[i] = i lw $24,total
total = total + i addu $24,$24,$8sw $24,totaladdi $8,$8,
i = i + 1 L.3:sw $8,0($sp)
update i in memory la $24,^
#^ loads const 10 blt $8,$24,L.
#} loops while i < 10 addu $sp,$sp,8j $
L08 – Assemblers and Compilers 9/19/
Loop-Invariant Code Motion · Assign globals to temp registers and movesassignments outside of loop .globl main.textmain:addu $sp,$sp,-
#allocates space for ra sw $0,total^
#total = 0 move $9,$^
#temp for total move $8,$^
#i = 0 b^ L.^
#goto test L.2:^
#for(...) { sll $24,$8,
make i a word offset sw $8,array($24)
#^ array[i] = i addu $9,$9,$8sw $9,totaladdi $8,$8,
i = i + 1 L.3:la $24,
#^ loads const 10 blt $8,$24,L.
#} loops while i < 10 addu $sp,$sp,4j $
L08 – Assemblers and Compilers 9/19/ Remove Unnecessary Tests · Since “i” is initially set to “0”, we already know it is lessthan “10”, so why test it the first time through? .globl main.textmain:addu $sp,$sp,-
#allocates space for ra sw $0,total^
#total = 0 move $9,$^
#temp for total move $8,$^
#i = 0 L.2:^
#for(...) { sll $24,$8,
make i a word offset sw $8,array($24)
#^ array[i] = i addu $9,$9,$8addi $8,$8,
i = i + 1 slti $24,$8,
loads const 10 bne $24,$0,L.
#} loops while i < 10 sw^ $9,totaladdu $sp,$sp,4j $
L08 – Assemblers and Compilers
Unrolling Loop 9/19/
·^ Two copies of the inner loop reduce the branchingoverhead^ .globl main.textmain:
addu $sp,$sp,-
#allocates space for ra and i sw $0,total^
#total = 0 move $9,$^
#temp for total move $8,$^
#i = 0 L.2:sll $24,$8,
#for(...) { sw $8,array($24)
#^ array[i] = i addu $9,$9,$8addi $8,$8,
i = i + 1 sll $24,$8,
sw $8,array($24)
#^ array[i] = i addu $9,$9,$8addi $8,$8,
i = i + 1 slti $24,$8,
loads const 10 bne $24,$0,L.
#} loops while i < 10 sw^ $9,totaladdu $sp,$sp,4j $