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 ) .

Monday, December 29, 2008

Data Structure problems

well, This is my first official post on Blog..

Today i have two problems which were asked to me in a test.. luckily i could solve them in time.

1. To display the list of on bits of a number which together makes that number.
for example
input 50 which is the sum of

(0 1 1 0 0 1 0 )
0 32 16 2 0

you need to display
2,16,32 .

that's it.
seems like an simple one ?
dnt worry . kahani me twist hai..
the input can be very long, such long that i might not be fitted into the range of a integer.
for example
input is - :
1111111111111111111111111111111111111111111111111111

the output should be like - :


POW(2,0)--> 1
POW(2,1)--> 2
POW(2,2)--> 4
POW(2,6)--> 64
POW(2,7)--> 128
POW(2,8)--> 256
POW(2,12)--> 4096
POW(2,13)--> 8192
POW(2,14)--> 16384
POW(2,18)--> 262144
POW(2,19)--> 524288
POW(2,20)--> 1048576
POW(2,24)--> 16777216
POW(2,25)--> 33554432
POW(2,26)--> 67108864
POW(2,30)--> 1073741824
POW(2,31)--> 2147483648
POW(2,32)--> 4294967296
POW(2,36)--> 68719476736
POW(2,37)--> 137438953472
POW(2,38)--> 274877906944
POW(2,42)--> 4398046511104
POW(2,43)--> 8796093022208
POW(2,44)--> 17592186044416
POW(2,48)--> 281474976710656
POW(2,49)--> 562949953421312
POW(2,50)--> 1125899906842624
POW(2,52)--> 4503599627370496
POW(2,54)--> 18014398509481984
POW(2,59)--> 576460752303423488
POW(2,60)--> 1152921504606846976
POW(2,65)--> 36893488147419103232
POW(2,67)--> 147573952589676412928
POW(2,70)--> 1180591620717411303424
POW(2,71)--> 2361183241434822606848
POW(2,74)--> 18889465931478580854784
POW(2,75)--> 37778931862957161709568
POW(2,78)--> 302231454903657293676544
POW(2,81)--> 2417851639229258349412352
POW(2,82)--> 4835703278458516698824704
POW(2,84)--> 19342813113834066795298816
POW(2,86)--> 77371252455336267181195264
POW(2,88)--> 309485009821345068724781056
POW(2,89)--> 618970019642690137449562112
POW(2,90)--> 1237940039285380274899124224
POW(2,92)--> 4951760157141521099596496896
POW(2,94)--> 19807040628566084398385987584
POW(2,95)--> 39614081257132168796771975168
POW(2,96)--> 79228162514264337593543950336
POW(2,98)--> 316912650057057350374175801344
POW(2,101)--> 2535301200456458802993406410752
POW(2,104)--> 20282409603651670423947251286016
POW(2,106)--> 81129638414606681695789005144064
POW(2,109)--> 649037107316853453566312041152512
POW(2,110)--> 1298074214633706907132624082305024
POW(2,111)--> 2596148429267413814265248164610048
POW(2,112)--> 5192296858534827628530496329220096
POW(2,114)--> 20769187434139310514121985316880384
POW(2,115)--> 41538374868278621028243970633760768
POW(2,116)--> 83076749736557242056487941267521536
POW(2,120)--> 1329227995784915872903807060280344576
POW(2,121)--> 2658455991569831745807614120560689152
POW(2,122)--> 5316911983139663491615228241121378304
POW(2,124)--> 21267647932558653966460912964485513216
POW(2,125)--> 42535295865117307932921825928971026432
POW(2,126)--> 85070591730234615865843651857942052864
POW(2,128)--> 340282366920938463463374607431768211456
POW(2,132)--> 5444517870735015415413993718908291383296
POW(2,133)--> 10889035741470030830827987437816582766592
POW(2,134)--> 21778071482940061661655974875633165533184
POW(2,135)--> 43556142965880123323311949751266331066368
POW(2,136)--> 87112285931760246646623899502532662132736
POW(2,138)--> 348449143727040986586495598010130648530944
POW(2,141)--> 2787593149816327892691964784081045188247552
POW(2,142)--> 5575186299632655785383929568162090376495104
POW(2,144)--> 22300745198530623141535718272648361505980416
POW(2,147)--> 178405961588244985132285746181186892047843328
POW(2,150)--> 1427247692705959881058285969449495136382746624
POW(2,151)--> 2854495385411919762116571938898990272765493248
POW(2,158)--> 365375409332725729550921208179070754913983135744
POW(2,163)--> 11692013098647223345629478661730264157247460343808
POW(2,164)--> 23384026197294446691258957323460528314494920687616
POW(2,165)--> 46768052394588893382517914646921056628989841375232
POW(2,166)--> 93536104789177786765035829293842113257979682750464
POW(2,167)--> 187072209578355573530071658587684226515959365500928
POW(2,169)--> 748288838313422294120286634350736906063837462003712




problem no 2-:


Again a simple one , to find out the factorial of a number.
Same constraint , the number might b any big.

input -: 5
output-: 120

input-: 1000
output-:
4023872600770937735437024339230039857193748642107146325437999
10429938512398629020592044208486969404800479988610197196058631
66687299480855890132382966994459099742450408707375991882362772
71887325197795059509952761208749754624970436014182780946464962
91056393887437886487337119181045825783647849977012476632889835
95573543251318532395846307555740911426241747434934755342864657
66116677973966688202912073791438537195882498081268678383745597
31746136085379534524221586593201928090878297308431392844403281
23155861103697680135730421616874760967587134831202547858932076
71691324484262361314125087802080002616831510273418279777047846
35868170164365024153691398281264810213092761244896359928705114
96497541990934222156683257208082133318611681155361583654698404
67089756029009505376164758477284218896796462449451607653534081
98901385442487984959953319101723355556602139450399736280750137
83761530712776192684903435262520001588853514733161170210396817
59215109077880193931781141945452572238655414610628921879602238
38971476088506276862967146674697562911234082439208160153780889
89396451826324367161676217916890977991190375403127462228998800
51954444142820121873617459926429565817466283029555702990243241
53181617210465832036786906117260158783520751516284225540265170
48330422614397428693306169089796848259012545832716822645806652
67699586526822728070757813918581788896522081643483448259932660
43367660176999612831860788386150279465955131156552036093988180
61213855860030143569452722420634463179746059468257310379008402
44324384656572450144028218852524709351906209290231364932734975
65513958720559654228749774011413346962715422845862377387538230
48386568897646192738381490014076731044664025989949022222176590
43399018860185665264850617997023561938970178600408118897299183
11021171229845901641921068884387121855646124960798722908519296
81937238864261483965738229112312502418664935314397013742853192
66498753372189406942814341185201580141233448280150513996942901
53483077644569099073152433278288269864602789864321139083506217
09500259738986355427719674282224875758676575234422020757363056
94988250879689281627538488633969099598262809561214509948717012
44516461260379029309120889086942028510640182154399457156805941
87274899809425474217358240106367740459574178516082923013535808
18400969963725242305608559037006242712434169090041536901059339
83835777939410970027753472000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000

Welcome..

Hi frnds ,
i hve created this blog to share all my technical knowledge, tips and tricks of the trade. I will write about lot of technical questions which might not be important for the placement point of view but are really important if u want to excel in the field of computers . It will include questions from general concepts of hardware to low level OS programming , networking ( all the layers , socket programming , platforms used for , Client Server architecure ) , Data structures, C , C++ , Java and many other things as well.