News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Current location counter '$'

Started by unktehi, February 25, 2009, 02:56:46 AM

Previous topic - Next topic

unktehi

Can someone help me understand how the $ works? I understand from the presentation that the current location counter subtracts the address of a variable and finds the difference in bytes.  Despite this fact I want to know how it's achieved.

Here was the code in the example:

list BYTE 10, 20, 30, 40
ListSize = ($ - list)

So, I'm going to tell you what I THINK it means, can someone help me if I'm wrong? (I'll use the starting address of 4000 for my example)

Address 4000     list BYTE 10, 20, 30, 40

(Byte 10 at address 4001)
(Byte 20 at address 4002)
(Byte 30 at address 4003)
(Byte 40 at address 4004)

Address 4005     ListSize = (4005 - 4000)

ListSize would equal 4 bytes?


BogdanOntanu

Close but not exactly :P

Why would the first byte be located at 4001 since you do assume a starting address of 4000 ?

The special symbol "$" does represent the current address counter of the assembler at compile time.
It is mostly use in order to avoid making a new label when you do not really need a named label again.

For example without using the "$" symbol you can write:

my_list_start db 10,20,30,40
my_list_end:

my_list_size EQU my_list_end - my_list_start


But if you do not need "my_list_end" the it is sometimes more convenient to write:

my_list_size equ $ - my_list_start


But you have to do this immediately after the list end because otherwise the $ will change with every new data or code definition.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

unktehi

Oh woops how about this:

Address 4000     list BYTE 10, 20, 30, 40
(Byte 10 at address 4000)
(Byte 20 at address 4001)
(Byte 30 at address 4002)
(Byte 40 at address 4003)

Address 4004     ListSize = (4004 - 4000)

Or would ListSize still be located at address 4003?

I assume address 4004 would be correct because if you take 4004 - 4000 then the byte size would be 4 which does equal the amount of BYTEs in 'list.'

Is that a correct assumption?

BogdanOntanu

Yes it is correct because the $ address counter will be incremented after the last byte is defined and will have a value of 4004 waiting for a new definition to emerge.
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

unktehi