Question
Task 1 : You are given a program in MIPS assembly language that computes the area of a rectangle given the width and the height
Task 1:
You are given a program in MIPS assembly language that computes the area of a rectangle given the width and the height (ex1.asm). The width and height are read from the standard input after prompting the user, and then the program computes the area and prints it on the standard output. Here's an example scenario:
Enter width (integer):
2
Enter height (integer):
4
Rectangle's area is 8
Modify the program so that it also calculates and prints the perimeter (i.e., sum of all sides) of the rectangle. Thus, after modification, the example scenario would become:
Enter width (integer):
2
Enter height (integer):
4
Rectangle's area is 8
Its perimeter is 12
Task 2:
Copy the fileex1.asmtoex2.asm. Modify the program inex2.asmto make it work on multiple inputs. In particular, it should repeatedly ask for width and height values, and print the corresponding area and perimeter, until the user enters the value0for width. At that point, the program should terminate. Here's an execution scenario:
Enter width (integer):
2
Enter height (integer):
4
Rectangle's area is 8
Its perimeter is 12
Enter width (integer):
5
Enter height (integer):
6
Rectangle's area is 30
Its perimeter is 22
Enter width (integer):
0
Essentially, this exercise involves introducing a loop around the main code of Task 1. As long as width is not equal to 0, the program repeats by looping back to the top of the loop. If width is 0, the program breaks out of the loop. The key instructions for forming such loops are:beq,bneandj. You may need one or more of these, depending on how you construct your loop.
-
Save all your programs as ex1.asm, ex2.asm and comment your code properly.
-
Test your program with your own test cases and save the screenshots.
-
Upload all the files, ex1.asm, ex2.asm and at least two screenshots showing the output to the Blackboard.
This is the code:
ex1.asm
# Assembly starter file for Exercise 1 .data 0x0 width: .word 0 height: .word 0 area: .word 0 perimeter: .word 0 widthPrompt: .asciiz "Enter width (integer): " heightPrompt: .asciiz "Enter height (integer): " areaIs: .asciiz "Rectangle's area is " perimeterIs: .asciiz "Its perimeter is " newline: .asciiz " " .text 0x3000 main: # Print the prompt for width addi $v0, $0, 4 # system call 4 is for printing a string la $a0, widthPrompt # address of widthPrompt is in $a0 syscall # print the string # Read the width addi $v0, $0, 5 # system call 5 is for reading an integer syscall # integer value read is in $v0 add $8, $0, $v0 # copy the width into $8 # Print the prompt for height addi $v0, $0, 4 # system call 4 is for printing a string la $a0, heightPrompt # address of heightPrompt is in $a0 syscall # print the string # Read the height addi $v0, $0, 5 # system call 5 is for reading an integer syscall # integer value read is in $v0 add $9, $0, $v0 # copy the height into $9 # Calculate area mult $8, $9 # multiply width * height mflo $10 # bring the product into $10 (assume hi not needed) # Print "Rectangle's area is " addi $v0, $0, 4 # system call 4 is for printing a string la $a0, areaIs # address of areaIs string is in $a0 syscall # print the string # Print the calculated area (in $10) addi $v0, $0, 1 # system call 1 is for printing an integer add $a0, $0, $10 # bring the area value from $10 into $a0 syscall # print the integer # Print a newline addi $v0, $0, 4 # system call 4 is for printing a string la $a0, newline # address of areaIs string is in $a0 syscall # print the string # Exit from the program exit: ori $v0, $0, 10 # system call code 10 for exit syscall # exit the program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started