NASM => $$ symbol...

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

NASM => $$ symbol...

Post 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)
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: NASM => $$ symbol...

Post 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:
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: NASM => $$ symbol...

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: NASM => $$ symbol...

Post 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?
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: NASM => $$ symbol...

Post 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
User avatar
f2
Member
Member
Posts: 311
Joined: Mon Jun 15, 2009 10:01 am
Location: France

Re: NASM => $$ symbol...

Post 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...
"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
TylerH
Member
Member
Posts: 285
Joined: Tue Apr 13, 2010 8:00 pm
Contact:

Re: NASM => $$ symbol...

Post by TylerH »

Post Reply