Lab 7 & 8
Lab 7 & 8
LAB # 7
OBJECTIVE
To Study and implement basic MIPs Arithmetic Instructions (add,Sub,Mul and Div)
using MARS Simulator.
THEORY
Most MIPS operators take 3 registers as parameters, which is the reason it is called a
3-address machine. They are: the first input to the ALU, always Rs (the first source
register); the second input to the ALU, either Rt (the second source register) or an
immediate value; and the register to write the result to, Rd (the destination register).
ARITHMETIC INSTRUCTIONS
add operator, which takes the value of the Rs and Rt registers containing integer
numbers, adds the numbers, and stores the value back to the Rd register. The format
and meaning are:
format: add Rd, Rs, Rt
meaning: Rd <- Rs + Rt
addi operator, which takes the value of Rs and adds the 16 bit immediate value in the
instruction, and stores the result back in Rt. The format and meaning are:
format: addi Rt, Rs, Immediate
meaning: Rt <- Rs + Immediate
addu operator, which is the same as the add operator, except that the values in the
registers are assumed to be unsigned, or whole, binary numbers. There are no negative
values, so the values run from 0..232-1. The format and the meaning are the same as
the add operator above:
format: addu Rd, Rs, Rt
meaning: Rd <- Rs + Rt
addiu operator, which is the same as the addi operator, but again the numbers are
assumed to be unsigned8:
format: addiu Rt, Rs, Immediate
meaning: Rt <- Rs + Immediate
Subtraction in MIPS assembly is similar to addition with one exception. The sub,
subu and subui behave like the add, addu, and addui operators. The only major
difference with subtraction is that the subi is not a real instruction. It is implemented
as a pseudo instruction, with the value to subtract loaded into the $at register, and
then the R instruction sub operator is used. This is the only difference between
addition and subtraction.
sub operator, which takes the value of the Rs and Rt registers containing integer
numbers, adds the numbers, and stores the value back to the Rd register. The format
and meaning are:
format: sub Rd, Rs, Rt
meaning: Rd <- Rs - Rt
sub pseudo operator, which takes the value of R s, subtracts the 16 bit immediate
value in the instruction, and stores the result back in Rt. The format, meaning, and
translation are:
format: subi Rt, Rs, Immediate
meaning: Rt <- Rs - Immediate
translation: addi $at, $zero, Immediate
Name :Isra Taj
Roll no: 256-SE-2020
SEC: F
sub Rt, Rs, $at
subu operator, which is the same as the add operator, except that the values in the
registers are assumed to be unsigned, or whole, binary numbers. There are no negative
values, so the values run from 0..232-1. The format and the meaning are the same as
the add operator above:
format: subu Rd, Rs, Rt
meaning: Rd <- Rs + Rt
Multiplication and division are more complicated than addition and subtraction, and
require the use of two new, special purpose registers, the hi and lo registers. The hi
and lo registers are not included in the 32 general purpose registers which have been
used up to this point, and so are not directly under programmer control.
Since multiplication of two 32-bit numbers requires 64-bits, two 32-bit registers are
required. All computers require two registers to store the result of a multiplication,
though the actual implementation of those two registers is different. It MIPS, the hi
and lo registers are used, with the hi register being used to store the 32 bit larger
part of the multiplication, and the lo register being used to the store the 32 bit smaller
part of the multiplication.
In MIPS, all integer values must be 32 bits. So if there is a valid answer, it must be
contained in the lower 32 bits of the answer. Thus to implement multiplication in
MIPS, the two numbers must be multiplied using the mult operator, and the valid
result moved from the lo register. This is shown in the following code fragment
which multiplies the value in $t1 by the value in
$t2, and stores the result in $t0.
mult $t1, $t2
mflo $t0
there are five MIPS multiplication operators which will be looked at. They are:
mult operator, which multiplies the values of Rs and Rt and saves it in the lo and hi
registers. The format and meaning of this operator is:
format: mult Rs, Rt
meaning: [hi,lo] <- Rs * Rt
mflo operator, which moves the value from the hi register into the Rd register. The
format and meaning of this operator is:
format: mflo Rd
meaning: Rd <- lo
mfhi operator, which moves the value from the hi register into the Rd register. The
Name :Isra Taj
Roll no: 256-SE-2020
SEC: F
format and meaning of this operator is:
mult operator, which multiples the values in Rs and Rt, and stores them in Rd. The
format
and meaning of this operator is:
format: mult Rd, Rs, Rt
meaning: Rd <- Rs * Rt
Division, like multiplication requires two registers to produce an answer. The reason
for this has to do with how the hardware calculates the result, and is harder to explain
without considering the hardware used.
EXAMPLE PROGRAM #1
Sum of Two Integers
################# Data segment #####################
.data
prompt: .asciiz "Please enter two numbers: \n"
sum_msg: .asciiz "The sum is: "
################### Code segment ###################
.text
.globl main
main:
la$a0,prompt # display prompt string
li $v0,4
syscall
Example Program 3:
Divide instruction divides the 32-bit binary value in register $t0 by the 32-
bit value in register $t1. The quotient is stored in the Low register and the remainder
is stored in the High register. The following code segment shows how the quotient is
moved into $t2 and the remainder is moved into $t3:
.DATA
prompt: .asciiz" Quotient is : "
prompt1: .asciiz"\nRemainder is: "
Output:
Output:
Code
Output:
LAB # 8
OBJECTIVE
To Implement MIPS conditional instructions. using MARS Simulator.
THEORY
Conditional Instructions
Unconditional Jumps:
blezRs, Label
Branch if Less Than or Equal to Zero.
if( Rs ≤ 0 ) go to Label.
bnezRs, Label
Branch if Not Equal to Zero.
if(Rs != 0) go to Label
40
PROGRAM #1
Write a MIPS assembly language program that calculates the sum of all positive
integers less than or equal to N and displays the result.
loop:
add $t0, $t0, $v0 # sum of integers in register $t0
addi $v0, $v0, -2 # summing integers in reverse order
bnez $v0, loop # branch to loop if $v0 is != zero
end:
li $v0, 4 # system call code for Print String
la $a0, bye # load address of msg. into $a0
syscall # print the string
LAB TASKS:
1. Write a MIPS assembly language program that calculates the sum of all
positive even integers less than or equal to N and displays the result.
Program code:
Output:
2. Write a MIPS Assembly program that takes an integer input and multiply that
number with last digit of your roll number. Display whether number is even or
odd.
42