DOC PREVIEW
Berkeley COMPSCI 61C - Lecture 11 - Starting a Program

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61C L11 Linker © UC Regents1CS61C - Machine StructuresLecture 11 - Starting a ProgramOctober 4, 2000David Pattersonhttp://www-inst.eecs.berkeley.edu/~cs61c/CS61C L11 Linker © UC Regents2Review (1/2)° IEEE 754 Floating Point Standard:Kahan pack as much in as could getaway with• +/- infinity, Not-a-Number (Nan), Denorms• 4 rounding modes° Stored Program Concept: Both data andactual code (instructions) are stored inthe same memory.° Type is not associated with data, bitshave no meaning unless given incontextCS61C L11 Linker © UC Regents3Things to Remember (1/2)° Machine Language Instruction: 32 bitsrepresenting a single MIPS instructionopcode rs rt rd functshamtopcode rs rt immediateRIopcode target addressJ° Instructions formats kept similar° Branches, Jumps optimized for greaterbranch distance and hence strange° New Logical, Shift Instructions:and, andi, or, ori,sll, srl, sraCS61C L11 Linker © UC Regents4Outline° Compiler° Assembler° Linker° Loader° ExampleCS61C L11 Linker © UC Regents5Steps to Starting a ProgramC program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS61C L11 Linker © UC Regents6Compiler° Input: High-Level Language Code(e.g., C, Java)° Output: Assembly Language Code(e.g., MIPS)° Note: Output may containpseudoinstructions° Pseudoinstructions: instructions thatassembler understands but not inmachine (e.g., HW#4); For example:° mov $s1, $s2 = or $s1, $s2, $zeroCS61C L11 Linker © UC Regents7Where Are We Now?C program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS61C L11 Linker © UC Regents8Assembler° Reads and Uses Directives° Replace Pseudoinstructions° Produce Machine Language° Creates Object FileCS61C L11 Linker © UC Regents9Assembler Directives (p. A-51 to A-53)° Give directions to assembler, but do notproduce machine instructions .text: Subsequent items put in user textsegment .data: Subsequent items put in user datasegment .globl sym: declares sym global and canbe referenced from other files .asciiz str: Store the string str inmemory and null-terminate it.word w1 wn: Store the n 32-bit quantitiesin successive memory wordsCS61C L11 Linker © UC Regents10Pseudoinstruction Replacement° Asm. treats convenient variations ofmachine language instructions as if realinstructionsPseudo: Real: subu $sp,$sp,32 addiu $sp,$sp,-32 sd $a0, 32($sp) sw $a0, 32($sp)sw $a1, 36($sp) mul $t7,$t6,$t5 mul $t6,$t5mflo $t7 addu $t0,$t6,1 addiu $t0,$t6,1 ble $t0,100,loop slti $at,$t0,101bne $at,$0,loop la $a0, str lui $at,left(str) ori $a0,$at,right(str)CS61C L11 Linker © UC Regents11Absolute Addresses in MIPS° Which instructions need relocationediting?° J-format: jump, jump and linkj/jal xxxxx° Loads and stores to variables in staticarea, relative to global pointerlw/sw $gp $x address° What about conditional branches?beq/bne $rs $rt address° PC-relative addressing preserved evenif code movesCS61C L11 Linker © UC Regents12Producing Machine Language (1/2)° Simple Case• Arithmetic, Logical, Shifts, and so on.• All necessary info is within theinstruction already.° What about Branches?• PC-Relative• So once pseudoinstructions are replacedby real ones, we know by how manyinstructions to branch.° So these can be handled easily.CS61C L11 Linker © UC Regents13Producing Machine Language (2/2)° What about jumps (j and jal)?• Jumps require absolute address.° What about references to data?¥la gets broken up into lui and ori• These will require the full 32-bit addressof the data.° These can’t be determined yet, so wecreate two tables…CS61C L11 Linker © UC Regents14Symbol Table° List of “items” in this file that may beused by other files.° What are they?• Labels: function calling• Data: anything in the .data section;variables which may be accessed acrossfiles° First Pass: record label-address pairs° Second Pass: produce machine code• Result: can jump to a later label withoutfirst declaring itCS61C L11 Linker © UC Regents15Relocation Table° List of “items” for which this fileneeds the address.° What are they?• Any label jumped to: j or jal- internal- external (including lib files)• Any piece of data- such as the la instructionCS61C L11 Linker © UC Regents16Object File Format° object file header: size and position ofthe other pieces of the object file°text segment: the machine code°data segment: binary representation ofthe data in the source file° relocation information: identifies linesof code that need to be “handled”°symbol table: list of this file’s labelsand data that can be referenced° debugging informationCS61C L11 Linker © UC Regents17Where Are We Now?C program: foo.cAssembly program: foo.sExecutable(mach lang pgm): a.outCompilerAssemblerLinkerLoaderMemoryObject(mach lang module): foo.olib.oCS61C L11 Linker © UC Regents18Link Editor/Linker (1/2)° What does it do?° Combines several object (.o) files intoa single executable (“linking”)° Enable Separate Compilation of files• Changes to one file do not requirerecompilation of whole program- Windows NT source is >30 M lines of code!And Growing!• Called a module• Link Editor name from editing the “links”in jump and link instructionsCS61C L11 Linker © UC Regents19Link Editor/Linker (2/2)° Step 1: Take text segment from each.o file and put them together.° Step 2: Take data segment from each.o file, put them together, andconcatenate this onto end of textsegments.° Step 3: Resolve References• Go through Relocation Table and handleeach entry• That is, fill in all absolute addressesCS61C L11 Linker © UC Regents20Four Types of Addresses° PC-Relative Addressing (beq, bne):never relocate° Absolute Address (j, jal): alwaysrelocate° External Reference (usually jal):always relocate° Data Reference (often lui and ori):always relocateCS61C L11 Linker © UC Regents21Resolving References (1/2)° Linker assumes first word of first textsegment is at address 0x00000000.° Linker knows:• length of each text and data segment• ordering of text and data segments° Linker calculates:• absolute address of each label to bejumped to (internal or external) and eachpiece of data being referencedCS61C L11 Linker © UC Regents22Resolving References (2/2)° To resolve references:• search for reference (data or label) in allsymbol tables• if not found, search


View Full Document

Berkeley COMPSCI 61C - Lecture 11 - Starting a Program

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Lecture 11 - Starting a Program
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 11 - Starting a Program and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 11 - Starting a Program 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?