CSCI341 Lecture 21, MIPS Programming REVIEW Assemblers understand - - PowerPoint PPT Presentation
CSCI341 Lecture 21, MIPS Programming REVIEW Assemblers understand - - PowerPoint PPT Presentation
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
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
- bject file
source assembler
- bject file
linker libs executable
ASSEMBLY PROCESS
high-level source compiler
- bject
file high-level source compiler
- bject
file linker executable
assembler assembler
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 /
- bject files.
- Some labels need to remain “local” or “private” to a file.
- Others need to be “global” and accessible from all files.
CONSIDER...
.text .globl main main: subu $sp, $sp, 32 ... flower_loop: lw $t6, 28($sp) ... jal printf ... ...
That better be a global label, since it’s not declared in this file. local label global label “hey assembler, make sure all the object files you create know about this label.”
.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.
label
(example) address
swing hm, line 23 0xN...N pitch hm, line 76 0xN...N
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.
- bject file
header text segment data segment
relocation information
symbol table debugging info
HEADER
- bject file
header text segment data segment
relocation information
symbol table debugging info
Describes the size and position
- f the other pieces of the file.
TEXT
Contains machine language from the source file. May contain unresolved references! (therefore unexecutable)
- bject file
header text segment data segment
relocation information
symbol table debugging info
.text
DATA
Contains binary representation of data from your source file. May also contain unresolved references.
- bject file
header text segment data segment
relocation information
symbol table debugging info
.data
RELOCATION
Instructions and data that depend on specific, absolute addresses. These references have to change if portions of the program are moved in memory.
- bject file
header text segment data segment
relocation information
symbol table debugging info
RELOCATION
Instructions and data that depend on specific, absolute addresses.
- bject file
header text segment data segment
relocation information
symbol table debugging info
“Grab a beverage, the third bottle on the third shelf.” If the whole shelf moves, you need to revise your specific instruction.
RELOCATION
- bject file
header text segment data segment
relocation information
symbol table debugging 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
A mapping of labels to addresses, as well as unresolved references.
- bject file
header text segment data segment
relocation information
symbol table
debugging info
DEBUGGING INFORMATION
A description of how the program was compiled.
- bject file
header text segment data segment
relocation information symbol table
debugging info
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.”)
- Reading 19
- Textbook sections B.6 & 2.8