Question

In: Computer Science

Our MIPS assembly version will “compile” the following C program: #include void Compare(int b1, int h1,...

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).

Solutions

Expert Solution

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;
}


Related Solutions

Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int...
Write the MIPS assembly version of this C code: int weird(char[] s, int x) { int i; for (i = 0; i < x; i++) { if (s[i] == ‘A’) { return power(10, i); } } return -1; } int power(int base, int i) { int j = 0; while (j < base) { base = base * base; j++; } return base; }
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void...
Write the below C program using ARM assembly language and compile for Cortex A53. #include<stdio.h> void quicksort(int number[25],int first,int last){ int i, j, pivot, temp; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(number[i]<=number[pivot]&&i<last) i++; while(number[j]>number[pivot]) j--; if(i<j){ temp=number[i]; number[i]=number[j]; number[j]=temp; } } temp=number[pivot]; number[pivot]=number[j]; number[j]=temp; quicksort(number,first,j-1); quicksort(number,j+1,last); } } int main() { int i, count, number[25]; printf("Enter some elements (Maximum 25): "); scanf("%d",&count); printf("Enter %d elements: ", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1); printf("The Sorted Order is: "); for(i=0;i<count;i++) printf(" %d",number[i]); return...
convert following C++ code into MIPS assembly: int main() {                                 &
convert following C++ code into MIPS assembly: int main() {                                         int x[10], occur, count = 0;                                                              cout << "Type in array numbers:" << endl; for (int i=0; i<10; i++) // reading in integers                               { cin >> x[i];        } cout << "Type in occurrence value:" << endl;                                 cin >> occur;                                                 // Finding and printing out occurrence indexes in the array                                  cout << "Occurrences indices are:" <<...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
Compile the following C code to MIPS assembly. a. Assume that i and j are stored...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j; b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I; Explain each step in detail! my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2 2) lw $t0, 0 ($s3)...
Translate the following C code to MIPS assembly. int a = 1; int b = 2;...
Translate the following C code to MIPS assembly. int a = 1; int b = 2; if (a<b)           a=a+1; b = b + a; printf("The value of b is: %d", b); Translate the following C code to MIPS assembly. int a = 2; int b = 2; if (a<b)           a=a+1; else           a=a-1; b = b + a; printf("The value of b is: %d", b);
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT