首页 > 解决方案 > 打印用户输入的字符,也许是 `cout` 的方式?

问题描述

我试图弄清楚如何打印用户用汇编语言输入的字符。(假设我已经有了小写的值,我想把它打印成大写。)

lowercase  DB  "Input a letter: $"
uppercase  DB  "The uppercase equivalent of letter <the lowercase letter the user inputted> is: $"

相反,如果它是用 c++ 编写的,它将是:

cout << "The uppercase equivalent of letter" << lowercase << "is: " << endl"

我该怎么做?

标签: c++assemblyoutputx86-16emu8086

解决方案


首选的解决方案是在输出字符串中插入“用户输入的小写字母” 。我看到您已经对字符串进行了 $ 终止,因此可以使用 DOS.PrintString 函数 09h 一次性打印所有内容。

  • 使用占位问号(或您喜欢的任何其他字符)定义大写字符串:

      uppercase  db "The uppercase equivalent of letter ? is: $"
                                                        ^
                                              placeholder
    
  • 为了便于寻址占位符,您可以将字符串写成 2 行,因此您可以在占位符前面放置一个标签:

      uppercase  db "The uppercase equivalent of letter "
      uppercase_ db "? is: $"
                     ^
           placeholder
    
  • 用用户输入的小写字母替换占位符:

      mov     [uppercase_], al      ; AL is lowercase
    
  • 使用 DOS.PrintString 函数 09h 打印字符串:

      mov     dx, offset uppercase
      mov     ah, 09h
      int     21h
    

显示大写字母怎么样?

无需单独输出。最简单的解决方案是将它也包含在输出字符串中。

  • 只需在终止 $ 字符之前添加第二个占位符。我们可以通过计算字符数来轻松建立偏移量,而不是拆分第三行(两个占位符相隔6 个字符):

      uppercase  db "The uppercase equivalent of letter "
      uppercase_ db "? is: ?$"
                     ^     ^
           placeholder1    placeholder2
    
  • 替换两个占位符。在省略号处...您必须从小写转换为大写!

      mov     [uppercase_], al      ; AL is lowercase
      ...
      mov     [uppercase_ + 6], al  ; AL is uppercase
    
  • 使用 DOS.PrintString 函数 09h 打印字符串。

      mov     dx, offset uppercase
      mov     ah, 09h
      int     21h
    

有关显示文本的一些背景信息,请阅读此 Q/A Displaying characters with DOS or BIOS。它有每个字符打印文本字符串的示例。

使它更符合cout什么

如果需要覆盖占位符的数据长度不同,则上述解决方案不再适用。使用 5 个字符的占位符,请考虑:

db 'Your donation of       € is much appreciated.$'

如果捐赠的金额有 5 位数字,这将很好地打印出来:

Your donation of 20000 € is much appreciated.

但是如果捐赠的金额很小,就不再那么好看了:

Your donation of     5 € is much appreciated.

在这种情况下,我们将不得不使用不同的方法,将随附的文本与中间的数字分开输出。

ThankYou1 db 'Your donation of $'
ThankYou2 db ' € is much appreciated.$'

...

mov     dx, offset ThankYou1
mov     ah, 09h
int     21h

mov     ax, [amount]
call    DisplayNumber

mov     dx, offset ThankYou2
mov     ah, 09h
int     21h

有关如何编写DisplayNumber例程的想法,请参阅使用 DOS 显示数字


推荐阅读