Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CCCN 221 Architecture Lab 6 MIPS Loops In MIPS Statement Purpose: The purpose of this Lab. is to familiarize student with control instructions and how

CCCN 221 Architecture

Lab 6 MIPS Loops In MIPS

Statement Purpose:

The purpose of this Lab. is to familiarize student with control instructions and how to deal with looping. This lab will explain how to control the sequence of instructions execution, depending on the value of an registers using the branching instructions and also how to repeat some instructions for many times.

Activity Outcomes:

Student will learn how to write MIPS programs that make decisions, execute different parts and repeat some instructions depending on registers values. Student will solve different problem that will help them to choose appropriate Logical and branching statement for a given task. There are some exercises, through which they will understand the concept learn in this chapter.

Integer Arithmetic and movement Instructions

Most of them use three operands

All operands are registers; no RAM or indirect addressing

Operand size is word (4 bytes)

add $t0,$t1,$t2 # $t0 = $t1 + $t2; add as a signed integers

sub $t2,$t3,$t4 # $t2 = $t3 - $t4

addi $t2,$t3, 5 # $t2 = $t3 + 5; (no sub immediate)

addu $t1,$t6,$t7 # $t1= $t6 + $t7; add as unsigned integers

subu $t1,$t6,$t7 #$t1=$t6-$t7; sub as unsigned integers

mult $t3,$t4 # multiply 32-bit quantities in $t3 and $t4, and store 64-bit

result in special registers #Lo and Hi: (Hi,Lo) = $t3 * $t4

div $t5,$t6 # Lo = $t5 / $t6 (integer quotient) and Hi = $t5 mod $t6

(remainder)

mfhi $t0 # move quantity in special register Hi to $t0

mflo $t1 # move quantity in special register Lo to $t1 # used to get at result

of product or quotient

move $t2,$t3 # $t2 = $t3

Example1: how to swap values between two registers

#swap values in registers $t0 and $t1, destroys $t2):

.text

.globl start

main:

# swap values $t0 and $t1

move $t2, $t0

move $t0, $t1

move $t1, $t2

# no .data segment

Comparison Instructions

Set on Less than

slt $t1,$t2,$t3 # $t2 and $t3 contain signed integers # if ( $t2 < $t3 ) # $t1 = 1 # else #

$t1 = 0

sltu $t1,$t2,$t3 # $t2 and $t3 contain unsigned integers

Branch and Jump Instructions

Branches

beq $t0,$t1,target # branch to target if $t0 = $t1

bne $t0,$t1,target # branch to target if $t0 <> $t1

blt $t0,$t1,target # branch to target if $t0 < $t1

ble $t0,$t1,target # branch to target if $t0 <= $t1

bgt $t0,$t1,target # branch to target if $t0 > $t1

bge $t0,$t1,target # branch to target if $t0 >= $t1

Jumps

j target # unconditional jump to program label target

Looping Instructions

Loops in MIPS assembly:

Use comparison, branch and jump instructions.

Structure could be as follows:

main:

move $t0,$zero #Initialize counter = 0

addi $t4,$zero,10 #Initialize limit = 10

# while not counter==limit do

loop:

. . . . . . . . .

slt $t3, $t0, $t4

beq $t3, $zero, done

j loop

done:

..

Example: Prints out all the multiples of A from A to A*B

# multiples.s -- takes two numbers A and B, and prints out

# all the multiples of A from A to A * B.

# If B <= 0, then no multiples are printed.

# Registers used:

# $t0 - used to hold A.

# $t1 - used to hold B.

# $t2 - used to store S, the sentinel value A * B.

# $t3 - used to store m, the current multiple of A.

.text

main:

li $v0, 5 # syscall 5 = read_int

syscall

move $t0, $v0 # A = integer just read

li $v0, 5 # syscall 5 = read_int

syscall

move $t1, $v0 # B = integer just read

blez $t1, exit # if B <= 0, exit.

mul $t2, $t0, $t1 # S = A * B.

move $t3, $t0 # m = A

loop:

move $a0, $t3 # print m.

li $v0,1 # syscall 1 = print_int

syscall # make the system call.

beq $t2, $t3, endloop # if m == S, we're done.

add $t3, $t3, $t0 # otherwise, m = m + A.

la $a0, space # print a space.

li $v0, 4 # syscall 4 = print_string

syscall

b loop # iterate.

endloop:

la $a0, newline # print a newline:

li $v0, 4 # syscall 4 = print_string

syscall

exit: # exit the program:

li $v0, 10 # syscall 10 = exit

syscall # we're outta here.

.data

space: .asciiz " "

newline: .asciiz " "

Practices

Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. For example, if your input is 10, the output should be

Is 10 divisible by 5 and 6? false

Is 10 divisible by 5 or 6? true

Is 10 divisible by 5 or 6, but not both? True

(Hint: Use the AND , OR ,X_OR operators)

Write a program that prints the numbers from 1 to given number N.

Write a program that asks user to enter Numbers and displays the Average.

*PLEASE SOLVE THE EXERCISE BY MIPS ASSEMBLY

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

*PLEASE SOLVE AS COMPUTER TYPING (NOT HAND WRITING)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

Methods of Delivery Guidelines for

Answered: 1 week ago