2. Instructions use the REX prefix byte in 64bit mode to specify 64bit operands. The REX byte is placed immediately before the first opcode byte and after all legacy prefixes.
3. 64bit mode has more registers available so more arguments are passed via registers than on the stack. The call to printf is smaller in 64bit.
4. The REX prefix occupies range 40h - 4Fh, so the single byte opcode forms of the INC/DEC instructions, that occupy the same range, are not available in 64bit mode. The ModR/M forms of these instructions (FFh) is still available.
[REX byte]
This is a byte-sized prefix used in 64bit mode in order specify 64bit operands and registers of size other than 32bits. Format of the REX byte is -
Bit position - 7 6 5 4 3 2 1 0
Bit value - 0 1 0 0 W R X B
The high nibble is always 0100 which is 4h. So the REX prefix range is 40h – 4Fh.
W bit : 1 – 64bit operand, 0 – operand size determined by CS.D bit (CodeSegment descriptor, refer Intel system programming manual).
R bit : This adds a 4th bit to the reg field in ModR/M byte. Enables addressing the new registers (R8-R15).
X bit : This adds a 4th bit to the Index field in SIB byte.
B bit : This adds a 4th bit to the r/m field in ModR/M or the Base field in SIB or Opcode reg field.
[Dissecting two instructions]
00007FF780AE17F5 48 C7 45 28 00 00 00 00 mov qword ptr [f0], 0
REX = 48 : which means only the W bit is set, indicating 64bit operand (the qword ptr [f0] operand).
00007FF780AE1876 4C 8B 45 68 mov r8, qword ptr [fn]
REX = 4C : which means both W and R bits are set. W indicates 64bit operand and the R bit adds a 4th bit to the reg field in ModRM byte.
MOV = 8B : this is 64bit register/memory to 64bit register MOV instruction
ModRM = 45 : Mod = 01, R/M = 101 – this is [RBP]+disp so this is the effective address of 'fn'. Register R8 is identified by the 4bit reg field 1000.
Mod R Reg R/M
7 6 5 4 3 2 1 0
0 1 1 0 0 0 1 0 1 (the Rbit from REX adds a 4th bit to the reg field)
Disp = 68 : This is the 8bit displacement added to RBP to get effective address of 'fn'.
int main() { UINT n = 5; size_t f0 = size_t(0); size_t f1 = size_t(1); size_t fn = f0; if (n == 1) { fn = f0 + f1; } else if (n > 1) { for (UINT itr = 2; itr <= n; ++itr) { fn = f0 + f1; f0 = f1; f1 = fn; } } printf("Fibonacci(%u) = %Iu\n", n, fn); return 0; }
size_t f0 = size_t(0);

