In: Computer Science
Our MIPS assembly version will “compile” the following C program: #include void Compare(int b1, int h1, int b2, int h2); int Area(int b, int h); int main(int argc, char **argv) { int base1, base2, height1, height2; base1=10; base2=12; height1=8; height2=7; Compare(base1, height1, base2, height2); return 0; } void Compare(int b1, int h1, int b2, int h2) { int A1=Area(b1, h1); int A2=Area(b2, h2); if (A1>=A2) printf("Area1 is greater than Area2.\n"); else printf("Area2 is greater than Area1.\n"); } int Area(int b, int h) { int A=(b*h)/2; return A; } The MIPS Assembly Triangles Program .data: b1: .word 10 h1: .word 12 b2: .word 8 h2: .word 7 msg1: .asciiz "Area1 greater than Area2.\n" msg2: .asciiz "Area2 greater than Area1.\n" 5 Adriana WISE—CS301 Thursday, September 26, 2019 debug1: .asciiz "Area1=" debug2: .asciiz "Area2=" newline: .asciiz "\n" .text: main: lw $a0, b1 lw $a1, h1 lw $a2, b2 lw $a3, h2 jal Compare li $v0, 10 syscall Compare: subi $sp, $sp, 4 sw $ra, 0($sp) jal Area add $s0, $v0, $zero la $a0, debug1 li $v0, 4 syscall add $a0, $s0, $zero li $v0, 1 syscall la $a0, newline li $v0, 4 syscall la $a0, debug2 li $v0, 4 syscall add $a0, $a2, $zero add $a1, $a3, $zero jal Area add $a0, $v0, $zero li $v0, 1 syscall la $a0, newline li $v0, 4 syscall sub $s0, $s0, $v0 6 Adriana WISE—CS301 Thursday, September 26, 2019 bltz $s0, LESS j MORE LESS: li $v0, 4 la $a0, msg2 syscall MORE: li $v0, 4 la $a0, msg1 syscall lw $ra, 0($sp) addi $sp, $sp, 4 jr $ra Area: subi $sp, $sp, 8 sw $ra, 0($sp) sw $s0, 4($sp) mul $s1, $a0, $a1 srl $v0, $s1, 1 lw $ra, 0($sp) lw $s0, 4($sp) addi $sp, $sp, 8 jr $ra Output: Area1=40 Area2=42 Area1 greater than Area2. -- program is finished running — Re-write the Triangles C and MIPS programs to read the triangles’ bases and heights as interactive user input. Write a MIPS assembly program (instructions at the end of Lecture 5 notes) to compare the areas of two triangles, taking as user input the bases and heights of the two triangles, and using two functions, Compare() and Area(), where Compare() calls Area(). For practice, also modify the existing C program to accept user input, as presented in class (lecture notes assume hard-coded values).
Screenshot
-----------------------------------------------------------------
Program
.data
b1: .word 0
h1: .word 0
b2: .word 0
h2: .word 0
prompt1: .asciiz "Enter breadth of triangle1:
"
prompt2: .asciiz "Enter height of triangle1:
"
prompt3: .asciiz "Enter breadth of triangle2:
"
prompt4: .asciiz "Enter height of triangle2:
"
msg1: .asciiz "Area1 greater than
Area2.\n"
msg2: .asciiz "Area2 greater than
Area1.\n"
debug1: .asciiz "Area1="
debug2: .asciiz "Area2="
newline: .asciiz "\n"
.text
main:
#Prompt for first triangle's breadth
addi $v0,$0,4
la $a0,prompt1
syscall
#Read and store input
addi $v0,$0,5
syscall
sw $v0,b1
#Prompt for first triangle's height
addi $v0,$0,4
la $a0,prompt2
syscall
#Read and store input
addi $v0,$0,5
syscall
sw $v0,h1
#Prompt for second triangle's breadth
addi $v0,$0,4
la $a0,prompt3
syscall
#Read and store input
addi $v0,$0,5
syscall
sw $v0,b2
#Prompt for second triangle's height
addi $v0,$0,4
la $a0,prompt4
syscall
#Read and store input
addi $v0,$0,5
syscall
sw $v0,h2
#Load value into registers for function call
lw $a0, b1
lw $a1, h1
lw $a2, b2
lw $a3, h2
#Call function to compare and get result
jal Compare
#End of the program
li $v0, 10
syscall
#Function to compare area of 2 triangles
#Take breadth and height of 2 triangles as arguments from a0 - a3
registers
Compare:
#Space allocate in stack for return address
subi $sp, $sp, 4
sw $ra, 0($sp)
#Call function to find first triangles area
jal Area
#Store area in s0
add $s0, $v0, $zero
#Display area
la $a0, debug1
li $v0, 4
syscall
add $a0, $s0, $zero
li $v0, 1
syscall
#Display \n
la $a0, newline
li $v0, 4
syscall
#Find second triangle area and display it
la $a0, debug2
li $v0, 4
syscall
add $a0, $a2, $zero
add $a1, $a3, $zero
jal Area
add $a0, $v0, $zero
li $v0, 1
syscall
la $a0, newline
li $v0, 4
syscall
#Compare and display appropriate message
sub $s0, $s0, $v0
bltz $s0, LESS
j MORE
#Lesser than message
LESS:
li $v0, 4
la $a0, msg2
syscall
#Greater than messagee
MORE:
li $v0, 4
la $a0, msg1
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
#Functon to find area of a triangle
#Take breadth and height of triangle as input arguments
#return area using 1/2*bh
Area:
#Store return address
subi $sp, $sp, 8
sw $ra, 0($sp)
sw $s0, 4($sp)
#bh
mul $s1, $a0, $a1
#1/2 bh
srl $v0, $s1, 1
#Return
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8
jr $ra
-------------------------------------------------
Output
Enter breadth of triangle1: 10
Enter height of triangle1: 12
Enter breadth of triangle2: 7
Enter height of triangle2: 8
Area1=60
Area2=28
Area1 greater than Area2.
-- program is finished running --
------------------------------------------------------------------------------
C program implementation
//Header file for I/O
#include<stdio.h>
//Function prototypes
void Compare(int, int, int, int);
int Area(int, int);
//Main function
int main(int argc,int **argv) {
//Variable declaration
int base1, base2, height1, height2;
//Prompts for inputs
printf("Enter first triangle's breadth: ");
scanf("%d", &base1);
printf("Enter first triangle's height: ");
scanf("%d", &height1);
printf("Enter second triangle's breadth: ");
scanf("%d", &base2);
printf("Enter second triangle's height: ");
scanf("%d", &height2);
//Call function to compare area of 2 triangles
Compare(base1, height1, base2, height2);
return 0;
}
/*
Implementation of compare function
Take 2 triangles base and height as parameters
Return none
*/
void Compare(int b1, int h1, int b2, int h2) {
int A1=Area(b1, h1);
printf("Area1=%d\n", A1);
int A2=Area(b2, h2);
printf("Area2=%d\n", A2);
if (A1>=A2)
printf("Area1 is greater than
Area2.\n");
else
printf("Area2 is greater than
Area1.\n");
}
/*
Function to find area of triangle
Take base and height of triangle as input
Return area
*/
int Area(int b, int h) {
int A=(b*h)/2;
return A;
}