-1

I found this string manipulation code on the internet, that is used to reverse the contents of a string. Can anyone explain how the length of the string is determined, what does the dollar signify in any sort of string program?

model small
.stack 100h  
.data  
String1 db 'assembly language program', $  
Length dw $-String1-1  
.code  
Main proc  
MOV AX, @data  
MOV DS, AX  
MOV SI, offset String1  
MOV CX, Length  
ADD SI, CX  
Back: MOV DL, [SI]  
MOV AH, 02H  
INT 21H  
DEC SI  
LOOP Back  
MOV AH, 4CH  
INT 21H  
End
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
yashz123
  • 11
  • 5
  • 1
    Possible duplicate of [What does the dollar sign ($) mean in x86 assembly when calculating string lengths like "$ - label"?](http://stackoverflow.com/questions/10361231/what-does-the-dollar-sign-mean-in-x86-assembly-when-calculating-string-lengt) – Iłya Bursov May 19 '17 at 17:32
  • A much better question would be "What's the purpose of `$` in `db 'assembly language program', $` ?" That one has me puzzled... C sriings are null-terminated, Pascal keeps the length at start. Who adds the byte with lenght(?) at end? –  May 19 '17 at 17:43
  • assembly language syntax is defined by the assembler (not the target) so this is not so much an 8086 question but a I have some assembler (a program like masm, nasm, gas) and what does this syntax mean. What software are you using? – old_timer May 19 '17 at 18:27
  • @Arkadiy take a look at http://stanislavs.org/helppc/int_21-9.html it looks like somebody mixed different usages of `$` sign – Iłya Bursov May 19 '17 at 18:31
  • @Lashane, sorry, don't see the answer there... The `$` there seems to be a literal dollar sign. –  May 19 '17 at 18:39
  • @Arkadiy right, so it looks like original author of the OP's code mixed $ as "here" and $ as string terminator – Iłya Bursov May 19 '17 at 18:43
  • @Lashane Aha! Thanks, never ran into that one. http://stackoverflow.com/questions/481344/dollar-terminated-strings –  May 19 '17 at 18:46
  • @Arkadiy CP/M hat a system call to print a dollar-terminated string and DOS copied it. It's a really weird design. – fuz May 19 '17 at 21:26

1 Answers1

1
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
Sep Roland
  • 33,889
  • 7
  • 43
  • 76