Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

CE-207 Computer Organization & Architecture SSUET/QR/114

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

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
Addition Operators:
There are 4 real addition operators in MIPS assembly. They are:

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

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

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
subi pseudo operator, which takes the value of Rs, 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
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 in MIPS assembly

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:

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
format: mfhi Rd
meaning: Rd <- hi

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 in MIPS Assembly

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

li $v0,5 # read 1st integer into $t0


syscall
move $t0,$v0
li $v0,5 # read 2nd integer into $t1
syscall
move $t1,$v0

addu $t0,$t0,$t1 # accumulate the sum

la $a0,sum_msg # write sum message


li $v0,4
syscall
move $a0,$t0 # output sum
li $v0,1
syscall
li $v0,10 # exit
syscall
Example Program 2:
Name :Isra Taj
Roll no: 256-SE-2020
SEC: F
The multiply instruction multiplies two 32-bit binary values and produces a 64-bit

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
product which is stored in two registers named High and Low. The following code
segment shows how the lower 32 bits of the product of $t0 times $t1 can be moved
into $t3:

mult $t0, $t1


mflo $t3 # t3 = Lower 32-bits of product
mfhi $t4 # t4 = Higher 32-bits of product
################# Data segment #####################
.DATA
Prompt : .asciiz" resuit of multiplication is\n”

################# Code segment #####################


.text
.globl main # main program entry
main:
li $t0,99#$t0=99
li $t1,99 #$t1=99

mult $t0, $t1 # $t0*$t1


mflo $t2# $t2 = Lower 32-bits of product

la $a0,prompt# Print message of prompt


li $v0,4
syscall

move $a0,$t2 #move value of #t2 into $a0


li $v0,1# Print integer
syscall

li $v0,10# Exit program


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:

div $t0, $t1


mflo $t2 # here lower register $t2 contain the quotient.
mfhi $t3 # here high register $t3 contain the remainder.
################# Data segment #####################

.DATA
prompt: .asciiz" Quotient is : "
prompt1: .asciiz"\nRemainder is: "

################# Code segment #####################


Name :Isra Taj
Roll no: 256-SE-2020
SEC: F
.text
.globl main # main program entry
main:
li $t0,42 #$t0=42
li $t1,2 #$t1=2

div $t0, $t1


mflo $t2 # here lower register $t2 contain the quotient.
mfhi $t3 # here high register $t3 contain the remainder.

la $a0,prompt # Print message of prompt


li $v0,4
syscall

move $a0,$t2 #move value of #t2 into $a0


li $v0,1 # Print value of quotient
syscall

la $a0,prompt1 # Print message of prompt1


li $v0,4
syscall

move $a0,$t3 #move value of #t3 into $a0


li $v0,1 # Print value of remainder
syscall

li $v0,10 # Exit program


syscall

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
LAB TASKS:
Object:
Write a MIPS Assembly program that Take three digits of your Roll Numbers
as inputs and add them.
CODE:

Output:

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
Object:
Write a MIPS Assembly program that Take first and last digits of your Roll Numbers and
subtract them.
Code:

Output:

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
Object:
1. Write a MIPS Assembly program that takes an integer input and multiply that number with
last digit of your roll number

Code

Output:

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
2. Write a MIPS Assembly program that takes your three-digit roll number as input and divide
it with second digit of your roll number.
code:

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
Output:

Name :Isra Taj


Roll no: 256-SE-2020
SEC: F
CE-207 Computer Organization & Architecture SSUET/QR/114

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.

################### Data segment ###################


.data
prompt: .asciiz "\n Please Input a value for N = "
result: .asciiz " The sum of the integers from 1 to N is "
bye: .asciiz "\n **** Have a good day ****"
################### Code segment ###################
.text
.globl main
main:
li $v0, 4 # system call code for Print String
la $a0, prompt # load address of prompt into $a0
syscall # print the prompt message

li $v0, 5 # system call code for Read Integer


syscall # reads the value of N into $v0
blez $v0, end # branch to end if $v0 < = 0
li $t0, 0 # clear register $t0 to zero

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

li $v0, 4 # system call code for Print String


la $a0, result # load address of message into $a0
syscall # print the string

li $v0, 1 # system call code for Print Integer


move $a0, $t0 # move value to be printed to $a0
syscall # print sum of integers

end:
li $v0, 4 # system call code for Print String
la $a0, bye # load address of msg. into $a0
syscall # print the string

li $v0, 10 # terminate program run and


syscall # return control to system
CE-207 Computer Organization & Architecture SSUET/QR/114

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

You might also like