In: Computer Science
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
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 :
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 .