Skip to content

MIPS Instructions and Syscall

MARS Assemby 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 "pseudo instructions" (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

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

InstructionExampleMeaningComments
addadd $1,$2,$3$1=$2+$3
subtractsub $1,$2,$3$1=$2-$3
add immediateaddi $1,$2,100$1=$2+100"Immediate" means a constant number
add unsignedaddu $1,$2,$3$1=$2+$3Values are treated as unsigned integers,not two's complement integer
subtract unsignedsubu $1,$2,$3$1=$2-$3Values are treated as unsigned integers,not two's complement integers
add immediate unsignedaddiu $1,$2,100$1=$2+100Values are treated as unsigned integers,not two's complement integers
multiply (without overflow)mul $1,$2,$3$1=$2*$3Result is only 32 bits!
multiplymult $1,$2,$3$hi, $low=$2*$3Upper 32 bits stored in special register hi. Lower 32 bits stored in special register lo
dividediv $1,$2,$3$hi,$low=$2/$3Remainder stored in special register hi. Quotient stored in special register lo

Logical

InstructionExampleMeaningComments
andand $1,$2,$3$1=$2&$3Bitwise AND
oror $1,$2,$3$1=$2I$3Bitwise OR
and immediateandi $1,$2,100$1=$2&100Bitwise AND with immediate value
or immediateori $1,$2,100$1=$2I100Bitwise OR with immediate value
shift left logicalsll $1,$2,10$1=$2<<10Shift left by constant number of bits
shift right logicalsrl $1,$2,10$1=$2>>10Shift right by constant number of bits

Data Transfer

InstructionExampleMeaningComments
load wordlw $1,100($2)$1=Memory[$2+100]Copy from memory to register
store wordsw $1,100($2)Memory[$2+100]=$1Copy from register to memory
load upper immediatelui $1,100$1=100x2^16Load constant into upper 16 bits. Lower 16 bits are set to zero
load addressla $1,label$1=Address of the labelPseudo-instruction (provided by assembler). Loads computed address of label (not it's contents) into register
load immediateli $1,100$1=100Pseudo instruction (provided by assembler). Loads immediate value into the register.
move from himfhi $2$2=hiCopy from special register hi to general register
move from lomflo $2$2=loCopy from special register lo to general register
load wordlw $1,100($2)$1=Memory[$2+100]Copy from memory to register
movemove $1,$2$1=$2Pseudo instruction (provided by assembler). Copy from register to register

Conditional Branch

InstructionExampleMeaningComments
branch on equalbeq $1,$2,100if($1==$2) go to PC+4+100Test if registers are equal
branch on not equalbne $1,$2,100if($1!=$2) go to PC+4+100Test if registers are not equal
branch on greater thanbgt $1,$2,100if($1>$2) go to PC+4+100Pseudo-instruction
branch on greater than or equalbge $1,$2,100if($1>=$2) go to PC+4+100Pseudo-instruction
branch on less thanblt $1,$2,100if($1<$2) go to PC+4+100Pseudo-instruction
branch on less than or equalble $1,$2,100if($1<=$2) go to PC+4+100Pseudo-instruction

Comparison

InstructionExampleMeaningComments
set on less thanslt $s1,$s2,$s3if($2<$3) $1 = 1;else $1 = 0Test if less than. If true set $1 to 1. Otherwise set $1 to 0.
set on less than immediateslti $s1,$s2,100if($2<100) $1 = 1;else $1 = 0Test if less than. If true set $1 to 1. Otherwise set $1 to 0.

Unconditional Jump

InstructionExampleMeaningComments
jumpj 1000go to address 1000Jump to target address
jump registerjr $1go to address stored in $1For switch procedure return
jump and linkjal 1000$ra=PC+4 go to address 1000Use when making procedure call. This saves the return address in $ra

System Calls

ServiceOperationCode(in $v0)ArgumentsResults
print_intPrint integer number (32 bit)1$a0 = integer to be printedNone
print_floatPrint floating-point number (32 bit)2$f12 = float to be printedNone
print_doublePrint floating-point number (64 bit)3$f12 = integer to be printedNone
print_stringPrint null-terminated character string4$a0 = address of string in memoryNone
read_intRead integer number from user5NoneInteger returned in $v0
read_floatRead floating-point number from user6 NoneFloat returned in $f0
read_doubleRead double floating-point number from user7NoneDouble returned in $f0
read_stringWorks the same as Standard Clibrary fgets()8$a0 = memory address of string input buffer $a1 = length of string buffer (n)None
sbrkReturns the address to a block of memory containing n additional bytes (Useful for dynamic memory allocation)9$a0=amountaddress in $v0
exitStop program from running10NoneNone
print_charPrint character11$a0 = character to be printedNone
read_charRead character from user12NoneChar returned in $v0
exit2Stops program from running and returns an integer17$a0 = result(integer number)None

The complete list of syscalls can be accessed at
https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html

Assembler Directives

DirectiveResult
.word w1, ..., wnStore n 32-bit values in successive memory words
.half h1, ..., hnStore n 16-bit values in successive memory words
.byte b1, ..., bnStore n 8-bit values in successive memory words
.ascii strStore the ASCII string str in memory. Strings are in double-quotes, i.e. "Computer Science"
.asciiz strStore the ASCII string str in memory and null terminate it. Strings are in double-quotes, i.e. "Computer Science"
.space nLeave an empty n-byte region of memory for later use
.align nAlign the next datum on a 2^n byte boundary. For example, .align 2 aligns the next value ona word boundary

Registers

Register NumberRegister NameDescription
0$zeroThe value 0
2-3$v0-$v1Values from expression evaluation and function results
4-7$a0-$a3(arguments) First four parameters for subroutine
8-15, 24-25$t0-$t9Temporary variables
16-23$s0-$s7Saved values representing final computed results
31$raReturn 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) Enter the code as shown below into the editor and save the file.

Tutorial Code
asm
# 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?