Converting C code to MIPS Assembly Language

Lord Bahamut

Here's the C code:

int A[10];
int sum = 0;
int i = 0;

while (i<10){
sum += A[i++];
sum *= 2;
}

Here's my take at converting into MIPS:

**Reg. Allocation Table:
A = $s1
sum = $s2
i = $s3
10 = $s4**

loop: beq $s3, $s4, endloop 

"here's where i get stuck, inside the while loop."

j: loop
endloop:

I understand that in a basic while loop such as: i = $s1, 5 = $s3

i=0;
while(i != 5)
i=i+1;

addi $s1, $zero, 0 #i=0

loop: beq $s1, $s3, endloop
add   $s1, $s1, 1
j     loop:

endloop:

I'm just having trouble, or having a hard time comprehending a slightly more difficult loop where sum+= A[i++]; and sum*= 2; are introduced.

Any help is greatly appreciated. I'm not looking for a complete solution, so please help me think.

Thank you!

Konrad Lindenbach

Break it down:

sum += A[i++]; becomes sum = sum + A[i]; i = i + 1; which in MIPS assembly could be expressed:

add $t0 $s3 $s1 #index A at i
lw  $t1 0($t0)  #load A at i

add  $s2 $s2 $t1 #add A[i] to sum
addi $s3 $s3 1   #increment i

sll $s2 $s2 1 #double sum

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Bad Instruction - Inline Assembly Language in C Code

From Dev

Difference between "move" and "li" in MIPS assembly language

From Dev

converting a C for loop to MIPS

From Dev

MIPS Assembly language traversing an array

From Dev

Converting from C Loop to MIPS assembly language (bad address error)

From Dev

MIPS assembly language - temporary register vs saved registers

From Dev

Difference between ISA (e.g. MIPS) and Assembly language

From Dev

Converting Assembly to C

From Dev

Assembly language with C++

From Dev

Is this MIPS assembly code converted from C code correct?

From Dev

Converting a snippit of x86 Assembly Code into C

From Dev

Converting WinSCP script to C# code using WinSCP .NET assembly

From Dev

Converting FindScanline assembly code to purepascal

From Dev

Process of Converting a MIPS Instruction to its Hex code

From Dev

What assembly language does C code compile into in Visual Studio?

From Dev

Why do we use .globl main in MIPS assembly language?

From Dev

Converting C++ function to MIPS assembly

From Dev

Converting C to MIPS

From Dev

C to MIPS Assembly

From Dev

MIPS - Call C function in Assembly code

From Dev

Assembly Language to C equivalent

From Dev

Converting Assembly to C

From Dev

Converting C to mips assembly, Unaligned address error

From Dev

Process of Converting a MIPS Instruction to its Hex code

From Dev

Converting from c name sorting code to NASM assembly code crashes

From Dev

C to MIPS Assembly

From Dev

map c language into assembly language

From Dev

Converting C code to mips gives error

From Dev

Converting Assembly Language to Python Code