简体   繁体   English

使用子例程分隔偶数和素数的汇编语言程序

[英]Assembly language program to separate even and prime numbers using subroutines

I wish to generate an assembly language program to separate even and prime numbers from a given list of numbers.我希望生成一个汇编语言程序来从给定的数字列表中分离偶数和素数。 The program should make use of subroutines, stack and indirect addressing mode.程序应利用子程序、堆栈和间接寻址方式。

What I have tried is我试过的是

.model small
  .data
     num db 0,13,4,7,8,9,14,15,2,10,19,20
     even dw 20 dup ?
     prime dw 20 dup ?

 .code
   mov ax, @ data
   mov ds, ax
   LEA BX,num
   LEA SI, even
   LEA DI, prime
   mov dh,02
L1:
   mov ah,00
   mov al, [BX]
   mov dl, al
   div dh
    cmp ah,00
    JE EVEN
EVEN:
     mov [SI], dl
     INC SI
     INC BX
     LOOP L1

As I am a beginner to assembly language, I wish to know the correct code for the above requirement.由于我是汇编语言的初学者,我想知道上述要求的正确代码。 Any help would be appreciated.任何帮助,将不胜感激。

The program should make use of subroutines程序应该使用子程序

What your program must then do is run over the num array and call upon 2 subroutines, one that finds out if the number is prime and one that finds out if the number is even.然后,您的程序必须做的是在num数组上运行并调用 2 个子例程,一个确定数字是否为素数,另一个确定数字是否为偶数。
Currently you almost got a good loop, were it not that you forgot to initialize the CX register that the LOOP instruction depends on!目前你几乎得到了一个很好的循环,不是你忘记初始化LOOP指令所依赖的CX寄存器!

This is already a good loop:这已经是一个很好的循环:

    lea     bx, num
    lea     si, even
    lea     di, prime
    mov     cx, 12         ; There are 12 numbers in the 'num' array
L1:
    mov     al, [bx]
    call    TestForEven
    call    TestForPrime
    inc     bx
    loop    L1

Your test to find out if a number is even uses a division by 2. This is a wasteful solution.确定一个数字是否为偶数的测试使用除以 2。这是一种浪费的解决方案。 All you got to do is check if the lowest bit of the number is 0.您所要做的就是检查数字的最低位是否为 0。

TestForEven:
    test    al, 1         ; This tests the lowest bit
    jnz     NotEven
    mov     [si], al      ; Adding to the 'even' array
    inc     si
NotEven:
    ret

Here are some tips to complete the task:以下是完成任务的一些提示:

  • Like @Michael wrote in his comment, you should have the even and prime arrays defined as bytes.就像@Michael在他的评论中所写的那样,您应该将偶数素数arrays 定义为字节。
  • If any number is found to be even, it can't be prime also.如果发现任何数字是偶数,它也不能是素数。 This means that you could make the call to TestForPrime from within TestForEven (instead of from the main loop).这意味着您可以从TestForEven 中(而不是从主循环)调用TestForPrime

     TestForEven: test al, 1; This tests the lowest bit jnz NotEven mov [si], al; Adding to the 'even' array inc si ret NotEven: call TestForPrime ret

    And because this embedded call has now become what is called a tail-call, the call instruction is no longer needed:而且因为这个嵌入式调用现在变成了所谓的尾调用,所以不再需要call指令:

     TestForEven: test al, 1; This tests the lowest bit jnz TestForPrime mov [si], al; Adding to the 'even' array inc si ret TestForPrime: ... ret
  • If you search this forum (or google) you will certainly find good ways to test for a prime number.如果你搜索这个论坛(或谷歌),你肯定会找到测试素数的好方法。 Good hunting...好猎...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM