对数组中的偶数进行计数并求平均值,并检查素数

发布于 2024-10-16 21:38:04 字数 325 浏览 1 评论 0原文

使用时我的所有显示都是 ASCII 格式 (mov al,2; int 21h)。

我只能检查一个条件,但不能检查全部。如何同时检查并输出?

我的任务是

考虑一组预先存储的 1 字节正整数元素 大批。执行以下操作。

  1. 计算数组中元素的数量并显示它们;
  2. 识别并显示小于特定阈值的所有数字(您可以根据需要设置此阈值)。如果没有的话, 显示消息;
  3. 识别偶数,计算它们并计算平均值。显示您的结果;
  4. 识别数组中的所有素数并显示它们;

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.

  1. Count the number of elements in your array and display them;
  2. 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;
  3. Identify EVEN numbers, count them and compute their average. Display your result;
  4. Identify all prime numbers in your array and display them;

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

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

发布评论

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

评论(2

丢了幸福的猪 2024-10-23 21:38:04

你需要分段处理这样的事情。考虑到您需要做什么,我可能要解决的第一部分是将单字节整数转换为表示十进制数字的字符串。

为此,您可以将该数字除以 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.

桃气十足 2024-10-23 21:38:04

循环遍历字节数组非常简单(我假设您应该在遇到负数时停止,但您的情况可能会有所不同):

mov  esi, pointerToByteArray
loop_start:
  mov  al, [esi]
  cmp  al, 80h
  jge  loop_end

  ; Do your byte-checking code here

  inc  esi
  jmp  loop_start
loop_end:

当循环结束时,元素总数将为 esi-pointerToByteArray< /代码>。第二部分你已经完成了,只是你没有意识到。您可以像这样使用 cmp 操作码:

cmp  al, threshold
jge  not_threshhold
; add to threshold counter
not_threshold:

第三部分也非常简单,但需要一些位旋转知识。您应该知道,每个偶数都会清除最低有效位(1 的位置),因此如果删除前 7 位,您将得到 1(对于奇数)或 0(对于偶数):

mov  bl, al
and  bl, 1
jnz  not_even
; add to even counter
not_even:

最简单的方法第 4 部分是测试每个小于或等于 127(正字节的最大值)的素数:

cmp  al, 2
je  is_prime
cmp  al, 3
je  is_prime
cmp  al, 5
je  is_prime
cmp  al, 7
je  is_prime
; all the primes up to 127
jmp not_prime

is_prime:
; add to prime counter
not_prime:

请注意,有比这更好、更高效、更优雅的方法来检查素数,但它应该可以完成工作。在不了解更多信息的情况下,我无法告诉您如何输出您收集的信息。

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):

mov  esi, pointerToByteArray
loop_start:
  mov  al, [esi]
  cmp  al, 80h
  jge  loop_end

  ; Do your byte-checking code here

  inc  esi
  jmp  loop_start
loop_end:

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:

cmp  al, threshold
jge  not_threshhold
; add to threshold counter
not_threshold:

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):

mov  bl, al
and  bl, 1
jnz  not_even
; add to even counter
not_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):

cmp  al, 2
je  is_prime
cmp  al, 3
je  is_prime
cmp  al, 5
je  is_prime
cmp  al, 7
je  is_prime
; all the primes up to 127
jmp not_prime

is_prime:
; add to prime counter
not_prime:

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.

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