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.

Parents
  • 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.

Reply
  • 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.

Children
No data