cancel
Showing results for 
Search instead for 
Did you mean: 

What is the syntax to import and use a global variable from C in an assembly script? Im using stm32cube IDE, and have tried various ways with no luck.

JRigh.1
Associate II
 
7 REPLIES 7

Not a big GNU/GAS guy but

/* uint32_t foo; */
 
 
   ldr r1, =foo
   ldr r0, [r1, #0]
   adds r0, r0, #1
   str  r0, [r1, #0]

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks for responding Clive. I have zero experience in assembly, so apologies in advance. Here is part of the code I have in the assembly script, toggling a gpio pin. I want to tell the assembly snippet to choose between different pins by turning GPIO_ODR_REG and PIN_LOCATION into global variables set in the main C code. So far only complaints by the compiler with no assembly declaration of the two variables, a .extern declaration, a .globl declaration, and a few others that I have forgotton. Any ideas?

turnOnZero:

  LDR R4,=GPIO_ODR_REG //SET R4 GPIO_ODR register location

  LDR R0,[R4] //load number at R4 to R0

  ORR R1,R0, #PIN_LOCATION //set bit location

  STR R1,[R4] //Write new ord bits to ODR register

  LDR R2,=ZERO_ON_INTERVAL

delay3:

CBZ R2, turnOffZero

SUBS R2,R2,#1

B delay3

R4 is the address of the variable (symbol), not the content. ie a pointer

You'd want LDR R4,[R4] to pull the value

You should be able to use the debugger, and step into the code and watch the registers. Often people just pass in parameters, as these transfer as registers and avoid the need for global variables and functions that don't scale well.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I think my problem is how to get the external variable (or its address) into an assembly register at all, or to find out its name if it exists. For example if my external variable is PIN_LOCATION, I get an error when I do something like this

LDR R3,=_BIT_POSITION

or

LDR R3,=BIT_POSITION

I get an "undefined reference" error.

Regardless of the programming language, RMW to GPIO_ODR is a bad idea. Use GPIO_BSRR.

As Clive said, write it in C, then look at the asm output of compiler.

JW

I've got a very narrow window of view into what you're doing at either end.

The .MAP file should list the exact name of your variable on the C side of things.

Like I said people normally pass in these things as parameters when calling functions from C

If doing from the assembler side

.global  _pin_number
_pin_number:  .word  3   /* C side  extern uint32_t _pin_number; */
 
   ldr  r1, =_pin_number /* read the address */
   ldr r2, [r1, #0] /* read the value */

/* In close proximity this would also work, ie pc relative */
 
   ldr r0, _pin_number /* r0 would read the value in _pin_number, ie 3 */

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
JRigh.1
Associate II

Thank you both for the advice. Im going to convert over to RMW GPIO_BSRR, and I think it makes sense to rework everything to pass parameters via the function call. In that case, the parameters go into R0-R4? I'm trying out several methods for my own understanding of assembly and how to work with it in the context of STM32Cube IDE.