Question

Write a MIPS assembly code that corresponds to the following C code. Note: use the stack...

Write a MIPS assembly code that corresponds to the following C code. Note: use the stack to store all register values that you use in the procedures.

int aver(int * array, int N){

int i, sum = 0;

for ( i=0;i i<N; i++)

sum += array[i];

return sum/N;}

int Max( int * array, int N){

int i, Maximum = array[i];

for ( i = 1; i< N; i++)

if ( array[i] > Maximum)

Maximum = array[i];

return Maximum; }

int main ( ){

int average=0, max =0;

int A[10]; // suppose A is initialized

average = aver(A);

max = max(A);

return 0; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM:

.data
   A: .word 2, 8, 64, 57, 29, 75, 61, 97, 34, 20
   out1: .asciiz "Average of array elements is: "
   out2: .asciiz "\nMaximum number in array is: "
.text
.globl main
main:
   li $v0,4       # 4 is the print string syscall
   la $a0,out1       # Load the address of string to be printed
   syscall           # Print the string
  
   la $a0,A       # Load the address of array
   li $a1,10       # Load the size of array
   jal aver       # Call the function aver
   # store the return value from function
   move $a0,$v0       # Load $a0 with integer to be printed
   li $v0,1       # 1 is the print integer syscall
   syscall           # Print the integer
  
   li $v0,4       # 4 is the print string syscall
   la $a0,out2       # Load the address of string to be printed
   syscall           # Print the string
  
   la $a0,A       # Load the address of array
   li $a1,10       # Load the size of array
   jal max           # Call the function max
   # store the return value from function
   move $a0,$v0       # Load $a0 with integer to be printed
   li $v0,1       # 1 is the print integer syscall
   syscall           # Print the integer
  
   li $v0,10       # 10 is the exit program syscall
   syscall           # Exit the program

# Definition of average function
aver:
   # Adjust the stack pointer
   addi $sp,$sp,-8
   sw $s1,4($sp)       # Store $s1 on stack
   sw $ra,0($sp)       # Store return address on stack
  
   li $t0,0       # Loop counter (i = 0)
   li $t3,0       # Accumulator (sum = 0)
for1:   bge $t0,$a1,end1   # If loop counter >= size of array, jump to label end1
   mul $t1,$t0,4       # Calculate the offset index i
   add $t1,$a0,$t1       # $t1 = &A[i]
   lw $t2,0($t1)       # $t2 = A[i]
   add $t3,$t3,$t2       # sum = sum + A[i]
   addi $t0,$t0,1       # Increment loop counter
   b for1           # Repeat the loop
end1:   div $v0,$t3,$a1       # $v0 = sum/N
   lw $s1,4($sp)       # Restore $s1 from stack
   lw $ra,0($sp)       # Restore $ra from stack
   addi $sp,$sp,-8       # Readjust stack pointer to its original position
   jr $ra           # Jump to return address (i.e., to calling function, main)

# Definition of max function
max:
   # Adjust the stack pointer
   addi $sp,$sp,-8
   sw $s1,4($sp)       # Store $s1 on stack
   sw $ra,0($sp)       # Store return address on stack
  
   li $t0,1       # Loop counter (i = 0)
   lw $t1,0($a0)       # $t1(max) = A[0]
for2:   bge $t0,$a1,end2   # If loop counter >= size of array, jump to label end1
   mul $t2,$t0,4       # Calculate the offset index i
   add $t2,$a0,$t2       # $t2 = &A[i]
   lw $t3,0($t2)       # $t3 = A[i]
   ble $t3,$t1,next   # If A[i] <= max, jump to label next
   move $t1,$t3       # Else max = A[i]
next:   addi $t0,$t0,1       # Increment loop counter
   b for2           # Repeat the loop
end2:   move $v0,$t1       # return the value of max
   lw $s1,4($sp)       # Restore $s1 from stack
   lw $ra,0($sp)       # Restore $ra from stack
   addi $sp,$sp,-8       # Readjust stack pointer to its original position
   jr $ra           # Jump to return address (i.e., to calling function, main)

Please refer to the following screenshot of the program for indentation of the code:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a MIPS assembly code that corresponds to the following C code. Note: use the stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4,...

    MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4, 5, 6); int main) int num, position; scanf("%d",&num) ; position search(array, printf("The position is: num, 5); %d\n",positio int search(int array, int num, int size int position =-1; for(int i-0;i<size; i++) if(array [i]=num) { position-i; break; return position; Register map $s1: position $a0: array address $a1: num . $a2: size . $VO: return value

  • its brr[8] (40%) Convert the following C-pseudo code into MIPS assembly code as a standalone program...

    its brr[8] (40%) Convert the following C-pseudo code into MIPS assembly code as a standalone program (including main and all the required directives). You can use any register. You must comply, however, with the convention of register usage. Before writing your code perform an explicit register allocation phase. Note that the C snippet is int arr[8]; int brr[4]-{1, 2, 3, 4, 5, 6, 7, 8) int i-8; while (i>-0) arrli]-brr[i-); (40%) Convert the following C-pseudo code into MIPS assembly code...

  • what is the corresponding MIPS assembly code for the statements shown below that written in C....

    what is the corresponding MIPS assembly code for the statements shown below that written in C. Assume that the variables sum and n assigned to register $t1 and $t2 respectively, and the base address of the array A is in register $t0. int sum = 0 for(n=100 ; n>=0; n=n-5) { if(A[n]>0 sum = a[n] +3 }

  • IN MIPS AND MUST RUN IN QTSPIM Translate the following C code to MIPS assembly code....

    IN MIPS AND MUST RUN IN QTSPIM Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i and j are stored in registers Ss0, Ss1, St0 and Stl, respectively. Also assume that register Ss2 holds the base address of the array D. for (i=0; i<a; itt) for (i-0j<b:jt+)

  • 7. Translate the following C code to MIPS assembly code. Use a minimum number of instructions....

    7. Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a,b, i and j are in registers Ss0, Ss1, St0, and St1, respectively. Also, assume that register SS2 holds the base address of the array D. for(i-0; i<a; i++) for(j=0 ; j<b; j++)

  • B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: ...

    B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: timer-v0 int timer = 0x0AC8 0001; B3. Write MIPS assembly code segment for the following C code snippet for (i - 0, i < 100; i++) -array Register assignment: i-) $ao Base of array -> $s0 array [ i+1] [i] / 2; B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: timer-v0 int timer = 0x0AC8 0001; B3. Write...

  • In mips code please Implement the following C code in MAL and develop a small main...

    In mips code please Implement the following C code in MAL and develop a small main program that tests your implementation on an integer array. You must use nested procedures in your implementation. int getArrAvg (int Arr) int sum0 for (int i = 0; i < 10; i += 1) sum-addfn (sum, Arr[i]) return sum/10; int addfn (int a, int b) return ab;

  • C3. Convert the following C-code to MIPS code. [Use register $al for the variable i, temporary re...

    C3. Convert the following C-code to MIPS code. [Use register $al for the variable i, temporary registers for other values, and load the base memory address of the array OxA0000080 to Şao] int i i int array [101; for (i= 0; i<5 ; i=i+1 ) { [i+1] [i] 8; * array = array C3. Convert the following C-code to MIPS code. [Use register $al for the variable i, temporary registers for other values, and load the base memory address of...

  • 6. Write the MIPS assembly code that corresponds to the pseudo code below. Assume that the...

    6. Write the MIPS assembly code that corresponds to the pseudo code below. Assume that the address for integer i is baseaddress+4 and the address for a[0] is baseaddress+8. Assume that the baseaddress is stored in $gp. The code initializes i to O; it then iterates from i-0 to i-9, setting a4i in each iteration. To make your code efficient, i must be loaded into a register at the start, and it must be updated in memory only after you've...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT