CSCI341 Lecture 21, MIPS Programming
REVIEW • Assemblers understand special commands called “directives” • instructions for the assembler, eg, .text • Assemblers understand “macro” commands • higher-level instructions that abstract multiple “integer” instructions, eg, mult vs mul
ASSEMBLY LANGUAGE One-to-one correspondence with machine instruction. Assembly is easier for humans to read than 010101, etc.
ASSEMBLY LANGUAGE mnemonics Symbols that help us “remember” something else. The mnemonic addi helps us “remember” 00...10..11.
ASSEMBLY PROCESS source assembler object file linker executable source assembler object file libs
ASSEMBLY PROCESS high-level object compiler assembler source file linker executable high-level object compiler assembler source file
ASSEMBLY = CONTROL (AND SPEED) Often difficult to ensure high-level language program responds within definite time interval.
PROFILING Measuring where a program spends its time (among other things). Performance improvements may require recoding a critical part in assembly language. eg, Embedding assembly in parts of your C program.
MAN VS. MACHINE Historically, when compilers were more primitive, humans could often out-optimize the compiler.
Today? Not so much.
HOW DOES AN ASSEMBLER WORK? • Bind program labels to memory locations. • Translate assembly instructions to an object file.
WHAT’S AN OBJECT FILE? • Contains machine instructions, data, and “bookkeeping” • Typically is never directly executed, because it references... • procedures or data in other files
LOCAL VS. GLOBAL • Programs usually generated from multiple assembly files / object files. • Some labels need to remain “local” or “private” to a file. • Others need to be “global” and accessible from all files.
CONSIDER... .text “hey assembler, make sure all the object .globl main files you create know about this label.” main: global label subu $sp, $sp, 32 ... flower_loop: local label lw $t6, 28($sp) ... jal printf That better be a global label, since it’s not declared in this file. ... ...
.globl SomeSymbol Declare SomeSymbol as global, so it can be referenced from other files.
LOCAL SYMBOLS If a label isn’t global, then it’s local. “A label is local if the label can be used only within the file in which it is defined.”
THE LINKER • The assembler processes one file at a time, in isolation. • It only knows the addresses of local labels! • The linker resolves external labels. • But the assembler “helps,” by providing lists of labels and unresolved references. (Part of the “bookkeeping”)
HOW DOES AN ASSEMBLER WORK? • Bind program labels to memory locations. • Translate assembly instructions to an object file.
FIRST PASS • Read each line of assembly • Break it into pieces or lexemes. • ble $t0, 100, loop (six lexemes) • If the line begins with a label, then record that label and its instruction address in a symbol table.
SYMBOL TABLE A table that maps labels to addresses. (example) address label hm, line swing 0xN...N 23 hm, line pitch 0xN...N 76
HOW DOES AN ASSEMBLER WORK? • Bind program labels to memory locations. • Translate assembly instructions to an object file.
SECOND PASS • Examine each line of the instructions • If it contains an instruction, translate it to binary machine code • Replace local label references with addresses in the symbol table • Leave instructions using external symbols unresolved. • The linker will help resolve addresses needed by these instructions.
WHAT IS AN OBJECT FILE, REALLY? Structured encoding of your assembly instructions. Six segments. object file text data symbol debugging relocation header segment segment information table info
HEADER object file text data symbol debugging relocation header segment segment information table info Describes the size and position of the other pieces of the file.
TEXT object file text data symbol debugging relocation header segment segment information table info Contains machine language from the source file. May contain unresolved references! (therefore unexecutable) .text
DATA object file text data symbol debugging relocation header segment segment information table info Contains binary representation of data from your source file. May also contain unresolved references. .data
RELOCATION object file text data symbol debugging relocation header segment segment information table info Instructions and data that depend on specific, absolute addresses . These references have to change if portions of the program are moved in memory.
RELOCATION object file text data symbol debugging relocation header segment segment information table info Instructions and data that depend on specific, absolute addresses . “Grab a beverage, the third bottle on the third shelf.” If the whole shelf moves, you need to revise your specific instruction.
RELOCATION object file text data symbol debugging relocation header segment segment information table info The assembler doesn’t know the final, specific memory locations a procedure or piece of data will occupy after it is linked with other object files. (The linker will determine where the shelf of beverages will be finally located.)
SYMBOL TABLE object file text data debugging relocation symbol header segment segment information table info A mapping of labels to addresses, as well as unresolved references.
DEBUGGING INFORMATION object file text data relocation symbol debugging header segment segment information table info A description of how the program was compiled. For a debugger/assembler can find which memory addresses correspond to which lines of source code. (“Look at line 6” is better than “look at instruction 0xnnnnnnnn.”)
HOMEWORK • Reading 19 • Textbook sections B.6 & 2.8 FUNctions!
Recommend
More recommend