Question

In MIPS assembly, write an assembly language version of the following C code segment: int A[100],...

In MIPS assembly, write an assembly language version of the following C code segment:

int A[100], B[100];

for (i=1; i < 100; i++) {

A[i] = A[i-1] + B[i];

}

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

C code
int A[100], B[100];
for (i=1; i < 100; i++) {
   A[i] = A[i-1] + B[i];
}

Let the Base address of array A[] is stored in Register $s1
the Base address of array B[] is stored in Register $s2

MIPS code

   addi $t0, $0,100   //$t0 = 100
   addi $t1, $0, 1       //i = 1

Loop:
   slt $t2, $t1, $t0    //if(i<100) set $t2 = 1
   beq $t2, $0, Exit   //if($t2 = 0) branch to Exit
   sll $t2, $t1, 2       //$t2 = 4*i
   add $t3, $t2, $s1   //$t3 = address of A[i]
   add $t4, $t2, $s2   //$t4 = address of B[i]
   lw $t2, -4($t3)       //$t2 = A[i-1]
   lw $t4, 0($t4)       //$t4 = B[i]
   add $t2, $t2, $t4   //$t2 = A[i-1] + B[i]
   sw $t2, 0($t3)       //A[i] = A[i-1] + B[i]
   addi $t1, $t1, 1   // i = i + 1
   j Loop           //Jump to Loop
Exit:
  
   

Add a comment
Know the answer?
Add Answer to:
In MIPS assembly, write an assembly language version of the following C code segment: int A[100],...
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) Write a MIPS assembly language version of the following C code segment: int A[75], B[75];...

    2) Write a MIPS assembly language version of the following C code segment: int A[75], B[75]; for (i = 1; i < 73; i ++) { C[i] = (A[i + 1] + A[i]) * (B[i + 2] - A[i-1]); } Arrays A, B and C start at memory location A000hex, B000hex and C000hex respectively. Try to reduce the total number of instructions and the number of expensive instructions such as multiplies. WRITE A SIMPLE CODE WORKING FOR MARS 4.5

  • CDA-3101 – MIPS Assembly Programming 1. Write the following code segment in MIPS assembly language code:...

    CDA-3101 – MIPS Assembly Programming 1. Write the following code segment in MIPS assembly language code: 3. Write a MIPS program to find the sum of squares from 1 to n. Where n=10. For example, the sum of squares for 10 is as follows: 12+22+32+……+n2=385

  • B2. Translate the following MIPS assembly instruction into machine language: lw $t8, 100($s0) B3 ...

    B2. Translate the following MIPS assembly instruction into machine language: lw $t8, 100($s0) B3 Write MIPS assembly code segment for the following C code snippet: for (i - O i < 10i i++) i array[i+l] - array [i8; Register assignment: i-> Şa0 Base of array -> $s0 B2. Translate the following MIPS assembly instruction into machine language: lw $t8, 100($s0) B3 Write MIPS assembly code segment for the following C code snippet: for (i - O i Şa0 Base of...

  • Problem #2 (20pts) Please write the MIPS assembly code corresponding to the following C code segment....

    Problem #2 (20pts) Please write the MIPS assembly code corresponding to the following C code segment. Assume that f is assigned to register $s2 int Example int i, int j, int k) int f; f i+j-k-1: return fi

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

  • MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4,...

    MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4, 5, 6); int main) int num, position; scanf("%d",&num) ; position search(array, printf("The position is: num, 5); %d\n",positio int search(int array, int num, int size int position =-1; for(int i-0;i<size; i++) if(array [i]=num) { position-i; break; return position; Register map $s1: position $a0: array address $a1: num . $a2: size . $VO: return value

  • MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i...

    MIPS assembly language Covert this code to MIPS: #include <stdio.h> int function (int a) int main)i int x=5 ; int y: y function(x); printf "yd",y); return 0; int function (int a) return 3*a+5; Assumptions: . Place arguments in $a0-$a3 . Place return values in $vO-$v1 Return address saved automatically in $ra . lgnore the stack for this example. (Thus, the function will destroy registers used by calling function

  • Translate function f into MIPS assembly language. The code for function f is as follows: int...

    Translate function f into MIPS assembly language. The code for function f is as follows: int func(int a, int b){ return a + b; } int f(int a, int b, int c, int d){ return func(func(a, b), c - d);

  • 1. [2 points] Write a MIPS assembly language program of the following C function and the...

    1. [2 points] Write a MIPS assembly language program of the following C function and the code to call the function: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; مهه Arguments g, h, i, and j are passed to the function in registers $a0, $al, Şa2, and $a3, respectively while f in $50 (hence, need to save $50 on stack), and the result is to be stored...

  • Please write a simple code in mars.mips 4.5 with simple mips commands a beginner would use...

    Please write a simple code in mars.mips 4.5 with simple mips commands a beginner would use Please make sure it works and goes through each step without crashing or dropping out from the bottom, thanks <3 #comments would really help please read the text below carefully and answer it correctly 2. Write a MIPS assembly language version of the following C code segment: int A[75], B[75]; for (i = 1; i < 73; i ++) { C[i] = (A[i +...

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