I agree, it would be nice to have functions built into PBP. But until that happens, here is a round about way of achieving the same thing, kinda, sorta.
First, you need to create a "macro" that copies the passed parameters into known locations. Then create your subroutine just like normal, keeping in mind that the input and output are always thru the known Temp variables.
After the macro copies the parameters to the Temp variables, it will call the code for the function. When the function is finished it will return to the macro which will copy the result to the third parameter. You can pass as many parameters as you want (as long as they fit on 1 line).
Code:
Temp1 var word
Temp2 var word
Temp3 var word
asm
Foo macro W1, W2, R1
MOVE?WW W1, _Temp1 ; Copy first parameter to Temp Variable 1
MOVE?WW W2, _Temp2 ; Copy second parameter to Temp Variable 2
L?CALL _FooCode ; call the Foo functions code
MOVE?WW _Temp3, R1 ; Copy Temp3 to result
endm
endasm
FooCode: ' PBP code for the function
Temp3 = Temp1 * Temp2
' and whatever else Foo does
' final result must be in Temp3
return
To use the new function
Code:
bar var word
bleh var word
belch var word
@ Foo _bar, _bleh, _belch
or
Code:
Dogs var word
FleasPerDog var word
TotalFleas var word
@ Foo _Dogs, _FleasPerDog, _TotalFleas
The example shown will only work with words, to use bytes: change the appropriate MOVE?WW to MOVE?BB
The biggest benefit of a function is the ability to embed it in a math formula or conditional test. Unfortunately, this Macro type function DOES NOT allow you to do that. So I'm still hoping for true functions in PBP also.
Bookmarks