You are on page 1of 19

Index

1. Setting up Java SDK

2. Setting up MARS Simulator

3. MARS Assembly and System Calls

4. MARS Instructions Set

5. References
Setting up JAVA SDK
(Please note this tutorial is to download JDK Ver 11)
Downloading Java SDK on macOS:

1. Open a web browser on your macOS computer.


2. Go to the Oracle Java SE Downloads page at https://www.oracle.com/java/
technologies/javase-jdk11-downloads.html.
3. On the Downloads page, locate the JDK (Java Development Kit) section.
4. Click on the "Download" button for the version of Java you want to install.
Make sure to select the version suitable for your macOS version (e.g.,
macOS x64).
5. You may be prompted to log in or create an Oracle account. Follow the
instructions on the screen to proceed.
6. Once logged in, review and accept the license agreement.
7. On the download page, locate the macOS version and click on the provided
download link.
8. The JDK installer package will be downloaded to your computer.
9. Locate the downloaded package in your Downloads folder or the folder you
specified for downloads.
10. Double-click on the JDK installer package to launch the installation wizard.
11. Follow the instructions in the installation wizard to complete the installation
of the Java SDK on your macOS computer.

Downloading Java SDK on Windows:


1. Open a web browser on your Windows computer.
2. Go to the Oracle Java SE Downloads page at https://www.oracle.com/java/
technologies/javase-jdk11-downloads.html.
3. On the Downloads page, locate the JDK (Java Development Kit) section.
4. Click on the "Download" button for the version of Java you want to install.
Make sure to select the version suitable for your Windows version (e.g.,
Windows x64).
5. You may be prompted to log in or create an Oracle account. Follow the
instructions on the screen to proceed.
6. Once logged in, review and accept the license agreement.
7. On the download page, locate the Windows version and click on the
provided download link.
8. The JDK installer executable file (e.g., jdk-11.0.x_windows-x64_bin.exe)
will be downloaded to your computer.
9. Locate the downloaded executable file, and double-click on it to launch the
installation wizard.
10. Follow the instructions in the installation wizard to complete the installation
of the Java SDK on your Windows computer.
Downloading Java SDK on Linux:
1. Open a web browser on your Linux computer.
2. Go to the Oracle Java SE Downloads page at https://www.oracle.com/java/
technologies/javase-jdk11-downloads.html.
3. On the Downloads page, locate the JDK (Java Development Kit) section.
4. Click on the "Download" button for the version of Java you want to install.
Make sure to select the version suitable for your Linux distribution.
5. You may be prompted to log in or create an Oracle account. Follow the
instructions on the screen to proceed.
6. Once logged in, review and accept the license agreement.
7. On the download page, locate the Linux version and click on the provided
download link.
8. The JDK archive file (e.g., jdk-11.0.x_linux-x64_bin.tar.gz) will be
downloaded to your computer.
9. Open the terminal on your Linux system. You can typically find it in the
Applications menu or by using the shortcut Ctrl+Alt+T.
10. Navigate to the directory where you downloaded the JDK archive file. For
example, if it's in the Downloads folder, you can use the following
command:

cd ~/Downloads

11. Extract the contents of the JDK archive using the following command:
tar -xvzf jdk-11.0.x_linux-x64_bin
Setting Up MARS Simulator

Note: Is your MARS text unreadably small? Download and use a


new release Java 9 or above which contains a fix to automatically
scale and size AWT and Swing components for High Dots Per Inch
(HiDPI) displays on Windows and Linux

Downloading MIPS MARS Simulator on macOS:


1. Open a web browser on your macOS computer.
2. Go to the o cial website of MIPS MARS at http://
courses.missouristate.edu/kenvollmar/mars/.
3. On the homepage, click on the "MARS 4.5" link under the
"Download MARS" section.
4. A ZIP le named "mars4_5.jar.zip" will be downloaded to your
computer.
5. Locate the downloaded ZIP le in your Downloads folder or the folder
you specified for downloads.
6. Double-click on the ZIP file to extract its contents. This will create a JAR
file named "mars4_5.jar".
7. Move the "mars4_5.jar" file to a suitable location on your computer, such
as the Applications folder.
8. Open Terminal on your macOS by going to Applications > Utilities >
Terminal.
9. In the Terminal window, navigate to the directory where you placed the
"mars4_5.jar" file. For example, if you placed it in the Applications folder,
you can use the following command:

cd /Applications

10. Once you are in the correct directory, execute the following command to
run the MIPS MARS simulator:

java -jar mars4_5.jar

11. The MIPS MARS simulator should now launch on your macOS computer.
fi
ffi
fi
Downloading MIPS MARS Simulator on Windows:
1. Open a web browser on your Windows computer.

2. Go to the official website of MIPS MARS at http://courses.missouristate.edu/


kenvollmar/mars/.
3. On the homepage, click on the "MARS 4.5" link under the "Download
MARS" section.
4. A ZIP file named "mars4_5.jar.zip" will be downloaded to your computer.
5. Locate the downloaded ZIP file in your Downloads folder or the folder you
specified for downloads.
6. Right-click on the ZIP file and select "Extract All" to extract its contents. This
will create a JAR file named "mars4_5.jar".
7. Move the "mars4_5.jar" file to a suitable location on your computer, such as
the Program Files folder.
8. Open the Command Prompt on your Windows computer by pressing the
Windows key + R, typing "cmd," and hitting Enter.
9. In the Command Prompt window, navigate to the directory where you placed
the "mars4_5.jar" file. For example, if you placed it in the Program Files
folder, you can use the following command:

cd "C:\Program Files”

10. Once you are in the correct directory, execute the following command to run
the MIPS MARS simulator:

java -jar mars4_5.jar

11. The MIPS MARS simulator should now launch on your Windows computer.

Downloading MIPS MARS Simulator on Linux:

1. Open a web browser on your Linux computer.


2. Go to the official website of MIPS MARS at http://courses.missouristate.edu/
kenvollmar/mars/.
3. On the homepage, click on the "MARS 4.5" link under the "Download
MARS" section.
4. A ZIP file named "Mars4_5.jar.zip" will be downloaded to your computer.
5. Open the terminal on your Linux system. You can typically find it in the
Applications menu or by using the shortcut Ctrl+Alt+T.
6. Navigate to the directory where you downloaded the ZIP file. For example, if
it's in the Downloads folder, you can use the following command:
cd ~/Downloads
7. Unzip the ZIP file using the following command:
unzip Mars4_5.jar.zip

8. This will extract the "Mars4_5.jar" file from the ZIP archive.
9. Move the "Mars4_5.jar" file to a suitable location on your computer. For
instance, you can move it to the /opt directory using the following command:
sudo mv Mars4_5.jar /opt
10. To run the MIPS MARS simulator, open the terminal and navigate to the
directory where you placed the JAR file. For example, if you moved it to the /
opt directory, use the following command:
cd /opt
11. Execute the following command to launch the MIPS MARS simulator

java -jar Mars4_5.jar

12. The MIPS MARS simulator should now launch on your Linux computer.
MARS ASSEMBLY AND SYSTEM CALLS

Before assembling, the environment of this simulator can be simplisticly split to three
segments: the editor at the upper left where all of the code is being written, the
compiler/output right beneath the editor and the list of registers that represent the
"CPU" for our program.

After assembling (by simply pressing F3) the environment changes, with two new
segments getting the position of the editor: the text segment where
i) each line of assembly code gets cleared of "pseudoinstructions" (we'll talk about
those in a sec) at the "basic" column and

ii) the machine code for each instruction at the "code" column,
and the data segment where we can have a look at a representation of the memory of
a processor with little-endian order.

After assembling, we can execute our code either all at once (F5) or step by step (F7),
as well as rewinding the execution several steps backwards to the back (F8).

MARS accepts and exports files with the .asm filetype

• System calls are a set of services provided from the operating system. To use
a system call, a call code is needed to be put to $v0 register for the needed
operation. If a system call has arguments, those are put at the $a0-$a2
registers. Here are all the system calls.
• li (load immediate) is a pseudo-instruction that instantly loads a register with
a value. la (load address) is also a pseudo-instruction that loads an address to
a register. With li $v0, 4 the $v0 register has now 4 as value, while la $a0,
str loads the string of str to the $a0 register.
• A word is (as much as we are talking about MIPS) a 32 bits sequence, with bit
31 being the Most Significant Bit and bit 0 being the Least Significant Bit.
• lw (load word) transfers from the memory to a register, while sw (store word)
transfers from a register to the memory. With the lw $s1, 0($t0) command, we
loaded to $s1 register the value that was at the LSB of the $t0 register (thats
what the 0 symbolises here, the offset of the word), aka 256. $t0 here has the
address, while $s1 has the value. sw $t2, 0($t0) does just the opposite job.
• MARS uses the Little Endian, meaning that the LSB of a word is stored to the
smallest byte address of the memory.
• MIPS uses byte addresses, so an address is apart of its previous and next by 4.
MIPS Instruction Set

Pseudo instructions
Before looking at the instruction set , let us look at a few pseudo instructions that’ll
help you understand the IS better.

Here's a list of useful pseudo-instructions.

mov $t0, $t1: Copy contents of register t1 to register t0.

li $s0, immed: Load immediate into to register s0.

The way this is translated depends on whether immed is 16 bits or 32 bits.

la $s0, addr: Load address into to register s0.

lw $t0, address: Load a word at address into register t0

Given below are some standard arithmetic and logical instructions standard to
the MIPS Instruction Set Architecture.

MARS provides a small set of operating system-like services through the system
call (syscall) instruction

To request a service, a program loads the system call code into register $v0 and
arguments into registers $a0~$a3

System calls that return values put their results in register $v0
Arithmetic Instructions

Instruction Example Meaning Comments

add add $1,$2,$3 $1=$2+$3

subtract sub $1,$2,$3 $1=$2-$3

add immediate addi $1=$2+100 "Immediate" means a


$1,$2,100 constantnumber

add unsigned addu $1,$2,$3 $1=$2+$3 Values are treated as


unsignedintegers,
not two's complement integers

subtract unsigned subu $1,$2,$3 $1=$2-$3 Values are treated as


unsignedintegers,
not two's complement integers

add addiu $1=$2+100 Values are treated as


immediate $1,$2,100 unsignedintegers,
unsigned not two's complement integers

Multiply mul $1,$2,$3 $1=$2*$3 Result is only 32 bits!


(without
overflow)

Multiply mult $2,$3 $hi, Upper 32 bits stored in


$low=$2*$3 specialregister hi
Lower 32 bits stored in
specialregister lo

Divide div $2,$3 $hi, Remainder stored in


$low=$2/$3 specialregister hi
Quotient stored in special registerlo
Logical

Instruction Example Meaning Comments

and and $1,$2,$3 $1=$2&$3 Bitwise AND

or or $1,$2,$3 $1=$2|$3 Bitwise OR

and immediate andi $1=$2&10 Bitwise AND with immediate value


$1,$2,100 0

or immediate or $1,$2,100 $1=$2|100 Bitwise OR with immediate value

shift left logical sll $1,$2,10 $1=$2<<1 Shift left by constant number of bits
0

shift right srl $1,$2,10 $1=$2>>1 Shift right by constant number of


logical 0 bits

Data Transfer

Instruction Example Meaning Comments

load word lw $1=Memory[$2+100 Copy from memory to register


$1,100($2) ]

store word sw Memory[$2+100]=$ Copy from register to memory


$1,100($2) 1

load lui $1,100 $1=100x2^16 Load constant into upper 16


upper bits.Lower 16 bits are set to
immedia zero.
te
load address la $1,label $1=Address of label Pseudo-instruction (provided
byassembler, not processor!)
Loads computed address of label (not
itscontents) into register

load immediate li $1,100 $1=100 Pseudo-instruction (provided


byassembler, not processor!)
Loads immediate value into register
move from hi mfhi $2 $2=hi Copy from special register hi to general
register

move from lo mflo $2 $2=lo Copy from special register lo to general


register

move move $1,$2 $1=$2 Pseudo-instruction (provided


byassembler, not processor!)
Copy from register to register.

Conditional Branch

Instruction Example Meaning Comments

branch on equal beq if($1==$2) go Test if registers are equal


$1,$2,100 toPC+4+100

branch on not equal bne if($1!=$2) go Test if registers are


$1,$2,100 toPC+4+100 notequal

branch on greater than bgt if($1>$2) go Pseduo-instruction


$1,$2,100 toPC+4+100

branch on greater than bge if($1>=$2) go Pseduo-instruction


orequal $1,$2,100 toPC+4+100

branch on less than blt if($1<$2) go Pseduo-instruction


$1,$2,100 toPC+4+100

branch on less than ble if($1<=$2) go Pseduo-instruction


orequal $1,$2,100 toPC+4+100
Comparison

Instruction Example Meaning Comments

set on less than slt $1,$2,$3 if($2<$3)$1= Test if less than.


1;else $1=0 If true, set $1 to 1. Otherwise, set
$1to 0.

set on less slti if($2<100)$1=1 Test if less than.


than $1,$2,100 ; If true, set $1 to 1. Otherwise, set
immediate else $1=0 $1to 0.

Unconditional Jump

Instruction Example Meaning Comments

jump j 1000 go to address 1000 Jump to target address

jump jr $1 go to address stored in $1 For switch, procedure return


register

jump and jal $ra=PC+4; go to address Use when making procedure call.
link 1000 1000 This saves the return address in
$ra

System Calls

Service Operation Co Arguments Results


de
(in
$v0)
print_int Print integer number (32 bit) 1 $a0 = integer to None
beprinted

print_float Print floating-point number (32 bit) 2 $f12 = float to None


beprinted

print_doubl Print floating-point number (64 bit) 3 $f12 = double to None


e beprinted
print_string Print null-terminated character string 4 $a0 = address None
ofstring in
memory
read_int Read integer number from user 5 None Integer
returned
in
$v0
read_float Read floating-point number from user 6 None Float
returned
in
$f0
read_doubl Read double floating-point number 7 None Double
e fromuser returned
in
$f0
read_string Works the same as Standard 8 $a0 = memory None
CLibrary fgets() function. address of
stringinput
buffer
$a1 = length of
stringbuffer (n)
sbrk Returns the address to a block of 9 $a0 = amount address in
memory containing n additional $v0
bytes. (Useful for dynamic memory
allocation)
exit Stop program from running 10 None None

print_char Print character 11 $a0 = character to None


beprinted

read_char Read character from user 12 None Char


returned
in
$v0
exit2 Stops program from running and 17 $a0 = result None
returnsan integer (integernumber)

The complete list of syscalls can be accessed at


https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html
Assembler Directives

Directive Result

.word w1, ..., Store n 32-bit values in successive memory words


wn
.half h1, ..., Store n 16-bit values in successive memory words
hn
.byte b1, ..., Store n 8-bit values in successive memory words
bn

.ascii str Store the ASCII string str in memory.


Strings are in double-quotes, i.e. "Computer Science"

.asciiz str Store the ASCII string str in memory and null-terminate
itStrings are in double-quotes, i.e. "Computer Science"

.space n Leave an empty n-byte region of memory for later use

.align n Align the next datum on a 2^n byte boundary.


For example, .align 2 aligns the next value on a word
boundary

Registers

Register Registe Description


Number rName

0 $zero The value 0

2-3 $v0 - (values) from expression evaluation and function


$v1 results

4-7 $a0 - (arguments) First four parameters for subroutine


$a3

8-15, $t0 - Temporary variables


24-25 $t9

16-23 $s0 - Saved values representing final computed results


$s7

31 $ra Return address


MARS (MIPS Assembler/Simulator) Tutorial
1. Input the Tutorial program
1.1) Open the MARS program and click from the file menu choose “File…New”. A
black document will open which you can enter your assembly code into. Click
“File…Save As” and save the file as “Tutorial1.asm
1.2) ”.
1.3) Enter the code as shown below into the editor and save the file.

# Program File: Tutorial1.asm


# Written by: MoSaad
# Date Created: 10/05/2023
# Description: Tutorial program to introduce MARS simulator including: breakpoints, single-
stepping, and register and memory windows.
#-----------------------------------------------------------
#-----------------------
# Declare some constants
#-----------------------
.data
string1: .asciiz "Welcome to Hardware Lab at NIT Calicut\n"
string2: .asciiz "Hello World \n"
string3: .asciiz "\nLoop #"
#------------------
# Main program body
#------------------
.text
main:
li $v0,4
la $a0,string1
syscall
la $a0,string2
syscall
li $t0, 1
loop:
li $v0, 4
la $a0,string3
syscall
li $v0,1
move $a0,$t0
syscall
addi $t0,$t0,1
bne $t0,4,loop
#-----
# Halt
#-----
li $v0, 10
syscall
1.4) From the menu, choose “Run…Assemble”. The “Mars Messages” window at the
bottom of the screen will indicate if any errors occurred. No errors should occur.

2. Simulate the tutorial program


2.1) From the menu, choose “Run…Go” to execute the program. The program will
execute displaying two lines of text and three iterations of a loop to the Run /IO
window.

2.2) The buttons at the top of the window can be used as shortcuts for the run menu.
Use the “Reset” button to reset the program, and then try tracing through the
program by clicking the step button.
2.3) You can adjust the speed the program runs by moving the slider to the right of the
buttons. If you have an infinite loop in your program, it may be necessary to adjust
(slow down) the speed of the simulator to prevent the MARS program from
crashing.
Run the program. If a breakpoint has been set the
program will stop at the next breakpoint.
Trace (Step) Into. Executes a single instruction. If
the instruction is a procedure call (jal) the
simulator will stop at the first instruction of the
procedure.
Backstep. Undo the last step taken in the
code.

Pause the currently running program. Press the run


button to continue execution.

Stop the currently running program. You will need


to reset the simulator to execute the program again
after stopping it.
Reset. Resets the simulator, reinitializing the
registers, program counter, and memory.

Adjusts the speed that the simulator runs at.

3. Using the Debugging Tools


3.1) When a program does not work as expected you will need to use the debugging
tools provided with the simulator.
3.2) One of the primary tools used to debug a program is setting a breakpoint. You can
break before execution of an instruction by clicking on the checkbox associated
with each instruction on the far left of the execute window. Set a breakpoint at
the instruction: addi $t0,$t0,1

3.3) Run the program until the breakpoint by clicking “Run”. At this point in the
program only the first loop iteration has been printed. (You will need to click
back to the Run/IO window to see the output.)
3.4) Now use the “Trace Into” button to step through the loop that prints out the next line
of text one character at a time. Step through the instructions until “Loop #2” is
printed to the output window. Stop and find the value of the registers “t0” and
“pc” at that point? Has the line of code that the program counter points to executed
yet?
3.5) The simulator also allows you to view the memory contents. The memory window
appears in the middle of the screen and is titled “Data Segment”. Remove the
earlier breakpoint and add a breakpoint to line 33, “syscall”. Click the run button
so that the program executes up until the new breakpoint. We are now in the code
right before “Loop #” is about to be printed for the third iteration. Notice that the
$a0 register is now a pointer to the address where the “Loop #” text is stored. What
is the memory location the register is pointing to?
3.6) Now look in the data segment area, and find the address $a0 points to. This is the
memory section where the characters of the text “Loop #” is stored. Using an
ASCII table find the address where the ‘p’ in “Loop” is located?
3.7) Exercise: Can you find where the word “Welcome” is stored in the memory?

Figure 4 - Memory Window

American Standard Code for Information Interchange (ASCII)


0b6b5b4
b3b2b1b0
0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7
0x0 NU DL SP 0 @ P ` p
L E
0x1 SO DC ! 1 A Q a q
H 1
0x2 ST DC “ 2 B R b r
X 2
0x3 ET DC # 3 C S c s
X 3
0x4 EO DC $ 4 D T d t
T 4
0x5 EN NA % 5 E U e u
Q K
0x6 AC SY & 6 F V f v
K N
0x7 BE ET ‘ 7 G W g w
L B
0x8 BS CA ( 8 H X h x
N
0x9 HT EM ) 9 I Y i y
0xA LF SU * : J Z j z
B
0xB VT ESC + ; K [ k {
0xC FF FS ‘ < L \ l |
0xD CR GS - = M ] m }
0xE SO RS . > N ^ n ~
0xF SI US / ? O - o DE
L
References:
http://www.cs.missouristate.edu/MARS/.
https://www.d.umn.edu/~gshute/mips/directives-registers.pdf

https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html

https://courses.missouristate.edu/KenVollmar/mars/Help/MarsHelpIntro.html

https://riptutorial.com/mips/example/29993/mars-mips-simulator

https://bytes.usc.edu/ les/ee109/documents/MARS_Tutorial.pdf
fi

You might also like