Implement this design in 80x86 assembly language. Using the windows32 or windows
ID: 3564737 • Letter: I
Question
Implement this design in 80x86 assembly language. Using the windows32 or windows64 framework, write a program that stores the primes in an array of doublewords primeArray, and display the prime numbers in primeArray using a single output box with 5 numbers per line. Pseudo code is below.
.data
;declare corresponding data here
;the text to hold the output message should have enough space to hold the numbers and
;the new line charaters.
.code
prime[1] := 2; { first prime number }
prime[2] := 3; { second prime number }
primeCount := 2;
candidate := 4; { first candidate for a new prime }
while primeCount < 100 loop
index := 1;
while (index ? primeCount)
and (prime[index] does not evenly divide candidate) loop
add 1 to index;
end while;
if (index > primeCount)
then {no existing prime evenly divides the candidate, so it is a new prime}
add 1 to primeCount;
prime[primeCount] := candidate;
end if;
add 2 to candidate;
end while;
store the "Prime Numbers" in the byte array with 0dh, oah embedded in there;
for index := 1 to 100 loop {display the numbers 5 per line }
insert the number in the corresponding position;
if index is divisible by 5
then
insert 0dh, 0ah;
end if;
end for;
call output macro to display the numbers.
I'm using visual studio to attempt to figure this out and so far haven't even gotten close. A full working program will earn you the points.
Explanation / Answer
include 'emu8086.inc'
org 100h; set location counter to 100h
jmp CodeStart
DataStart:
max dw 100
space db " ", 0
CodeStart:
mov bx, 2;set this value to the number you want to start at I used 2 because 1 is not a prime.
LoopStart:
;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 count 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
; check remainder (in dx), if zero then we found a divisor
; and the number cannot be prime
cmp dx, 0
; if dx = 0 then we found a divisor and can stop looking
je StopLabel
; increment count
add cx, 1
jmp PrimeLoop
StopLabel:
ret
IsPrime ENDP
DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS