This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Declare assembler variable for C source file

Hello,

how to declare the register R1 in my assembly code to R1 in the C source file to be able to use:

C-Program

extern void key_board(void);

extern unsigned char R1;     // ?

main {

....

 if (R1 == 0x03) { ….. } 

....

}

Assembler-Code

NAME	KEY_BOARD

TASTEN_PORT     EQU P1             ; benuetzter PORT
P_ANZ           EQU  03H           ; Anzahl Portleitungen
;TASTATUR_MODUS  BIT  [0]          ; Steuerbit fuer Modus (20.0)
EINGAENGE       EQU  00010111B     ; Maske fuer Tastaturport:
								   ; Tastaturpins = 1
								  ; unbenuetzte Portpins:
								  ; Eingaenge : 1
								  ; Ausgaenge : 0
TASTEN_NUMMER   EQU  01H          ; Register 1
SPALTEN_ZAEHLER EQU  02H          ;          2
ZEILEN_ZAEHLER  EQU  03H          ;          3
AUSGABE_MASKE   EQU  04H          ;          4
EINLESE_MASKE   EQU  05H          ;          5

ZAHLEN          EQU    00H
FERTIG          EQU    03H
LOESCHEN        EQU    05H
	

?PR?key_board?KEY_BOARD    SEGMENT CODE
	EXTRN	CODE (c_func)
	EXTRN   CODE (key_input)
	PUBLIC	key_board

	RSEG  ?PR?key_board?KEY_BOARD
key_board:
;****************************************************************
TASTATUR:
	 MOV   A,TASTEN_PORT      ; Pruefen, ob Taste noch
	 ANL   A,R5               ; aktiv ist
	 JZ    TADA               ; Sprung, falls Taste aktiv
	 SJMP  WARTE              ; Modus 0+1 : Taste wieder inaktiv

TADA:
	 SJMP  KEINE_TASTE1       ; Modus = 1 : alte Taste noch aktiv

WARTE:
	 LCALL ENTPRELL           ; Entprellzeit abwarten
	 MOV   A,TASTEN_PORT      ; Pruefen ob Taste noch aktiv ist
	 ANL   A,R5
	 JZ    WARTE              ; Sprung, falls Taste noch prellt

STARTE_ZYKLUS:
    MOV   R4,#0FEH           ; Startwert fuer Ausgabemaske
    MOV   R1,#00H           ; Startwert fuer Tastennummer
    MOV   R2,#P_ANZ         ; Startwert fuer Spaltenzaehler
......

    RET
    
    END

Kind Regards

Juergen B.

  • Hi Juergen,

    #include <absacc.h>
    // Register Bank 0
    #define R1 DBYTE[1]

    Be aware that this most likely does not work as expected, because the compiler is free to re-use R1 at any time. Instead, create a global variable in RAM. You're using C, so you do so (easier):

    data unsigned char MYR1;  // explicitly placed in DATA
    


    In the ASM file, you refer to it by using EXTRN:

    EXTRN DATA:BYTE(MYR1)
    .
    .
    .
    mov MYR1, #012h
    

    I'm not quite sure what you're really trying to achieve, though. The compiler generates code it assumes to be best for the C you wrote. This is certainly not the same as the hand-written code by you, and you can't tell the compiler to do so. I tend to say: If you don't want to use C then, well, don't use it. Otherwise, write C and stop using assembly language for something as simple as debouncing keys. Use assembly language only directly where you have to or where the code generated really stinks.

  • how to declare the register R1 in my assembly code to R1 in the C source file to be able to use:

    In a nutshell: you don't.  In a mixed C-with-some-assembly program, you must generally assume that none of core registers of the CPU are free to use by your assembler code.  The compiler needs them for its own work.

    As to the job this particular assembly function is meant to do for you, let's just say that the reasoning by which you decided at this is something you need, or even should do in assembler, is almost certainly flawed.

  • Hello,

    The Arm Compiler (and likely all Arm-based compilers) obey the Arm Procedure Call Standard.

    https://github.com/ARM-software/abi-aa/releases

    When a function is called, registers r0-r3 are used to pass parameters, and the return value is passed back in r0.

    When writing asm code to work with C code, you MUST also obey this same standard.

    You may wish to also see this Learning Path:

    https://learn.arm.com/learning-paths/microcontrollers/efficient_embedded/

  • Apologies - I believe from other threads you are using 8051 class devices.

  • Hello,

    thanks for your advice.

    I understood the thing with the assembler function in my C source file.

    I will of course convert the assembler function to “C”.

    But this is not just a simple function to query keys.

    My function:

    6 buttons should be queried via three port pins (with three diodes for decoupling).

    Kind Regards

    Juergen