Question

Question 1. Assume the following C program and You are writing code using SPIM to implement this program.

int a [4], b[4], dot-prod, i; .............. dot_prod = 0; for (i=0; i<4; i++) dot_prod += a[i]*b[i];

lw reg, offset Load word from variable into reg lw reg, (addr) Load word from address in register into reg la register,addr L

You begin as follows: .data a: b: dot-prod: . word 5,6,7,8 .word 4,3,2,1 .word 0 .word 0 # array of 4 word-size elements # ar

Write the results of each of the following in MIPS instructions. Use registers $t0 - $t7 as needed, Note: int requires 8 bytes

1. Load the contents of dot_prod and i into registers
2. Load the contents of a[i] and b[i] into registers
3. Calculate dot_prod + a[i]*b[i] and save the result into dot_prod

please answer each point from (1,2,3) in separated like this :
1.
2.
3.
to help me to understand

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

Please up vote ,comment if any query i will answer , Thanks.

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

Note : in Answer label i answered all your 1,2,3 questions.

Program Plan :

  1. load address of dot_prod and i variable in register and from register get its value.
  2. load address of array A and B into registers
  3. run a loop in loop label using branch instruction where till i less than 4
  4. multiply a[i] and b[i] and multiply add into dot_prod value
  5. after loop store dot_prod value into its RAM location and print value.

Program ;

.data
   a: .word 5,6,7,8 #array A
   b: .word 4,3,2,1 #array B
   dot_prod : .word 0 #int variable dot_prod
   i: .word 0 #int variable i
.text
   .globl main
   main:
       la $t0,dot_prod #load address of dot_prod in $t0
       lw $t7,($t0) #load value at address stored in $t0
       la $t1,i #load address of i in $t1
       lw $t1,($t1) #load value at address stored in $t1 and store value in $t1(to reduce register usage)
       la $t2,a #load address of Array A in $t2
       la $t3,b #load address of Array B in $t3
  
   loop:
       #branch to exit if $t1 equal to 4
       beq $t1,4,exit   
       lw $t4,($t2) #load int value (word) from location store in $t2 iton $t4
       lw $t5,($t3) #load int value (word) from location store in $t3 into $t5
      
       mul $t6,$t4,$t5 #$t6= a[i]*b[i]
       add $t7,$t7,$t6 #$t7=$t7+$t6
      
       addi $t2,$t2,4 #go to next location array A
       addi $t3,$t3,4 #go to next location array B
       addi $t1,$t1,1 #increment i i=i+1
       #jump to loop label
       j loop
   exit:
       #store dot_prod at its location stored in $t0
       sw $t7,($t0)
      
       #again load address of dot_prod in $t1
       la $t1,dot_prod
       #load value from address of dot_prod store in $t1
       lw $t2,($t1)
      
       #print value of dot_prod
      
       li $v0,1
       #move $t2 value into $a0 to print dot_prod value
       move $a0,$t2
       syscall
       #terminate program
       li $v0,10
       syscall
      
Answer :

1. Load the content of dot_prod and i into registers

la $t0,dot_prod #load address of dot_prod in $t0
       lw $t7,($t0) #load value from  address stored in $t0
       la $t1,i #load address of i in $t1
       lw $t1,($t1) #load value from  address stored in $t1 and store value in $t1(to reduce register usage)

2.  Load the contents of a[i] and b[i] into registers

loop:

#$t2 have location of Array A

#$t4 have content of a[i] $t4=a[i]

#$t3 have location of Array B

#$t5 have content of $t5=b[i]


       lw $t4,($t2) #load int value (word) from location store in $t2 into $t4
       lw $t5,($t3) #load int value (word) from location store in $t3 into $t5
      
#go to next index value by adding 4 to its address

#word in MIPS takes 4 bytes so add 4

  
           addi $t2,$t2,4 #go to next location array A
       addi $t3,$t3,4 #go to next location array B

3. Calculate dot_prod + a[i]*b[i] and save the result into dot_prod

#mul multiply a[i]*b[i ] and store into $t6

#add instruction add $t7 and $t6 and store it into $t7

#$t7 have value of dot_prod variable

mul $t6,$t4,$t5 #$t6= a[i]*b[i]
       add $t7,$t7,$t6 #$t7=$t7+$t6
      
#now store value into dot_prod variable RAM location

#$t0 have address of dot_prod and sw store $t7 value into location at store in $t0

#store dot_prod at its location stored in $t0
       sw $t7,($t0)

Output :

Mars Messages Run 110 Clear |-- program is finished running --

Please up vote .comment if any query . Thanks.

Add a comment
Know the answer?
Add Answer to:
Question 1. Assume the following C program and You are writing code using SPIM to implement...
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
  • 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...

  • Implement the following statements using MS430 assembly instructions. You may use more than one, ...

    Implement the following statements using MS430 assembly instructions. You may use more than one, but you should minimize the number of instructions required. You can use both native and emulated instructions. Use hex notation for all numbers 1. (a) Move the word located in register R14 to R15 (b) Increment the word in R6 by 2. (c) Perform a bitwise ANDing of the word located at address 0x0240 with the datum in R15, placing the results in R15. (d) Rotate...

  • Assignment 3 Translate the following MIPS code to C. Assume that the variables f, g, h,...

    Assignment 3 Translate the following MIPS code to C. Assume that the variables f, g, h, i and j are assigned to registers Ss0, Ss1, Ss2, Ss3 and Ss4, respectively. Assume that the base address of the arrays A and B are in registers Ss6 and $s7, respectively. addi St0, Ss6, 4 add $t1, $s6, $0 #register $0 always holds 320s sw St1, 0(Sto) add Ss0, St1, Sto

  • The following C program fill in an array C with the larger elements from another two...

    The following C program fill in an array C with the larger elements from another two arrays A and B. Convert the code to its RISC-V assembly code. For reference to array elements A[i], you can use A(i) as its base+offset representation in the LW/SW instruction, e.g. to load an integer from A[i], you can use LW x5, A(i); Use register t0 and t1 for storing i and N respectively. int i; for (i=0; i < N; i++) {    ...

  • QT Spim question. Program and answer already given please explaine it. Please explain the reason why...

    QT Spim question. Program and answer already given please explaine it. Please explain the reason why each instruction was used throughout the program step by step given the prompt, what is its purpose to achive the goal asked in the prompt, why is written in that order. Please not just write to the side what each instruction means in words like load $t0 to $t1. I want to understand the program thoroughly. Thanks in advance Previous information needed to solve...

  • 1.) a.) Using the simplified instruction set shown for part b, write code for the following....

    1.) a.) Using the simplified instruction set shown for part b, write code for the following. Suppose memory locations 1400 to 1449 contain 16-bit words. Each word represents 2 ASCII characters. Write code to read in and write out these 100 characters. Left-side character from location 1400 should be first, right-side character from location 1400 should be second, and remaining characters follow in numeric order. Assume you have access to 4 registers: R1, R2, R3, R4. Each register holds one...

  • 1. (15 pts) For the following C statement, what is the corresponding MIPS assembly code? Assume...

    1. (15 pts) For the following C statement, what is the corresponding MIPS assembly code? Assume f, g, h correspond to $80, $s1, and $s2, respectively. f=g+(h-5) 2. (15 pts) For the following pseudo-MIPS assembly instructions, what is the corresponding C code? add f, g, h add f,i, f 3. (30 pts) Provide the instruction type, assembly language instruction, and binary representation of the instruction described by the following MIPS fields: a. op = 0, rs = 18, rt=9, rd...

  • Group Project 1 The Micro-1 Processor Simulation <Micro-1 Computer> Here's the organization of a computer equipped...

    Group Project 1 The Micro-1 Processor Simulation <Micro-1 Computer> Here's the organization of a computer equipped with a Micro-1 processor Memory contains an array of integer cells: int cell[] = new int[CAP]; where CAP is the capacity of memory. Initially this is set to 256. Internally, the Micro-1 processor is equipped with eight 32-bit data/address registers and two 32 bit control registers: PC, the program counter, contains the address of the next instruction to execute. IR, the instruction register, contains...

  • 1. Compile the following C program using the most effective DLX code, similar to the example give...

    1. Compile the following C program using the most effective DLX code, similar to the example given in the class. int A[200], B[200], C[200]; ..... for(i=0;i<200;i++) A[i]=B[i]+C[i]*5; Assume array A, B and C each starts at memory location x1000, x2000 and x3000, respectively. You will need to establish each of these addresses in a register (use r1, r2 and r3 for each of the three) before the loop. Also, you need to establish a counter register for “i” (use r4)...

  • Note: The question needs to be answered in "C Programming Languange ". And after the question fin...

    Note: The question needs to be answered in "C Programming Languange ". And after the question find 3 pages for needed informations. Spring CE4717 Language Processors Q1. Consider the following LEx program. return R1 return R2 return R3 return R4 return R5; return R6; IA-2a-z)[A-Za-z0-9]- -2 10-91+ 10-9a-EA-FI Ihi] [01] [01] 이삐 t Vtin) int main (void) int tcode; do f tcode -yylex()i printf ("token type td \"%s\"\n", tcode, yytext); ) while (tcode)i return 0; i. Explain the steps needed...

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
ADVERTISEMENT