Download Handout # 2 Assemblers, Linkers, and the SPIM Simulator

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS402
Handout # 2
Assemblers, Linkers, and the SPIM
Simulator
1
Introduction
• A tool called an assembler translates assembly language
into binary instructions.
• Most programs consist of several files – also called
modules – that are written, compiled, and assembled
independently.
• A linker combines a collection of object and library files
into an executable file, which a computer can run.
• SPIM is a software simulator that runs programs written
for MIPS R200/R3000 processors.
2
The process that produces an
executable file
3
Assembly language
• Assembly language either is written by a programmer or is the output
of a compiler
4
Assemblers (Two Passes)
• An assembler’s first pass reads each line of an assembly file and
breaks it into its component pieces. These pieces, which are called
lexemes, are individual words, numbers, and punctuation characters.
For example:
ble $t0, 100, loop (6 lexemes)
• If a line begins with a label, the assembler records in its symbol table
the name of the label and the address of the memory word that the
instruction occupies (the first pass).
• The assembler uses the information in the symbol table during a
second pass over the file, which actually produces machine code.
5
Assemblers (One Pass)
• If an assembler’s speed is important, this two-step process can done in
one pass over the assembly file with a technique known as
backpatching.
• The assembler builds a (possibly incomplete) binary representation of
every instruction. If the instruction references a label that has not yet
been defined, the assembler records the label and instruction in a table.
When a label is defined, the assembler consults this table to find all
instructions that contain a forward reference to the label.
• The assembler goes back and corrects their binary representation to
incorporate the address of the label.
• Backpatching speeds assembly because the assembler only reads its
input once. However, it requires an assembler to hold the entire binary
representation of a program in memory so instructions can be
backpatched.
6
Linkers
• The linker searches a collection of object files and program libraries to
find non local routines used in a program, combines them into a single
executable file, and resolves references between routines in different
files.
7
Layout of memory
8
MIPS Registers
9
MIPS REGISTERS
• MIPS CPU contains 32 general-purpose registers that are numbered 031.
• Register $0 (0) always contains the hardwired value 0.
• Register $at (1), $k0 (26), and $k1 (27) are reserved for the assembler
and operating system and should not be used by user programs or
compilers.
• Registers $a0 - $a3 (4 –7)are used to pass the first four arguments to
routines (remaining arguments are passed on the stack).
• Registers $v0 and $v1 (2, 3) are used to return values from functions.
• Register $t0 - $t9 (8 - 15, 24, 25) are caller-saved registers that are
used to hold temporary quantities that need not be preserved across
call.
10
MIPS REGISTERS
• Registers $s0 - $s7 (16 –23) are callee saved registers that hold longlived values that should be preserved across calls.
• Register $gp (28) is a global pointer that points to the middle of a 64K
block of memory in the static data segment.
• Register $sp (29) is the stack pointer, which points to the last location
on the stack.
• Register $fp (30) is the frame pointer.
• Register $ra (31) has the return address for a call.
11
MIPS AND SPIM
• SPIM is a software simulator that runs programs written
for MIPS R200/R3000 processors.
• MIPS (Microprocessor without Interlocked Pipeline
Stages): A project at Stanford University intended to
simplify processor design by eliminating hardware
interlocks between the five pipeline stages.
• Five pipeline stages are IF (Instruction Fetch), ID
(Instructor Decode and register fetch), EX (Execution and
effective address calculation), MEM (Memory access), and
WB (Write Back).
12
SPIM (Virtual Machine)
• SPIM is based on a virtual machine (a simulation of the
actual microprocessor).
• The virtual computer provides machine instructions. The
virtual computer also provides pseudoinstructions, which
appear as real instructions in assembly language programs.
• The hardware, however, knows nothing about
pseudoinstructions, so the assembler must translate them
into equivalent sequences of actual, machine instruction.
13
SPIM Software &Documentation
• Setup PCSpim software
–
Step 1: Download PCSpim Version 1.0 (pcspim.zip)
http://www.cs.wisc.edu/~larus/spim.html
–
Step 2: Setup the trap handler file
Go to Simulator menu and Settings, then indicate file location of
"trap.handler" (the same installation directory of PCSpim)
• Documentation of the assembly language can be found in:
– Appendix A of the Computer Organization & Design
– By going to the web site http://www.cs.wisc.edu/~larus/spim.html and
downloading the pdf file (spim_documentation.pdf).
– The back cover of the textbook has a synopsis of the main MIPS assembly
language instructions.
14
PCSpim Interface
15
Assembler Syntax
• Comments start with a # . Everything from the the sharp
sign to the end of the line is ignored.
• Identifiers are a sequence of alphanumeric characters,
underbars ( _ ) and ( . ) that do not begin with a number.
• Opcodes for instructions are reserved words that are not
valid identifiers.
• Labels are declared at the beginning of a line followed by a
colon
Ex. Item: .word 1
• Strings are enclosed in double quotes.
– Special characters follow C , \n (newline), \t (tab), \” (quote) .
16
Assembler Syntax
• .align n Align the next data on a 2 raised to the n
power bytes boundary.
– .align 2
– .align 0
.kdata
Means aligns the next value on a word boundary
Turns off automatic alignment until the next .data or
• .ascii str
Store the string in memory, but do not nullterminate it.
• .asciiz str Store the string in memory and null terminate
it.
• .byte b1, … , bn Store the n values in successive bytes
in memory.
17
Assembler Syntax
• .data <addr> The following data items should be stored in the data
segment. If the argument addr is present the arguments are stored
beginning with address <addr>.
• .double d1, … .., dn
Store the n floating point double precision
numbers in successive memory locations.
• .extern sym size
Declare that the data stored in sym is “size”
bytes large and it is a global symbol. This directive allows the
assembler to store the datum in a portion of the data segment that is
efficiently accessed via register $gp .
• .float f1, … ,fn
Store the n floating point single precision
.globl sym
Declare that symbol sym is global and can be
referenced from other files.
• .half h1, … , hn
Store the n 16-bit quantities in successive
memory halfwords.
18
Assembler Syntax
• .kdata <addr>
The following data items should be stored in
the kernel data segment. If argument addr is present, the items are
stored beginning with address <addr>.
• .ktext <addr>
The next items are put in the kernel text
segment. In SPIM these items may only be instructions or words. If
the argument addr is there, the items are stored beginning with address
<addr>.
• .space n
Allocate n bytes of space in the current
segment (which must be the data segment in SPIM).
• .text <addr>
The next items are put in the user text segment.
In SPIM these items my only be instructions or words. If the argument
addr is there, the items are stored beginning with address <addr>.
• .word w1, … , wn
Store the n-32 bit quantities in successive
memory words.
19
System Calls
•
SPIM provides a small set of operating-system like services through the
system call instruction. To request a service a program loads the system call
code into register $v0 and the arguments into registers $a0 … $a3 (or $f12 for
floating point values). System calls that return values put their results in
register $v0 (or $f0 for floating-point results):
20
A System Call Example
For example to print “the answer = 5”, use:
str:
.data
.asciiz “the answer = “
.text
main:
li
$v0, 4 # system call code for print_str
la
$a0, str # address of string to print
syscall
# print the string
li
$v0, 1
li
$a0, 5
syscall
# system call code for print_int
# integer to print
# print the integer
21