.p2align 2 (ASM)

This is a discussion on .p2align 2 (ASM) within the Linux Programming forums, part of the Platform Specific Boards category; I was looking at some ASM code for some of my programs using the 'gcc -S' option to generate it ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    .p2align 2 (ASM)

    I was looking at some ASM code for some of my programs using the 'gcc -S' option to generate it and I noticed a few things that I have NO IDEA what they do, why they are there and if they have any effect on my programs.

    Here is a list:
    Code:
    ;at the begining
    .text
        .align 4
    
    .p2align 2 ;what's this?!
    
    ;and I used to know what these were but I forgot
    
    leave ;I think this does popl %ebp to get the original base pointer
    ret ;and does this jump to where the program was called or after it or what, I forgot...
    Any help would be appreciated, im not too concerned about the last two commands but more or less the aligns, what are they, and are they important, I would imagine they are relatively significant because they are there to begin with.


    EDIT:

    And another question in ASM:
    I can't seem to figure out why I continually get a seg-fault for this prog:
    Code:
    .lor:
    	.string "Hello, World!\n"
    
    .text
    	.align 4
    .globl main
    main:
    	pushl %ebp
    	movl %esp, %ebp
    	movl $10, %ebx
    LOOP:
    	cmpl $0, %ebx
    	jne NE
    	jmp GO
    NE:
    	pushl $.lor
    	call printf
    	popl %eax
    	decl %ebx
    	jmp LOOP
    GO:
    	movl $0, %eax
    	leave
    	ret
    Any explanations of just how things work at all could help, thanks.

    -LC
    Last edited by Lynux-Penguin; 10-29-2003 at 11:25 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    please never mind. I fixed the ASM probelm and I figured out what all that stuff meant, but I can't delete the thread. ::shrug::

    The solution was:
    Code:
    NE:
    	subl $4, %esp
    	pushl $.lor
    	call printf
    	addl $4, %esp
    	decl %ebx
    	jmp LOOP
    Note it will leave a little bit of memory free at the end about 40 bytes, but its better and easier (maybe not faster) because the leave command automatically moves the esp and ebp back to where they were.

    a better fix might be:
    Code:
    NE:
    	pushl $.lor
    	call printf
    	addl $4, %esp
    	decl %ebx
    	jmp LOOP
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 02:16 AM
  2. Understanding ASM
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 07-12-2002, 08:39 PM
  3. Inline asm
    By wavering in forum C Programming
    Replies: 2
    Last Post: 01-29-2002, 02:42 AM
  4. asm mov
    By manu in forum C++ Programming
    Replies: 1
    Last Post: 12-15-2001, 12:59 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM

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