Question

C3. Convert the following C-code to MIPS code. [Use register $al for the variable i, temporary registers for other values, an

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

Let's first take a look directly at the converted MIPS code.

1) # i -> $a1

2) # array address -> $a0

3) # to hold immediate value 8 -> $t3

3) .globl main

4) main:

5) li $a1, 0

6) la $a0, 0xA0000080

7) loop:

8) bgt $a1, 4, exit

9) lw $t1, 0($a0)

10) mul $t2, $t1, $t3

11) sw $t2, 4($a0)

12) addi $a0, $a0, 4

13) addi $a1, $a1, 1

14) j loop

15) exit:

Now let's move through the code and see how it works.

First three lines are comments indicating which register is used to store what.

4), 7) and 14) are labels which help to branch from one part to another.

5) 'li' loads immediate value '0' to register '$a1'

6) 'la' loads address '0xA0000080' to $a0

8) 'bgt' branches to label 'exit' i.e line 14 if $a1 is greater than 4, else it continues to next statement. 'bgt' stands for branch if greater than.

9) loads value at address in $a0 register to register $t1 i.e. first element of array.

10) multiplies the element with 8 (stored in $t3) and stores the result in temporary register $t2. i.e. value of 'array[i]*8'. This is to be stored in the next location of the array according to the C code.

11) stores the value ($t2) to the memory address specified as '$a0 + 4' i.e. next memory location (represented as '4($a0)' in MIPS assembly syntax).

12) now the address in $a0 is increased by 4 i.e. it's made to point to the next memory location so that the next iteration of the loop can deal with the next set of values of the array.

13) the loop variable 'i' value is incremented i.e. $a1 register is incremented.

14) the statement execution jumps to 'loop' label. and the next iteration starts.

This repeats until the value $a1 register (i value) increases to 5 upon which the 'bgt $a1, 4, exit' statement will branch to 'exit' label and the program will end. This means the loop will repeat 5 times $a1 register will vary from 0 to 4, at the end of which the array in memory will have gotten modified as per the description in the C code.

Hope this Helped :) All the best.

Add a comment
Know the answer?
Add Answer to:
C3. Convert the following C-code to MIPS code. [Use register $al for the variable i, temporary re...
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
  • C2. Convert the following c-code to MIPS code. The base memory address of the array a is 0x8000_4...

    C2. Convert the following c-code to MIPS code. The base memory address of the array a is 0x8000_400C, which must be loaded in the base register Ss0. Register assignments: int a[10]; while (i !- 10) 1 if ( i%2 == 0) a[i] = i * 2; else ail - i* 3 i++i C2. Convert the following c-code to MIPS code. The base memory address of the array a is 0x8000_400C, which must be loaded in the base register Ss0. Register...

  • B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: ...

    B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: timer-v0 int timer = 0x0AC8 0001; B3. Write MIPS assembly code segment for the following C code snippet for (i - 0, i < 100; i++) -array Register assignment: i-) $ao Base of array -> $s0 array [ i+1] [i] / 2; B2. Convert the C code to MIPS assembly with only 2 efficient instructions: Register assignment: timer-v0 int timer = 0x0AC8 0001; B3. Write...

  • 7. Translate the following C code to MIPS assembly code. Use a minimum number of instructions....

    7. 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 Ss0, Ss1, St0, and St1, respectively. Also, assume that register SS2 holds the base address of the array D. for(i-0; i<a; i++) for(j=0 ; j<b; j++)

  • IN MIPS AND MUST RUN IN QTSPIM Translate the following C code to MIPS assembly code....

    IN MIPS AND MUST RUN IN QTSPIM 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 stored in registers Ss0, Ss1, St0 and Stl, respectively. Also assume that register Ss2 holds the base address of the array D. for (i=0; i<a; itt) for (i-0j<b:jt+)

  • 1. Convert the following C statement to MIPS code. Assume x, y and z are assigned...

    1. Convert the following C statement to MIPS code. Assume x, y and z are assigned to registers $s0, $s1 and $s2 respectively and the starting address of array A is $s6. x = y + z + A[3] 2. 2.Convert the following C statement to MIPS code. Assume the base address of array A is $s3 and h is assigned to register $s2 . A[7] = h + A[7]

  • Using MIPS: Consider the following fragment of C code: for i=0: i < = 100: i...

    Using MIPS: Consider the following fragment of C code: for i=0: i < = 100: i = i + 1) {a[i] = b[i] + c;} Assume that a and bare arrays of words and the base address of a is in $a0 and the base address of b is in $a1. Register $t0 is associated with variable i and register $s0 with c. Write the code for MIPS. How many instructions are executed during the running of this code? How...

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

  • its brr[8] (40%) Convert the following C-pseudo code into MIPS assembly code as a standalone program...

    its brr[8] (40%) Convert the following C-pseudo code into MIPS assembly code as a standalone program (including main and all the required directives). You can use any register. You must comply, however, with the convention of register usage. Before writing your code perform an explicit register allocation phase. Note that the C snippet is int arr[8]; int brr[4]-{1, 2, 3, 4, 5, 6, 7, 8) int i-8; while (i>-0) arrli]-brr[i-); (40%) Convert the following C-pseudo code into MIPS assembly code...

  • Translate the following C code to MIPS assembly. Assume that the values of a, b, i,...

    Translate the following C code to MIPS assembly. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also assume that $s2 holds the base address of the array D. for (i = 0: i < a: i++) for (j = 0: j < b: j++) D[2 * j] = i + j;

  • Write an equivalent MIPS code for the following C code. Assume that the base address of...

    Write an equivalent MIPS code for the following C code. Assume that the base address of an array A is stored in register 450. Variables x, i, j and h are stored in registers $t0,$t1, $t2 and $t3 respectively. do{ x = x + A[i]; i = i + j; } (while i != h); Make sure your MIPS code has sufficient comments.

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