对数组中的偶数进行计数并求平均值,并检查素数
使用时我的所有显示都是 ASCII 格式 (mov al,2; int 21h
)。
我只能检查一个条件,但不能检查全部。如何同时检查并输出?
我的任务是
考虑一组预先存储的 1 字节正整数元素 大批。执行以下操作。
- 计算数组中元素的数量并显示它们;
- 识别并显示小于特定阈值的所有数字(您可以根据需要设置此阈值)。如果没有的话, 显示消息;
- 识别偶数,计算它们并计算平均值。显示您的结果;
- 识别数组中的所有素数并显示它们;
All my display is in ASCII when use (mov al,2; int 21h
).
I'm only able to check one condition but not all. How can I check all at the same time and output it?
My assignment is
Consider a set of pre-stored 1-byte positive integer elements in an
array. Do the following.
- Count the number of elements in your array and display them;
- Identify and display all numbers less than a specific threshold number (you can set this threshold as you wish). If there are none,
display a message;- Identify EVEN numbers, count them and compute their average. Display your result;
- Identify all prime numbers in your array and display them;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要分段处理这样的事情。考虑到您需要做什么,我可能要解决的第一部分是将单字节整数转换为表示十进制数字的字符串。
为此,您可以将该数字除以 10。余数将代表最低有效数字(0 到 9 之间的数字(包括 0 到 9)。通过添加“0”将其转换为数字。重复该过程,直到数字为 0。为了便于打印,您可能需要从四个字节的缓冲区开始,最后一个字节中带有“$”。将数字从“$”之前的最后一个字节开始放入缓冲区,每次迭代都会减少指向缓冲区的指针。
识别偶数非常容易:奇数总是设置其最低有效位,而偶数则没有。
You need to approach something like this in pieces. Considering what you need to do, the first piece I'd probably tackle would be converting a one-byte integer to a string representing that number in decimal.
To do that, you divide the number by 10. The remainder will represent the least significant digit (a number from 0 to 9 inclusive). Convert that to a digit by adding '0'. Repeat that process until your number is 0. To make that easy to print, you might want to start with a buffer of four bytes with a '$' in the last byte. Put the digits into the buffer starting in the last byte before the '$', decrementing a pointer into the buffer for each iteration.
Identifying even numbers is pretty easy: an odd number always has its least significant bit set, and even number doesn't.
循环遍历字节数组非常简单(我假设您应该在遇到负数时停止,但您的情况可能会有所不同):
当循环结束时,元素总数将为 esi-pointerToByteArray< /代码>。第二部分你已经完成了,只是你没有意识到。您可以像这样使用 cmp 操作码:
第三部分也非常简单,但需要一些位旋转知识。您应该知道,每个偶数都会清除最低有效位(1 的位置),因此如果删除前 7 位,您将得到 1(对于奇数)或 0(对于偶数):
最简单的方法第 4 部分是测试每个小于或等于 127(正字节的最大值)的素数:
请注意,有比这更好、更高效、更优雅的方法来检查素数,但它应该可以完成工作。在不了解更多信息的情况下,我无法告诉您如何输出您收集的信息。
Looping through an array of bytes is pretty easy (I'm assuming you should stop when you encounter a negative number, but your situation might vary):
When the loop ends, the total number of elements will be
esi-pointerToByteArray
. Part two you've already done, you didn't realize it. You use the cmp opcode just like:Part three is pretty easy too, but will take some knowledge of bit twiddling. You should know that every even number will have the least significant bit clear (the 1's place) so if you remove the top 7 bits you'll either have 1 (for odd numbers) or 0 (for even):
The easiest way to do part 4 is test each prime less than or equal to 127 (the maximum value of a positive byte):
Please not that there are much better, more efficient, and elegant ways to check for primeness than that, but it should get the job done. Without knowing more information I can't tell you how to output the information you collect.