Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*WITH SCREENSHOT PLEASE* CCCS217 Computer Organization and Architecture Lab 3 MIPS Arithmetic Statement Purpose: The purpose of this Lab. is to familiarize student how to

*WITH SCREENSHOT PLEASE*

CCCS217 Computer Organization and Architecture

Lab 3 MIPS Arithmetic

Statement Purpose:

The purpose of this Lab. is to familiarize student how to solve practical problems programmatically; they will practice on elementary programming using data declarations, data transfer (load and store with memory), and input and output. Also, they will learn how to diagnose errors that may occur when a program is executed. There are some exercises, through which they will understand the concept learn in this chapter.

Activity Outcomes:

Student will learn how to write MIPS programs to perform simple calculations and how to use the input and output, they will use syscall to obtain input from and send the output to the console. The use data types: ,byte, .word, and .asciiz.

Assembly Program Structure

  • Plain text file with data declarations, program code.
  • Name of file should end in suffix .s to be used with SPIM simulator.
  • Data declaration section followed by program code section.

Data Declarations

  • Placed in section of program identified with assembler directive .data
  • Declares variable names used in program; storage allocated in main memory (RAM)

Code

  • Open a New file in MIPS and click on Edit tab.
  • Contains program code (instructions)
  • MIPS program is divided in two sections, i.e. .data and .text.
  • Ending point of main code should use exit system call (see below under System Calls)

Comments

  • Anything following # on a line.
  • # This stuff would be considered a comment

MIPS Arithmetic

  • In MIPS all instruction has three operands.
  • Operand order is fixed (destination first).

For Example:

In Java In MIPS:

Z = A + B; add $s0 , $S1 , $S2

or Example:

In Java

A = B + C + D;

E = F - A;

In MIPS

add $t0, $s1, $s2

add $s0, $t0, $s3

sub $s4, $s5, $s0

Operands must be registers, only 32 registers provided.

Example 1: Adding two integers in MIPS

.data

no1: .word 10 # create a single integer variable with initial value 10

no2: .word 15

.text

.globl main

main:

lw $t0, no1

lw $t1, no2

add $t2, $t0, $t1 # t2=t0+t1

li $v0,1 # Printing the result stored in t2

add $a0,$zero, $t2

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Example 2: Subtracting numbers in MIPS

.data

no1: .word 10

no2: .word 8

.text

.globl main

main:

lw $s0, no1 #s0=10

lw $s1, no2 #s1=8

sub $t0, $s0, $s1 # t0=10-8

li $v0,1 # Printing the result stored in t0

move $a0, $t0

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Example 3 (A): Multiplication in MIPS using mul

Multiplication can be done in several ways in MIPS. i.e

  • mul (It takes 3 registers)
  • mult (It takes 2 registers)
  • sll (shift left logical) (sll is very efficient but cant give much flexibility)

In the following example we use the first way i.e mul

.data

no1: .word 10

no2: .word 8

.text

.globl main

main:

addi $t0,$zero,3000

addi $t1,$zero,20

mult $t0,$t1

mflo $s0

#Display the result to screen

li $v0,1

add $a0, $zero, $s0

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Explanation: In the above code we use mul function to multiply two numbers. We didnt use Random Access Memory to get the numbers for multiplication. We use addi to get values into registers without use of RAM. Thats is the reason there is nothing in our .data section of the program. addi is just a constant like 10, 40 or whatever. For example, (addi $s0,$zero,10), this statement means take 10 add with register zero and store the result into register $s0. There is a drawback of this method, we can only multiply two numbers of 16 bits long and the result is stored in 32 bits register. What if we want to multiply two long numbers then we use another instruction (see Example 3(B)).

Example 3 (B): Multiplication in MIPS using mult

Explanation: mult is used for multiply big numbers. mult take only two registers in which the values are stored. So where is the product of these two numbers? We have two special registers i.e. hi and lo, the product of these two number can be stored in lo or hi register. We used mofl (move from low) to move the number to $s0 and then display the product to screen.

Example 3 (C): Multiplication in MIPS using sll

.text

.globl main

main:

addi $s0,$zero,4

sll $t0, $s0,2

#Display the result to screen

li $v0,1

add $a0, $zero, $t0

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Explanation: sll (shift left logical) is used for multiplication in MIPS. First we stored 4 in $s0 register. In sll statement in above code 2 mean 2 to the i. Its work like exponent. It will be 2 to the power of 2 which is 4. If we write 3 at the end of sll statement, it means 2 to the power of 3 which is 8. In our case it will be 4 and will be multiply with 4 and the result will be 16.

Example 4 (A): Division in MIPS

. .data

no1: .word 10

no2: .word 8

.text

.globl main

main:

addi $t0, $zero, 40

addi $t1, $zero, 4

div $s0, $t0, $t1

# Print the result

li $v0, 1

add $a0, $zero, $s0

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Example 4(B): Division in MIPS

.text

.globl main

main:

addi $t0, $zero, 40

div $s0, $t0, 10

# Print the result

li $v0, 1

add $a0, $zero, $s0

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Example 4 (C) : Division in MIPS Quotient and Remainder

.text

.globl main

main:

addi $t0, $zero, 40

addi $t1, $zero, 3

div $t0, $t1

mflo $s0 #Quotient

mfhi $s1 #Remainder

# Print the result

li $v0, 1

add $a0, $zero, $s0

syscall

li $v0, 1

add $a0, $zero, $s1

syscall

li $v0, 10 # To terminate the program

syscall

.end main

Questions:

For all arithmetic operations performed above, change the values, execute the codes, take the print screen of console with modified code. Make a lab report in hard form and submit it to your lab Teacher.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

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

Intermediate Accounting

Authors: James D. Stice, Earl K. Stice, Fred Skousen

17th Edition

978-0324592375

Students also viewed these Programming questions