Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 236
claws Avatar asked Dec 11 '22 20:12

claws


1 Answers

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.

like image 193
Mysticial Avatar answered Jan 07 '23 22:01

Mysticial