What does "13, 10" mean in "DB 13, 10," hello world ", 0"?

I printed DB 13, 10, 'hello world', 0 for a long time, without wondering what 13, 10, and 0 were for.

I recently noticed that:

 PTHIS DB 'hello world', 0 

gave the same result, so I'm wondering why the first parameters are needed, and is there a good idea to just write it that way. Can someone write a short explanation about this? (I assume line declarations will be the topic)

+1
source share
3 answers

This is the ASCII CR / LF (carriage / line return) sequence used to jump to the beginning of the next line.

History lesson:. On older teletype machines, carriage return did just that, he returned the carriage (printhead) to the beginning of the current line, while the feed line moved the paper so that printing would occur on the next line.

And your two samples should not give the same result. If your cursor is not at the beginning of the line when outputting a line without CR/LF , Hello world will appear in the middle of the line somewhere and, even if you start at the beginning of the line, the version with CR/LF should first move the cursor one line.

Zero at the end is just a terminator for the string. Some early systems used other characters, such as $ in the original BIOS:

 str db "Hello, world$" 

which makes it painful to display the $ sign on the console :-)

A terminator exists because your line output will almost certainly be written as a symbol output, such as pseudo-asm code:

 ; func: out_str ; input: r1 = address of nul-terminated string ; uses: out_chr ; reguse: r1, r2 (all restored on exit) ; notes: none out_str push r1 ; save registers push r2 push r1 ; get address to r2 (need r1 for out_chr) pop r2 loop: ld r1, (r2) ; get char, finish if nul cmp r2, 0 jeq done call out_chr ; output char, advance to next, loop back incr r2 jmp loop done: pop r2 ; restore registers and return pop r1 ret ; func: out_chr ; input: r1 = character to output ; uses: nothing ; reguse: none ; notes: correctly handles control characters out_chr ; insert function here to output r1 to screen 
+3
source

13 is the decimal value of the CR ASCII code ( carriage return ), 10 is the decimal value of the LF ASCII code ( line feed ), 0 is the terminating zero for the line.

The idea behind this constant is to go to the next line before printing hello world . A zero terminator is required so that the print routine knows when to stop printing. This is similar to the null termination of C lines .

+1
source

try it

 PTHIS DB 'hello world' DB 10 ;line feed DB 13 ;carriage return DB 'hello world2',0 

Then drag the code

 PTHIS DB 'hello world' DB 10 ;line feed no carriage return DB 'hello world2',0 PTHIS DB 'hello world' DB 13 ;carriage return no line feed DB 'hello world2',0 

and see what happens

0
source

Source: https://habr.com/ru/post/903039/


All Articles