In: Computer Science
Write MIPS assembly code for the following C code.
for (i = 10; i < 30; i ++)
{
if ((ar[i] > b) || (ar[i] <= c))
ar[i] = 0;
else
ar[i] = a;
}
I created a function named s to get the mips output.
int s() {
int i,a,b,c;
int ar[20];
for (i = 10; i < 30; i ++)
{
if ((ar[i] > b) || (ar[i] <= c))
ar[i] = 0;
else
ar[i] = a;
}
}
Explanation:
Here is the Mips conversion
s:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 10
jmp .L2
.L6:
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-96+rax*4]
cmp DWORD PTR [rbp-8], eax
jl .L3
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-96+rax*4]
cmp DWORD PTR [rbp-12], eax
jl .L4
.L3:
mov eax, DWORD PTR [rbp-4]
cdqe
mov DWORD PTR [rbp-96+rax*4], 0
jmp .L5
.L4:
mov eax, DWORD PTR [rbp-4]
cdqe
mov edx, DWORD PTR [rbp-16]
mov DWORD PTR [rbp-96+rax*4], edx
.L5:
add DWORD PTR [rbp-4], 1
.L2:
cmp DWORD PTR [rbp-4], 29
jle .L6
nop
pop rbp
ret