0

enter image description here

This is the code I'm writing. I'm building a future OS and I'm just now getting into assembly. So far everything is good but Im just stuck on the "$-$$" I know $ refers to the current memory address but then it says $$ refers to the first section. I don't understand what a section is exactly. If its talking about ".section" in my code I don't have that so what is it about?

I tried asking different people but it seems like they were also struggling to explain.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
audiotore
  • 11
  • 2
  • `$$` refers to the start of the current section. It's the same value as if you put a symbol, for example named `start`, at the beginning of your file. If you never specify a section NASM uses a default. – ecm Nov 07 '22 at 05:59
  • 1
    *Any code which comes before an explicit SECTION directive is directed by default into the .text section.* - https://nasm.us/doc/nasmdoc8.html#section-8.1.3 . Legacy BIOS MBR bootloaders don't have segment metadata, so sections aren't super meaningful, and are more likely to just make things more difficult unless you put everything in the same section, as explained in [Why can't my HELLO\_WORLD string be loaded from section .data?](https://stackoverflow.com/q/74322564) – Peter Cordes Nov 07 '22 at 06:06
  • 1
    Hmm, the question title is focused on `$-$$` which has been asked and answered multiple times before. The part about "what's a section" in a flat binary is separate, and seems to have mostly only been answered in comments, or indirectly or as part of other answers. Searching on the quote from the NASM manual in my previous comment only found SO comments, not answers. But as far as how `$-$$` interacts with sections if you do use them in a flat binary, my recent answer covers that. – Peter Cordes Nov 07 '22 at 06:11
  • Effectively this formula gives you the necessary padding so that `db 0x55,0xAA` is always stored at memory locations `0x1FE-0x1FF`, without you having to rewrite the amount of padding bytes every time you change your program. – puppydrum64 Nov 28 '22 at 16:41

0 Answers0