Wednesday, May 12, 2010

Lvalue & Rvalue.

Lvalue and Rvalue discussion unwittingly took many minutes of students time in his collage days. And it bought smiles on the face of an interviewer and frown on the face of interviewee. But with such an impressive resume, a programmer does not put much thought to it once he/she get out of the collage and clears the interview. This is one topic which is comparatively hard to explain than actually put it into action. Because it comes in naturally , which is very hard thing to come by in 'C'.

The rule simply put, LV is the address of , and RV is the actual contents.

Lets see what Lvalue (LV) & Rvalue (RV) are with a simple 'C' program.

Lesson:01: This will not compile.

...
int a = 0;
/* Here 'a' is RV. And its actual contents is 0 (zero). While numerical 5 is LV (or trying to be) because what then is the address of '5'. This program will fail with "lvalue required ..." error. By this the compiler is saying that to store the contents ( RV aka 0) i need an address. With numerical 5 i am unable to get it. So i am angry and i will not proceed */
5 = a;
printf("a = %d\n",a);
...

Lesson:02: This will compile.

...
int a = 0;
/* Here 'a' is LV. And its address will be '&a'. While numerical 5 is RV and its contents is also 5. */
a = 5;
printf("a = %d\n",a);
...


Lesson:03: This will compile. This is extension of Lesson:02 to explain things better.

...
int a = 0;
int b = 5;
/* Here 'a' is LV. And its address will be '&a'. While for 'b', the compiler will take the contents which is 5 , so here 'b' is RV */
a = b;
printf("a = %d\n",a);
...


Thanks
Arshad

Sunday, May 9, 2010

The Computer Architecture, The Engineers and The Programmer.

I was lucky to see computer when it was still growing in India. My GWBASIC course in school (sometimes i used to get a BASICA floppy) would happily run on Modi-Olivetti 8086 machines. And I started to call myself a programmer. With GWBAISC ( programming language ) I thought I could do anything with the machine. But in fact it would drive me crazy thinking how people could code prince of Persia, while i would struggle to get a "block" in graphic mode move (XORing, And I learned about Blitting later). Few years later while going through Ray Duncan ( Advance MSDOS programming ) book it hit me, I came to know about 4 screens in x86 Architecture, where you loop through and give an impression of movement.

Architecture... is this important ?

Few years rolled by and I was caught in the likes of Assembly, C & Linux File-system, while I used to encounter "sizeof(int)", but Architecture still got a step motherly treatment from me. Architecture for me was "morris mano" and it ended with collage.

One day, being confident in C and assembly language i decided to code my own OS. As a challenge to myself and as a test to myself that if I claim I know every bit of this computer system, lets write a minimal 32bit protected OS. After all I know C and assembly, how hard could this be.

I was wrong. Very wrong. My pride too a beating, my ego was thrashed. With all the C and assembly I could not budge an inch with my new OS. I understood programming language is just a vehicle to express my ideas to the CPU. I must be friend with the structure of the computer or the Architecture. Once I shelved my C & UNIX books and got hold of Intel/AMD developers guide. My OS since then has made huge strides.

Now I understood GDT's & IDT's, why LDT's are not used ( seldom used ) after x386. Now i can take bare cpu and bring it up. I completely understood the relationship between ring0-3 and DPL. The call-gates, and why if you do not set up interrupt while OS bring-up and that interrupt is generated ... it goes for a triple fault. For many more please refer Intel/AMD manual. ;-)

People working on board bring-up or device driver, or any OS, knowledge of CPU Architecture is compulsory and it completes the programmer. Any training institution or collage or university who does not stress upon training Architecture leaves its students incomplete. And I see many esteem one's doing this...unfortunately.

I'll conclude this by quoting Intel Architecture S/w Developer Manual Starting Note " ... Refer all four volumes when evaluating your design needs".

Saturday, May 8, 2010

Writing Device Driver, without installing full kernel source

It is possible to write device driver without installing and compiling
the full kernel source. But there is a disadvantage, with this method
as any changes to the kernel ( eg: enable lguest etc ...) will not be
possible. For changes to kernel options you will have to install the
full kernel source. And do the cycle of make ; make install ; make modules
etc...

Ok , back to topic. Quick way to write device driver....

; install few packages
# apt get install make build-essential exuberant-ctags libncurses5-dev
; get your kernel version
# uname -r
; install kernel headers
# apt-get install linux-headers-()

; Fire away you device driver (kernel) code.

Allow Root to Remote login via ssh

; install package
# apt-get install openssh-server

; edit sshd_config
# vi /etc/ssh/sshd_config
And have this line "PermitRootLogin Yes" uncommented. Or add
your own.

; restart ssh server
# /etc/init.d/ssh restart