could we declare variable by give address ?

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

Has any one ever declare variable by give address? Such as I need to declare the variable name "temp" at address 0x0180. How to implement it in C ? I tried to find it in manual, but never help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you can certainly assign 0x180 to a pointer and then write through that to write a char/int/long value (depending on pointer type) at 0x180 onwards but this is VERY dangerous in a C program. How do you know that the compiler hasn't already located one of the other variables at location 0x180?

About the ONLY reason I can think of for needing variables at absolute addresses is when you want to run two co-operating programs (a bootloader and an application) in the same AVR. But if you are doing this there's far better methods than just agreeing on some common address that you don't know the existing usage of!

(to pass a value from boot to app an unused SFR is probably a far better idea)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

About the ONLY reason I can think of for needing variables at absolute addresses ...

Memory-mapped peripherals?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I need to send out more variable value on the terminal not array variable, my idea if I could declare the variable by give them the address, it is easy to use the pointer to point them for send out to the terminal. Actually if we declare variable, the complier will automatically give them the address, but not a sequence. I need to do in sequence.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

variables+sequences = arrays ?!?

Doesn't the C compiler do an equally valid job of locating arrays for you as it does for individual variables then?

Last Edited: Tue. Aug 7, 2007 - 03:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Different C compilers will have different mechanisms.

For the "in sequence" part, many compilers WILL allocate memory in sequence. But it might depend.

The short answer is that I'm sure that many of us have had an application similar to yours, and rarely or ever resorted to specifically allocating at an address. There are many alternatives. A structure comes to mind. Arrays as well, but you have discarded them for some reason. Drop into ASM and use .ORG . Write the whole thing in ASM (right, js?). Each in its own linker section, and then place them where you desire. Or, with my compiler, just put them next to each other in the order desired.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
Or, with my compiler, just put them next to each other in the order desired.

Same with my compiler EXCEPT that there are two groups - those that are assigned an initial value and those that aren't (which default to 0). Otherwise (in ONE compilation unit) you are pretty much guaranteed that they locate in memory in the order in which the compiler comes to their definitions - though I'm not sure I'd rely on this - as Lee says, that's what structures are for - to group data objects.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I ever do like this in some other C complier, it provide me "_directmap" command, like this

_directmap unsigned char temp = 0x0180;

I found this methods in manual, after tried it work fine. So, I never consider on a structure. I will try on structure.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you told us yet which compiler you are using?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And I still don't understand how locating things at absolute addresses makes them any easier to send to the terminal? If you need to know the location of a variable (that the compiler assigned for you) what's wrong with the "address of" operator '&' ? (unless it's an array in which case the variable name itself provides its address as it's really a pointer already)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm guessing, Cliff, that OP is building some kind of string or packet and doesn't want to rebuild from scratch each time. Sure would be easier if we knew the destination when we pack for the journey. [Which compiler, work so far, desired output, problem areas, ...]

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Have you told us yet which compiler you are using?
I'm ever use this method in the C compiler for NEC microcontroller (CC78K0) not in WINAVR. So I'm trying to find the same method in AVR.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Aaaah--so we now finally know it is GCC.

Those users frequent here, but you might re-phrase and re-post on the GCC forum. Have you done a good search for prior discussions? IIRC there have been several.

[edit] Yup, a search of the GCC forum for "absolute address" quickly uncovers a number of pertinent threads.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah but the majority of those threads basically say (like this one) - "why bother?" This whole things seems to be an arseways solution to a problem that doesn't actually exist

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, Cliff,

I know how to write circular buffers in both C and assembler. The issue here is that by making the buffer 2^n bytes long and positioning it at a 2^n byte boundary, the compiler doesn't need math to do the wrapping. The code:
index = (index+1) & 0x1f;
will actually require mathematical tests if the first byte of "buffer" doesn't sit at an address for which the lowest 5 bits are zero.

How can I ensure that "buffer" will be placed to meet this condiction?

Regards,
Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just split the post you made to this thread to here:

https://www.avrfreaks.net/index.p...

as it has NOTHING ot do with the existing subject of this thread - please start new threads in future or append to on topic threads.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought it relevant because I also require a variable stored at an absolute address, or at least one that meets a specific condition. However, if that isn't close enough, then thanks for splitting it.
Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No this is C - the linker decides where to position things in memory - not the programmer (usually) - but let's continue this in the new thread...