我想弄清楚如何打印用户在汇编语言中输入的字符。(让我们假设我已经有了小写的值,我想把它打印成大写。)
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函数
-
定义大写字符串,其中包含一个占位问号(或任何其他您喜欢的字符):
uppercase db "The uppercase equivalent of letter ? is: $" ^ placeholder
-
为了方便定位占位符,您可以将字符串写在两行,因此您可以在占位符前面放一个标签:
uppercase db "The uppercase equivalent of letter " uppercase_ db "? is: $" ^ placeholder
-
用用户输入的小写字母替换占位符:
mov [uppercase_], al ; AL is lowercase
-
使用DOS打印字符串。PrintString function:
mov dx, offset uppercase mov ah, 09h int 21h
如何显示大写字母?
无需单独输出。最简单的解决方案是在输出字符串中也包含它。
-
只需在结束$字符之前添加第二个占位符。我们可以很容易地通过计数字符来建立偏移量,而不是分割第三行(2个占位符是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函数
mov dx, offset uppercase mov ah, 09h int 21h
有关显示文本的一些背景信息,请阅读此Q/A显示DOS或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显示数字。