Question

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++) {

    if (A[i] >= B[j]) C[i] = A[i];

    else C[i] = B[i];

}

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

The equivalent RISC assembly level language for the question is:

li $t0,N

li $t1,0

loop_start:

beq $t1,$t0,loop_end

lw $t2,A($t1)

lw $t3,A($t1)

beq $t2,$t3,else

lw C($t1),$t2

else:

lw C($t1),$t3

addi $t1,$t1,1

loop_end:

In RISC assembly level language the value of N is stored in t0 register and the register t1 is initialized with 0 which represents the iterator i. Then the loop begins with loop_start and the value of t0 and t1 register will be kept comparing until it reaches to the condition and it ends with loop end. In the body of the for loop two more registers are used to store the value of the arrays which are t2=A[i] and t3=B[i]. Then the value is compared with beq and if it is true C[i] loads the value of A[i] which is stored in t2 register and done by the lw command. If the above condtiion is not satisfied the control goes to the else part where the C[i] is loaded with t3 register. The value of t1 register is incremented by addi with 1 after each iteration.

Add a comment
Know the answer?
Add Answer to:
The following C program fill in an array C with the larger elements from another two...
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
  • MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First...

    MIPS MIPS MIPS PLEASE INCLUDE COMMENTS AND OUTPUT Sort array using Bubble sort algorithm. 1) First ask the user how many elements of his/her array. 2) Then, read the integer array elements as input from the User. 3) Then, print out the array before the sorting 4) Apply Bubble sort algorithm on your array 5) Print out the array after the sorting 6) Print some welcome text to th user 7) Add comments to your code to describe how is...

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

    Question 1. Assume the following C program and You are writing code using SPIM to implement this program. 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...

  • For Computer Organization and Design: Suppose we have 3 arrays A, B, and C, array A’s...

    For Computer Organization and Design: Suppose we have 3 arrays A, B, and C, array A’s base address is in $S3, B’s base address is in $S4, C’s base address is in $S5. You can only use two temporal registers $t0 and $t1. Convert the following code into MIPS assembly language code. Suppose every array element occupies 1 word in MIPS (32 bits). A[20] = B[0] + C[6] - 1

  • I AM POSTING MY QUESTION 3RD TIME . FIRST TWO TIMES I DIDNOT GET THE PROPER...

    I AM POSTING MY QUESTION 3RD TIME . FIRST TWO TIMES I DIDNOT GET THE PROPER ANSWER. PLEASE DO ALL STEPS BY LABELING THE EACH STEP. THIS IS THE ASSIGNMENT IN WHICH THE EACH STEP SHOULD BE LABEL . I MEAN EVERY THING SHOULD BE WRITTEN WHAT WE HAVE DONE IN OUR PROGRAM THIS IS THE REQUIREMENT OF TEACHER , PLEASE DO IT PROPERLY   1) MIPS to C. Assume that the variables f, g, h, i, and j are assigned...

  • Please answer the following questions involving MIPS assembly code: A) For the C statement below, what...

    Please answer the following questions involving MIPS assembly code: A) For the C statement below, what is the corresponding MIPS assembly code? Assume f, g are stored in S1 and S2. f = g + (-f -5) B) For the C statement below, what is the corresponding MIPS assembly code? Assume i and j are assigned in registers S1 and S2 respectively and base of address of arrays A and B are in registers S6 and S7. B[8] = A[i...

  • MIPS Insertion program.........I could really use some help ASAP

    I have this MIPS program and I'm having trouble with it. This program is user inputs numbers until zero and sorts and print the numbers in order. Please soove this issue. You can use any sorting algorithm except bubble sort.  Need it as soon as possible. Here is the code:.datanum: .word 0space: .byte ' ' .text main:  # la $t0, val # loads val into a register  # li $t1, 0      #keeps track of how many numbers entered  la $a0,...

  • For the following C statement, what is the corresponding MIPS assembly code? Assume the arrays hold...

    For the following C statement, what is the corresponding MIPS assembly code? Assume the arrays hold 16-bit integer values, $ s0 is the base for array X, $ s1 is the base for array Y, and $ t0 and $ t1 are index variables i and j respectively. Comment code X [j] = Y [i + j];

  • The task will be to write a program in assembler to enter a number and calculate...

    The task will be to write a program in assembler to enter a number and calculate its associated Fibonacci number using a procedure (subroutine) that is called recursively. Factorial and Fibonacci(outline Programs) # MIPS assembly assembly assemblyassemblycode .data n: .word 4 .text main: la $s0,n lw $a0, 0($s0) jal factorial # move what ever is returned into $a0 move $a0, $v0 li $v0,1 syscall b finished factorial: add $sp , $sp , -8 # make room sw $a0, 4($sp )...

  • 2.7 5<S$2.2, 2.3> Translate the following C code to RISC-V. Assume that the variables f, g, h, i, and j are assigned to registers x5, x6. x7. x28, and x29, y. Assume that the base address of th...

    2.7 5<S$2.2, 2.3> Translate the following C code to RISC-V. Assume that the variables f, g, h, i, and j are assigned to registers x5, x6. x7. x28, and x29, y. Assume that the base address of the arrays A and B are in registers x10 and x11, respectively. Assume that the elements of the arrays A and B are 8-byte words: 2.7 5 Translate the following C code to RISC-V. Assume that the variables f, g, h, i, and...

  • Using beq only, not bge! Translate the following C code to MIPS assembly code. Use a...

    Using beq only, not bge! 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 $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds the base address of the integer array D. Comments are required. for(i=1; i<a; i++) for(j=1; j<b; j++) D[2*j] = i + j;

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