2

The ISO C standard states that A "plain" int object has the natural size suggested by the architecture of the execution environment

However, it is also guaranteed that int is at least as large as short, which is at least 16 bits in size.

The natural size suggested by an 8-bit processor, such as a 6502 or 8080, would seem to be an 8-bit int, however that would make int shorter than 16 bits. So, how large would int be on one of these 8 bit processors?

Danh
  • 5,916
  • 7
  • 30
  • 45
Orion
  • 1,157
  • 9
  • 18
  • 3
    C and C++ guarantee that `sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)`, so an `int` can never be smaller than a `short`, even if the "natural" size would be otherwise. C++ guarantees that `short` is at least 16 bits. Not sure if C makes the same guarantee. – Remy Lebeau Nov 17 '16 at 06:02
  • 2
    Section 5.2.4.2.1 of N1548: `SHRT_MAX` and `INT_MAX` must be at least `32767` – Danh Nov 17 '16 at 06:04

2 Answers2

4

The 6502 had only the instruction pointer as 16 bit register, the 16 bit integers were handled with 8 bits with multiple statements, e.g. if you do in 16 bits c = a + b

clc                 ; clear carry bit
lda A_lo            ; lower byte of A into accumulator
adc B_lo            ; add lower byte of B to accumulator, put carry to carry bit
sta C_lo            ; store the result to lower byte of C
lda A_hi            ; higher byte of A into accumulator
adc B_hi            ; add higher byte of B using carry bit
sta C_hi            ; store the result to higher byte of C

8080 and Z80 CPUs at that time had 16 bit registers as well.

The Z80 CPU was still 8 bit architecture. It's 16 bit registers were eventually pairing two 8 bit registers, like BC, DE. The operations with them were much slower then with 8 bit registers because the CPU architecture was 8 bit, but this way 16 bit registers and 16 operations were provided.

8088 architecture was mixed, because it also had 8 bit data bus, but it had 16 bit registers, AX, BX, etc., lower and higher bytes also separately usably as 8 bit registers, AL, AH, etc.

So there were different solutions to use 16 bit integers but 8 bit is simply not a useful integer. That's why C and C++ used also 16 bit for int.

quantummind
  • 2,086
  • 1
  • 14
  • 20
  • I used to work with an Introl-C compiler on a 6809 (8-bit) architecture and `sizeof(int)` was 16 bits. 6809 also had internal 16 bits registers. – Joël Hecht Nov 17 '16 at 06:17
  • The 6502 definitely did not have any 16 bit registers. You had 8-bit A, 8-bit X, and 8-bit Y. Even the stack pointer was 8 bits. You could argue the instruction pointer was 16 bit, but it was inaccessible and _the instruction pointer_ – Orion Nov 17 '16 at 07:07
3

From Section 6.2.5 Types, p5

5 An object declared as type signed char occupies the same amount of storage as a ''plain'' char object. A ''plain'' int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).

And 5.2.4.2.1 Sizes of integer types <limits.h> p1

Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

...

  • minimum value for an object of type int

    INT_MIN -32767 // -(215 - 1)

  • maximum value for an object of type int

    INT_MAX +32767 // 215 - 1

Then in those platforms, int must be at least 16 bits

Danh
  • 5,916
  • 7
  • 30
  • 45
  • In practice, however, some might use 8-bit `int`: `avr-gcc` has `-mint8` switch for that. –  Nov 14 '17 at 14:49