What does “13, 10” mean in “DB 13, 10, 'hello world', 0”?

允我心安 提交于 2019-12-08 05:31:59

问题


I've been typing DB 13, 10, 'hello world', 0 for a long time without wondering what the 13, the 10 and the 0 were for.

I recently noticed that doing:

PTHIS
DB 'hello world', 0

produced the same result, so I'm wondering what the first parameters are for and whether is a good idea to simply write it this way. Could someone write a quick explanation on this? (I suppose string declarations would be the topic)


回答1:


It's the ASCII CR/LF (carriage return/line feed) sequence, used for advancing to the beginning of the next line.

History lesson: On old teletype machines, carriage return did exactly that, it returned the carriage (printing head) to the start of the current line, while line feed advance the paper so that printing would happen on the next line.

And your two samples shouldn't produce the same result. If your cursor is not at the start of a line when you output the string without CR/LF, the Hello world will show up mid-line somewhere and, even if you do start at the start of a line, the version with CR/LF should first move the cursor down one row.

The zero at the end is simply a terminator for the string. Some early systems used other characters like the $ in the original BIOS:

str   db "Hello, world$"

which made it rather a pain to output the $ sign to the console :-)

The terminator is there because your string output will almost certainly be written in terms of a character output, such as the 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



回答2:


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

The idea behind this constant is to change to the next line before printing hello world. Zero terminator is necessary for the printing subroutine to know when to end printing. This is similar to null terminating of C strings.




回答3:


Try this

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

Then jiggle 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



来源:https://stackoverflow.com/questions/17266837/what-does-13-10-mean-in-db-13-10-hello-world-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!