Thread: What is the meaning of these assembly lines?

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275

    What is the meaning of these assembly lines?

    Hi

    When I compile my program with -S option of gcc it always produces some meaningless (for me) lines! Below is what i did
    Code:
    fnoyan@linux:~> cat > trial.c
    #include <stdio.h>
    
    int main()
    {
            return 0;
    }
    fnoyan@linux:~> gcc -Wall trial.c -S -o trial.s
    fnoyan@linux:~> cat trial.s
            .file   "trial.c"
            .text
    .globl main
            .type   main, @function
    main:
            pushl   %ebp
            movl    %esp, %ebp
            subl    $8, %esp
            andl    $-16, %esp
            movl    $0, %eax
            addl    $15, %eax
            addl    $15, %eax
            shrl    $4, %eax
            sall    $4, %eax
            subl    %eax, %esp
            movl    $0, %eax
            leave
            ret
            .size   main, .-main
            .ident  "GCC: (GNU) 4.0.2 20050901 (prerelease) (SUSE Linux)"
            .section        .note.GNU-stack,"",@progbits
    All the parts are ok except the part below
    Code:
            andl    $-16, %esp
            movl    $0, %eax
            addl    $15, %eax
            addl    $15, %eax
            shrl    $4, %eax
            sall    $4, %eax
            subl    %eax, %esp
    Even if I delete these lines and compile the corresponding assembly file (with gcc -Wall trial.s -o trial), the program runs fine! Also, after compilation, I chechked the corresponinding assembly code for the file with GDB. The compiler did not add any other line.

    What is the aim of these lines? Why does it add 15 two times instead of adding 30?

    Thanks...

  2. #2
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    I would recomend just not using asemble at all. It is just way to much of an ugly language that almost nobody uses anymore.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Actually I am not using language itself. I just wonder the aim of the generated code!

    But sometimes it may be required to write the code in assembly and most of the times being familiar with the assembly helps you to understand the underlaying procedures.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I just wonder the aim of the generated code!
    Try the same thing with a function other than main().
    main() is different, it does things no other function does (recursive callers of main take note).

    One of the things it seems to be doing with esp for example is making sure it's aligned on a 16-byte boundary. This is to ensure optimal access to local variables later 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.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    > One of the things it seems to be doing with esp for example is making sure it's aligned on a 16-byte boundary. This is to ensure optimal access to local variables later on.

    Yeah, but these lines make no sence
    Code:
    movl    $0,   %eax  ; eax: 00 00 00 00
    addl    $15,  %eax  ; eax: 00 00 00 0F
    addl    $15,  %eax  ; eax: 00 00 00 1E
    shrl    $4,   %eax  ; eax: 00 00 00 01
    sall    $4,   %eax  ; eax: 00 00 00 10
    subl    %eax, %esp
    since it could be simply replaced by
    Code:
    subl    $16, %esp
    and why would anyone even want to do that? andl rounds esp down so there _should_ be no need for it...

    BTW. there doesn't seem to be such code on amd64

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Compiling with -O2 also removes some of the apparent redundancy.
    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.

  7. #7
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Quote Originally Posted by tuxinator
    I would recomend just not using asemble at all. It is just way to much of an ugly language that almost nobody uses anymore.
    funny, you have a lot of software on your system written with assembler.
    the kernel is c and assembler, and most device drivers make heavy use of assembler.
    the real intention of the language, extremely fine control of hardware.

    C is one step above, in that it has a much more human readable syntax, but it is almost as powerful at hardware control as assembler, that almost being why assembler is still used in os / systems development.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I would say definatly learn some assembly... it really shows you how computers do things. It removes one more layer of abstraction and shows you more of the raw "this is how you do it" code.

    It'll give you a better understanding of what your code is doing and hints as to how you can write more efficient code.

    and lots of people still use ASM. your crazy if you think otherwise.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Meaning of Code Lines
    By shwetha_siddu in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 04:54 PM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  4. Assembly....
    By Victor in forum Linux Programming
    Replies: 8
    Last Post: 12-28-2004, 01:46 AM
  5. 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