-2

I saw a code like below:

section .data
   db 'hello'
   db  'h', 'e', 'l','l','o'
   data_segment_size  equ $-$$

Why data_segment_size is equal to 25? I know what $ and $$ indicate, but I can't figure out why this variable is 25 according to my book. maybe that is a mistake in that book.

Alireza
  • 43
  • 1
  • 1
  • 8
  • those questions don't have a direct relation to my question. I have to symbol, a $ and a $$. – Alireza Dec 18 '17 at 11:18
  • 1
    Certainly you are capable of reading about two different constructs and then inferring how one might be used with the other. – Jonathon Reinhart Dec 18 '17 at 11:22
  • If you have reproducible example (working source, which is compiled by NASM, and does produce value `25`), you can check with listing option (`-l `) to see, how the `.data` section is translated into machine code bytes, and what did produce 25 of them. – Ped7g Dec 18 '17 at 11:40
  • (thinking about it, check if the book has example sources for chapters, maybe there's asm source producing the 25 ... I was also afraid the book doesn't use UTF8 encoding, so even English letters are multi-byte, but by checking NASM docs I think NASM supports only UTF8 and nothing else, so your source in question should produce 10). .... it may be also the author did use Persian "hello" in UTF8 first, got `25`, then later edited the book to have "hello", but didn't fix length? Try to rewrite the hello in Persian and compile it with listing, what you will get. Maybe that's the source of 25. – Ped7g Dec 18 '17 at 11:46
  • But 2x hello should be even-length any way.... Unless your language is so complex, that `'hello'` translates differently than separate letters `'h', 'e', ...`. – Ped7g Dec 18 '17 at 11:49
  • Is it possible that somewhere in the same file there is another `.data` section? – Michael Petch Dec 18 '17 at 13:20

1 Answers1

5

Short answer:

$ means "address of here".

$$ means "address of start of current section".

So $-$$ means "current size of section".

For the example you've given, this will be 10, as there are 10 bytes of data given.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • how is it equal to 25? – Alireza Dec 18 '17 at 11:20
  • 1
    Perhaps you have other things in the `.data` section you haven't shown. Is what you've posted a [mcve]? – Jonathon Reinhart Dec 18 '17 at 11:23
  • it is an example in a book. it stated that this variable is 25 and i could'nt find out how. – Alireza Dec 18 '17 at 11:25
  • I would expect it to be 10. Can you point us to the example? Which book? – Jonathon Reinhart Dec 18 '17 at 11:26
  • 1
    @Alireza: it's either "just example" with made out values, or some detail is missing from your description (target platform? command line options?). Include maybe in question as many details as possible, including the book name and page, etc... When I tried to assembly your `.data` with nasm as elf32 binary, the data segment size is expected `10`, not `25`. – Ped7g Dec 18 '17 at 11:27
  • it is an academic book in my native language. It is in Persian. – Alireza Dec 18 '17 at 11:28
  • Well I believe the book is wrong. – Jonathon Reinhart Dec 18 '17 at 11:30
  • maybe you are right and we have 10 bytes, 5 bytes for each line. thank you. – Alireza Dec 18 '17 at 11:30
  • @Alireza that will quite limit people capable to verify it, but you would certainly get less downvotes, if it is clear you are actually doing your research and have very particular problems with the book. At the moment it looks like you did write your question at a first contact with `$` and `$$` symbols, i.e. no effort/research. Anyway you need reproducible source achieving value `25` to have real case. If the book just writes 25 without any explanation/justification, then you are out of luck. I tried now also elf64 target, and I'm still getting `10`. – Ped7g Dec 18 '17 at 11:33
  • 1
    @Alireza: is the `.data` section in book complete, isn't it "and add this to your old .data" example, building up on previous source? Is it using english letters in ASCII encoding? (I can imagine Persian (not sure which alphabet is that, imagine any symbol-like, like arabic, hebrew, kanji, etc) "hello" in UTF8 encoding would take much more than 5 bytes, at least 2 bytes per character, for rare alphabets even 3-4 bytes). And NASM works with UTF8 source and `db` "correctly", as I would expect it, producing valid UTF8 content in memory. I.e. `ř` translates as two bytes `C5 99`. – Ped7g Dec 18 '17 at 11:36