IA32 Assembly

This is a discussion on IA32 Assembly within the C Programming forums, part of the General Programming Boards category; My question is geared towards the IA32 Assembly language that a C program produces. I've been given the following and ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    7

    IA32 Assembly

    My question is geared towards the IA32 Assembly language that a C program produces. I've been given the following and my task is to figure out what the funciton does. I've written my comments next to each line and as far as I can tell it looks to me that if local variable a >= y then 0 gets returned, if not then other actions are taken until a is > and then a value for b is returned. Any help on exactly what this function does would be greatly appreciated.
    Code:
     pushl   %ebp
            movl    %esp, %ebp
            subl    $16, %esp
            movl    $0, -8(%ebp)   SET LOCAL VARIABLE B TO 0
            movl    $0, -4(%ebp)   SET LOCAL VARIABLE A TO 0
            jmp     .L2	       JUMP TO L2
    
    			IF (A<Y)
    .L3:
            movl    -4(%ebp), %eax  MOVE A TO EAX
            sall    $2, %eax	A * 2^2 IN EAX
            addl    8(%ebp), %eax   ADD X + A IN EAX
            movl    (%eax), %eax	MOVE (X + A) IN EAX
            addl    %eax, -8(%ebp)  ADD (X + A) IN B
            addl    $1, -4(%ebp)	ADD 1 TO A
    .L2:
            movl    -4(%ebp), %eax  MOVE A TO EAX
            cmpl    12(%ebp), %eax	COMPARE A TO Y
            jl      .L3		IF A < Y JUMP TO L3
            movl    -8(%ebp), %eax  MOVE B TO EAX
            leave
            ret			RETURN B
    Last edited by Salem; 05-04-2009 at 10:47 PM. Reason: Added [code][/code] tags, learn to use them yourself

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    I won't give you the answer directly, because this sounds like homework. However, I'll give you a starting point, because you may not necessarily be looking at this the right way. Or maybe you are, and this starting point is not very helpful; at any rate, see what you can do if I tell you the prototype for this function is:
    Code:
    int f(int *array, int size);

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's clearly not very well optimized...

    Code:
            push   %esi
            push   %edi
            mov    $12(%esp), %esi
            mov    $16(%esp), %edi
      
            movl    $0, %ecx  SET LOCAL VARIABLE B TO 0
            movl    $0, %edx   SET LOCAL VARIABLE A TO 0
            jmp     .L2	       JUMP TO L2
    
    			IF (A<Y)
    .L3:
            addl    %ecx, (%esi), %eax	
            addl     %esi, 4
            incl       %edx
    .L2:
            cmpl    %edi, %edx	COMPARE A TO Y
            jl      .L3		IF A < Y JUMP TO L3
            movl    %ecx, %eax  MOVE B TO EAX
            pop     %edi
            pop     %esi
            ret			RETURN B
    I actually find this easier to understand...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by cas View Post
    I won't give you the answer directly, because this sounds like homework. However, I'll give you a starting point, because you may not necessarily be looking at this the right way. Or maybe you are, and this starting point is not very helpful; at any rate, see what you can do if I tell you the prototype for this function is:
    Code:
    int f(int *array, int size);
    It is homework and I'm not looking for the exact answer I just need a big push in the right direction....so this a function that fills an array? Something like with for loop below?

    int f(int *array, int size)

    for (int a = 0; a < size; a++)

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    7
    last question....I read the sall $2, %eax as 4 * a where a is initially = 0. So my I would say the the array just fills with multiple's of 4 up to whatever size is specified. Does that sound right?

    something like (0, 4, 8, 12,......)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is not storing data into the array. But yes, it walks in steps of 4.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by matsp View Post
    It is not storing data into the array. But yes, it walks in steps of 4.

    --
    Mats
    eh...I'm still confused...if it's not storing data into an array what is it doing?

  9. #9
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    Quote Originally Posted by Smohr View Post
    eh...I'm still confused...if it's not storing data into an array what is it doing?
    Code:
    movl    (%eax), %eax
    Figure out what that means and it might start falling into place.

    If you're at your wits' end, write something simple (that is similar to what you expect this code to be doing) in C and see what gcc spits out for the assembly. Perhaps seeing how gcc translates C->asm will help figure out what it did here.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by cas View Post
    Code:
    movl    (%eax), %eax
    Figure out what that means and it might start falling into place.

    If you're at your wits' end, write something simple (that is similar to what you expect this code to be doing) in C and see what gcc spits out for the assembly. Perhaps seeing how gcc translates C->asm will help figure out what it did here.
    I read that as move the value that's store in the memory location located in eax into eax

    so eax = *eax

    sound right?

  11. #11
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    Quote Originally Posted by Smohr View Post
    I read that as move the value that's store in the memory location located in eax into eax

    so eax = *eax

    sound right?
    Yep. Now you should understand why it's not storing data in the array...

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by cas View Post
    Yep. Now you should understand why it's not storing data in the array...
    Nope...still lost

  13. #13
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    987
    Quote Originally Posted by Smohr View Post
    Nope...still lost
    Code:
    int main(void)
    {
      int array[1];
      int i = 0;
    
      /* Which of these is storing a value in the array? */
      *array = i;
      i = *array;
    }
    Which compares to what the assembly version is doing?

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    7
    Quote Originally Posted by cas View Post
    Code:
    int main(void)
    {
      int array[1];
      int i = 0;
    
      /* Which of these is storing a value in the array? */
      *array = i;
      i = *array;
    }
    Which compares to what the assembly version is doing?
    *array = i

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. an help with IA32 assembly?
    By smoking81 in forum Linux Programming
    Replies: 15
    Last Post: 03-29-2008, 01:38 AM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 02:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21