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?
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.
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?
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.
Great thanks for your help!