2

Hey, im using WinAVR and programing an ATMEGA32 in C.
Basically, I want to link my C program to asm via the:

asm(" ") command.

Im trying to define memory locations in C to exact memory locations so that I can then access them within the asm command line.

I have 5 variables:

unsigned char var1, var2, var3, var4, var5;

I know I could use pointers to a memory location, but im unsure how to do this.
Any help would be appreciated.
Thanks,
Oliver.

Ospho
  • 2,538
  • 5
  • 23
  • 38

2 Answers2

3

You can access memory locations by their C variable names with inline assembler using GCC. Please refer to the AVR-GCC Inline Assembler Cookbook for more information. You can also place C variables at exact memory locations using compiler extensions or linker scripts. However, the whole point of compilers and assemblers is so that we don't have to manage tedious details like that.

Judge Maygarden
  • 25,264
  • 8
  • 76
  • 98
3

This method is completely compiler-independent, simple, and straightforward:

#define var1 (*(volatile unsigned char *)0xDEADBEEF)
#define var2 (*(volatile unsigned char *)0xDECAFBAD)
#define var3 (*(volatile unsigned char *)0xBADC0DE)

etc.

R.. GitHub STOP HELPING ICE
  • 195,354
  • 31
  • 331
  • 669
  • 1
    You should use `volatile unsigned char` instead of `unsigned char`. This will ensure that the compiler doesn't optimize away reads/writes from/to the memory locations. – Emile Cormier Apr 11 '11 at 03:01
  • I've defined those statements; now Im trying to access the variable in my C program: var1 = new_key. This returns a syntax error, i've tried playing around with pointers such as *var1 = new_key but to no avail... – Ospho Apr 11 '11 at 09:29
  • What is the error you're getting? Are you really writing C, or are you writing C++? – R.. GitHub STOP HELPING ICE Apr 11 '11 at 14:17
  • The define statements work. But accessing them is returning a syntax error. var1 = new_key; this returns the error:../MorseOut.c:43: error: syntax error before '=' token in winAVR. Im 99% sure this is C, not C++... Both var1 and new_key are volatile unsigned char's... – Ospho Apr 13 '11 at 01:13
  • Did you perhaps mistakenly put a semicolon at the end of your `#define` lines? Show the actual code that's giving syntax errors (both the `#define` and the place you use it) since I'm nearly certain this is a simple typo. – R.. GitHub STOP HELPING ICE Apr 13 '11 at 01:58
  • Yes your right, I added semicolons at the end of the define statements. Thank you so much for your help! – Ospho Apr 13 '11 at 07:41
  • Semicolons belong at the end of statements, but `#define` is not a statement, it's a preprocessor directive. – R.. GitHub STOP HELPING ICE Apr 13 '11 at 13:00