Menu

C variables & arguments access in asm

Help
joe
2023-09-27
2023-10-05
  • joe

    joe - 2023-09-27

    Hi

    How can I read a C argument in ASM ? And vice versa.

    void    fon(char arg) {
    
        __asm        
    
            ld a, #_arg    ; ?? 
    
        __endasm;                                                                                                                                       
    } 
    

    I get this error:

    ?ASlink-Warning-Undefined Global '_arg' ...

    Version:

    sdcc -v
    SDCC : stm8 TD- 4.3.0 #14184 (Linux)
    published under GNU General Public License (GPL)

     

    Last edit: joe 2023-09-27
    • Benedikt Freisen

      I am unfortunately not familiar enough with inline assembly in SDCC to know whether there is a convenient way to read C arguments from asm blocks, but when in doubt, you can always look up where the calling convention puts that argument and read it from there, i.e from the appropriate register or stack location.

       
      • joe

        joe - 2023-09-27

        It's not via registers like in GNU C. Looking at the examples in the SDDC manual you just have to add a underscore to the argument. "arg" => "_arg".

        But it doesn't work. Perhaps something to add in the compilation line ?

         
        • Philipp Klaus Krause

          Registers vs. stack vs. other ways depends on the target architecture (and the use of the --sdcccall option, and keywords that modify the calling convention; see the manual sections about the calling conventions).

           
          • joe

            joe - 2023-09-28

            So I found out that the argument is passed through the A register, at least the first one, if 8 bits.

            Variables can be access by adding an underscore to their name.
            Only if it's a global variable.

            char    var = 0b000001000;
            
            void    fon(char arg) {
            
                arg; /* just to skip the message: 
                      "warning 85: in function fon 
                      unreferenced function argument : 'arg'" */
               __asm
            
                   ; register A already contains "arg" 
                   ; "var" is copied into it.
            
                    ld      A, _var
            
                __endasm;
            
            }
            
             
  • Maarten Brock

    Maarten Brock - 2023-10-05

    The simplest way to find this out is to create a dummy function that uses the argument and look at the generated asm.

    void fon(char arg) {
        volatile char dummy = arg;
    }
    
     

Log in to post a comment.