Result in Asm

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Result in Asm

#1 Post by arco » 29 Jun 2021 10:39

How do you use Result (function return parameter) in an Asm part of a function?
(Result, _Result, MyFunction_Result, FARG_MyFunction_Result,...?)
Regards,

Peter.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Result in Asm

#2 Post by janni » 29 Jun 2021 15:47

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

Code: Select all

long maxi(long a, long b){
 long result;
 if (a>b) result=a;
  else result=b;
 return result;
}
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)

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
 }
}
Compiler will issue warning about missing result but still will use the result placed in R0.

arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Re: Result in Asm

#3 Post by arco » 29 Jun 2021 19:23

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...
Regards,

Peter.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Result in Asm

#4 Post by janni » 29 Jun 2021 22:38

arco wrote:
29 Jun 2021 19:23
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...
Oh, sorry, I was switching between posts and thought I was still in mC forum :oops: . In case of mB (and mP) situation is much simpler - the format is

Code: Select all

<routine_name>_local_result
and no tricks are necessary. Other info on assembly syntax may be found here.

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
It's handy in time-critical applications (where use of assembly makes most sense).

arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Re: Result in Asm

#5 Post by arco » 30 Jun 2021 08:26

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?
Last edited by arco on 30 Jun 2021 12:31, edited 1 time in total.
Regards,

Peter.

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Result in Asm

#6 Post by janni » 30 Jun 2021 11:54

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.

arco
Posts: 312
Joined: 15 Apr 2008 13:54
Location: The Netherlands

Re: Result in Asm

#7 Post by arco » 02 Jul 2021 09:43

After studying assembly and listing files, this seems to be the way to access Result within assembly: (with _local_ added)

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
Would be nice to have some detailed helpfile on the assembly syntax.
(so we don't have to re-invent the wheel everytime... :( )
Regards,

Peter.

Post Reply

Return to “mikroBasic PRO for PIC General”