MIPS Assembler and Runtime Simulator (MARS) Lab

I have few tasks to finish in a short time. I hope you have an experience with MIPS(MARS) before.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

1
Introduction to MARS Lecture Review Notes
MARS:
A. We will use an Integrated Development Environment (IDE) called the MIPS Assembler and
Runtime Simulator (MARS).
– There are a number of MIPS SIMULATORS available, some for educational use, and some for
commercial use, such as SimMips, SPIM, and miniMIPS.
– There are a number of advantages in using a simulator when first learning to program in
assembly language. First, we can learn the language without having to buy a MIPS based computer.
– The simulator provides debugging features.
– We can single step through a program and watch the contents of the registers change as each
instruction executes, and we can also look at the contents of memory as the instructions execute.
– We can set breakpoints.
– A programming mistake in a simulated environment will NOT cause the actual machine running
the simulation to crash. A programming mistake in a simulated environment will usually result in
a simulated exception, where a trap handler will print a message to help us identify what instruction
caused the exception.
– MARS is the easiest IDE to use, and provides the best tools for explaining how MIPS assembly
and the MIPS CPU function.
– MARS was written by Pete Sanderson and Kenneth Vollmar, and is documented at the site
http://courses.missouristate.edu/kenvollmar/mars/index.htm, and should be downloaded from
this site.
– MARS is written in Java and is an executable jar file, so you must have the Java Runtime
Environment (JRE) installed to run MARS. There is a link to Java on the MARS download page,
so you must install Java if it is not on your computer.
– As of Release 4.0, MARS assembles and simulates 155 basic instructions of the MIPS32
instruction set, approximately 370 pseudo-macro instructions or instruction variations, the 17
syscall functions mainly for console and file I/O, and an additional 22 syscalls for other uses
such as MIDI output and random number generation.
– Instructions for running MARS are on the download page. When MARS is started, the
following page should appear.
2
– One advantage of learning assembly language programming is that it directly exposes many of
the TYPES of memory used in a program and forces the programmer to deal with them.
Launch MARS from the Start menu. You will see something like the screenshot shown below:
3
– Notice the Edit/Execute tabs and the Mars Messages and Run I/O tabs. These are used to
switch between the panels used for those purposes.
4
– Also, notice the Registers panel along the right-hand side of the screen. All 32 MIPS registers
are labeled and numbered, in addition to three more registers listed at the bottom (the pc, or
program counter, and hi and lo registers, which we will cover later in the course).
ANSWER ALL 14 QUESTIONS THAT HAVE *NUMBER SHOWN IN RED.
*1. Which registers have a non-zero value at this point?
*2. Explain what the value of the pc indicates.
Use File…New to copy and paste the file in *12. into MARS. Name the file addInts.asm, which
contains a MIPS program to add two numbers. You will see the file open in the Edit panel.
NOTE: If you receive an assembly error for the string, you might have to retype the string.
– All icons have menu bar equivalents; the remainder of these steps will use the icon whenever
possible.
– Assemble the program using the icon
(also available from the Run menu).
Examine the Mars Messages panel on the bottom, and notice that the message indicates that the
assembly was successful.
Also, notice that the tab automatically changes from Edit to Execute, and that the Text Segment
and Data Segment (Labels) panels are now displayed (similar to the earlier screenshot).
*3. What does the 0x notation mean, which precedes the 8-digit numbers you see displayed in
these panels?
*4. The Text Segment contains the code from the .text section of the program, which is the
program instructions.
Define the following columns in this panel.
a) Bkpt:
b) Address:
c) Code:
d) Basic:
e) Source:
5
*5. What is the starting address of the program?
*6. The Data Segment contains the code from the .data section of the program (the variables
and constants defined in the program). What is the starting address of the Data Segment?
*7. Each ROW in the Data Segment lists the contents of 8 words in memory, each of which
contains 32 bits, or 4 bytes, of data. Notice that the first 12 words in the Data Segment contain
NON-ZERO VALUES. Why are these non-zero values for this program?
Use the Settings menu to configure the MARS displays. The settings will be retained for the
next MARS session.
• The Labels display contains the addresses of the assembly code statements with a label, but
the default is to NOT show this display. Select the checkbox from the Settings menu.


Select the checkbox to allow pseudo-instructions (programmer-friendly instruction
substitutions and shorthand).
Select the startup display format of addresses and values to be hexadecimal.
– Use the slider bar to change the “run speed” to 1 instruction per second.
This allows us to “watch the action” instead of the assembly program directly finishing.
– There are a number of ways to execute the program:

The
icon RUNS the program to completion. Using this icon, you should observe the
yellow highlight showing the program’s progress in the Text Segment, and green highlight
showing the registers being modified in the Registers panel. When there are changes to the
Data Segment, they are also highlighted in blue.

The
icon RESETS the program’s registers and memory to initial values. Memory
contents are those specified within the program, and register contents are generally zero.

The
icon is “SINGLE-STEP.” Its complement is
(undo the last step).
, “SINGLE-STEP backward”
6
Run the program to completion, using the very slow 1 second per instruction speed. You will
need to enter values twice, in the Run I/O panel, to be used for the addition.
Upon completion, the following will be displayed:
Enter a number:2
Enter a number:3
The sum of your numbers is:5
program is finished running –
Reset
and run the program one instruction at a time, using the single-step
the registers after each instruction to verify that you understand any new values.
. Examine
Set a breakpoint at address 0x400034 by clicking on the checkbox at the left of the instruction.
Reset
and run
the program again, which STOPS at the breakpoint, BEFORE
executing the instruction.
*8a. Examine the value of $t0 and $v0 at this point. What are their values written as full words?

PRESS ONCE to perform a single step is
to execute the add instruction.
*8b. Examine the value of $t0 again. What is it now written as a full word?

Click
to continue from the breakpoint.
NOTE: you can modify register or memory values directly at a breakpoint before continuing, if it
is necessary to do so for testing purposes.
Open the Help
for information on MIPS instructions, pseudo-instructions, directives,
syscalls, exceptions and macros.
7
– Now that you have seen the basic operation of MARS, we will modify the add program so that
it prompts the user for his/her name and age, and outputs a message that greets you and tells you
how old you will be in 4 years.
# To read in a string, do the following. String will be stored in memory at location
“answer”
li
$v0, 8
# system code 8 for read string
la
$a0, answer
# put address of answer string in $a0
lw
$a1, length
# put length of string in $a1
syscall
# You also now need the following definitions in your .data section.
name:
length:
age:
.space
.word
.word
51 # holds up to 50 characters or less for name
50
When you run your program, your console should look like the following:
*9. Type the following program (you do not have to type the # comments), and save it as lab11.asm. Then assemble and run the program. Show your source code and a screen shot of the
output. In order to print the output of a program, hit the “Print Screen” key on your keyboard.
This puts the screen into a copy buffer, which you can then paste into a Word document and crop
it properly.
prompt1:
prompt2:
prompt3:
prompt4:
name:
length:
age:
.data
.asciiz
.asciiz
.asciiz
.asciiz
.space
.word
.word
“What is your name? **** user input : ”
“What is your age? **** user input : ”
“You will be ”
” years old in four years.”
50
50
0
.text
li $v0, 4
la $a0, prompt1
syscall
8
li $v0, 8
la $a0, name
la $a1, length
syscall
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5
syscall
addi $t0, $v0, 4
sw $t0, age
li
$v0, 4
la
$a0, prompt3
syscall
li
$v0, 1
lw
$a0, age
syscall
li
$v0, 4
la $a0, prompt4
syscall
li
$v0, 10
syscall
*10. Begin editing a source file for a new MIPS program. Type the following program (you do
not have to type the # comments), and save it as lab1-2.asm. Then assemble and run the
program. Show your source code and a screen shot of the output. In order to print the output
of a program, hit the “Print Screen” key on your keyboard. This puts the screen into a copy
buffer, which you can then paste into a Word document and crop it properly.
.text
.globl main
main:
li
$v0, 4
la
$a0, prompt
syscall
# load sys call code for print string to $v0
li
# system call for print integer
$v0, 1
# address of string to print
# print the string
9
lb
$a0, val
syscall
# load the integer byte to print to $a0
# print it
addi
#increment the value of $t0 by 1
$t0, $t0, 1
li $v0, 10
syscall
prompt:
val:
.data
.asciiz
.byte
#system call for exit
“your code is: ”
8
*11. Create another source file containing the following program, and save it
as lab1-3.asm. Assemble the program: If you have typed the program in with NO syntax errors,
the Mars Messages window should indicate that your program loaded successfully. Run the
program to observe its simple output. Show the source code and a screen shot of your output.
.text
.globl main
main:
nums:
letters:
neg1s:
li $v0, 10
syscall
#sys call for exit
.data
.byte 4,3,2,1
.half 8,7,6,5
.word 1,2,3,4
.space 1
.word 12
.asciiz “EFG”
# E=45 (hex)
.ascii “efg” # e=65 (hex)
.byte
-1,-1
.word 15
*12. Create another source file containing the following program, and save it
as lab1-4.asm. Assemble the program: If you have typed the program in with NO syntax errors,
the Mars Messages window should indicate that your program loaded successfully. Run the
program to observe its simple output. Show the source code and a screen shot of your output.
#
# read num1 and num2 from keyboard
10
# print (on screen) the sum of the numbers read
#
# v0: num1 and num2
# t0: sum
#
.data
prompt:
.asciiz “\n Enter a number: ”
result:
.asciiz “The sum of your numbers is ”
.globl main
.text
main:
# read and add first num
# system call code for print string
li $v0, 4
# system call code for print string
la $a0, prompt # load addr of prompt in $a0
syscall
# print prompt
li $v0, 5
syscall
# system call code for read int
# read num1 into $v0
add $t0, $t0, $v0
# add num1 to sum
# read and add second num
li $v0, 4
# system call code for print string
la $a0, prompt # load addr of prompt in $a0
syscall
# print prompt
li $v0, 5
syscall
add $t0, $t0, $v0
# print the sum
li $v0, 4
la $a0, result
syscall
li $v0, 1
move $a0, $t0
syscall
# exit from program
li $v0, 10
syscall
# system call code for read int
# read num2 into $v0
# add num2 to sum
# system call code for print string
# load addr of result msg in $a0
# print result msg
# system call code for print int
# copy sum into $a0
# print sum
# terminate execution and
# return control to system
11
*13. Assemble one more time the above *12. program in MARS and then step through it line by
line in order to examine the registers. Fill in the chart below.
Fill in the appropriate full words that are stored in the registers.
INSTRUCTION
At the start, regs with values are:
li $v0, 4
la $a0, prompt
ori $4, $1, 0x00000000
syscall
li $v0, 5
syscall
MIPS Keyboard Input:
Dialog box enter: 2
add $t0, $t0, $v0
li $v0, 4
la $a0, prompt
ori $4, $1, 0x00000000
syscall
li $v0, 5
syscall
MIPS Keyboard Input:
Dialog box enter: 3
add $t0, $t0, $v0
NOTE THE REGISTER CHANGES
12
li $v0, 4
la $a0, result
ori $4, $1, 0x00000013
syscall
li $v0, 1
move $a0, $t0
li $v0, 10
syscall
*14a. Step through the program again, but this time enter an illegal integer when prompted.
Enter 2a
What is the error message?
*14b. Step through the program again, but this time enter an illegal integer when prompted.
Enter 2.5
What is the error message?
END OF LAB

Still stressed with your coursework?
Get quality coursework help from an expert!