Thread: IA32 Assembly

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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.

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, 03: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, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM