Combined x86 / x64 source

Bare metal programming in PureBasic, for experienced users
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Combined x86 / x64 source

Post 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 ?
Thorium
Addict
Addict
Posts: 1271
Joined: Sat Aug 15, 2009 6:59 pm

Re: Combined x86 / x64 source

Post 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.
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Combined x86 / x64 source

Post 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
Post Reply