0% found this document useful (0 votes)
81 views6 pages

MIPS Programming Tutorial with MARS

This tutorial provides examples of MIPS assembly language code for printing characters, numbers, and strings; taking user input; performing arithmetic operations like addition and multiplication; conditional branching; and using loops. The code segments are meant to demonstrate basic MIPS programming concepts and instructions to beginners using the MARS simulator. Students are instructed to step through each code example to observe register values and memory addresses.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views6 pages

MIPS Programming Tutorial with MARS

This tutorial provides examples of MIPS assembly language code for printing characters, numbers, and strings; taking user input; performing arithmetic operations like addition and multiplication; conditional branching; and using loops. The code segments are meant to demonstrate basic MIPS programming concepts and instructions to beginners using the MARS simulator. Students are instructed to step through each code example to observe register values and memory addresses.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial on MIPS Programming using MARS

It is expected that students should go through the code segments provided in this tutorial before
proceeding with the asignments. This tutorial is meant for beginners of MIPS programming and
assumes use of the MARS simulator for execution and debugging.

Execute the codes given below ( in steps) and observe the values of registers and memory addresses
as shown in the simulator during each step. It would help you understand how the code works.

1. Printing a character :

.data
character : .byte 'a'

.text

li $v0, 11 #11=system code for printing a character, $v0=register that gets the system
code for printing as value

la $a0, character #'a'=our example character, $a0=register that accepts the character for
printing
syscall #Call to the System to execute the instructions and print the character at the
a0

2. Printing a number :

.data
age : .word 21

.text

li $v0, 1 #1= system code for printing a word ( 32 bit integer), $v0=register that gets
the system code for printing as value

la $a0, age # age is the variable that contains the word to be printed, $a0=register that
accepts the word for printing
syscall #Call to the System to execute the instructions and print the word at a0

3. Printing a floating point number :

.data
PI : .float 3.14 # PI is the variable that contains the floating point nmumber 3.14 to be
printed ( loaded in the data memory)
.text

li $v0, 2 # 2= system code for printing a floating point number (32-bit IEEE 754
format), $v0=register that gets the system code for printing as value

lcw1 $f12, PI # $f12 register is not available with MIPS but with the co-processor 1; lwfc1
means load the $f12 register of coprocessor 1
syscall #Call to the System to execute our instructions

4. Printing a double- precision floating point number :

.data
test : .double 7.202 # test is the variable that contains the double precision floating point
number 7.202 (64-bit IEEE 754 format), $v0=register that gets the
system code for printing as value to be printed ( loaded in the data
memory)
.text

ldc1 $f2, test # the 64-bit value in test variable is stored in $f2 (32-bit LSB) and $f3 ( 32-
bit USB)

li $v0, 3 #3= system code for printing a double precisionfloating point number
( IEEE 754 format),$v0=register that gets the system code for printing as
value

move $f12,$f2 # move is a pseudo-instruction that transfers contents of $f2 to $f12

syscall #Call to the System to execute our instructions

5. Adding two numbers :

.data
num1 : .word 2 # first number to be added stored in data memory
num2: .word 3 # second number to be added stored in data memory

.text

lw $t0, num1 # num 1 is stored in temporary register $t0


lw $t1, num2 # num 2 is stored in temporary register $t1

add $t2 , $t0, $t1 # t2 <- t0 + t1

li $v0, 1 #1= system code for printing a word,


$v0=register that gets the system code for printing as value

move $a0, $t2 # move is a pseudo-instruction that transfers contents of $t2 to $a0
#a0 is the register that needs to hold the value that needs to be printed

syscall #Call to the System to execute our instructions


6. Multiply two numbers :

.data

.text

addi $t0,$zero,10 # t0 <- 0+10


addi $t1, $zero,4 # t1 <- 0+4

mult $t0,$t1 # The result is in hi and low registers

li $v0, 1 #1= system code for printing a word,


$v0=register that gets the system code for printing as value

add $a0, $zer0, $s0 # a0 <- 0+t0


#a0 is the register that needs to hold the value that needs to be printed

syscall #Call to the System to execute our instructions

7. To get the user input :


.data
prompt : .aciiz “Enter your age”
message : .asciiz “ \n Your age is”
.text
li $v0, 4 #4= system code for printing a string,
$v0=register that gets the system code for printing as value

la $a0,prompt # load address of prompt in $a0


syscall # prints the string “ Enter your age”

# Get the users age


li $v0,5 #5= system code for user input
syscall #Call to the System to execute the instruction

# Store the result in $t0


move $t0, $v0 # move is a pseudo-instruction that transfers contents of $t0 to $v0
t0 now contains the user input
# Display the user input
li $v0, 4 #4= system code for printing a string,
$v0=register that gets the system code for printing as value
la $a0, message # load address of prompt in $a0
syscall # prints the string “ Your age is”

# Show the age


li $v0, 1 #1= system code for printing a word,
$v0=register that gets the system code for printing as value
move $a0, $t0 # move is a pseudo-instruction that transfers contents of $t0 to $a0
#a0 is the register that needs to hold the value that needs to be printed

syscall #Call to the System to execute our instructions


8. Passing Arguments to Functions :

.data #data section

.text #code section

main:

addi $a1, #zero, 50 # a1 <- 50


addi $a2, #zero, 100 # a2 <- 100

jal addnumbers # Call the subroutine addnumbers and pass on values of a1 and a2 as
arguments of addnumbers; Save the return address in $ra

li $v0, 1 #1= system code for printing a word,


$v0=register that gets the system code for printing as value
move $a0, $v1 # move is a pseudo-instruction that transfers contents of $v1 to $a0
#a0 is the register that needs to hold the value that needs to be printed

syscall #Call to the System to execute our instructions

li $v0, 10 # system call for terminating the execution


syscall

addnumbers :

add $v1, $a1, $a2 # v1 <- a1 + a2

jr $ra # return to the address pointed to by the address held in return address register
9. Branch Instructions ( If Statements) :

.data #data section


message : .asciiz “ The numbers are different”

.text #code section

main:

addi $t0, #zero, 5 # t0 <- 5


addi $t1, #zero, 20 # t1 <- 20

# Conditional jump to label numbersdifferent if numbers $t0 and $t1 are different

bne $t0, $t1, numbersdifferent


li $v0, 10 #10= system code for exit
$v0=register that gets the system code for printing as value
syscall
numbersdifferent :

# Display the user input

li $v0, 4 #4= system code for printing a string,


$v0=register that gets the system code for printing as value
la $a0, message # load address of prompt in $a0
syscall # prints the string

li $v0, 10 #10= system code for exit


$v0=register that gets the system code for printing as value
syscall

--------------------------------------------------------------------------------------------------------
There are alternate ways to compute the same problem :

1. use of slt instruction - it compares two registers and returns the value as 1 if true and 0 for
false

2. Use of Pseudo branch instructions such as bgt/blt

---------------------------------------------------------------------------------------------------------
10. Using While Loops :

.data #data section


message : .asciiz “ After the while loop is done”
message 2 : .asciiz “\n”

.text #code section

main:

addi $t0, #zero, 50 # to hold the index of the array

while :

bgt $t0,10, exit # if(i>10

jal printnumbers

addi $t0,$t0,1

j while

exit :
li $v0, 4
la $a0, message
syscall

# End of program

li $v0, 10
syscall

printnumbers :

# Print the number

li $v0, 1
move $a0, $t0
syscall

# Move to the next line

li $v0, 4
la $a0, message2
syscall

# Return to main

jr $ra

Common questions

Powered by AI

User input in MIPS assembly is captured through system calls, specifically using the syscall with code '5', which reads an integer from standard input and places it into the $v0 register. The input data is then often transferred to another register like $t0 for processing. Proper utilization requires following the syscall with operations that manipulate the captured input or display it back using appropriate print syscalls. This sequence demonstrates how MIPS handles input/output via direct interactions with registers rather than complex Input/Output streams, streamlining simple program interactions with users .

The syscall instruction in MIPS assembly programming is used to request a service from the operating system. It interacts with register values by using the $v0 register to specify the system service code. Before calling syscall, the appropriate registers (such as $a0 for arguments) must be loaded with the values required by the particular syscall function. For example, to print a character, the system code '11' is loaded into $v0, and the character is loaded into $a0, after which syscall is called to execute the instruction .

Register conventions in MIPS support parameter passing primarily via the argument registers $a0-$a3, where input values for functions are stored before a subroutine is called. The function results are generally handled using the $v0 and $v1 registers, which hold return values. This convention simplifies parameter passing and result retrieval across function calls, enhancing code clarity and consistency. The convention allows the main program and the called subroutine to effectively share data without explicitly transferring values through memory, maintaining efficient execution flow .

In MIPS assembly, data is typically passed to a function using argument registers such as $a0-$a3. When a function is called using the 'jal' (jump and link) instruction, it saves the return address in the $ra (return address) register, allowing the program to know where to continue execution after the function finishes. This linkage ensures control can return seamlessly to the point after the function call. For example, when executing 'jal addnumbers', the current program counter is saved in $ra, allowing a 'jr $ra' instruction within the addnumbers function to return control back to the caller .

The 'mult' instruction in MIPS performs multiplication on two registers and stores the resulting 64-bits across two special-purpose registers: hi and lo. These registers temporarily hold the upper and lower 32 bits of the result, respectively. Subsequent operations must explicitly transfer these results from hi and lo into general-purpose registers before they can be used for further manipulations, often involving additional instructions such as 'mfhi' and 'mflo'. This architecture highlights MIPS' design for accommodating operations producing larger outputs than a single register can handle, while also necessitating careful management to ensure data integrity .

Pseudo-instructions in MIPS, such as 'move', simplify programming by providing shorthand for more complex sequences of native instructions. The pseudo-instruction 'move' represents a common operation—copying one register's value to another—that might otherwise require multiple assembly instructions, like using 'add' with a zero register. Pseudo-instructions do not exist in machine language but are translated into one or more basic instructions by the assembler, improving code readability and maintainability .

The 'jal' (jump and link) instruction in MIPS plays a crucial role in implementing function calls by linking the jump execution to a target label while saving the return address in the $ra (return address) register. By storing the address of the instruction following the 'jal', it enables the program to return control after the function completes execution. This functionality is vital for modular programming in MIPS, where a program's flow can navigate into and out of subroutines, enhancing code modularity and reuse .

The 'bgt' (branch if greater than) instruction in MIPS is used to facilitate control flow in a while loop by comparing two register values and branching to a designated label if the first register's value is greater than the second. It helps conditionally maintain the loop's iteration based on the outcome of the comparison. In a typical while loop, it checks whether the loop condition is satisfied before executing the loop's body; if the condition remains true, it jumps back to the loop's start, otherwise exits the loop. For instance, 'bgt $t0,10, exit' exits the loop when $t0 surpasses 10, allowing for controlled loop termination .

Branch instructions in MIPS, such as 'bne' (branch if not equal), allow for conditional control flow by testing register values and jumping to a specified label if the condition is met. This makes it possible to implement constructs like if statements through direct comparisons. Alternatives for conditional operations include using pseudo-instructions like 'bgt' (branch if greater than) or arithmetic instructions such as 'slt' (set on less than), which set a register to 1 or 0 based on a comparison, enabling conditional logic circuits .

MIPS handles floating-point arithmetic using a set of floating-point registers that are separate from the general-purpose integer registers. Specifically, floating-point operations utilize the coprocessor 1 registers, such as $f12 for single precision and $f2/$f3 for double precision. This distinction is necessary because floating-point values are represented in the IEEE 754 format, which requires different processing. For example, to print a floating-point number, register $f12 needs to be loaded with the data, unlike integer operations that use the $a0 register for output through syscall .

You might also like