打印用户输入的字符,也许可以使用`cout`的方式? [英] Printing a char that is inputted by the user, perhaps the way `cout` does it?

查看:37
本文介绍了打印用户输入的字符,也许可以使用`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"

我应该怎么做?

推荐答案

首选解决方案是在输出字符串中插入用户输入的小写字母";。我看到您已经对字符串进行了$结尾,因此可以使用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
    

有关显示文本的一些背景信息,请阅读此问答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例程的想法,请参阅Displaying numbers with DOS

这篇关于打印用户输入的字符,也许可以使用`cout`的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆