This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Global variables

Other Parts Discussed in Thread: MSP430G2553

Hi,

I am a total newbie in both assembly programming and microcontrollers, but I am having a lot of fun reading J. Davis "MSP430 microcontroller basics". I have chosen mspgcc & mspdebug as my IDE since I am a linux user.

I am having trouble finding out how to allocate global variables, i.e. stored in flash memory. Davis uses RSEG directive as in relocatable assembly. My question is where can I find the absolute addresses for storing global variables using ORG directives instead?

By the way, I am using MSP430G2553 on Launchpad.

Thank you in advance!

  • Hello anakletor,

    If you are using the ORG directive, then you are specifying the memory location for that segment.  The manual for the device will have a memory map, usually in the first section.

    Since the MAP430 uses the von-Neumann architecture, you will want specify the address from the beginning of RAM 0x200 to the end of Flash 0xFFDH.  The address you chose is the absolute address.

    I hope this answers your question.

    Thanks,

  • Thank you, Greenja!

    So I assume that I am free to choose whichever address in Flash I like, as long as it doesn't interfere with the rest of the code. I could for instance have global variables stored at the begynning of Flash (0xC000) and the text code 100 bytes later (0xC064).

  • I would advise you not to use Flash to store any variable, global or not.

  • old_cow_yellow said:

    I would advise you not to use Flash to store any variable, global or not.

    I kind of got the impression he meant global constants. IMHO it is ok to use FLASH region for that and not use limited RAM and initialized storage for constants. Of course, C language keyword const can be used to help guarantee correct usage. Since he's using asm, it's up to him to do that.

  • The user guide says that Flash can be used for both code and data. But anyway, whether it is good or bad programming practice is not my concern at this moment. I am a beginner, so all my programs so far are just meant for checking and consolidating the concepts I've learnt.

    Thank you for answering!

  • anakletor said:
    The user guide says that Flash can be used for both code and data.

    Data, yes. Variables, not really. <- no nitpicking, please.

    If you are looking for true variables that you can both read AND write during program execution, then you should be using RAM, not FLASH.

    If you just want to store constant data (lookup tables, math constants, etc) then it is ok to put those in FLASH.

  • Brian Boorman said:

    Data, yes. Variables, not really.

    That was a more precise definition.

  • Well if we are going to give the new guy a lecture, let's give him a proper one!

    As you will discover in your readings, most textbooks recommend only using FLASH for code.  The reason for this was due to the fabrication methods of FLASH which led to failure after repeated writes.  So the idea was only use it for constants and lookup tables, as Brian had stated.  The general exception is for storing critical pieces of data such as configurations, date and time in the event of a power failure.

    Early micro contained a suffient block of EEPROM that had better write life cycles.  If you notice today, there are few MCU that have EEPROM.  Perhaps it has to do with the change in fabrication of FLASH and the life cycle has increased 10x since the early days.  I will leave that to you to research.

    The other reason for not using FLASH for variables was speed.  RAM is the fastest then EEPROM then FLASH.  Also, if your MCU was in the middle of a write and there was a power failure, you could end up corrupting your entire code.

    There are exceptions to every rule, and FLASH is one of them.  There are manufactures of MCU that allow you to write over a single Byte of FLASH, without having to erase an entire block and have life cycles that will outlast your great grandchildren.  So the FLASH restriction is not written in stone.  In developing your future applications, one of the things to check is the write/rewrite life cycle of  FLASH if blocks of data have to be retained.  Or select one with EEPROM.

    Hope we didn't scare you off...cause we are just getting warmed up!

    When I was a youngster I saw a professor working on a project.  I said, "Wow, I would like to be able to do that!"  He said, "no you don't".  I should have listened!

    Cheers,

  • anakletor said:
    global variables, i.e. stored in flash memory

    These are two independent things.

    Usually, you want to store constants in flash (whether locally or globally available). Variables msu tbe in ram, as they aren't "variable" if you cannot change them. :)

    Teh difference between local and global variabels is the scope.

    Local variables do not generate a global symbol. So their name isn't passed to the linker and you cnanot use them in two different code files. Global variables can be used in multiple files and the linekr is fusing them together at linktime. However, as they are variables, they are usually in ram, whether local or global.

    anakletor said:
    where can I find the absolute addresses for storing global variables

    You do not find them, you define them. YOu only use an absolute address when you know that the address is free for this use. Usually, these are hardware registrers disguised as variables. They are at a fixed location and won't move.
    If oyu use a fixed ram address, this is possible too, btu then all code parts as well as the linekr need to know that this address is used for this purpose.
    You then can as well use the address itself in the code instead of a variabel name. The assembler will replace any name token by the value anyway, so the linker won't touch it.

    In the very msot cases, this is really not desireable. Use normal (relocatable) variables and let the linker place them. Much less chance for unintentional double-usage of memory locations.

  • What a wealth of information in these posts! Thank you very much Jens-Michael Gross for clarifying these terms. Global vs local and constant vs variable are actually familiar concepts to me since I have some experience in C programming. Somehow I thought that everything outside main() or any other functions would go to Flash, which of course is wrong, since global and constant are totally independent things. I think that the fact that I can choose between flash and RAM for storing values is just confusing me a bit. As I said I am new to MCUs.

    I can perfectly understand that using relocatable variables is safer than using absolute addresses. I am just too fascinated by having total control over the hardware! I can even decide where the code is going to be stored in flash! Same applies to variables and constants. Some day I might get tired of getting errors because some data is stepping onto others territory, but until then I think I will continue using absolute values. hehe

    This leads me to another question. You mentioned linkers. Is the linker doing anything to my assembly program if all addresses are given as absolute values? When I disassemble my programs, they look exactly the same as I wrote. I can see no trace of linker activity whatsoever. I might be missing something.

  • I told ya we just getting warmed up....you thought I was joking didn't you?!

  • anakletor said:
    I think that the fact that I can choose between flash and RAM for storing values is just confusing me a bit. As I said I am new to MCUs.

    Well, from PC, people are used to the the method of loading programs from storage (flash) to ram for execution. Hwoeve,r even on PC, there's teh BIOS rom which directly executes. The MSPs flash is more like this BIOS rom than like the OS or the OS-loaded applications.
    Since most people never had to code a BIOS rom, this concept is new when you move from PC to MCUs.

    By the way, I once bought (and still have) a device called OmniGo100. Based on a V20 or so processor (an embedded 16MHz 80186). It was running the GEOS OS from ROM, as well as all preinstalled applicaitons. Due to the limited ram (1MB for storage, video-memory and applicaiton memory) it was executing the applications diretly from rom. This method, called XIP (eXecute In Place) was revolutionary for what was basically a handheld PC. This whole device did run for two weeks (actual moderate usage, not just standby) from just two AA cells. The best of two worlds, years ahead of its time, and now, barely 20 years later, forgotten.

    anakletor said:
    Is the linker doing anything to my assembly program if all addresses are given as absolute values?

    If all addresses, including the code locations (ORG) are explicitely given and there are no symbold left to resolve across code modules, then the linker is just packing things into an output file that can be written to the MSP. The output of the assembler is not in a form to be directly written to the MSP.

**Attention** This is a public forum