Defining variables in assembly

Discussion in 'Embedded Systems and Microcontrollers' started by hira287, May 9, 2009.

  1. hira287

    Thread Starter New Member

    May 8, 2009
    7
    0
    Hey,

    I was wondering if there is a way to define variables in 8051 apart from using EQU. I tried the define storage directive but then the compiler gives errors. I would be grateful if anyone could let me know.

    Thanks.
     
  2. DonQ

    Active Member

    May 6, 2009
    320
    11
    The Define Storage I'm used to ( "DS", I've always called it Define Space, sounds like the same thing), it to simply reserve a section of uninitialized memory. So even if you were not getting compiler errors, I don't know if this would do what you wanted.

    These things depend a lot on the compiler. Different compilers, even for the same processor, can do these these things differently. Best bet is to go through the section of the manual called something like "Assembler Directives".

    It seems like you want a different effect than EQU (merely gives a numerical value to a label, but does not define the space to store it.) Assemblers I am used to use DB (define byte), DW (define word), etc. to initialize values in memory. These are sort of like a combination of EQU and DS, reserve memory and initialize it to a value. Your assembler may use other abbreviations for these same directives.

    You may even find directives for re-defining a storage space, good for temporary storage to be shared by different routines.
     
  3. hira287

    Thread Starter New Member

    May 8, 2009
    7
    0
    Thanks for the reply DonQ.

    Same thing I think. Well, I am using Keil. I have tried DB and DS both but they do not work. Initially I thought I was using them the wrongly but then I checked at a few places, and the instruction I used (label: DS 1) does seem to be right.

    Do not want to use EQU simply because I wouldn't know which memory location to reserve for my variable. Do you think Keil does not understand the DS directive?
     
  4. DonQ

    Active Member

    May 6, 2009
    320
    11
    The only way to know what directives a particular assembler understands it to consult the manual.

    You say it "doesn't work". How doesn't it work? What is the exact error message from the assembler? Or is it a linker error? Or does the program just not work as you expect?

    I know that some assemblers don't want colons after labels. I think some want DS on a separate line (like the line after the label). Look for an example in the documents and follow it, then read the exact error message to find where it is wrong. You might find that the error is something different than what you're thinking.

    Assembler is not like "C" in that there is no published standard for how a particular assembler should work. Even with "C", different vendors seem to know how to do things "better" than the standard, so even there you sometimes have to go to the manual. It's anybodies guess about what the assembler is wanting, except if you get an error message from the assembler/linker.
     
    Last edited: May 10, 2009
  5. hira287

    Thread Starter New Member

    May 8, 2009
    7
    0
    Its working now!:) I have no idea why though. I just looked at the manual, Keil has it the same way I was using it. There must have been some other problem. Oh, yes, I was redefining a variable. Imagine I've spent hours on this!

    I really didn't know that assemblers have no standard directives. Will consult the manual more often in the future.

    Thanks a lot for your help Don.
     
  6. DonQ

    Active Member

    May 6, 2009
    320
    11
    Good advice for us all! :D
     
Loading...