我在考试中是怎么做到的:)计算按键次数

发布于 2024-11-01 04:31:22 字数 774 浏览 1 评论 0原文

我今天考试时遇到了这个问题,我为此写了一些代码。我希望你告诉我我哪里出错了。

问题:对于 8086 微处理器,编写执行以下任务的代码。它要求用户输入数据并计算按键次数..
执行 ALP 时,获得第一条消息“正在计算按键次数,按 escape 停止”。 (不带引号),如果用户输入转义符,则显示第二条消息“counting keypresses”,并将计数结果存储在 BX 中并以二进制形式显示。还有按下的键(输入数据时),输入键应在下一行中回显。

解决方案:(我写的)
.模型微小
.数据
text1 db“计算按键次数,按转义键停止$”
text2 db“计算按键次数”
.代码
.启动
mov dx,offset text1 ;显示text1(我猜是这样) 移动啊,09h
21小时内
和 bx,00h
label1 mov ah,01h ;(从用户获取输入并在每次点击时递增 bx) 21小时内
公司bx
cmp al,1bh ;与 'escape' 比较 np 标签1
mov dx,offset text2 ;如果为零则执行此操作即显示 text2 移动啊,09h
21小时内
mov dx,偏移bx ;显示bx 移动啊,09h
21小时内
.退出
end

我不确定我添加的评论是否合适。 我知道我的代码不会正确,事实上它根本不会正确,但我在我一晚准备的基础上设法写了这个。所以如果有人能告诉我,我做错了什么,那么我将非常感激。因为我真的需要在下次考试中取得好成绩。

i got this question in my exam today and i wrote some code for it.i would like you to tell me where did i go wrong.

Problem: for an 8086 microprocessor,write code that does the following task. it asks for user to enter the data and it counts the keypresses..
on execution of the ALP,the first message is obtained "counting keypresses , press escape to stop." (without quotation marks) , if the user enters the escape, the second message "counting keypresses" is shown and the result of the count is stored in BX and shown in displayed in binary. also the keys pressed (while entering data) ,the input key shall be echoed in the next line.

Solution: (that i wrote)
.model tiny
.data
text1 db "counting keypresses,press escape to stop $"
text2 db "counting keypresses"
.code
.startup
mov dx,offset text1 ;displaying text1 (i guess so)
mov ah,09h
int 21h
and bx,00h
label1 mov ah,01h ;(getting input from user and incrementing bx on every click)
int 21h
inc bx
cmp al,1bh ;comparing with 'escape'
np label1
mov dx,offset text2 ;if zero then do this i.e display text2
mov ah,09h
int 21h
mov dx,offset bx ;display bx
mov ah,09h
int 21h
.exit
end

i am not sure if the comments i added are suitable or not.
i know my code wont be correct,infact it wont be correct at all,but i managed to write this on the basis of my one night preparation.so if anyone could tell me,what wrong i am doing,then i will be highly thankful. because i really need to perform well in the next exam.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

风轻花落早 2024-11-08 04:31:22

尽管还有一些细节,但您已经有了基本的想法。

首先,你有这个:

cmp al, 1bh
np label1

我不确定 np 是什么意思。您想要的指令可能是jnz label1。如果未设置 Z 标志(这意味着用户没有按 ESC),则会发生分支。

您可以使用以下方法来显示计数:

mov dx,offset bx ;display bx
mov ah,09h
int 21h

这是行不通的。事实上,这不应该编译,因为你不能获取寄存器的偏移量。您需要将 BX 中的值存储到内存中,然后将该内存地址传递给输出函数。因此,在数据区域中定义一个 2 字节值:

count dw 0  ; counter for output
      db '

然后您可以编写:

move [count],bx
mov dx, offset count
mov ah, 09h
int 21h

您确实说过要以二进制形式输出计数,但我不确定您是否知道其后果。如果用户只按两个键,则输出将是二进制 0 和二进制 2,它们可能显示为方框,或者根本不显示任何内容,或者可能是时髦的字符。我不记得 int21 函数如何解释所有控制字符。但可以肯定的是,如果用户按 13 个键,您将得到的只是一个回车符。如果用户按下 36 个字符,输出将是……什么也没有。因为 36 是“$”的 ASCII 代码,并且它是输出终止符。

如果你想更好地输出,你需要将 BX 中的值转换为十六进制或十进制(十六进制更容易)为 ASCII 字符,然后输出它们。我手头没有样品。

最后,我不记得保存寄存器的 int21 函数的约定是什么。 BX 是否在调用中保留?如果没有,您需要在调用任何 int21 函数之前push bx,并在返回时pop bx。否则你的计数将是......“未定义”。

; terminate output

然后您可以编写:

您确实说过要以二进制形式输出计数,但我不确定您是否知道其后果。如果用户只按两个键,则输出将是二进制 0 和二进制 2,它们可能显示为方框,或者根本不显示任何内容,或者可能是时髦的字符。我不记得 int21 函数如何解释所有控制字符。但可以肯定的是,如果用户按 13 个键,您将得到的只是一个回车符。如果用户按下 36 个字符,输出将是……什么也没有。因为 36 是“$”的 ASCII 代码,并且它是输出终止符。

如果你想更好地输出,你需要将 BX 中的值转换为十六进制或十进制(十六进制更容易)为 ASCII 字符,然后输出它们。我手头没有样品。

最后,我不记得保存寄存器的 int21 函数的约定是什么。 BX 是否在调用中保留?如果没有,您需要在调用任何 int21 函数之前push bx,并在返回时pop bx。否则你的计数将是......“未定义”。

You have the basic idea, although there are a few details.

First, you have this:

cmp al, 1bh
np label1

I'm not sure what np is supposed to mean. The instruction you want there is probably jnz label1. That will branch if the Z flag is not set, meaning that the user didn't press ESC.

You have the following to display the count:

mov dx,offset bx ;display bx
mov ah,09h
int 21h

That's not going to work. In fact, that shouldn't compile, since you can't take the offset of a register. You need to store the value from BX into memory, and then pass that memory address to the output function. So define a 2-byte value in your data area:

count dw 0  ; counter for output
      db '

Then you can write:

move [count],bx
mov dx, offset count
mov ah, 09h
int 21h

You did say that you want to output the count in binary, but I'm not sure you know the ramifications of that. If the user presses only two keys, the output will be a binary 0 and a binary 2, which might display as boxes, or nothing at all, or perhaps funky characters. I don't remember how the int21 functions interpret all the control characters. For sure, though, if the user presses 13 keys, all you're going to get is a carriage return. And if the user presses 36 characters, the output will be ... nothing. Because 36 is the ASCII code for '$', and that's the output terminator character.

If you want to do a better job of output, you'll need to convert the value in BX to hexadecimal or decimal (hex is easier) to ASCII characters, and output those. I don't have a sample at hand.

Finally, I don't recall what the convention is for int21 functions saving registers. Is BX preserved across calls? If not, you'll need to push bx before calling any int21 function, and pop bx when it returns. Otherwise your count is going to be ... "undefined".

; terminate output

Then you can write:

You did say that you want to output the count in binary, but I'm not sure you know the ramifications of that. If the user presses only two keys, the output will be a binary 0 and a binary 2, which might display as boxes, or nothing at all, or perhaps funky characters. I don't remember how the int21 functions interpret all the control characters. For sure, though, if the user presses 13 keys, all you're going to get is a carriage return. And if the user presses 36 characters, the output will be ... nothing. Because 36 is the ASCII code for '$', and that's the output terminator character.

If you want to do a better job of output, you'll need to convert the value in BX to hexadecimal or decimal (hex is easier) to ASCII characters, and output those. I don't have a sample at hand.

Finally, I don't recall what the convention is for int21 functions saving registers. Is BX preserved across calls? If not, you'll need to push bx before calling any int21 function, and pop bx when it returns. Otherwise your count is going to be ... "undefined".

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