Monday, December 20, 2010

print out ebp, esp, eax by gcc inline assembly

Here is the code:

#include <stdio.h>
int main(int argc, char* argv[])
{
    unsigned esp, ebp, eax, ebx, ecx, edx;

    asm(
        "movl %%esp, %0;"
        "movl %%ebp, %1;"
        "movl %%eax, %2;"
        "movl %%ebx, %3;"
        "movl %%ecx, %4;"
        "movl %%edx, %5;"
        :"=r"(esp), "=r"(ebp), "=r"(eax), "=r"(ebx), "=r"(ecx), "=r"(edx)
        );
    printf("esp is: %x\n", esp);
    printf("ebp is: %x\n", ebp);   
    printf("eax is: %x\n", eax);
    printf("ebx is: %x\n", ebx);
    printf("ecx is: %x\n", ecx);   
    printf("edx is: %x\n", edx); 

    return 1;
}

GCC inline assembly syntax:
asm(
//assembly code;
: output operand
: input operand
: registers

"=r" I could use any registers for caculation. "=" means write only
"a": I could use %eax for caculation.

Here is the list:
please see Register operand constraint
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html



)

No comments: