以美元结尾的字符串

发布于 2024-07-11 20:49:49 字数 357 浏览 5 评论 0原文

在我的汇编语言课上,我们的第一个作业是编写一个程序,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

预谋 2024-07-18 20:49:49

您不能使用 DOS 的 0x09 服务来显示 $ 符号,您需要使用 0x02。 请参阅此处

You can't use DOS's 0x09 service to display $ signs, you'll need to use 0x02. See here.

清欢 2024-07-18 20:49:49

或者使用未记录的 INT 29h(AL 中的打印字符)创建自己的 print_string 来打印以 NULL 结尾的字符串。

; ds:si = address of string to print
print_string:
    lodsb                   ; load next character from ds:si
    or al, al               ; test for NULL-character
    jz .end_of_string       ; end of string encountered, return.
    int 29h                 ; print character in AL on screen
    jmp print_string        ; print next character
.end_of_string:
    ret                     ; return to callers cs:ip

(假设您使用 NASM)

Or make your own print_string to print a NULL-terminated string using the undocumented INT 29h (print character in AL).

; ds:si = address of string to print
print_string:
    lodsb                   ; load next character from ds:si
    or al, al               ; test for NULL-character
    jz .end_of_string       ; end of string encountered, return.
    int 29h                 ; print character in AL on screen
    jmp print_string        ; print next character
.end_of_string:
    ret                     ; return to callers cs:ip

(Assuming you are using NASM)

生来就爱笑 2024-07-18 20:49:49

我更喜欢使用 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 service AH=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 written
  • DS: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.

孤独患者 2024-07-18 20:49:49

一种方法是找到打印单个字符的调用。 您可以用它打印任何字符。 将字符串拆分并打印“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.

埋情葬爱 2024-07-18 20:49:49

您可以使用INT 21H的02服务来代替09服务

这是示例。

mov dl, '


mov ah,02

int 21h

You can use 02 service of INT 21H instead of 09 service.

Here is the sample.

mov dl, '


mov ah,02

int 21h
就此别过 2024-07-18 20:49:49

嗯。 您可以编写考虑转义 $ 的程序集,例如 \$
但是你的 \ 也变成了一个特殊的符号,你需要使用 \\ 来打印 \

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 \

过去的过去 2024-07-18 20:49:49

尝试“$$”、“\044”(八进制)或“\x24”(十六进制)

Try '$$', '\044' (octal) or '\x24' (hex)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文