Assembly question

This is a discussion on Assembly question within the Tech Board forums, part of the Community Boards category; Hi, Can you please explain what the following program does? Code: Execution begins at address 0 .pos 0 init: irmovl ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    15

    Assembly question

    Hi,

    Can you please explain what the following program does?
    Code:
    Execution begins at address 0
            .pos 0
    init:   irmovl Stack, %esp      # Set up Stack pointer
            irmovl Stack, %ebp      # Set up base pointer
            jmp Main                # Execute main program
    
    # Array of 4 elements
            .align 4
    array:  .long 0xd
            .long 0xc0
            .long 0xb00
            .long 0xa000
    
    Main:   irmovl $4,%eax
            pushl %eax      # Push 4
            irmovl array,%edx
            pushl %edx      # Push array
            call rSum       # Sum(array, 4)
            halt
    
    /* $begin rsum-ys */
            # int Sum(int *Start, int Count)
    rSum:   pushl %ebp
            rrmovl %esp,%ebp
            pushl %ebx           # Save value of %ebx
            mrmovl 8(%ebp),%ebx  # Get Start
            mrmovl 12(%ebp),%eax # Get Count
            andl %eax,%eax       # Test value of Count
            jle L38              # If <= 0, goto zreturn
            irmovl $-1,%edx
            addl %edx,%eax       # Count--
            pushl %eax           # Push Count
            irmovl $4,%edx
            rrmovl %ebx,%eax
            addl %edx,%eax
            pushl %eax           # Push Start+1
            call rSum            # Sum(Start+1, Count-1)
            mrmovl (%ebx),%edx
            addl %edx,%eax       # Add *Start
            jmp L39              # goto done
    L38:    xorl %eax,%eax       # zreturn:
    L39:    mrmovl -4(%ebp),%ebx # done: Restore %ebx
            rrmovl %ebp,%esp     # Deallocate stack frame
    popl %ebp            # Restore %ebp
            ret
    /* $end rsum-ys */
            .pos 0x400
    Stack:  # The stack goes here
    Last edited by Salem; 11-01-2007 at 10:11 AM. Reason: Added code tags - use them!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Do you have a specific question?

    The comments seem to make a fairly good job of explaining what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    15
    Does it add the values of the 4 element array given?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks like it. But it's reallty weird syntax (looks like x86, but not quite), and it's doing it in a very weird way - why would you spend that many instructions on adding 4 numbers together.

    --
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's x86 assembly in AT&T syntax, as used by the GNU assembler. And it's not a very weird way. It's pretty much equivalent to this C code:
    Code:
    int rSum(int *ar, int size)
    {
      return size == 0 ? 0 : rSum(ar+1, size-1) + ar[0];
    }
    i.e. a recursive array summation function. The main function then uses this function to sum up the global array.

    The weird part is the recursion, not the separate function.
    Last edited by CornedBee; 11-01-2007 at 12:55 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It may be a similar syntax to AT&T, but as far as I know, you don't differentiat different types of move instructions in standard AT&T, whilst this has irmov, rrmov to indiate "immedate to register" and "register to register" move.

    And yes, I did understand that it's recursive, which is a solution, but hardly an efficient one.

    --
    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.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The move differentiation is interesting indeed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    That's not standard AT&T assembly syntax. I dunno what it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly question
    By geek@02 in forum Tech Board
    Replies: 8
    Last Post: 08-11-2008, 06:48 AM
  2. Assembly Language Question
    By John_L in forum Tech Board
    Replies: 2
    Last Post: 03-13-2008, 07:44 PM
  3. 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
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. C/C++ vs assembly: speed comparison
    By Just in forum C++ Programming
    Replies: 11
    Last Post: 11-25-2002, 02:33 PM

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