I am back with a new post.
Today's topic is .. "how to check assembly of a C code ? "
There is a inbuilt option for this in gcc compiler
for example let say i have a file called "hello.c"
#include
void print(char *name)
{
printf("%s",name);
}
int main()
{
print("pankaj");
}
and when you compile the code with
$ gcc -S hello.c
A new file will be generated by the compiler called "hello.s", which will have the content like
.file "test.c"
.section .rodata
.LC0:
.string "%s"
.text
.globl print
.type print, @function
print:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl 8(%ebp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
leave
ret
.size print, .-print
.section .rodata
.LC1:
.string "pankaj"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $20, %esp
movl $.LC1, (%esp)
call print
addl $20, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (Ubuntu 4.3.2-1ubuntu12) 4.3.2"
.section .note.GNU-stack,"",@progbits
so this is all for today ..
have fun..
with regards
Pankaj anand.
Ya.. thats nice. The way the instructions popl,pushl,movl,call and ret are called make sense on an overview,but it would be even better if you throw some light on the way memory is accessed using '%esp','%ebp'. It would be awesome if you could please explain the need for leal instruction and the movement of stackpointer for this simple example.
ReplyDeleteI will try to answer as soon as possible..
ReplyDelete