;registers in 64 bit programming

;The following information about 64 bit registers is from: Writing 64-bit programs by Jeremy Gordon
;http://www.godevtool.com/GoasmHelp/64bits.htm#zerox

;The code examples in the code section are by Fred Kennedy

;"The EAX,EBX,ECX,EDX,ESI,EDI,EBP and ESP "general purpose" registers are all enlarged to 64-bits. 
;The enlarged registers are accessed using RAX,RBX,RCX,RDX,RSI,RDI,RBP and RSP
;You can still access the low dword of these registers (ie. the least significant 32 bits) by 
           ;using the existing names EAX,EBX,ECX,EDX,ESI,EDI,EBP and ESP.
;You can still access the lowest word of these registers (ie. the least significant 16 bits) by 
           ;using the existing names AX,BX,CX,DX,SI,DI,BP and SP.
;You can still access the first byte of RAX,RBX,RCX and RDX (ie. the least significant 8 bits) by 
           ;using the existing names AL,BL,CL,DL as in the 86 processor. 
;But you can now also address the first byte of the "index" registers by 
           ;using SIL,DIL,BPL and SPL. So for example SIL is the least significant 8 bits of the index register RSI.
;You can still access the second byte of RAX,RBX,RCX and RDX (bits 8 to 15) by using the existing names AH,BH,CH,DH as in the 86 processor. 
           ;However, the opcodes for this have been altered in the AMD64 processor. 
		   ;They now clash with the opcodes required to address the byte versions of the extended registers R8 to R15. 
		   ;So you cannot use AH,BH,CH,DH and R8B to R15B in the same instruction.
;
;There are eight new 64-bit registers (the "extended registers") named R8 to R15.
;The low dword of these registers (ie. the least significant 32 bits) can be addressed using the R8D to R15D forms.
;The low word of these registers (ie. the least significant 16 bits) can be addressed using the R8W to R15W forms.
;The first byte of these registers (ie. the least significant 8 bits) can be addressed using the R8B to R15B forms."
;
;


;*************************PROTOTYPES*****************************

ExitProcess PROTO  ;from Win32 api not Irvine

;************************DATA SEGMENT***************************

.data

    qnum1    qword   1234567812345678h
    qnum2    qword   8765432187654321h

;************************CODE SEGMENT****************************

.code

main PROC

 	mov		rax, qnum1			    ;rax = qnum1

    ;the following is illegal
    ;If you try to copy to memory an immediate value larger than a dword
    ;you will get an error. But you can legally copy an qword to memory from
    ;a 64 bit register


    ;mov qnum1, 1234567812345678h  ;illegal

    ;but the copying a 64 bit value to memory from a 64 bit register is legal

    mov     rax, 1234567812345678h  ;rax = 1234567812345678h

    mov     qnum1, rax              ;legal to copy to mem from a 64 bit register

    ;copying an immediate dword value to 64bit variable is legal

    mov     qnum1, 12345678h        ;legal to copy a dword or smaller immediate value to memory

	mov		al, 0aah			    ;al = aah, and upper 56 bits of rax does not change

	mov		ax, 0bbbbh			    ;ax = bbbbh and upper 48 bits of rax does not change

	;Note: when you work with the dword part of a 64 bit register the upper 32 bits are automatically zeroed out.
	
	mov		eax, 0ffffffffh		    ;eax = ffffffffh and upper 32 bits of rax is zeroed out

	mov		r8, qnum1			    ;r8 = qnum1

	;mov ah, r8b    ;compile error: AH, BH, CH, or DH may not be used with R8b-R15b

	mov		r8b, 12h			    ;low byte of r8 = 12h, upper part of r8 not changed
	mov		r8w, 0ffffh			    ;low word of r8 = ffffh, upper part of r8 not changed
	mov		r8d, 0cccccccch		    ;low dword of r8 = cccccccch, upper part of r8 is zeroed out

    mov     r8, 0ffffffffffffffffh  ;r8 filled with ffffffffffffffffh

    mov     r8d, 1                  ;mov 1 into the dword part of r8. Also clears out the upper part of r8

	mov		rsi, qnum2			    ;rsi = qnum2

	;you can address the low byte of rsi (sil) which is also the low byte of esi and si
	;(same would apply to rdi whose low byte would be dil)
	
	mov		sil, 0aah			    ;low byte of rsi = aah, upper part of rsi not changed

	mov		si, 0bbbbh			    ;low word of rsi = bbbbh, upper part of rsi not changed

	mov		esi, 0cccccccch		    ;low dword of rsi = cccccccch, upper part of rsi is zeroed out

	;you can also address the low byte in rsp (esp, sp) with spl
	;and the low byte in rbp (ebp,bp) with bpl

	mov		ecx, 0				    ; 0 exit code for exit process
	
	call    ExitProcess			    ;exit program

main ENDP
END
