wordsize is decided upon target architecture when compiling. Your compiler will normally compile using
wordsize for current system.
gcc (among others) you can also tune this by using various flags. E.g. on a 64-bit host you can compile for 32-bit machine, or force 32-bit words.
-m32 # int, long and pointer to 32 bits, generates code for i386.
-m64 # int, long and pointer to 64 bits, generates code for x86-64.
-mx32 # int, long and pointer to 32 bits, generates code for x86-64.
You should also look at
inttypes.h to see the usage of this
For cross-compiling check out multilib (32-bit link on SO) and search tha web.
Check what flags your GCC was built with by:
As to the sizes they are usually closely related to the central processing unit
and related – such as maximum size of a memory address, size of CPU registers
For a quick glimpse, you do not need to understand much of this, but depending
on where you are it can give some insight:
If you use
gcc and compile with the
-S flag you can also look at the assembly instructions. Here, a bit confusing, on e.g. 32-bit machine a
word is 16 bit and long is 32-bit. (
movl $123, %eax means move long (32-bit -
eax register, and
movw means move word (16-bit).
This is naming conventions, – and only to say that
WORDSIZE can mean more
then one thing. You can also come across code where they e.g. define something
#define WORD_SIZE 16
as it all depends on context. If you read data from a file or stream where
source has word-size of 16 bit, this would be natural. Only to point out that
do not always assume word-size means
__WORDSIZE when read it in code.
Example with user defined
WORD_SIZE would not affect instruction set in the
generated machine code. For GCC in general I would recommend this book. (Unfortunately it is a bit old – but have yet to find a similar easy to read more up to date book. (Not that I have looked that hard.) It is short, concise and sweet. And if you only keep in
mind that things can have changed, such as added features etc., it gives good introduction non the less.)
It gives a quick and nice introduction of the various aspects when compiling.
Look at chapter 11 for a nice compile chain explanation.
I do not know of any options in GCC to compile 16-bit. One way to do it would
be to write in assembly using
.code16 to instruct the code should be 16-bits.
.code16 /* Tel GAS to use 16-bit instructions. */
.globl start, _start
movb $0x48, %al
This is needed by e.g. boot loaders such as GRUB and LILO for the code
MBR on your hard drive.
Reason for this is that when your computer boots up the CPU is in a special
mode where it does not have 32-bit but max 16-bit instructions AKA
In short what happens is that BIOS do a hardware test, then it
loads the first 512 bytes of your boot disk into memory and leaves control to
that code starting at address
0. This code in turn locates where next stage
of files reside, load these into memory and continue executing finally entering
Protected Mode where you have normal 32-bit mode.