claws
claws

Reputation: 54100

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

enter image description here

I've problem with the size of long int on a 16-bit CPU. Looking at its architecture:

enter image description here

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?

Upvotes: 4

Views: 5870

Answers (2)

Mysticial
Mysticial

Reputation: 471299

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.

Upvotes: 12

Dirk Holsopple
Dirk Holsopple

Reputation: 8831

No. If the machine doesn't have registers that can handle 32-bit values, it has to simulate them in software. It could do this using the same techniques that are used in any library for arbitrary precision arithmetic.

Upvotes: 2

Related Questions