Results 1 to 6 of 6

Thread: Simplest assembly program for gnu assembler(gas)

  1. #1
    Join Date
    Dec 2011
    Beans
    22

    Cool Simplest assembly program for gnu assembler(gas)

    Hello, I am trying to learn programming using gas.But I did not find any tutorial that will show me the simplest program sample that I can run on my PC.
    I searched over Internet from a day, but I didn't find it.

    I want a sample code that will store(move) value 1 into register eax and steps for assembling and loading ..thanks..That will surely help me to get started , into writing the assembly language programs..
    thanks..
    Last edited by 681ankit; December 15th, 2012 at 10:40 AM.

  2. #2
    Join Date
    Dec 2012
    Beans
    24

    Re: Simplest assembly program for gnu assembler(gas)

    Here's a fairly short example:

    Code:
    .section .text
    
            .globl main
    main:   
            movl $1, %eax
            movl $5, %ebx
    
            int $0x80
    save as myprogram.s and compile and run with:

    Code:
    gcc myprogram.s
    ./a.out
    echo $?
    Taken (with some changes) from: http://savannah.nongnu.org/projects/pgubook/

  3. #3
    Join Date
    Dec 2011
    Beans
    22

    Talking Re: Simplest assembly program for gnu assembler(gas)

    thanks for reply .. I got it now..

  4. #4
    Join Date
    Dec 2012
    Beans
    24

    Re: Simplest assembly program for gnu assembler(gas)

    There's one thing worth mentioning as the book has certain problems. The world has changed since 2002!

    For example, the book states that an assembly program starts at the _start label. I tried this, and it didn't compile, so I wrote a minimum C program:
    Code:
    int main() {
      return 2;
    }
    and compiled it with
    Code:
    > gcc myfile.c -S -O5
    to see what the current convention is. If you ever get stuck because of changes, this could be a good method to use.
    Last edited by ehrt74; December 15th, 2012 at 01:21 PM.

  5. #5
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Simplest assembly program for gnu assembler(gas)

    Good job omitting the step where _start comes in. The assembly output of a C compiler is not equivalent to what you would write if you were programming in "raw" assembly. For instance, the label "main" is only meaningful in C, which is why that program won't do anything if you assemble it without using gcc (gcc links against libc). I don't know what problem you had with _start but I imagine it might be related to using gcc as an assembler.

    If you want to see what gcc actually outputs, disassemble the binary (Note this is not recommended as a way to learn assembly! It contains a lot of libc nonsense that you would not write).

    Code:
    $ gcc -o file file.c
    $ objdump -d file

  6. #6
    Join Date
    Dec 2012
    Beans
    24

    Re: Simplest assembly program for gnu assembler(gas)

    trent.josephsen is quite correct that you can assemble and link an executable without using the label 'main'. an example would be:

    Code:
    .section .text
    
            .globl _start
    _start:   
            movl $1, %eax
            movl $5, %ebx
    
            int $0x80
    which you would then assemble, link and run by entering:
    Code:
    > as myfile.s -o myfile.o
    > ld -s myfile.o -o myfile
    > ./myfile
    Hope this adds to the general confusion

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •