Docsity

Assemblers and Compilers - Lecture Slides | COMP 411, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
L08 – Assemblers and Compilers 1
Comp 411 – Fall 2007 9/19/06
Assemblers and Compilers
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 remember
How mnemonics used to make me smile...
And I knew that with just the opcode names
that I could play those assembly games
and 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…
Study sections 2.10-2.15
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Assemblers and Compilers - Lecture Slides | COMP 411 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

L08 – Assemblers and Compilers 9/19/

Assemblers and Compilers

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…

Study sections 2.10-2.

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

Pass 1

  • In the first pass, data andinstructions are encodedand assigned offsets withintheir segment, while thesymbol table isconstructed.- Unresolved addressreferences are set to 0Symbol table after Pass 1

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

Libraries

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

  • Before any call is made toa procedure in “stdio.dll^ .globl^ stdio:stdio:fopen:^ sysloadfclose:^ sysloadfgetc:^ sysloadfputc:^ sysloadfprintf:^ sysload
  • After first call is made toany procedure in “stdio.dll^ .globl^ stdio:stdio:fopen:^ dfopenfclose:^ dclosefgetc:^ dfgetcfputc:^ dfputcfprintf:^ dprintf

sysload: Because, the entrypoints to dynamiclibrary routines arestored in a TABLE.And the contents ofthis table are loadedon an “as needed”basis!

addui^ $sp,$sp,16.. #^ check^

if^ stdio^ module# is loaded,^ if^ not

load^ it

.. #^ backpatch

jump^ table

la^ $t1,stdiola^ $t0,$dfopensw^ $t0,($t1) la^ $t0,$dfclosesw^ $t0,4($t1)la^ $t0,$dfputcsw^ $t0,8($t1)la^ $t0,$dfgetcsw^ $t0,12($t1)la^ $t0,$dfprintfsw^ $t0,16($t1)

-^ Lazy address resolution:

L08 – Assemblers and Compilers 9/19/ Modern Languages ·^ Intermediate “object code language”

Java program^ Compiler JVM bytecodes^ Interpreter

“Library Routines”

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:

int a[10];int total;int main( ) {int i;total = 0;for (i = 0; i < 10; i++) {a[i] = i;total = total + i;} }

L08 – Assemblers and Compilers 9/19/

Unoptimized Assembly Output · With debug flags set: .globl main.textmain:addu $sp,$sp,-

allocates space for ra and i

sw $0,total^

total = 0

sw $0,0($sp)

i = 0

lw $8,0($sp)

copy i to $t

b^ L.^

goto test

L.2:^

for(...) {

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 $