You are on page 1of 20

An Assembly Language I.D.E.

To Engage Students Of All Levels


* A Tutorial *
2007 S! entral "lains onferen#e
Pete Sanderson, Otterbein College, PSanderson@otterbein.edu
Ken Vollmar, Missouri State University, KenVollmar@missouristate.edu
MARS is a software simulator for the MIPS assembly language intended for educational
use. We will explore the capabilities of MARS release 3..! in this three part tutorial.
MARS may be downloaded from www.cs.missouristate.edu/MARS.
Part 1 : Basic MARS Use
"he example program is Fibonacci.asm to compute e#eryone$s fa#orite number
se%uence.
!. Start MARS from the Start menu or des&top icon.
. 'se the menubar (ile)*pen or the *pen icon to open (ibonacci.asm in the
default folder. (All icons ave menubar e!uivalents" te remainder o# tese ste$s %ill
use te icon %enever $ossible.&
3. "he pro#ided assembly program is complete. Assemble the program using the icon
+. Identify the location and #alues of the program$s initiali,ed data. 'se the chec&box to
toggle the display format between decimal and hexadecimal .
"he nineteen-element array fibs is initiali,ed to ,ero. at addresses /x!//!////
) /x!//!//+0.
"he data location size has #alue !1ten at /x!//!//+c.
"he addresses /x!//!//2/ ) /x!//!//3c contain null-terminated AS4II strings.
'se the chec&box to toggle the display format between decimal and hexadecimal.
.
2. 'se the Settings menu to configure the MARS displays. "he settings will be retained
for the next MARS session.
Page !
"he 5abels display contains the addresses of the assembly code statements with a
label. but the default is to not show this display. Select the chec&box from the
Settings menu.
Select your preference for allowing pseudo-instructions 6programmer-friendly
instruction substitutions and shorthand7.
Select your preference for assembling only one file. or many files together 6all the
files in the current folder7. "his feature is useful for subroutines contained in
separate files. etc.
Select the startup display format of addresses and #alues 6decimal or
hexadecimal7.
3. 5ocate the Registers display. which shows the 3 common MIPS registers. *ther tabs
in the Registers display show the floating-point registers 64oproc !7 and status codes
64oproc /7.
8. 'se the slider bar to change the run speed to about !/ instructions per second.
"his allows us to 9watch the action: instead of the
assembly program finishing directly.
0. 4hoose how you will execute the program;
"he icon runs the program to completion. 'sing this icon. you should
obser#e the yellow highlight showing the program$s progress and the #alues of the
(ibonacci se%uence appearing in the <ata Segment display.
"he icon resets the program and simulator to initial #alues. Memory
contents are those specified within the program. and register contents are
generally ,ero.
"he icon is 9single-step.: Its complement is . 9single-step bac&wards:
6undoes each operation7.
1. *bser#e the output of the program in the Run I=* display window;
The Fibonacci numbers are:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
-- program is finished running --
!/. Modify the contents of memory. 6Modifying a register #alue is exactly the same.7
Set a brea&point at the first instruction of the subroutine which prints results. 'se
the chec&box at the left of the instruction whose address is /x//+///3/ >
+!1++//ten.
Page

Reset and re-run the program. which stops at the brea&point.
<ouble-clic& in one of the memory locations containing the computed (ibonacci
numbers. "he cell will be highlighted and will accept &eyboard entry. similar to a
spreadsheet. ?nter some noticeably different #alue. and use the ?nter &ey or clic&
outside the cell to indicate that the change is complete. '(am$le) Memory address
*(+**+**,* - ,./0*+*,1 ten $resently contains data *(******,, - 21 ten.
4lic& to continue from the brea&point. "he program output includes your
entered #alue instead of the computed (ibonacci number.
!!. *pen the @elp for information on MIPS instructions. pseudoinstructions.
directi#es. and syscalls.
!. Modify the program so that it prompts the user for the (ibonacci se%uence length.
Select the ?dit tab in the upper right to return to the program editor.
"he MIPS comment symbol is A. All characters on the line after the character A
are ignored.
'n-comment lines !-!1. "he newly exposed program fragment will prompt the
user for the length of the (ibonacci se%uence to generate. in the range !1 ( .
6"he length of the se%uence must be limited to the si,e of the declared space for
result storage.7
<etermine the correct syscall parameter to perform 9read integer: from the user.
and insert the parameter at line "he correct syscall parameter may be found at
@elp ) Syscall tab)read integer ser#ice. "he completed line will ha#e the
form li $v0, 42 6where in this case + is not the right answer7.
Reset and re-run the program. "he program will stop at the brea&point
you inserted pre#iously. 4ontinue and finish with .
Page 3
Part 2 : MARS Tools
Bou may ha#e noticed that MARS has a Tools menu. "he capabilities pro#ided through
this menu really catapult MARS into a different league of computer science educational
software.
We call each of the items in the "ools menu a MARS "ool. A MARS "ool is best
described as a pop-up application that obser#es MIPS memory and=or register acti#ity
during MIPS program execution then communicates that acti#ity to the tool user to ser#e
a particular purpose. "his is best seen by example.
MARS Tools Activity 1 : Running the Data Cache Simulator tool
!. 4lose any MIPS programs you are currently using.
. *pen the program row-major.asm from the Examples folder. "his program
will tra#erse a !3 by !3 element integer matrix in row-maCor order. assigning
elements the #alues / through 22 in order. It performs the following algorithm;

for (row = 0; row < 16; row!
for (col = 0; col < 16; col!
data"row#"col# = $alue;
3. Assemble the program.
+. (rom the Tools menu. select Data Cache Simulator. A new frame will appear in the
middle of the screen.
Page +
"his is a MARS "ool that will simulate the use and performance of cache memory
when the underlying MIPS program executes. Dotice its three maCor sections;
Cace Organi3ation) Bou can use the combo boxes to specify how the cache
will be configured for this run. (eel free to explore the different settings. but
the default is fine for now.
Cace Per#ormance) With each memory access during program execution.
the simulator will determine whether or not that access can be satisfied from
cache and update the performance display accordingly.
4ool Control) "hese buttons perform generic control functions as described
by their labels.
2. 4lic& the toolEs Connect to MIPS button. "his causes the tool to register as an
obser#er of MIPS memory and thus respond during program execution.
3. Fac& in MARS. adCust the Run See! sli!er to 3/ instructions per second. It is
located at the right side of the toolbar. "his slows execution so you can watch the
4ache Performance animation.
8. In MARS. run the program using the Run toolbar button . the menu item or
&eyboard shortcut. Watch the 4ache Performance animate as it is updated with e#ery
access to MIPS memory.
0. 5at %as te #inal cace it rate6 GGGGGGGGGGGGG. With each miss. a bloc& of +
words are written into the cache. In a row-maCor tra#ersal. matrix elements are
accessed in the same order they are stored in memory. "hus each cache miss is
followed by 3 hits as the next 3 elements are found in the same cache bloc&. "his is
followed by another miss when <irect Mapping maps to the next cache bloc&. and the
patterns repeats itself. So 3 of e#ery + memory accesses will be resol#ed in cache.
1. Hi#en that explanation. %at do you $redict te it rate %ill be i# te bloc7 si3e is
increased #rom 1 %ords to / %ords6 GGGGGGGGGGGGGG. 8ecreased #rom 1 %ords to ,
%ords6 GGGGGGGGGGG.
!/. Ierify your predictions by modifying the bloc& si,e and re-running the program from
step 8.
9O4') when you modify the 4ache *rgani,ation. the performance #alues are
automatically reset 6you can always use the toolEs Reset button7.
9O4') Bou ha#e to reset the MIPS program before you can re-run it.
9O4') (eel free to adCust the Run See! sli!er to maximum speed anytime you
want.
Page 2
!!. Repeat steps through !/ for program column-major.asm from the Examples
folder. "his program will tra#erse a !3 by !3 element integer matrix in column-maCor
order. assigning elements the #alues / through 22 in order. It performs the following
algorithm;

for (col = 0; col < 16; col!
for (row = 0; row < 16; row!
data"row#"col# = $alue;
9O4') Bou can lea#e the 4ache Simulator in place. mo#e it out of the way. or close
it. It will not interfere with the actions needed to open. assemble. or run this new
program and will remain connected to MIPS memory. If you do not close the tool.
then s&ip steps + and 2.
!. 5at %as te cace $er#ormance #or tis $rogram6 GGGGGGGGGGGG. "he problem is
the memory locations are now accessed not se%uentially as before. but each access is
!3 words beyond the pre#ious one 6circularly7. With the settings weE#e used. no two
consecuti#e memory accesses occur in the same bloc& so e#ery access is a miss.
!3. 4hange the bloc& si,e to !3. Dote this will reset the tool.
!+. 4reate a second instance of the 4ache Simulator by once again selecting Data Cache
Simulator from the Tools menu. AdCust the two frames so you can #iew both at the
same time. 4onnect the new tool instance to MIPS. change its bloc& si,e to !3 and
change its number of bloc&s to !3.
!2. Re-run the program. 5at is te cace $er#ormance o# te original tool instance6
GGGGGGGGGGGG. Floc& si,e !3 didnEt help because there was still only one access to
each bloc&. the initial miss. before that bloc& was replaced with a new one. 5at is
te cace $er#ormance o# te second tool instance6 GGGGGGGGGGGG. At this point. the
entire matrix will fit into cache and so once a bloc& is read in it is ne#er replaced.
*nly the first access to a bloc& results in a miss.
In what courses might an exercise li&e this one be useful for your studentsJ I ha#e used a
#ariation on this exercise as a student exercise in *perating Systems. and for a lecture
illustration of the cache concept in *tterbeinEs 4S / course. K"he Scope of 4omputer
ScienceK.
Page 3
MARS Tools Activity 2 : Running the Cache Simulator as a stan!"alone
!. In command mode. tra#erse to the directory containing Mars.Car and enter the
command;
%a$a &classpat' Mars.%ar mars.tools.(ac'eSimulator
. "he cache simulator tool is launched. Its "ool 4ontrol section is replaced by
Application 4ontrol. which contains additional controls for loading. assembling and
running MIPS programs. It uses MARSE MIPS assembler and runtime simulator in
the bac&ground to control MIPS execution.
3. 4lic& the #en MIPS rogram button and a (ile *pen dialog will pop up. Frowse
to and select a MIPS program to run. Select row_major.asm again if you wish.
+. "he Assem$le an! Run button is now enabled. 4lic& it to assemble and run the
program. "he animation will be #ery rapid.
2. 'se the Run See! sli!er to adCust the running speed. clic& the Reset button then
clic& Assem$le an! Run again. While the program is running. the Sto button is
enabled. Program status is updated in the single line text field.
We plan to implement a small MARS "ool Suite application to simplify the selection and
launching of tools such as the 4ache Simulator that are capable of running outside the
MARS integrated de#elopment en#ironment.
Page 8
MARS Tools Activity % : The Memory Re&erence 'isuali(ation tool
!. *pen the program row-major.asm from the Examples folder if it is not already
open.
. Assemble the program.
3. (rom the Tools menu. select Memory Re&erence 'isuali(ation. A new frame will
appear in the middle of the screen.
"his tool will paint a grid unit each time the corresponding MIPS memory word is
referenced. "he base address. the first static data segment 6.data directi#e7 word.
corresponds to the upper-left grid unit. Address correspondence continues in row-
maCor order 6left to right. then next row down7.
"he color depends on the number of times the word has been referenced. Flac& is /.
blue is !. green is . yellow is 3 and +. orange is 2 through 1. red is !/ or higher.
Iiew the scale using the tool$s slider control. Bou can change the color 6but not the
reference count7 by clic&ing on the color patch.
+. 4lic& the toolEs Connect to MIPS button. "his causes the tool to register as an
obser#er of MIPS memory and thus respond during program execution.
2. Fac& in MARS. adCust the Run See! sli!er to 3/ instructions per second.
Page 0
3. Run the program. Watch the tool animate as it is updated with e#ery access to MIPS
memory. :eel #ree to sto$ te $rogram at any time.
8. @opefully you obser#ed that the animation se%uence corresponded to the expected
memory access se%uence of the row-maCor.asm program. ;# you ave trouble seeing
te blue, reset the tool. mo#e the slider to position !. change the color to something
brighter. and re-run.
0. Repeat steps through 8. for column-major.asm. Bou should obser#e that the
animation se%uence corresponded to the expected memory access se%uence of this
program.
1. Repeat again for fibonacci.asm to obser#e the animated pattern of memory
references. AdCust the run speed and re-run if necessary.
!/. (O$tional& 4reate a new instance of the <ata 4ache Simulator. Mo#e the two frames
around so you can see both. 4onnect the cache simulator to MIPS and reset the
Memory Reference Iisuali,ation. Re-run the program. "his exercise illustrates that
two different tools can be used simultaneously.
"he Memory Reference Iisuali,ation tool could be useful in an operating systems course
to illustrate spatial and temporal locality and memory reference patterns in general.
Page 1
Part % : )*ten!ing MARS Caa$ilities
Our session today is not long enoug #or interactive activities in tis area, but %e<ll
$rovide you %it enoug detail tat you can $ursue tem on your o%n i# you desire.
A$stract
MARS can be customi,ed and extended in four different ways; 6!7 writing new MARS
"ools. 67 writing new MIPS system calls. 637 writing new MIPS pseudo-instructions. and
6+7 writing new MIPS basic instructions. "echni%ues for all four are described here.
Bou ha#e the ability to extend and customi,e certain MARS capabilities to ma&e it more
useful in your courses. "his document describes four different techni%ues for extending
MARS capabilities;
!. Ability to write MARS "ools for inclusion in the "ools menu and stand-alone use.
. Ability to define and add new system calls for subse%uent use by MIPS programs.
3. Ability to customi,e the instruction set by adding. remo#ing or modifying pseudo
6macro7 instruction specifications.
+. Ability to customi,e the basic instruction set by adding. remo#ing or modifying
basic instruction specifications.
"hese procedures apply to MARS 3..!. released Lanuary //8. Some may be
streamlined in future releases.
"he ability to define and plug in new MARS "ools will be used to de#elop new learning
aids for students in a #ariety of computer science courses and pro#ide micro-worlds for
assembly language students to target in their proCects. "hrough those tools it is possible
to deeply engage students in both settings.
"he ability to modify the set of system calls. basic instructions. and pseudo-instructions
can be used to define a complete instruction set for an alternati#e RIS4 architecture.
MARS was not designed with this in mind howe#er. so support is une#en. (or example.
instruction syntax analysis and code generation is dri#en by the example and template
pro#ided with each instruction. whereas lexical analysis such as the recognition of
register names is embedded in program logic and cannot easily be modified. A
customi,ed instruction set has to use MIPS con#entions for labeling. register naming. and
so forth. @opefully MARS can be refactored in future releases to facilitate its use for
alternati#e instruction sets.
Page !/
1+ ,riting your o-n MARS Tool an! lugging it into MARS
A$stract
If you thin& MARS "ools li&e the 4ache Simulator are cool then why not de#elop your
ownJ "here are two different ways to do so; 6!7 write a class that implements the
Mars"ool interface and is launched from the "ools menu in MARS. or 67 write a class
that extends the AbstractMars"oolAndApplication class and runs either from the "ools
menu or as a free-standing application. Bou can also write a free-standing application
that uses the Mars API.
"his section describes two different techni%ues for de#eloping your own MARS "ool.
!. Write a class that implements the mars.tools.MarsTool interface and &eep
it in the mars.tools pac&age. It will automatically be added to the Tools menu
the next time MARS is run and launched when its menu item is selected.
. Write a class that extends
mars.tools.bstractMarsTooln!""lication and &eep it in the
mars.tools pac&age. It will automatically be added to the Tools menu the
next time MARS is run and launched when its menu item is selected. It can also
be launched from outside MARS as a free-standing La#a application that uses the
Mars API to control the assembly and execution of MIPS programs.
It is also possible to write your own application from scratch using the Mars API. "his
should be considered only for #ery speciali,ed applications and should be underta&en
with great care. "he A)stractMars*oolA+dApplicatio+ class pro#ides full
support for assembling and running MIPS programs in the bac&ground and is the
preferred approach.
MyTool im"lements MarsTool aroach
!. ?xtract the MARS distribution from its LAR file. "he LAR file does not ha#e an
outermost folder to contain e#erything. so youEll want to create one and extract it into
that folder.
. <e#elop your class in the mars.tools pac&age 6mars/tools folder7.
3. Bour class must implement the MarsTool interface. which is in that pac&age. "his
has only two methods; Stri+, ,et-ame(! to return the name to be displayed in
Page !!
its "ools menu item. and $oid actio+(! which is in#o&ed when that menu item
is selected by the MARS user. "hese will assure its inclusion in the "ools menu when
MARS is launched.
+. "he user interface should be based on the java#.swin$.%&ialo$ class. "he
tool interacts with simulated MIPS memory and registers through the
mars.mi"s.'ar!ware.Memory and mars.mi"s.'ar!ware.(e$ister
classes. both of which extend java.util.)bservable. "he Memory class
pro#ides se#eral add*bser#er67 methods that permit an *bser#er to register for
selected memory addresses or ranges. La#adoc-produced documentation is a#ailable
in the doc folder of the MARS distribution.
2. After successful compilation. MARS will automatically include the new tool in its
"ools menu.
MyTool e#ten!s bstractMarsTooln!""lication aroach
A better alternati#e is to extend the bstractMarsTooln!""lication class.
which is also in the mars.tools pac&age. Fy doing so. you get the following elements;
ability to run either from the "ools menu or as a free-standing application
basic user interface L<ialog with "ool 4ontrol section 6for tools7
basic user interface L(rame with Application 4ontrol section 6for applications7
basic user interface layout 6Forder5ayout7 and rendering algorithm
basic MIPS memory and register obser#er capabilities
!. ?xtract the MARS distribution from its LAR file if you ha#e not already done so.
. <e#elop your class in the mars.tools pac&age 6mars/tools folder7.
3. Bour class must extend the bstractMarsTooln!""lication abstract
class. which is in that pac&age. Dineteen of the ! methods in this class ha#e default
implementations.
+. <efine at least the two abstract methods; Stri+, ,et-ame(! to return the tool$s
display name. and .(ompo+e+t )uildMai+/ispla0Area(! to construct the
central area of the tool$s graphical user interface. It will automatically be placed in
the 4?D"?R of a Forder5ayout. with title information to its D*R"@ and tool control
buttons to its S*'"@. Se#eral addAs*bser#er67 methods are a#ailable for
registering as a memory and=or register obser#er.
Page !
2. *#erride additional methods as desired. Some do nothing by default.
3. After successful compilation. MARS will automatically include the new tool in its
"ools menu.
8. "o run it as a stand-alone application. you either need to add a mai+(! to create the
tool obCect and call its ,o(! method or write a short external application to do the
same.
(or a better idea of the H'I elements that come with this abstract class. launch MARS
and select Intro!uction to Tools from the "ools menu.
Fy extending the pro#ided abstract class
A)stractMars*oolA+dApplicatio+. you
get e#erything in this figure except the
scrolled .*extArea displayed in the
center.
Bou build the main user interface of your
tool by defining the inherited abstract
)uildMai+/ispla0Area(! method. "his
method returns a .(ompo+e+t that will
automatically be displayed in the center of
the default Forder5ayout.
La#adoc-produced documentation of the
A)stractMars*oolA+dApplicati
o+ class is a#ailable in the doc folder of
the MARS distribution.
Source code files for the MARS "ools are
included together with their bytecode files
in the mars/tools folder. (eel free to
refer to them. All MARS source files.
including tools. are included in the src
folder.
If you de#elop a nifty MARS "ool. feel free to send it to us and with your permission
weEll post it on the MARS web site for others to useM
Page !3
2+ ,riting a system call .syscall/ an! lugging it into MARS
A$stract
MIPS System calls are functions that interface with the operating system to perform I=*
and related operations. "hey are accessible to MIPS assembly programs through ser#ice
numbers and the syscall instruction. MARS pro#ides the !8 system calls documented
in Com$uter Organi3ation and 8esign 4ird 'dition. but you can de#elop and add your
own by writing a class that implements the Syscall interface or extends the
AbstractSyscall class.
MIPS system calls perform operating system functions and input=output operations in
particular. A system call is used in MIPS assembly by loading its identifier 6an integer7
into register N#/. loading any re%uired arguments into registers as specified by the callEs
protocol. then issuing the syscall instruction.
MARS supports the MIPS system call specifications gi#en in Appendix A of Patterson
and @ennessyEs Com$uter Organi3ation and 8esign 4ird 'dition. "his appendix is
freely a#ailable on the web at 'tt"*++www.cs.wisc.e!u+,larus+-._""."!f.
MARS pro#ides all !8 of the system calls specified there.
Page !+
My/ystem0all im"lements /yscall aroach
!. ?xtract the MARS distribution from its LAR file. if you ha#e not done so.
. <e#elop your class in the mars.mi"s.instructions.syscalls pac&age
6mars/mips/i+structio+s/s0scalls folder7.
3. Bour class must implement the /yscall interface. which is in that pac&age. "his
has four methods; Stri+, ,et-ame(! to return an assigned name for this
ser#ice. set-um)er(! and ,et-um)er(! to set and get its associated ser#ice
number. and simulate(! to simulate the execution of the system call at MIPS
runtime.
+. After your class compiles successfully. it will be automatically plugged in and
a#ailable to MIPS programs the next time you launch MARS.
My/ystem0all e#ten!s bstract/yscall aroach
!. ?xtract the MARS distribution from its LAR file. if you ha#e not done so.
. <e#elop your class in the mars.mi"s.instructions.syscalls pac&age
6mars/mips/i+structio+s/s0scalls folder7.
3. Bour class must extend the bstract/yscall class. which is in that pac&age. It
pro#ides a constructor and defines the three S0scall getter and setter methods. "he
simulate(! method is abstract so you must define that yourself.
+. After your class compiles successfully. it will be automatically plugged in and
a#ailable to MIPS programs the next time you launch MARS.
A!!itional In&ormation
Implementation of the simulate(! method may re%uire access to one or more
simulated MIPS memory and register obCects. "he pertinent classes are
mars.mips.'ardware.Memor0 and mars.mips.'ardware.Re,ister1ile.
Also study the source code of existing system calls. Source code files for the !8 MARS
system calls are located in the src folder. (ollow the folder hierarchy
mars/mips/i+structio+s/s0scalls.
Page !2
"he MARS distribution includes a text file S0scall.properties which you can edit
to o#erride any system ser#ice number assigned in a constructor. "his allows you to
change number assignments without re-compiling any La#a source files.
"he Syscalls help page is static at this time so it will not reflect any additional system
calls or renumbering.
If you de#elop a nifty system call. please send it to us and with your permission weEll post
it on the MARS web site for others to accessM
We ha#e had philosophical discussions concerning the de#elopment of additional system
calls and so far ha#e resisted doing so. *n one hand. it would be #ery useful to ha#e a
small library of handy functions such as random number generators a#ailable through the
syscall mechanism. *n the other hand. such functions are not considered operating
system operations that would be performed in &ernel mode and it would be misleading to
infer such status. 5et us &now what you thin&M
Page !3
%+ Mo!i&ying the set o& seu!o .e*ten!e!0 macro/ instructions
A$stract
"o define a new pseudo-instruction. edit the 2seudo3ps.txt file included in the
MARS distribution. "he instruction specification is one line long and consists of a tab-
separated list containing an example usage of the instruction followed by the one or more
basic instructions that the assembler will expand it to. 'se specification language
symbols to control the substitution of program statement operands into the generated
basic instruction6s7.
"he MIPS instruction set includes a large number of pseudo-instructions. "hese are
instructions that follow MIPS instruction syntax specifications but are not directly
supported by the MIPS architecture. "hey are instead expanded into a se%uence of one or
more basic instructions by the assembler. (or this reason they are sometimes called
macro instructions.
"he classic example of a pseudo-instruction is la. or load address. @ereEs an example;
la 4t05 +um)er
where +um)er is the label for a data item. A corresponding MIPS machine instruction
would ha#e to include the operation code. the 2 bit register number and the 3 bit
address. Fut since machine instructions are only 3 bits long. this is not possible. It
re%uires two instructions.
Pseudo-instructions are pro#ided entirely for the programmerEs con#enience. and MARS
includes an assembler setting that will disallow their use.
To mo!i&y the seu!o"instruction set0 &ollo- this rocess:
!. ?xtract the MARS distribution from its LAR file if you ha#e not already done so.
. ?dit 2seudo3ps.txt appropriately. ?ach pseudo-instruction and its macro
expansion are specified on one line in this file. <etails below.
3. 5aunch MARS and test your instruction.
Page !8
@ereEs an example specification. for the a$solute value instruction abs
!. ?xample instruction use. "his instruction ta&es two register operands. "he N! and N
represent generic registers and are used only to generate a to&en se%uence for syntax
analysis. 4ere is no relationsi$ bet%een register re#erences in te e(am$le
instruction (item +& and register re#erences in te macro e($ansions (items 2 and 1&=
. ?ach item in the specification must be separated by a single "AF character. *nly the
first one is pointed out but there is also a "AF between each of the remaining items.
3. "he first instruction of the macro expansion. RH! is a formal parameter that will be
replaced by the actual first register operand of the statement being assembled. RH is
similarly replaced by the actual second register operand of the statement being
assembled.
+. Second and third instructions of the macro expansion. Perform similar substitutions.
Statement to assemble Macro e($ansion
a)s 4t65 4t7 addu 4t65 405 4t7
),e8 4t75 9
su) 4t65 405 4t7
"his example uses the simple substitutions RH! and RH. *ther macro substitution
symbols are more complex. such as :;926 which means to substitute the low order !3
bits of the 3 bit #alue in source operand after adding 3 to it 6used in the expansion of
certain unaligned loads and stores7.
"he macro substitution language is described in detail in the file itself.
If you add any pseudo-instructions. they will automatically be included in the instruction
list produced by the @elp featureEs )*ten!e! .seu!o/ Instructions subtab. If the
instruction specification ends with a comment on the same line 6A is the comment mar&er.
Cust li&e in MIPS7. that comment will be included in the list along with the example
instruction use.
Page !0
abs $1,$2 a!!u (21,$0,(22 b$ez (22,2 sub (21,$0,(22
! 3
+
1+ Mo!i&ying the set o& Basic Instructions
A$stract
It is possible to modify the basic instruction set recogni,ed by MARS. "his re%uires you
to edit and re-compile the mars.mips.i+structio+s.<+structio+Set class.
"he specification of a basic instruction includes four components; 6!7 an example of
instruction use. 67 the instruction format O R. I. or L. 637 a template for generating the 3
bit machine code from a program statement. and 6+7 a method to simulate the execution
of that program statement.
"his is the one type of MARS extension that re%uires re-compilation of a standard MARS
source file. Specifications for the MIPS basic instruction set are contained in the
mars.mips.i+structio+s.<+structio+Set class. ?ach specification follows
the same format. @ere is an example. for the an! instruction that performs logical AD<.
instruction3ist.a!!4new 5asic6nstruction4
7an! $1,$2,$87, 1. example use
5asic6nstructionFormat.(_F)(MT, 2. instruction format
7000000ssssstttttfffff000001001007, 3. machine code template
new /imulation0o!e49 4. execution simulator
:
"ublic voi! simulate4.ro$ram/tatement statement9
:
int;< o"eran!s = statement.$et)"eran!s49>
(e$isterFile.u"!ate(e$ister4
o"eran!s;0<,
(e$isterFile.$et?alue4o"eran!s;1<9 @
(e$isterFile.$et?alue4o"eran!s;2<9 9>
A
A
99>
"he =asic<+structio+ constructor ta&es four parameters. annotated on the
example;
!. ?xample instruction use. "his instruction ta&es three register operands. "he N!. N
and N3 represent generic registers and are used only to generate a to&en se%uence for
syntax analysis.
Page !1
. MIPS instruction format. MIPS defines the R-format 6wor&s with registers7. I-format
6wor&s with immediate #alue7. and L-format 6Cump instruction7. MARS defines a
second #ersion of the I-format called I-branch-format when the immediate #alue is
used in a branch instruction.
3. Machine code template. "his String of length 3 consists of the characters 0. 1. f. s.
and t. When the MIPS program statement syntactically matches this instruction. the
3 bit machine code instruction is constructed by substituting low order bits from the
&irst operand for f. low order bits from the second operand for s and low order bits
from the third operand for t. "he result is con#erted to 3 bit binary. "his example
has register numbers which are 2 bits because MIPS3 defines 3 integer registers.
+. 4onstructs the obCect whose simulate(! method will be called to simulate the
execution of the program statement at MIPS runtime. "he obCect is constructed from
an anonymous subclass of the Simulatio+(ode class defined right here. "he
simulate(! method recei#es information about the program statement as a
parameter and manipulates MIPS registers and=or memory to carry out the instruction.
To mo!i&y the $asic instruction set0 &ollo- this rocess:
!. ?xtract the MARS distribution from its LAR file if you ha#e not already done so.
. 4opy <+structio+Set.%a$a from the src/mars/mips/i+structio+s
folder into the mars/mips/i+structio+s folder.
3. Ma&e a bac&up copy of <+structio+Set.%a$a for safe &eeping.
+. ?dit <+structio+Set.%a$a appropriately. Implementation of the
simulate(! method will li&ely re%uire access to one or more simulated MIPS
memory and=or register obCects. "he pertinent classes are
mars.mips.'ardware.Memor0 and
mars.mips.'ardware.Re,ister1ile. La#adoc-generated documentation for
all MARS classes is pro#ided in the distributionEs doc folder. Also study the source
code of existing instructions.
2. 4ompile <+structio+Set.%a$a and test your instruction.
2#T): "his techni%ue applies to MARS release 3..!. It is possible that the techni%ue
for specifying basic instructions will change in a future release of MARS. If so. it would
be con#erted to a techni%ue similar to that now used for system calls.
Page /

You might also like