Page 1 of 1

Combined x86 / x64 source

Posted: Fri Mar 02, 2012 7:43 am
by wilbert
Sometimes all it takes to make x86 code work on x64 is to change some register names.
A lot of CompilerIf statements can make the code harder to read.
I tried to use Macro's to make things easier.

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  Macro _RegB : ebx : EndMacro  
  Macro _RegD : edx : EndMacro  
CompilerElse
  Macro _RegB : rbx : EndMacro  
  Macro _RegD : rdx : EndMacro  
CompilerEndIf

Procedure.l FNV32a(*key, len.l)
  EnableASM
  MOV _RegD, *key
  MOV ecx, len
  PUSH _RegB
  !mov eax, 2166136261 
  !fnv32a_loop:
  MOVZX ebx, byte [_RegD]
  !xor eax, ebx
  !imul eax, 0x01000193
  INC _RegD
  !dec ecx
  !jnz fnv32a_loop
  POP _RegB
  DisableASM
  ProcedureReturn
EndProcedure
Does someone know a better way to handle this ?

Re: Combined x86 / x64 source

Posted: Fri Mar 02, 2012 10:18 am
by Thorium
I think this is a nice way to handle it.

In most cases i do 2 versions of the complete ASM code, so i need only one CompilerIf. Makes the source big, but many CompilerIf's make the source confusing. But i think your approach using macros is much better.

Re: Combined x86 / x64 source

Posted: Fri Mar 02, 2012 10:20 am
by Michael Vogel
Looks already fine for me -- maybe enabling asm inline support will allow you to simplify the macros...

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	Macro rbx : ebx : EndMacro
	Macro rdx : edx : EndMacro
CompilerEndIf

Procedure.l FNV32a(*key, len.l)
	MOV rdx, *key
	MOV rbx, len
	PUSH rbx
	MOV eax, 2166136261
	!fnv32a_loop:
	MOVZX ebx, byte [rdx]
	XOR eax,rbx
	IMUL eax, 0x01000193
	INC rdx
	DEC ecx
	JNZ fnv32a_loop
	POP rbx
EndProcedure