8 位机器上 int 的大小

Size of int on 8-bit machines

ISO C 标准声明 A "plain" int object has the natural size suggested by the architecture of the execution environment

但是也保证int至少和short一样大,也就是至少16位大小

8 位处理器(例如 6502 或 8080)建议的自然大小似乎是 8 位 int,但是这会使 int 短于 16 位。 那么,这些 8 位处理器之一的 int 有多大?

来自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>).

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

那么在那些平台上,int必须至少是16位

6502只有指令指针作为16位寄存器,16位整数用8位处理,有多个语句,例如如果你用 16 位 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和Z80CPUs也是16位的寄存器

Z80 CPU 仍然是 8 位架构。它的 16 位寄存器最终配对两个 8 位寄存器,如 BC、DE。使用它们的操作比使用 8 位寄存器慢得多,因为 CPU 架构是 8 位的,但这种方式提供了 16 位寄存器和 16 次操作。

8088架构是混合的,因为它也有8位数据总线,但它有16位寄存器,AX,BX等,低字节和高字节也分别用作8位寄存器,AL,AH等.

所以有不同的解决方案来使用 16 位整数,但 8 位整数根本不是有用的整数。这就是为什么 C 和 C++ 也对 int 使用 16 位。