.ASCIIZ与DB的区别是什么?MASM环境下该如何选择?
Hey there! Let's clear up this confusion since you're working in MASM, which has its own syntax rules that differ from other assemblers like NASM.
First off, a critical point: MASM does NOT support the .asciiz pseudo-instruction. That syntax you saw in reference programs is for other assemblers (like NASM or GAS) — if you try to use .asciiz in MASM, you'll get a syntax error.
Now let's break down the two approaches:
1. BPrompt DB 'Enter the value of B: ','$' (MASM标准写法)
This is the standard way to define strings in MASM for DOS-based assembly programs. Here's what's happening:
DBis MASM's "define byte" pseudo-instruction, which lets you allocate bytes in memory for your string.- The
'$'at the end is a DOS-specific string terminator. When you use DOS interrupts likeint 21hfunction 09h to print strings, the interrupt will keep outputting characters until it hits a$— that's how it knows where the string ends.
2. .asciiz "Enter the value of B:" (Other assemblers only)
This syntax is used in assemblers like NASM or GNU Assembler (GAS). What it does is:
- Define the string characters, then automatically append a null byte (0x00) at the end. This is the "C-style" string terminator, used by system calls (like Linux's
writesyscall) or C library functions that expect null-terminated strings.
So which should you use in MASM?
It all depends on what you're doing with the string:
- If you're writing a classic DOS program using DOS interrupts for I/O, stick with
DBplus'$'— this is the only way DOS will recognize and print your string correctly. - If you need a null-terminated string (for example, if you're interfacing with C code), you can manually add the null byte with MASM:
This achieves the same result asBPrompt DB 'Enter the value of B:', 0.asciiz, but uses valid MASM syntax.
Quick recap
Forget about .asciiz when working in MASM — it's not supported. Use DB with the appropriate terminator: '$' for DOS I/O, or 0 for null-terminated strings.
内容的提问来源于stack exchange,提问作者Edward Talanian

