How do you use Result (function return parameter) in an Asm part of a function?
(Result, _Result, MyFunction_Result, FARG_MyFunction_Result,...?)
Result in Asm
Result in Asm
Regards,
Peter.
Peter.
Re: Result in Asm
mC compiler either forms some local variable for the result and/or places the result directly in internal registers Rx to be passed to calling function. One may force forming a result variable by declaring it, like
Then the result variable may be reached from assembly as <function name>_result_L0+[offset] (offset designates consecutive bytes of result, starting from 0).
If you write the function body in assembly then pass the result in Rx registers (as many as type of result requires, starting with R0 as least significant byte). Here's simplified example (for PIC18 processor, assuming valid RAM bank for parameters)Compiler will issue warning about missing result but still will use the result placed in R0.
Code: Select all
long maxi(long a, long b){
long result;
if (a>b) result=a;
else result=b;
return result;
}
If you write the function body in assembly then pass the result in Rx registers (as many as type of result requires, starting with R0 as least significant byte). Here's simplified example (for PIC18 processor, assuming valid RAM bank for parameters)
Code: Select all
char maxi(char a, char b){
asm {
MOVF FARG_maxi_a, 0
SUBWF FARG_maxi_b, 0
BTFSS STATUS,C
MOVFF FARG_maxi_a, R0
BTFSC STATUS,C
MOVFF FARG_maxi_b, R0
}
}
Re: Result in Asm
Compiler also uses a var named result, it would be silly to use yet another var.
I only need to know how to access result in the asm portion of a function...
I only need to know how to access result in the asm portion of a function...
Regards,
Peter.
Peter.
Re: Result in Asm
Oh, sorry, I was switching between posts and thought I was still in mC forum . In case of mB (and mP) situation is much simpler - the format is
Code: Select all
<routine_name>_local_result
One remark from my previous post still remains valid - If you write the function body in assembly then you may pass the result directly in Rx registers and then use the assembly return instruction. To avoid compiler complaints use the empty statement result=result, like in the following example
Code: Select all
sub function maxi(dim a,b as char) as char
asm
MOVF FARG_maxi_a, 0
SUBWF FARG_maxi_b, 0
BTFSS STATUS,C
MOVFF FARG_maxi_a, R0
BTFSC STATUS,C
MOVFF FARG_maxi_b, R0
RETURN
end asm
result=result
end sub
Re: Result in Asm
That would be usable, but the Rx registers are also used by the compiler.
How do I know a certain register (for example R0) is free?
How do I know a certain register (for example R0) is free?
Last edited by arco on 30 Jun 2021 12:31, edited 1 time in total.
Regards,
Peter.
Peter.
Re: Result in Asm
One certainly has to be careful with Rx registers but my suggestion concerned only those of Rx registers that compiler would use anyway for passing the result of function and then for exactly the same purpose. You could have a look at project's *.lst file generated by the compiler to see how it uses Rx registers to pass function result.
Re: Result in Asm
After studying assembly and listing files, this seems to be the way to access Result within assembly: (with _local_ added)
Would be nice to have some detailed helpfile on the assembly syntax.
(so we don't have to re-invent the wheel everytime... )
Code: Select all
Sub Function SwapBits(Dim ByteIn As Byte) As Byte
Result = 0
Asm
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
rrf FARG_Swapbits_ByteIn,f
rlf Swapbits_local_result,f
End Asm
End Sub
(so we don't have to re-invent the wheel everytime... )
Regards,
Peter.
Peter.