Assembly 汇编语言:将小写打印为大写
我有一个任务,描述如下:Assembly 汇编语言:将小写打印为大写,assembly,compare,Assembly,Compare,我有一个任务,描述如下: - Reads printable characters (20h-7Fh) from the keyboard without echoing - Uppercase letters are printed to display - Lowercase letters are converted to uppercase and then printed to display - Blank spaces and periods are printed to disp
- Reads printable characters (20h-7Fh) from the keyboard without echoing
- Uppercase letters are printed to display
- Lowercase letters are converted to uppercase and then printed to display
- Blank spaces and periods are printed to display; everything else is trashed
- Program ends when period is printed
到目前为止,我的计划是:
.model small
.8086
.data
.code
start:
mov ax,@data
mov ds,ax
read:
mov ah,8
int 21h
cmp al,61
jl write
cmp al,7fh
jg read
sub al,20h
jmp write
write: cmp al,20h
jl read
mov dl,al
mov ah,2
int 21h
cmp dl,2eh
jne read
exit:
mov ax,4c00h
int 21h
end start
我的程序成功地转换了小写字母并打印了相应的大写字母,但是我很难把其他的都弄糟。什么是只允许空白、句点和字母显示的最佳方式
看看ASCII图表
21h - 2Dh can be trashed
2Fh - 40h can be trashed
5bh - 60h can be trashed
7bh - 7fh can be trashed
有谁能帮我找出最好的逻辑来比较输入值和这些值,然后把那些介于上述范围之间的值剔除掉?我们对效率进行评分,0-20份书面说明获得满分。我在这里已经有20条说明了,我还没有包括查找垃圾值的比较
编辑以下是我将代码的范围缩小为:
.model small
.8086
.data
.code
read:
mov ah,8
int 21h
cmp al,' '
je write
cmp al,'.'
je write
cmp al,'a'
jl read
cmp al,'Z'
jg convert
convert:
cmp al,'a'
jl read
sub al,20h
write:
mov dl,al
mov ah,2
int 21h
cmp dl,'.'
jne read
exit:
mov ax, 4c00h
int 21h
end read
目前有21条指令!我的代码中是否有任何冗余可以删除以将其减少到20?您可以修改逻辑以执行以下操作:
- 如果是空格或句点,请跳转到
write
- 将小写字母折叠为大写字母
- 如果不是大写字母,请跳回
read
请注意,“将小写字母折叠为大写字母”不需要先检查输入是否为字母。只要
和xx
,其中xx
是一个合适的值就可以了(我会让你算出这个值)。此步骤还将修改字母以外的字符,但由于下一步是检查字母,因此这并不重要。我认为您可以通过执行以下操作来保存一些说明:
read:
if lowercase then make uppercase
if uppercase then print
if space then goto print
if period then quit
goto read
print:
do the print
goto read
我将为相应的值20..7f设置一个表,为每个范围设置标志,然后使用每个输入字节-20h作为表中的索引。标题示例汇编代码结构 .小型模型 堆栈 .数据 MSG1 DB“输入字母:$” MSG2 DB 10,“大写字母为:$” MSG3 DB 10,“[按任意键继续]$” .代码 MOV-AX,@DATA MOV-DS,AX 斧头 巴里克: 莫瓦克斯,3 INT 10H 第十三天代码的执行从这里开始------------------- 莫夫啊,9 LEA DX,MSG1 INT 21H 莫夫啊,1 INT 21H 和AL,00DFH 莫夫啊,9 LEA DX,MSG2 INT 21H MOV啊,2 MOV DL,AL INT 21H 莫夫啊,9 LEA DX,MSG3 INT 21H 莫夫啊,1 INT 21H 莫夫比勒,艾尔 《基本法》第27条 杰塔波斯 巴利克 塔波斯: )-------------------------------代码结束---------------------------------- MOV啊,4CH INT 21H
结束谢谢,你能澄清一下什么是将小写字母折叠成大写字母吗?我不熟悉这个术语。我正在阅读关于“and”布尔值的文章,我不确定我是否理解它实现了什么。你能给我举个例子,说明如何使用“and”吗?其实有一个很好的介绍。使用正确的值,您可以使用该值
和
输入字符,以便大写字符不变,小写字符转换为大写。感谢您的澄清。我已经手工转换了一些小写和大写字母,并比较了“and”。我看到它们都是32位小数,但我仍然不知道如何使用“and”。我似乎想不出一个值,在将小写字符转换为大写字符时,允许大写字符不变地通过。我确实看到“和”-大写和小写等价的结果大写。考虑32小数是0x20 HEX,这是一个特定位置只有一个位的差值。考虑如何使用<代码>和< /代码>强制该位成为您想要的(使字符大写)。一次考虑一个位的位置。要保存一些指令,您不需要前两个(因为您没有需要ds
)的指令。另外,int20h
将退出您的程序,而无需先设置ax
。@raphnguyen:使用文字Ascii表示法来帮助记录您的意思,而不是使用常量。例如,cmpal,'a'
比您拥有的更具可读性和意义。@wallyk感谢您的建议!投票失败有什么特别的原因吗?我知道这不是他的汇编语言,但翻译很简单。请格式化您的答案并添加一些解释。