In: Computer Science
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code.
int counter;
int x = 5;
int y = 6;
for (counter =10; counter >0;counter--)
IF(X==Y)
Y = Y + 1 ;
ELSE
Y = Y + 2}
Fill in the blanks in the following code:
MOV__________ ;loop counter into r0-ten times round the loop
MOV__________ ;Value of y loaded into r1
MOV__________ ;Value of x loaded into r2
Next CMP ____________ ;assume r1 contains y and r2 contains x: compare them
BNE ____________ ;if not equel then branch to the else part
ADD ____________ ;if equal fall through to here and add one to y
B _____________ ;now skip past the else part
plus2 ______________ ;ELSE part add 2 to y
counter ______________ ;decrement loop counter
BNE _____________ ;continue until all done
please fill out the blanks with assembly code according to C code above
Here is the c Langauage code:
#include
int main() {
int counter;
int x = 5;
int y = 6;
for (counter =10; counter >0;counter--)
{
if(x==y)
{
y=y+1;
}
else
{
y=y+2;
}
}
}
_____________________________________________________________
Ans here is the equivalent assembly code:
.file "Test.c"
.def ___main; .scl 2; .type 32; .endef
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call ___main
movl $5, 4(%esp)
movl $6, 8(%esp)
movl $10, 12(%esp)
jmp L2
L5:
movl 4(%esp), %eax
cmpl 8(%esp), %eax
jne L3
addl $1, 8(%esp)
jmp L4
L3:
addl $2, 8(%esp)
L4:
subl $1, 12(%esp)
L2:
cmpl $0, 12(%esp)
jg L5
movl $0, %eax
leave
ret
.ident "GCC: (tdm-1) 4.9.2"