Monday, June 1, 2009

How to check assembly code of a C code ?

hi friends ,
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.

Tuesday, February 3, 2009

GCC / ANSI C stanadards..

Hi friends ,
i want to share one more thing with you people..

first of all something about ANSI C standard,

It depicts that when we write code in C , it should be according to the standards described by ANSI. for example -: all the variables should be declared before their scope begins .

the code according to the Ansi C standard -:
int main()
{
int i ,j;
printf("%d %d",i,j);

}


int main()
{
int i=29;
printf("%d ", i);

{
int j=30; /* Even this would work fine as the definition says about scope ,not about program*/
printf("%d",j);
}
}


the code which is not according to the ANSI C standard -:

int main()
{

printf("Hello World ");
int j ; /* This is incorrent according to ANSI C standard */
}



Now the questions is , the most popular C compilers like GCC , do they follow the rules of ANSI C standards ?

because the the second example also gets compiled by Gcc.



Well , GCC supports ANSI C standard ,but by default they provide some optimization over standard C. That is why the later version of the code gets compiled . But what if you want that ur codde is according to the ANSI standard. Actually there are options for this in GCC .

$ gcc -pedantic test.c

The above will generate the rules violations as warning but will generate the object code as well as the the executable.

$ gcc -pedantic-errors test.c

This will generate the violations as errors .


this is it for today ..
Enjoy.

Friday, January 16, 2009

A typical C confusion..

Most of the books on C says that a " if we want to make variable global in a file and dnt want that other files can access that variable" what we simple do is to define that variable as static. After that it can't accessed outside the file.

Prove this theory.

for example :

mylib.h
/* Start of file */

void display();

/* End of file */

file1.c

#incude
#include"mylib.h"

static int privateVariable=10;


void display()
{
printf("\n%d \n", privateVariable );

}

/* End of File */

file2.c
/* Start of file */
#include"mylib.h"

int main()
{

display();
privatevariable=15 ; /* Should give a error at this stage */

}

/* End of file */


Just prove the above theory .. i mean just try to execute the above code.
According the Ansi standard this generates a error and it does.
For making things simpler i hve already written down the whole code.
All you need to do is execute it. According to aftr the above code ,its a very easy job to do.

If you want to read more about this issue just read ' The C programming Language ' by Dennis Ritchi (dnt remember the chapter ) .