Question

A) The following code fragment takes as input a number in t1 and produces a value in register $s2. Assume that initially $s2v (7 marks) Translate the shown C code into MIPS Assembly using minimum number of instructions. void swap (int v[], int k) in

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

In the given program, Loop2 runs for 10 iterations and in each increment s2 by 2.

Hence after Loop2 has run s2 is incremented by 20.

Loop2 is run t1 number of times.

Hence the program return the value of s2 = 20*t1.

Based on the above facts it is easy to find the answers for the given questions as follows:

i)

a) $t1 = 5

Answer = 20*5 = 100

b) $t1 = 10

Answer = 20*10 = 200

c) $t1 = 50

Answer = 20*50 = 1000

ii) $t2 is the counter register for Loop2. Loop2 stops when $t2 = 0.

Hence for each case, the answer is 0.

a) $t1 = 5

Answer = 0

b) $t1 = 10

Answer = 0

c) $t1 = 50

Answer = 0

iii) Loop2 is executed 10 times and has 3 instructions.

Hence each execution of the subroutine Loop2 executed 30 instructions.

The Loop subroutine executes 3 instructions and the Loop2 subroutine.

Sine the Loop subroutine is executed $t1 = N no. of times, the total instructions executed = (3 + 30)N = 33N

iv) The time taken to execute 33N instructions when each instruction takes 1 clock cycle = 33N/clock rate = 33N / 2.5 ns

i) $t1 = 5

Time Taken = 33*5/2.5 = 66 ns

ii) $t1 = 10

Time Taken = 33*10/2.5 = 132 ns

iii) $t1 = 50

Time Taken = 33*50/2.5 = 660 ns

v) Here is the MIPS code for the given C Program. I have added comments for better understanding:

swap:
# $a0: base address of v
# $a1: k
sll $t0,$a1,2 # Multiply by 4
addi $t0,$t0,$a0 # FInd address of v[k]
lw $t1,0($t0) # Load v[k]
addi $t2,$t0,4
lw $t3,0($t2)
sw $t3,0($t0)
sw $t1,0($t2)

Screenshot of the code:
swap: # $a0: base address of v # $al: sll $t0, $al, 2 # Multiply by 4 addi $t0,$t0, $aD # FInd address of v[k] lw $t1,0 ($to)

You can comment below the answer, for any doubts, and I will be happy to help!

Please give a thumbs up if my answer could be of help!

All the best!

Add a comment
Know the answer?
Add Answer to:
A) The following code fragment takes as input a number in t1 and produces a value...
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
  • 2. The table below holds MIPS assembly code fragments with different branch instructions LOOP addi $s2....

    2. The table below holds MIPS assembly code fragments with different branch instructions LOOP addi $s2. $s2. 2 subi $t1. st1. 1 bne t1. 0. LOOP DONE: LOOP: it st2. $0. stl beq t2. 0. DONE addi $s2. Ss2. 2 LOOP DONE: For the loops written in MIPS assembly in the above table, assume that the register Şt1 is initialized to the value of 10. What is the value in register $s2 assuming that $s2 initially has a value of...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • 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...

  • . U 8. (15 pts) Assuming the code fragment below is executed by the classic 5-stage...

    . U 8. (15 pts) Assuming the code fragment below is executed by the classic 5-stage MIPS architecture, answer the following questions. Ll: lw RI, O (R5) // 11 add R2, Ri, 20 // 12 lw R3, 0(R6) // 13 add R3, R2, R3 SW R3, 0(R5) addi R4, R4,-4 // 16 addi R5, R5,4 // 17 addi R6, R6,4 // 18 bne R4, RO, L1 // 19 a. (5 pts) Assuming R5 holds the address of array A, R6...

  • im trying to complete mips program code about a calculator program that can calculate integer addition...

    im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler. im having hard times to debug this. The input is given to the array of Formula char (base address $ s0) in the form of a formula. The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored. For example,...

  • Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here...

    Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here is the code, partially commented. .data Matrix: .word 41,45,5, 34,8, 15,16,23,44,48,12,32,18,47,22,8,22 .word 46,40,42,33,13,38,27,6, 29,25,18,40,47,22,26,14,3 .word 7, 48,35,9, 43,38,9, 49,28,25,42,5, 44,10,5, 38,14 .word 46,33,16,6, 13,20,31,1, 8, 17,1, 47,28,46,14,28,7 .word 32,2, 48,25,41,29,14,39,43,46,3, 39,32,49,41,28,46 .word 5, 43,2, 48,13,4, 33,41,32,19,9, 25,30,22,2, 9, 40 .word 14,47,22,18,47,3, 35,44,18,6, 33,22,11,6, 47,50,4 .word 28,34,20,30,18,27,38,5, 26,40,37,23,16,13,37,8,7 .word 48,38,39,12,10,39,23,20,21,20,33,16,24,21,25,3,46 .word 49,38,40,38,13,47,5, 13,4, 13,23,26,12,30,29,29, 3 .word 8, 20,10,13,31,7, 12,41,12,21,28,26,43,14,35,10,19 .word 49,33,25,26,24,29,46,22,7, 5, 15,41,10,31,19,41,27 .word 48,9,...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • 2.29 The following is the C codes and the translated MIPS codes. Assume that the C-level...

    2.29 The following is the C codes and the translated MIPS codes. Assume that the C-level integers t1 and a0 are held in register $t1 and $a0, and $s0 holds the base address of the integer MemArray. void foo () { int MemArray[100] = { 96, 98, 63, 69, 42, 27, 16, 6, 47, 74, 44, 33, 76, 7, 88, 33, 80, 86, 86, 64, 17, 67, 60, 51, 2, 61, 93, 87, 49, 98, 24, 98, 30, 65, 2,...

  • 4. Suppose the MIPS code below is running on the pipeline processor we introduced in the...

    4. Suppose the MIPS code below is running on the pipeline processor we introduced in the course: Original code: Reordered code: 11: Jw $50, $a0(4) 12: sub $s1, $50, $s3 13: add $81, $si, $s2 14: lw $t1, $a0(8) 15: Jw $t2, $a0(12) 16: add $s3, $t1, $t2 a) List all Read-After-Write (RAW) dependencies in the code. Highlight them over the code above. b) Assume there is no forwarding hardware capability available. How many cycles it takes to run the...

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