In this section you are required to write a program in ARM a

In this section, you are required to write a program in ARM assembly language to generate the first twenty prime numbers greater than 2 and save them into the memory locations called Primearray. You have to use the same control structures that are introduced in previous sections. For this part, you may use the following algorithm. You are required to optimize the code in terms of its execution time and required space. for (i = 3, n = 0; n

Solution

; prime.asm ; sample program to demonstrate procedures ; calulates and prints prime numbers from 1 to 20 include \'emu8086.inc\' org 100h; set location counter to 100h jmp CodeStart DataStart: max dw 20 space db \" \", 0 CodeStart: mov bx, 1 LoopStart: mov ax, bx ;call procedure IsPrime to test whether the number is ;prime or not call IsPrime cmp dx, 0 ;if dx is equal to 0 jump je Equal mov ax, bx call print_num ; print a space mov si, offset space call print_string Equal: add bx, 1 cmp bx, max jle LoopStart ret IsPrime PROC ; uses a loop to determine if number in bx is prime ; upon return if bx not prime dx will be 0, otherwise dx > 0 ; we only have to test divisors from 2 to bx/2 ; prepare to divide dx:ax / 2 mov ax, bx mov dx, 0 mov cx, 2 div cx ; move result into si for loop mov si, ax ; assume the value is prime mov dx, 1 ; start loop at 2 mov cx, 2 PrimeLoop: ; compare loop count(in cx) and max loop value (in si) cmp cx, si ; jump out of loop if count(cx) > si ja StopLabel ; divide test value (in bx) by loop count (in cx) mov ax, bx mov dx, 0 div cx ; increment count add cx, 1 ; check remainder (in dx), if zero then we found a divisor ; and the number cannot be prime cmp dx, 0 jmp PrimeLoop StopLabel: ret IsPrime ENDP DEFINE_PRINT_STRING DEFINE_SCAN_NUM DEFINE_PRINT_NUM DEFINE_PRINT_NUM_UNS
 In this section, you are required to write a program in ARM assembly language to generate the first twenty prime numbers greater than 2 and save them into the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site