Chapter 4.6

64-bit Programming

MOV Instruction

MOV instruction in 64-bit mode is pretty much the same as 32-bit mode one. There are few, small differences between them.
First, logically, immediate operands can be 8, 16, 32, or 64 bits.

Secondly, when you move a 32-bit constant into 64-bit register, upper 32-bits of the destination are cleared (no need for MOVZX or MOVSX)

	mov rax, 0FFFFFFFh   ; RAX = 000000000FFFFFFFh

The same goes for 16-bit and 8-bit constants. All upper bits are cleared

When moving memory operands into 64-bit registers, results are mixed.
Moving 32-bit memory operand into EAX (lower 32 bits of RAX) causes upper bits to be cleared
But moving 16-bit or 8-bit memory operand into lower bits of RAX, the higher bits are not affected.

To move 32-bit register or memory operand, MOVSXD (move with sign-extension) has to be used.

OFFSET operator generates 64-bit address, which has to be saves in 64-bit register or variable

LOOP instruciton in 64-bit mode uses RCX as the loop counter

64-bit Version of SumArray

ExitProcess PROTO

,data                              ; we're using 64-bit integers
	intArray QWORD 1000000000000000h, 2000000000000000h
	         QWORD 3000000000000000h, 4000000000000000h
.code
main PROC
	mov rdi, OFFSET intArray
	mov rcx, LENGTHOF intArray
	mov rax, 0
L1:
	add rax, [rdi]
	add rdi, TYPE intArray
	loop L1
	
	mov ecx, 0                     ; ECX is used as ExitProcess exit value
	call ExitProcess
main ENDP
END

Addition and Subtraction

The ADD, SUB, INC, and DEC instruction affect CPU status flags exactly the same as in 32-bit mode
It's good to remember the size of operands, as mixing them may create unwanted results

	mov rax, 0FFFFh   ; RAX = 000000000000FFFF
	mov bx, 1
	add ax, bx        ; RAX = 0000000000000000

64-bit general-purpose register must be used when an instruction contains an indirect operand. PTR operator must be used to set target operand's size