[dundee] Grabbing the stack pointer...

Andrew Clayton andrew at digital-domain.net
Mon Mar 23 10:54:44 UTC 2009


On Mon, 23 Mar 2009 09:25:04 +0000, Nicholas Walker wrote:

> gcc on linux :)
> 
> it seems that it prefers just "asm" over __asm__
> 
> so, compiling:
> 
> 
> #include<stdio.h>
> unsigned long sp(void)
> {
>     asm("movl %esp, %eax");
> }
> 
> void main(void)
> {
>     printf("0x%x\n", sp());
> }
> 
> 
> 
> returns:
> 
> 
> 
> tel0s at bufferoverflow:~$ cd Desktop/
> tel0s at bufferoverflow:~/Desktop$ gcc -o sp sp.c
> sp.c: In function ‘main’:
> sp.c:9: warning: format ‘%x’ expects type ‘unsigned int’, but
> argument 2 has type ‘long unsigned int’
> sp.c:8: warning: return type of ‘main’ is not ‘int’
> tel0s at bufferoverflow:~/Desktop$
> 
> 
> changing main to int main() kills the last error it seems:
> 
> tel0s at bufferoverflow:~/Desktop$ gcc -o sp sp.c
> sp.c: In function ‘main’:
> sp.c:9: warning: format ‘%x’ expects type ‘unsigned int’, but
> argument 2 has type ‘long unsigned int’
> tel0s at bufferoverflow:~/Desktop$

OK, so it's not a compile failure, just a warning.

#include <stdio.h>

unsigned int sp()
{
	__asm__("movl %esp, %eax");
}

int main()
{
	printf("0x%x\n", sp());
	
	return 0;
}

Is the cleanest I got it.

$ gcc -Wall -o sp sp.c
sp.c: In function ‘sp’:
sp.c:6: warning: control reaches end of non-void function


Andrew



More information about the dundee mailing list