以零字节结尾的 ASCIIZ 字符串
ASCIIZ string ending with a zero byte
我正在编写一个汇编级程序来创建一个文件。
.model small
.data
Fn db "test"
.code
mov ax,@data
mov ds,ax
mov CX,00
lea DX,Fn
mov ah,3ch
int 21h
Mov ah,4ch
Into 21h
End
虽然程序没有错误,但是没有创建文件,所以我在网上搜索了一下原因。
然后我找到了ASCIIZ。
所以我用
替换了数据段
.data
Fn db "test", 0
成功了。
为什么我们需要使用 ASCIIZ,为什么不能使用普通字符串来创建文件?
假设您的 .data
部分有多个字符串:
Fn db "test"
s1 db "aaa"
s2 db "bbb"
编译时,.data
部分将包含所有 3 个字符串,一个接一个:
0x74 0x65 0x73 0x74 0x61 0x61 0x61 0x62 0x62 0x62
这是 testaaabbb
的二进制表示。
必须有一种方法可以让函数找出第一个字符串的结束位置和第二个字符串的开始位置。这个 "marker" 是 0x00 字节( "\x00" ),也称为 "null byte terminated string" 或 ASCIIZ,这样你就可以知道你的字符串在哪里结束:
Fn db "test",0
s1 db "aaa",0x00 ; is the same
s2 db "bbb\x00" ; still same thing
现在您的 .data
部分将如下所示
0x74 0x65 0x73 0x74 0x00 0x61 0x61 0x61 0x00 0x62 0x62 0x62 0x00
这是 test\x00aaa\x00bbb\x00
,现在您在字符串之间有了分隔符,因此当您将字符串的起始地址提供给函数时,它会知道字符串的确切结束位置。
我正在编写一个汇编级程序来创建一个文件。
.model small
.data
Fn db "test"
.code
mov ax,@data
mov ds,ax
mov CX,00
lea DX,Fn
mov ah,3ch
int 21h
Mov ah,4ch
Into 21h
End
虽然程序没有错误,但是没有创建文件,所以我在网上搜索了一下原因。
然后我找到了ASCIIZ。
所以我用
替换了数据段.data
Fn db "test", 0
成功了。
为什么我们需要使用 ASCIIZ,为什么不能使用普通字符串来创建文件?
假设您的 .data
部分有多个字符串:
Fn db "test"
s1 db "aaa"
s2 db "bbb"
编译时,.data
部分将包含所有 3 个字符串,一个接一个:
0x74 0x65 0x73 0x74 0x61 0x61 0x61 0x62 0x62 0x62
这是 testaaabbb
的二进制表示。
必须有一种方法可以让函数找出第一个字符串的结束位置和第二个字符串的开始位置。这个 "marker" 是 0x00 字节( "\x00" ),也称为 "null byte terminated string" 或 ASCIIZ,这样你就可以知道你的字符串在哪里结束:
Fn db "test",0
s1 db "aaa",0x00 ; is the same
s2 db "bbb\x00" ; still same thing
现在您的 .data
部分将如下所示
0x74 0x65 0x73 0x74 0x00 0x61 0x61 0x61 0x00 0x62 0x62 0x62 0x00
这是 test\x00aaa\x00bbb\x00
,现在您在字符串之间有了分隔符,因此当您将字符串的起始地址提供给函数时,它会知道字符串的确切结束位置。