Page 1 of 1

NASM => $$ symbol...

Posted: Sun Nov 21, 2010 1:13 pm
by osdevkid
It is a OS Dev forum, but I want to post NASM related query. Pls help me if you know...

The symbol "$$" in NASM gives value 0x20, from the below code.

Code: Select all

[ORG 0]
jmp 07C0h:start     ; initialize CS:IP values (go to code segment 0x07C0)

SECTION .text
start:
   ; Update the segment registers
   mov ax, 0x7C0      ; 0x7C0 is code segment address, same initiated to other registers
   mov ds, ax
   mov es, ax
   mov ss, ax

   mov si, hello      ; print hello string on the screen

   cld         ; clear direction flag (set DF = 0) to increment SI register

   .l1:
      lodsb      ; load [SI] contents in to AL register and increment SI = SI + 1
      cmp al, 0      ; compare 0 (null) is received
      je .l2

      mov ah, 0Eh
      int 10h

      jmp .l1      ; continue to lable 'lbl_1'

   .l2:


   hello db 'Welcome hello world', 0

   times 510-($-$$) db 0
   dw 0AA55h
   db  $$
My understanding is, it should retrun the byte of segment address, am I right ?, please give a justification for the value 0x20 I have received from the above code (create bin file, and open with hex editor, you can see the value 0x20 at offset 0x220)

Re: NASM => $$ symbol...

Posted: Sun Nov 21, 2010 2:37 pm
by bewing
Well, I'm having trouble mentally justifying a value of 0x20 -- I would expect a value more like 7. I don't know why NASM would be aligning the SECTION on a 32byte boundary.

In any case, $$ represents the address of the beginning of the current SECTION or SEGMENT. Since your ORG is 0, and you have one far jump opcode before the beginning of the section, I would think that $$ would be equal to the length of that far jump opcode. Since you are assigning it to a byte, then it will give you the low byte of that address. You probably should be assigning it to a dd instead of a db, since addresses are more than one byte long.

You probably want the far jump inside the section -- because the point of the exercise is to make the signature bytes end up just before offset 0x200 -- not 0x220.

And I think you probably want to put a "BITS 16" in this code, too. :wink:

Re: NASM => $$ symbol...

Posted: Sun Nov 21, 2010 2:57 pm
by osdevkid
bewing wrote:Since your ORG is 0, and you have one far jump opcode before the beginning of the section, I would think that $$ would be equal to the length of that far jump opcode.
Hi thanks for the reply, I am new to ASM, please let me know, which one is the "far jump opcode" in my example code.

Re: NASM => $$ symbol...

Posted: Mon Nov 22, 2010 1:27 am
by Solar
osdevkid wrote:Hi thanks for the reply, I am new to ASM, please let me know, which one is the "far jump opcode" in my example code.
Well, how many jumps are there to chose from?

Re: NASM => $$ symbol...

Posted: Mon Nov 22, 2010 5:02 am
by JamesM
Solar wrote:
osdevkid wrote:Hi thanks for the reply, I am new to ASM, please let me know, which one is the "far jump opcode" in my example code.
Well, how many jumps are there to chose from?
Three.

The far jump opcode is the first instruction in your file, and is marked by the fact that it explicitly denotes a CS value in it:

Code: Select all

jmp 07C0h:start
Note the 07C0h: this causes the CS value to be set, hence "far" jump. Contrast that with a near jump:

Code: Select all

jmp .l1
In which CS stays the same.

James

Re: NASM => $$ symbol...

Posted: Tue Nov 23, 2010 12:40 am
by f2
@osdevkid:
1) You should search for some x86 assembly tutorials.
2) Of course, you should also read the manual of your assembler if it allows you to answer your question about $$ and (probably)
so many others... If you don't get what you really want with it, try another assembler...
3) Once you know all the tricks of assembly language, maybe you will be ready to start coding an OS...

Re: NASM => $$ symbol...

Posted: Thu Nov 25, 2010 11:32 pm
by TylerH