0

I came across this following code:

SYS_EXIT  equ 1
SYS_WRITE equ 4
STDIN     equ 0
STDOUT    equ 1
section  .text
   global _start    ;must be declared for using gcc
_start:   ;tell linker entry point
    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg1         
        mov edx, len1 
        int 0x80                

    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg2         
        mov edx, len2 
        int 0x80 

    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg3         
        mov edx, len3 
        int 0x80
        mov eax,SYS_EXIT    ;system call number (sys_exit)
        int 0x80            ;call kernel

section  .data
msg1 db 'Hello, programmers!',0xA,0xD   
len1 equ $ - msg1           
msg2 db 'Welcome to the world of,', 0xA,0xD 
len2 equ $ - msg2 
msg3 db 'Linux assembly programming! '
len3 equ $- msg3

with intuition i can make out that len1, len2 and len3 are variables holding the lengths of the three strings and that the $ - operator is fetching the length of it..

but i am not able to understand properly how the syntax to find the length works.. can anyone, please tell me how it does and give me links for further reading, to understand this concept..

Thanks in advance...

Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59

1 Answers1

3

$ evaluates to the "current address", so $ - msg1 means "the current address minus the address with the label msg1". This calculates the length of the string that starts at msg1.

Your snippet looks like it might be NASM. Is it? Anyway, NASM has documentation of its special tokens $ and $$.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
  • what if i declare two msgs continuously and then do `$ - msg1` will it give me the the length of both the strings? because as you say, $ is the current address – Anshu Dwibhashi Aug 19 '13 at 11:26
  • and in what sense is $ the "current address"? I mean, what do you mean by "current address" – Anshu Dwibhashi Aug 19 '13 at 11:26
  • The NASM documentation defines it like this: "$ evaluates to the assembly position at the beginning of the line". I found a [duplicate question](http://stackoverflow.com/questions/17900262/what-is-in-nasm-assembly-language?rq=1). It is discussed a bit more there. – Magnus Hoff Aug 19 '13 at 11:28
  • i apologize. but can you please explain it in a simpler manner? – Anshu Dwibhashi Aug 19 '13 at 11:29
  • To your "what if I declare two messages continuously"-question: Yes, it would give you the total length of both the strings. It looks like you have understood it. Do you have any other specific questions? – Magnus Hoff Aug 19 '13 at 11:31
  • nope, thankyou.. just confirming, the current address means, the address the computer is currently executing right? – Anshu Dwibhashi Aug 19 '13 at 11:33
  • i accepted and upvoted... – Anshu Dwibhashi Aug 19 '13 at 11:35
  • 2
    `equ` is an assembly time construct: it is not present in the produced executable, so it is never *executed* per se. The assembler (for example NASM) reads the file from top to bottom and calculates the address ("assembly position") that each line starts on based on the amount of space each previous line takes in the produced executable. This is the "current address" I am talking about. – Magnus Hoff Aug 19 '13 at 11:37