Translate the following Code into MIPS (.asm) Arrays and Reentrant Subprograms CallingSubroutinesDemo.asm Implement the following pseudo code in a...

90.2K

Verified Solution

Question

Programming

Translate the following Code into MIPS(.asm)

Arrays and Reentrant Subprograms

CallingSubroutinesDemo.asmImplement the following pseudo code in a program called SubRoutinePractice.asm. Be sure to follow proper protocol forcalling subroutines. Implement a prolog and epilog.// Main routine asks for three numbers from the user. Stores them in $s0, $s1 and $s2// It then calls a subroutine that determines the largest and sum.// Program then prints the resultsmain() {  // $s0, $s1 and $s2 hold input values  // $s3 and $s4 hold largest and average respectfully  // Print(“Enter 2 values”);  $s0 = readInt();    $s1 = readInt();  $s2 = readInt();  $a0 = $s0, $a1 = $s1, $a2 = $s2;  $s3, $s4 = largestAndAverage($a0, $a1, $a2);  // Print out “Largest is: “ + $s3  // Print out “The average is: “ + $s4}// This subroutine receives three arguments. It calls getLarger to// find the largest and then calculates the average// It returns largest and average in $v0 and $v1// Note: Since we are reusing $s0, $s1 and $s2, we MUST push those// registers onto the stack in the prolog and restore the values in the epilog// Don't forget the $ra!!Subprogram largestAndAverage($a0, $a1, $a2){  int $s0 = $a0, $s1 = $a1, $s2 = $a2;  $s3 = getLarger($a0, $a1);  // I need to move $s0 to $a0 and $s1 to $a1  $v0 = getLarger($a0, $a1);  // Move $s3 to $a0 and $s2 to $a1  $v1 = ($s0 + $s1 + $s2) / 3; // Average is in $v1  return;}// This program does stuff.Subprogram getLarger($a0, $a1) {  $v0 = $a0  if ($a1 > $a0)    $v0 = $a1  return;}

# Demonstrate calling subroutines
# We walked through this code on 10/13 to help with the homeworkassignments. Just a simple
# demonstration of calling subroutines.

.data
Prompt: .asciiz \"This is an unused prompt\"
  
.text
.globl main
main:
# Random numbers. Should get from user
li $s0, 4
li $s1, 5
li $s2, 6
  
# Want to call a subroutine. I need to move my variables in $s0,$s1 and $s2 into
# the registers that are ALWAYS used to pass data to asubroutine
  
move $a0, $s0 # Move my data to three by five cards and hand it tohim.
move $a1, $s1   # I don't want to give him my cards,because he could mess them up.
move $a2, $s2
  
jal someStupidSubroutine
  
# when you get here, you can only be guaranteed that $s0, $s1 and$s2 are the same
move $s3, $v0       # Because subroutinesALWAYS return the results in $v0
  
move $a0, $s3
li $v0, 1
syscall
  
li $v0, 10
syscall

# ------------------------------------------------
# Each subroutine can have its own .data and .text
# We are simply telling the assembler what the code is usedfor.
.data
s1Prompt: .asciiz \"This is not used\"

.text
someStupidSubroutine:
# Prolog - You have to do the following
# 1. You have to save any data that MUST be preserved on thestack
# This includes the $ra register and any $s registers you are goingto use
# 2. To do that, you need to make room. Adjust the $sp
# 3. Push your data onto the stack
# Assume in \"someStupidSubroutine\" I am going to use $s0 and$s1
# I need room for 3 words. $ra, $s0 and $s1
addi $sp, $sp, -12       # I just movedthe $sp down 12 bytes. So now I have room
# above the stack pointer to store information
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)

# Logic
   add $s0, $a0, $a1
   add $s1, $s0, $a2        #this is dumb but I'm doing it.
  
   move $a0, $s0          # You have to move stuff around to ensure it isin the right place
   move $a1, $s1
   jal someStupidSubroutine2
   move $v0, $v0       # Moveresults from someStupidSubroutine2 into $v0 (Not needed here,but)
                  # if you had done other workbefore the routine, you need to move the results)

# Epilog
# 1. Push your return value to $v0
# 2. Put the candle back. i.e. You need to put the information youstored back where it was
   lw $s1, 8($sp)
   lw $s0, 4($sp)
   lw $ra, 0($sp)          # I need to restore the return address
   addi $sp, $sp, 12
jr $ra

# ---------------------------------------------------
.data

.text
someStupidSubroutine2:
# If this doesn't call any other subroutines or modify any $sregisters, it doesn't need
# an epilog and prolog
# Every subroutine definitely needs a .text. It will only need a.data if it has static data
# Everything else is local and goes away when the subroutine does ajr $ra
add $v0, $a0, $a1
jr $ra

  
  
  

Answer & Explanation Solved by verified expert
4.4 Ratings (979 Votes)
Please find the answer belowPlease do comments in case of any issue Also dont forget to ratethe question Thank You So Much Demonstrate calling subroutines We walked through this code on 1013 to help with the homeworkassignments Just a simple demonstration of calling subroutinesdatadataprompt asciiz Enter 3 values belownprompt1 asciiz    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students