How does a program determine the end of a string?

210 views Asked by At

I can define a new variable like so msg db 'Hello, world!$', or another way msg2 db 'Hello, world!', 0 I know that the end of a string is determined using value 0 in memory. What is symbol $ standing for then?

1

There are 1 answers

2
Brendan On BEST ANSWER

How does a program determine the end of a string?

It depends on the program. A good program would do something like (NASM syntax):

string:    db "Hello World!"
.end:

    mov ecx,string.end - string   ; ecx = length of the string

..and would keep track of string length/s during any modifications (append, truncate, concatenate, etc) so that it always knows the length of strings with almost no overhead at all.

A "less good" program might put the string's length at the beginning of the string. This is something that was done by some old programming languages (e.g. Pascal). This causes problems when you want to work with overlapping strings (e.g. if string2 is the last half of string1 then you can't save memory by making the strings overlap in memory because you'd have to insert a length at the start of string2 that would corrupt the middle of string1).

A "less good" program might also waste CPU time searching the string looking for some kind of terminator (where how bad it is depends on how long the string is - extremely bad for extremely long strings). For MS-DOS that terminator is a '$' character (which makes it extra silly/annoying if you want to have a '$' character in the middle of the string), and for most other cases (e.g. C programming) it's a zero (null character).

Of course for assembly language you can mostly do whatever you like (and can write a good program); until you have to use code that someone else wrote (e.g. MS-DOS API, or code written in some other language).