How can a 16bit Processor have 4 byte sized long int?

assembly, c, cpu-architecture

Solution

Yes. In fact the C and C++ standards require that `sizeof(long int) >= 4`.*

(I'm assuming `CHAR_BIT == 8` in this case.)

This is the same deal with 64-bit integers on 32-bit machines. The way it is implemented is to use two registers to represent the lower and upper halves.

Addition and subtraction are done as two instructions:

On x86:

- Addition: `add` and `adc` where `adc` is "add with carry"

- Subtraction: `sub` and `sbb` where `sbb` is "subtract with borrow"

For example:

long long a = ...;
long long b = ...;

a += b;

will compile to something like:

add eax,ebx
adc edx,ecx

Where `eax` and `edx` are the lower and upper parts of `a`. And `ebx` and `ecx` are the lower and upper parts of `b`.

Multiplication and division for double-word integers is more complicated, but it follows the same sort of grade-school math - but where each "digit" is a processor word.

Problem

I've problem with the size of `long int` on a 16-bit CPU. Looking at its architecture: No register is more than 16-bit long. So, how come `long int` can have more than 16bits. In fact, according to me for any Processor, the maximum size of the data type must be the size of the general purpose register. Am I right?

Original source