ASM macros Tips ... or tricks...

Bare metal programming in PureBasic, for experienced users
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

ASM macros Tips ... or tricks...

Post by Tenaja »

Here is a set of macros that can remove the conditional compilation of most instructions between x86 and x64. There is no provision for instructions that are not 1:1, but that could be dealt with too, if there are not many.

The first macro is required to allow the substitution of the register names. Otherwise the code is handled directly as asm code, and substitutions are not made. The second conditional set allows the eight registers to be defined without the conditional wrap, so there are 10 macros instead of 16.

Code: Select all

Macro asm(instruction, arg1, arg2)
	!instruction arg1, arg2
EndMacro

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
	Macro eORr
		r
	EndMacro
CompilerElse
	Macro eORr
		e
	EndMacro
CompilerEndIf


Macro xax
	#eORr#ax
EndMacro
Macro Xbx
	#eORr#bx
EndMacro
Macro Xcx
	#eORr#cx
EndMacro
Macro Xdx
	#eORr#dx
EndMacro

Macro Xbp
	#eORr#bp
EndMacro
Macro Xsp
	#eORr#sp
EndMacro
Macro Xsi
	#eORr#SI
EndMacro
Macro Xdi
	#eORr#di
EndMacro


; Sample:
asm(MOV, xdx, [p.p_MemoryBuffer])
asm(MOV, xax, [p.p_Character])
User avatar
STARGÅTE
Addict
Addict
Posts: 2088
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ASM macros Tips ... or tricks...

Post by STARGÅTE »

These Macros are strange.
Why not only:

Code: Select all

CompilerIf SizeOf(Integer) = SizeOf(Long)
	Macro rax : eax : EndMacro 
	Macro rbx : ebx : EndMacro
	Macro rcx : ecx : EndMacro
	Macro rdx : edx : EndMacro
	Macro rsi : esi : EndMacro
	Macro rdi : edi : EndMacro
	; ...
CompilerEndIf

Define *Buffer.Long = AllocateStructure(Long)

EnableASM
	mov rax, *Buffer
	mov [rax], dword 123
DisableASM

Debug *Buffer\l
In this case I can use my normal x64-ASM code.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

Re: ASM macros Tips ... or tricks...

Post by Tenaja »

When I tried that using the "!" to denote asm, that style of macro did not work. I did not try it with EnableAsm.
Post Reply