我一直打字DB 13, 10, 'hello world', 0
很长一段时间没有想知道13,10和0是什么。“13,10”在“DB 13,10,'hello world',0”中的含义是什么?
我最近注意到,这样做:
PTHIS
DB 'hello world', 0
产生相同的结果,所以我想知道什么的第一个参数是是否是简单地这样写是个好主意。有人能写一个关于这个的快速解释吗? (我想字符串声明是主题)
我一直打字DB 13, 10, 'hello world', 0
很长一段时间没有想知道13,10和0是什么。“13,10”在“DB 13,10,'hello world',0”中的含义是什么?
我最近注意到,这样做:
PTHIS
DB 'hello world', 0
产生相同的结果,所以我想知道什么的第一个参数是是否是简单地这样写是个好主意。有人能写一个关于这个的快速解释吗? (我想字符串声明是主题)
这是ASCII CR/LF(回车/换行)序列,用于推进到下一行的开始。
历史课:在旧的电传打字机,回车确实做到了,它返回的马车(打印头)到当前行的开始,而换行推进纸,使印刷将在发生下一行。
你的两个样品不应产生相同的结果。如果光标不在CR/LF
的字符串的起始位置,则Hello world
将显示在某处的中间位置,并且即使确实从行的开始位置开始,CR/LF
的版本应首先将光标向下移动一行。
最后的零就是字符串的终止符。一些早期的系统中使用其他字符,如$
在原来的BIOS:
str db "Hello, world$"
这使得它相当痛苦输出$
标志控制台:-)
的终止是因为有您的字符串输出几乎肯定会被写入字符输出的术语,如伪汇编代码:
; 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
13是CR ASCII码(carriage return)的十进制值,图10是十进制VAL LF ASCII码(line feed)的ue,0是字符串的终止零。
这个常数背后的想法是在打印前更改为下一行hello world
。打印子程序需要零终止符来知道何时结束打印。这与null terminating of C strings类似。
试试这个
PTHIS
DB 'hello world'
DB 10 ;line feed
DB 13 ;carriage return
DB 'hello world2',0
然后轻摇代码
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
,并看看会发生什么