Oxygen Basic

Programming => Example Code => General => Topic started by: Charles Pegge on October 17, 2014, 03:02:09 PM

Title: Assembler Functions, Params and Variables
Post by: Charles Pegge on October 17, 2014, 03:02:09 PM

Code: [Select]
include "$/inc/console.inc"
indexbase 0

! funA cdecl (sys a,b,c,d) as sys at _funA
jmp fwd nfun
._funA
push ebp
mov  ebp,esp
sub  esp,0x100
scope
  sys  a at [ebp+0x08]
  sys  b at [ebp+0x0C]
  sys  c at [ebp+0x10]
  sys  d at [ebp+0x14]
  sys  r at edx
  sys  i at ecx
  r=0
  for i=1 to 10
     r=r+a+b+c+d
  next
  =r
end scope
mov  esp,ebp
pop ebp
ret
.nfun

'print "here"
sys a
a=funA 1,2,3,4
print a
waitkey
Title: Re: Assembler Functions, Params and Variables
Post by: JRS on October 17, 2014, 06:18:36 PM
You know how to hurt a guy.  8)

I'm still looking for the magic hooker that knows the ins and outs of BASIC functions.
Title: Re: Assembler Functions, Params and Variables
Post by: Mike Lobanovsky on October 18, 2014, 05:44:27 AM
Hi Charles,

What does =r mean?
Title: Re: Assembler Functions, Params and Variables
Post by: JRS on October 18, 2014, 05:54:37 AM
OT - Where you been? On vacation?
Title: Re: Assembler Functions, Params and Variables
Post by: Charles Pegge on October 18, 2014, 08:40:18 AM
Hi Mike,

=r
-->
mov eax,r
-->
mov eax,edx



Also works with floats

float s
#show =s
-->
fld dword [ebx+0x1000]
Title: Re: Assembler Functions, Params and Variables
Post by: Charles Pegge on October 19, 2014, 01:10:09 AM
Another trick for improving performance in simple numeric procedures, is to use the bespoke cpu calling convention:

Instead of passing params on the stack, registers  ecx,edx,esi,edi, are used. (max 4 params.)

Code: [Select]
include "$/inc/console.inc"
indexbase 0

function funA cpu (a,b,c,d) as sys
#show return a+(b*c)+d
end function

sys a
#show a=funA 1,2,3,4
print a
waitkey


Title: Re: Assembler Functions, Params and Variables
Post by: Emil_halim on February 06, 2015, 11:55:54 AM
nice work Charles,

but why you don't use registers as a parameters

e.g.

Code: [Select]
include "$/inc/console.inc"
indexbase 0

function funA cpu (ecx,edx,esi,edi) as sys
#show return ecx+(edx*esi)+edi
end function

sys a
#show a=funA 1,2,3,4
print a
waitkey
Title: Re: Assembler Functions, Params and Variables
Post by: Charles Pegge on February 06, 2015, 01:19:48 PM
Hi Emil,

The registers are platform-specific.

In 64 bit mode the registers will be  rcx  rdx  r8  r9
Title: Re: Assembler Functions, Params and Variables
Post by: Emil_halim on February 07, 2015, 07:24:50 AM
Quote
The registers are platform-specific.

In 64 bit mode the registers will be  rcx  rdx  r8  r9

i think it is not aproblem at all , with 32bits use ecx,....  and with 64bits use rec,.....

thanks ,

just want to know how much is Oxygen basic bug free?
Title: Re: Assembler Functions, Params and Variables
Post by: Charles Pegge on February 07, 2015, 09:35:10 AM

Hi Emil,

There are plenty more bugs. I guarantee it :)

Most of them are to be found in C/Basic mixed syntax, and the more complex constructs. But within simple Basic and Assembler, you are fairly safe.