以美元结尾的字符串
在我的汇编语言课上,我们的第一个作业是编写一个程序,在 DOS 中打印出一个简单的以美元结尾的字符串。 它看起来像这样:
BITS 32
global _main
section .data
msg db "Hello, world!", 13, 10, ’$’
section .text
_main:
mov ah, 9
mov edx, msg
int 21h
ret
据我了解,$ 符号用于终止字符串,就像 C 中的 null 一样。但是如果我想在字符串中放置一个美元符号(就像我想打印出“it花费 30 美元”)? 这似乎是一个简单的问题,但我的教授不知道答案,我似乎也没有使用谷歌搜索找到它。
In my assembly language class, our first assignment was to write a program to print out a simple dollar-terminated string in DOS. It looked something like this:
BITS 32
global _main
section .data
msg db "Hello, world!", 13, 10, ’$’
section .text
_main:
mov ah, 9
mov edx, msg
int 21h
ret
As I understand it, the $ sign serves to terminate the sting like null does in C. But what do I do if I want to put a dollar sign in the string (like I want to print out "it costs $30")? This seems like a simple question, but my professor didn't know the answer and I don't seem to find it using a google search.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您不能使用 DOS 的
0x09
服务来显示$
符号,您需要使用0x02
。 请参阅此处。You can't use DOS's
0x09
service to display$
signs, you'll need to use0x02
. See here.或者使用未记录的 INT 29h(AL 中的打印字符)创建自己的 print_string 来打印以 NULL 结尾的字符串。
(假设您使用 NASM)
Or make your own print_string to print a NULL-terminated string using the undocumented INT 29h (print character in AL).
(Assuming you are using NASM)
我更喜欢使用
write
服务 (AH=0x40
):AH=0x40
BX
是文件句柄; 使用值 1 写入同一设备(例如屏幕),因为服务AH=9
CX
是要写入的字节数; 数据不是“终止”的(既不是由 NUL 也不是由$
),因此所有值(从 0 到 255)都可以写成DS:DX
指向数据(在你的情况下:要写入的字符串(就像服务
AH=9
一样;如果您使用 32 位 DOS 扩展:EDX
,当然)该服务实际上用于将数据写入文件; 但是,通过将 BX 设置为 1,它也可以用于将“字符串”写入“输出”。
I prefer using the
write
service (AH=0x40
):AH=0x40
BX
is the file handle; use the value 1 to write to the same device (such as the screen) as serviceAH=9
CX
is the number of bytes to be written; the data is not "terminated" (neither by NUL nor by$
), so all values (from 0 to 255) can be writtenDS:DX
points to the data (in your case: the string) to be written(Just like for service
AH=9
; If you use a 32-bit DOS extension:EDX
, of course)The service is actually intended for writing data to a file; however, it can also be used to write a "string" to the "output" by setting
BX
to 1.一种方法是找到打印单个字符的调用。 您可以用它打印任何字符。 将字符串拆分并打印“it cost”,然后打印“$”,最后打印“30”。 更多的工作,但它完成了工作。
One way is to find the call that prints a single character. You can print any character with that. Break the string up and print "it costs ", then the '$', and finally, "30". More work, but it gets the job done.
您可以使用INT 21H的02服务来代替09服务。
这是示例。
You can use 02 service of INT 21H instead of 09 service.
Here is the sample.
嗯。 您可以编写考虑转义
$
的程序集,例如\$
?但是你的
\
也变成了一个特殊的符号,你需要使用\\
来打印\
Um. You could write assembly that would taken into account for escaped
$
, e.g.\$
?But then your
\
becomes a special symbol too, and you need to use\\
to print a\
尝试“$$”、“\044”(八进制)或“\x24”(十六进制)
Try '$$', '\044' (octal) or '\x24' (hex)