Question

In: Computer Science

I'm having trouble understanding a CS assignment. I would appreciate it if you all code do...

I'm having trouble understanding a CS assignment. I would appreciate it if you all code do this for me. The template for the lab is below which you must use. You're only supposed to create/edit the product function. The assignment has to be written in asm(Mips)

You will need to create a NOS table and use the structure below and write a function called product.

The structure used in this program is:

struct Values {
   short left;
   short right;
   int result;
};

The product function has the following C++ prototype:

int product(Values values[], int num_values);

This function will take an array of Values structures and multiply every Value structure's left times right and store the result in the result field. You will be doing this for num_values number of elements.

This is why the structure is NOT constant. It will be modified in place. Finally, when this function is done, it will return the sum of all results. Make sure that your function does not use any global variables.

Testing

As always, I recommend you write your logic in C++, then translate. If you ask for help with the TAs (or me), we may ask you to show us your logic. Do not change the prototype and do not change the Values structure.

Template:

.data
values:
   # element 0
   .half  20   # short left
   .half  3    # short right
   .word  0    # int result
   # element 1
   .half  7    # short left
   .half  5    # short right
   .word  0    # int result

.text
j  main
product:
   # Write your product function here

main:
   la $a0, values
   li $a1, 2
   jal product

   # Print out the sum
   move $a0, $v0
   li $v0, 1
   syscall
   # Put a newline character to separate
   li $v0, 11
   li $a0, '\n'
   syscall

   # Print out the product of the first element
   li $v0, 1
   la $a0, values
   lw $a0, 4($a0)
   syscall
   # Put a newline character to separate
   li $v0, 11
   li $a0, '\n'
   syscall

   # Print out the product of the second element
   li $v0, 1
   la $a0, values
   lw $a0, 12($a0)
   syscall
   # Put a newline character to separate
   li $v0, 11
   li $a0, '\n'
   syscall

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for question .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .

Program Plan :

  1. run a loop from 0 to num_values
  2. load half word in register
  3. load upper half word in register
  4. multiply both half word
  5. add sum
  6. after loop ends return sum value

Program :

.data
values:
# element 0
.half 20 # short left
.half 3 # short right
.word 0 # int result
# element 1
.half 7 # short left
.half 5 # short right
.word 0 # int result

.text
   j main #call main function

product:
# Write your product function here
li $t0,0 #loop index $t0=0
li $v0,0 #sum=0=$v0
loop:#loop label from 0 to num_values
   lh $t1,0($a0) #load half word from lower 16 bit into $t1
   lh $t2,2($a0) #load half word from upper 16 bit into $t2
   mul $t3,$t1,$t2 #multiply right and left $t3=$t1*$t2
   sw $t3,4($a0) #store multiply value in structure word place word= left*right
   add $v0,$v0,$t3 #sum=sum+$t3(left*right)
   addi $t0,$t0,1 #increment loop count or index
   addi $a0,$a0,8 #point to next structure address
    bne $t0,$a1,loop #if loop index not equal to num_values jump to loop label
   #else $t0==$a1 return sum and go to address from function called
   #address saved in $ra register
   jr $ra
#main function
main:
la $a0, values #address of values structure
li $a1, 2 #size of structure array
jal product #call product function

# Print out the sum
move $a0, $v0
li $v0, 1
syscall
# Put a newline character to separate
li $v0, 11 #syscall 11 to print character
li $a0, '\n'
syscall

# Print out the product of the first element
li $v0, 1
la $a0, values
lw $a0, 4($a0)
syscall
# Put a newline character to separate
li $v0, 11 #syscall 11 to print character
li $a0, '\n'
syscall

# Print out the product of the second element
li $v0, 1
la $a0, values
lw $a0, 12($a0)
syscall
# Put a newline character to separate
li $v0, 11 #syscall 11 to print character
li $a0, '\n'
syscall

Output :

Please up vote ,comment if any query .


Related Solutions

I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
I'm having difficulty understanding these questions. I would appreciate the answer with an explanation, thank you...
I'm having difficulty understanding these questions. I would appreciate the answer with an explanation, thank you ! Question 1 A one-year discount bond issued by X has a payout of $550 and today's price is $510. A one-year discount bond issued by Y has a payout of $1,290 and today's price is $1,155. Then the bond issued by X has a ____ yield than the bond issued by Y, and this could be because X has a ____ default risk...
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
If anyone could simplify this for me. I'm having trouble understanding the material and I just...
If anyone could simplify this for me. I'm having trouble understanding the material and I just need a keep it simple stupid approach Discuss the various non influential as well as influential investments that company may have on their financial statements. Also compare and contrast how they are treated/recorded on the companies financial statements.
hello, I'm having trouble understanding how to do these two problems could you show me a...
hello, I'm having trouble understanding how to do these two problems could you show me a step by step. 1)Eight sprinters have made it to the Olympic finals in the 100-meter race. In how many different ways can the gold, silver, and bronze medals be awarded? 2)Suppose that 34% of people own dogs. If you pick two people at random, what is the probability that they both own a dog? Give your answer as a decimal (to at least 3...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
I am currently having trouble understanding/finding the errors in this python code. I was told that...
I am currently having trouble understanding/finding the errors in this python code. I was told that there are 5 errors to fix. Code: #!/usr/bin/env python3 choice = "y" while choice == "y": # get monthly investment monthly_investment = float(input(f"Enter monthly investment (0-1000):\t")) if not(monthly_investment > 0 and monthly_investment <= 100): print(f"Entry must be greater than 0 and less than or equal to 1000. " "Please start over.")) #Error 1 extra ")" continue # get yearly interest rate yearly_interest_rate = float(input(f"Enter...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the...
We are learning about Ramayana in Mythology and I'm having a bit of trouble understanding the story. Why would Rama be set apart as a true hero? The story of Rama and Sita is a favorite story that parents tell their children. What purpose does the Ramayana serve as an instructional story for Indian culture? What effect do the test and temptations have on the heroic character?
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT