CS-280 Lecture

advertisement
Writing an Assembly-language
program
MIPS assembly language using
MARS: MIPS Assembler and
Runtime Simulator
CS-2710
Dr. Mark L. Hornick
1
The MARS Assembler
…is a modern, “single pass” Assembler
 Converts mnemonics to machine instructions (opcodes +
operands)

Ex: add $t0, $s1, $s2 is assembled to 0x02324020

Case on instructions is irrelevant; alternately: ADD $t0, $s1, $s2
Case on registers matters: $T0 is not permitted


Reports syntactical errors


Ex: add $t0, $s1 (e.g. too few operands)
Can generate files containing machine code in a format that
can be downloaded to real hardware
CS-2710
Dr. Mark L. Hornick
2
MARS also implements a
Debugger/Simulator

Simulates execution of the compiled program






Start, stop, single-step
Backwards step
Breakpoints
Can view contents of memory, registers
Tracks cycles required to execute
And lots more – see the help documentation
CS-2710
Dr. Mark L. Hornick
3
Number representation in
MIPS assembly language

All values are decimal unless notated
otherwise




addi $t0, $s1, 100 # decimal 100 (default)
addi $t0, $s1, 0144 # octal 100 (note leading 0)
addi $t0, $s1, 0x64 # hex 100 (note leading 0x)
Radix prefixes


0 – octal
0x, 0X – hexadecimal

0x002a is equivalent to 0x2A
CS-2710
Dr. Mark L. Hornick
4
Labels can be used in place of
actual addresses

Every input line can be preceded by a label


an alphanumeric string terminated by a colon (:)
Labels are used as targets for jump and
branch instructions

The assembler automatically figures out what
address to assign to a label
CS-2710
Dr. Mark L. Hornick
5
Every input line can be
preceded by a label
Example:
Start:
addi $t0, $s1, 100
Here:
j Here





# t0 = s1 + 100
# repeat forever
Labels are alphanumeric strings terminated by a colon(:)
Labels are given the value of the location in memory where the following
instruction will be placed
Labels can be used as jump and branch targets in program instructions
The label Start: is assigned the value of the location of the addi
instruction (0x00400000); Here: is assigned 0x0040001c, the location of
the j instruction
The assembler automatically figures out what address to assign to a
label
CS-2710
Dr. Mark L. Hornick
6
Directives are used within an assembly
language program to control certain
aspects of the operation of the Assembler
Directives begin with a period (.)




Directive often take operands, but not always
Ex: .text
Ex: .byte 1,2,3,4
Case does not matter

.TEXT is equivalent
Directives are not instructions – they are not translated to machine
instructions


They only “direct” the Assembler how to interpret subsequent
assembly language instructions
All directives are documented in the online help of MARS
Directives may appear anywhere in an assembly program
CS-2710
Dr. Mark L. Hornick
7
Some assembler directives are used to
define where to place things in
memory:
.text [loc]

Alerts the Assembler that subsequent assembly statements are intended to
generate instructions for the Text Segment of main memory



ie, where executable machine code is placed
Location set by MARS memory configuration settings
If absent, a default value will be used by the Assembler
.data [loc]


Directs the Assembler where to begin placing subsequent data in memory
Example:
.data
.byte 1,2,3,4
directs the subsequent data bytes 1,2,3,4,5 to be placed in the data
segment of main memory
CS-2710
Dr. Mark L. Hornick
8
Download