String1 db 'assembly language program', $
Length dw $-String1-1
You got 2 $-signs in this program, each with another meaning.
- The 1st $ is a string terminator symbol as required by the DOS string output function 09h. Usually this would have to be written as
...program','$'
or simply included with the rest of the string ...program$'
.
- The 2nd $ is a special assembler symbol that represents the current address. So no matter what line the assembler is at, the $ has the current address.
Let's focus on the 2nd case.
In the line String1 db 'assembly...'
, the user defined symbol String1 represents the address where the string starts in memory. When the assembler subsequently processes the line Length dw ...
, the $ has the address of this line and as a consequence also the address of the end of the previous line (Both are the same).
Since we know where the string starts (String1) and where it ends ($) a simply subtraction is enough to determine the length using Length dw $-String1
.
An additional 1 is subtracted because we don't want the string terminating $ character to be included in the count! Length dw $-String1-1
Be careful with what you found on the internet! This program is wrong. It mistakenly includes the terminating $ character but forgets to use the 1st character of the string.
Next code solves the problem:
MOV SI, offset String1
MOV CX, Length
ADD SI, CX
Back:
DEC SI
MOV DL, [SI]
MOV AH, 02h
INT 21h
LOOP Back