Access variable declared in C from asm.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable
        Hi,   
   
If I have a variable declared in a C file as;   
   
   
volatile long seconds;   
   
   
and want to access that from a timer interrupt ISR in asm to increment it, how do I go about that?   
   
The compiler is complaining about not knowing what seconds is in the asm file (undefined symbol). So how can I define it in the asm file?   
   
At the moment I'm doing something like   
   
and F, 0x00 ;   
inc [seconds] ;   
jnc _end ;   
   
   
   
And the same for each subsequent byte [seconds+1, 2 & 3].   
   
Is this the "right" way? Or is there a better way?   
   
Cheers,   
James.   
0 Likes
5 Replies
MR_41
Employee
Employee
First like received
        For accessing a C variable in assembly, you have to add an underscore before the variable. The code should be   
   
inc [_seconds]   
   
Some more points to remember.   
   
1. The variables in PSoC Designer are in Big Endian format. So, to increment the LSB of seconds, use [_seconds + 3]   
2. If you are dealing with a device with more than 256 bytes of RAM, you may also have to set the CUR_PP register to point to the currect RAM page. Check out the document on Large Memory model in the Help >> Documentation section of PSoC Designer for details about.   
0 Likes
Anonymous
Not applicable
        Thanks Ganesh.   
I've got to look in to the CUR_PP register stuff because although it compiles it doesn't seem to be incrementing the seconds variable.   
   
Cheers,   
James.   
0 Likes
MR_41
Employee
Employee
First like received
        James,   
   
There is one more approach you can try. This is to define the variable in assembly and then access it in C. You can place the variable in RAM Page-0 so that inside the interrupt, you do not have to change the CUR_PP register. Below is the procedure.   
   
1. In the Timer ISR, define the variable like this.   
   
area InterruptRAM(ram)   
_seconds:   
seconds: BLK 4   
   
This will place the seconds variable in Page-0. Make sure that you place the above instructions inside the custom user code area in the Timer's ISR file   
   
2. To access this variable from a C file, add the following declaration either in a header file that is included in the source file or directly in the source file   
   
extern long seconds;   
   
Now you can access the seconds variable in C as well. Also, as the variable is now placed in Page-0 and as all the memorey operations are by default done on Page-0 inside an interrupt, you can access the variable inside the ISR without changing the CUR_PP register.   
0 Likes
Anonymous
Not applicable
        That works a treat, thanks!   
0 Likes
MR_41
Employee
Employee
First like received

You are most welcome    

0 Likes