assembly loop question

IT would appreciate , if you can point me in the right direction with this question.

loop:

1     pushl     %ebp
2     movl      %esp,  %ebp
3     movl      8(%ebp) , %ecx
4     movl     12(%ebp) , %edx
5     xorl      %eax , %eax
6     cmpl      %edx , %ecx
7     jle       .L4


.L6

16    decl        %ecx    
17    incl        %edx
18    incl        %eax
19    cmpl        %edx , %ecx
20    jg         .L4



.L4

21    incl        %eax
22    movl        %ebp , %esp
23    popl        %ebp
      ret          
     


Fill the blanks of the corresponding C source code.

int loop(int x int y)
{
    int result;

    for (.........;.............;result++) {

    '''''''''''''' ;
    .............. ;
   }

    ............ ;

   return .........;
}
 
lecosAsked:
Who is Participating?
 
Infinity08Commented:
>>  Let me know if what i did is correct . thank you.

The assembly code interpretation looks all good, yes.

Now let's have a look at the corresponding C code.


>>     for (..result = 0...;

ok.

>> ... x <=y..........;

This should be the loop continue condition. As long as this condition is true, the loop will continue.

>> result++) {

>>  x = x-1
>>     '''''''''''''' ;
>>     ....y = y+1.......... ;
>>    }

Both ok.

>>     .....result  = result + 1....... ;

Ands this is ok too.
0
 
lecosAuthor Commented:


ok , this is what i understand from the code. I kinda know what is happening but im still having problems trying to express it in C code.
 Let me know if what i did is correct . thank you.

loop:

1     pushl     %ebp
2     movl      %esp,  %ebp
3     movl      8(%ebp) , %ecx          / value x is stored into % ecx /
4     movl     12(%ebp) , %edx        / value y is stored into % edx /
5     xorl      %eax , %eax               / %eax is set to zero, % eax is "result"
6     cmpl      %edx , %ecx              / compare x to y/
7     jle       .L4                                /   if   x <= y  , go  to L4 /
 

.L6

16    decl        %ecx                     /   x  = x-1  /
17    incl        %edx                      /   y  = y+1 /
18    incl        %eax                     /  result = result +1 /
19    cmpl        %edx , %ecx        /    if x > y , then keep on the loop /
20    jg         .L6                **** corrected ***  



.L4

21    incl        %eax                       /   result = result + 1 /
22    movl        %ebp , %esp
23    popl        %ebp                               return  result
      ret          
     


Fill the blanks of the corresponding C source code.

int loop(int x int y)
{
    int result;

    for (..result = 0...;... x <=y..........;result++) {
 x = x-1
    '''''''''''''' ;
    ....y = y+1.......... ;
   }

    .....result  = result + 1....... ;

   return result;
}
0
 
lecosAuthor Commented:


>> ... x <=y..........;

This should be the loop continue condition. As long as this condition is true, the loop will continue.


@@     if this condition is true , the loop will continue so     if x > y, the loop will continue.


0
 
Infinity08Commented:
That's better :) You've got it :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.