Assembler Functions (32/64 bit) Compatibility

Started by Charles Pegge, February 08, 2025, 12:57:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

This gets a bit complex. But it shows the protocol required for calling functions with ms64.

'08/02/2025

'32/64 bit compatible Assrmbler
'ms64 calling convention
'using a stack frame instead of push/pop
'passing params in registers
'
'8 byte register width is automatically
'down-sized to 4 bytes on 32bit system
'this is a feature of OxygenBasic

macro FunI as int(r, n,a=0,b=0,c=0,d=0)
=======================================
sub rsp,32 'create stack frame
mov ecx,a 'param 1
mov edx,b 'param 2
#if sizeof(sys)=8
  mov r8, c
  mov r9, d
#else 'sizeof(sys)=4
  mov [rsp+16],c
  mov [rsp+24],d
#endif
call n
'release srack frame
add rsp,32
mov r,eax
end macro


subroutine Calc
===============
'assume int32 params
mov eax,ecx 'param 1
mov ecx,edx 'param 2
#if sizeof(sys)=8
  #define aj 8
  sub rsp,aj 'maintain 16 byte alignment
  'mov edx,r8d param 3
  'mov edx,r9d param 4
#else 'sizeof(sys)=4
  #define aj 12
  sub rsp,aj 'maintain 16 byte alignment
  'mov edx,[rsp+32] 'param 3
  'mov edx,[rsp+40] 'param 4
#endif
add eax,ecx
'sub eax,ecx
'imul ecx
'cdq 'needed for div or idiv
'idiv ecx
'shl eax,2
'shr eax,2
'rol eax,1
'ror eax,1
'
'result in eax
add rsp,aj 'adjust stack ptr for return
'implicit ret
===============
end subroutine

'TEST
'====
print FunI(Calc,-1,3) '2