Finding variable addresses and overlay variables

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
RexSon
Posts: 25
Joined: 13 Oct 2010 10:29

Finding variable addresses and overlay variables

#1 Post by RexSon » 21 Jan 2011 18:47

Anyone have an idea how to find the start address of a variable defined in basic but accessed in ASM?

For example I define a string variable whilst in Basic:

Dim MSG1as string[11]

MSG1="Hello World"

so I know that somewhere in RAM there are 12 bytes reserved for MSG1([Hello World]plus [<NULL>])

If I now use the inline assembler to write some code how do I find the (for example) address in RAM of the third character ("l") in the string MSG1?

Interestingly:
asm
.
.
.

Mov _MSG1, W4

.
.
.
end asm

will move 0X4865 into W4

but trying anything like
mov _MSG1+3, W4
gives a compile error
as does
mov.b _MSG1,W4.

What I want to be able to do is manipulate the contents of string MSG1 (defined in Basic), on a byte by byte or bit by bit basis, whilst in an interrupt routine coded in ASM

The second part of the question is to do with "overlay" variables. These are variables (defined in basic not asm) that take up no addition ram but point (?) to parts of an existing variable.

So for example I might in some way define a variable as follows:

Dim Second_Char_of_MSG1 as byte but also pointing to the 2nd character in the MSG1 string

In this case
Second_Char_of_MSG1 would then equal 0x65, this being the ASCII value of "e"
or whilst in ASM:

Mov.b _Second_Char_of_MSG1, W4

in which case the lower byte of W4 would contain 0x65.

Any ideas would be welcome

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Finding variable addresses and overlay variables

#2 Post by slavisa.zlatanovic » 09 Feb 2011 10:27

Hi!

Please, see the Linker directives in the Help file:
Directive absolute
The directive absolute specifies the starting address in RAM for a variable. If the variable spans more than 1 word (16-bit), higher words will be stored at the consecutive locations.

The absolute directive is appended to the declaration of a variable:

dim x as word absolute 0x32
' Variable x will occupy 1 word (16 bits) at address 0x32

dim y as longint absolute 0x34
' Variable y will occupy 2 words at addresses 0x34 and 0x36Be careful when using absolute directive, as you may overlap two variables by accident. For example:

dim i as word absolute 0x42
' Variable i will occupy 1 word at address 0x42;

dim jj as longint absolute 0x40
' Variable will occupy 2 words at 0x40 and 0x42; thus,
' changing i changes jj at the same time and vice versaNote: You must specify an even address when using the directive absolute.
Therefore, you can absolute your variable to some address in the RAM and use this (compile-time known) address in your asm code.

P.S.
We strongly encourage users to switch to PRO version because non-PRO version is an ancestor of PRO compiler
and is no longer being developed.
Every new feature, improvement and bug fix will only affect new versions of PRO compilers.
If you're a registered user you'll receive PRO license key free of charge.
Best regards
Slavisa

RexSon
Posts: 25
Joined: 13 Oct 2010 10:29

Re: Finding variable addresses and overlay variables

#3 Post by RexSon » 10 Feb 2011 10:44

Thank you for your reply.
I have spent my time gainfully employed since I posted this request. So I have now "unravelled" the way variables are stored in memory (I think!)

I have have two further questions.
1.
If I define a string ( say dim MSG as string [20]) then MSG[4] will point to the 5th (?) character in the string.
Now if I assign as follows:
MSG[4]="A" works fine but so does MSG[4]=65. In both cases the 4th (or 5th if you count the 0th character as 1st). In this way MSG[4] is behaving both as a "Char" and a "Byte"
Question:
Are both methods correct?
Can the method support (legally) an expression say MSG[4]=60+4?

2.
Questions:
2.1
OVELAYS
May I legally define a second variable (say X) and force it to refer to the character/byte at MSG[4] such that If MSG="abcabc" and then set x=65 so that MSG now = "abcAbc". Conversley set X=0 and then MSG="123456" so that x now becomes 54 (decimal value for ASCII character "4")
2.2
If this is correct(legal?) then how do I force the location of X to be at the same position as the 4th character of MSG?
{My guess is that MSG could be defined as an absolute address so that we would know the location of the first character of MSG so that we could then define X also at an absolute address +4 on from the first character of MSG. However I would prefer to find away to allow the compiler to allocate a place for MSG and then deduce the position X. In my experience compilers like to do their own thing!!!!}

Caveats
I am aware of the need to maintain the correct location of the NULL character at the end of the string in memory AND after the last character if the string is less than the defined length

eg If DIM MSG as string [8] and MSG="ABCD" then the bytes in memory (decimal notation) will be 65 66 67 68 0 ? ? ? 0 (where ? means any value but probably 0 at the start)

User avatar
slavisa.zlatanovic
mikroElektronika team
Posts: 1321
Joined: 07 Apr 2009 09:39

Re: Finding variable addresses and overlay variables

#4 Post by slavisa.zlatanovic » 11 Feb 2011 15:42

Hi!
1.
If I define a string ( say dim MSG as string [20]) then MSG[4] will point to the 5th (?) character in the string.
Now if I assign as follows:
MSG[4]="A" works fine but so does MSG[4]=65. In both cases the 4th (or 5th if you count the 0th character as 1st). In this way MSG[4] is behaving both as a "Char" and a "Byte"
Question:
Are both methods correct?
Can the method support (legally) an expression say MSG[4]=60+4?
Yes, for both questions.
OVELAYS
May I legally define a second variable (say X) and force it to refer to the character/byte at MSG[4] such that If MSG="abcabc" and then set x=65 so that MSG now = "abcAbc". Conversley set X=0 and then MSG="123456" so that x now becomes 54 (decimal value for ASCII character "4")
2.2
If this is correct(legal?) then how do I force the location of X to be at the same position as the 4th character of MSG?
You could use the absolute directive or pointers as shown below:

Code: Select all

program Test

dim txt1 as char[20] absolute 0x300
dim char1 as char absolute 0x304
dim pchar as ^char

main:
pchar = @txt1[4]
txt1 = "mikroElektronika"
char1 = "E"
char1 = 32
pchar^ = "M"
pchar^ = " "
asm nop end asm
end.
Best regards
Slavisa

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”