I am stuck on problem 7 that is outlined clearly. I am trying to create a process that changes all of the non-prime number elements of a 65,000 element array to 1. Then I would like to verify that array was marked correctly so that only prime numbers have 0's at their location in the array. The displayArray process should display 1,3,5,7,11,13 etc. up to 65,000.
There is an include file also
Thanks for your help
TITLE MASM Template (main.asm)
; Program: Chapter 9 Problems 1,2,7
; Author: Jon Wayman
; Created Date: July 11th,2008
; Revision date:
INCLUDE Irvine32.inc
.data
intVal DWORD ?
sString BYTE "This string is 34 characters long.",0
tString BYTE 100 DUP (?)
msgMaxStr BYTE "Enter the maximum string size to copy: ",0
targetStr BYTE "ABCED",10 DUP(0)
sourceStr BYTE "FGH",0
.data?
count = 65000 ; 65,000 element array
sieve DWORD count DUP(?)
.code
main PROC
;------------- Problem 1 -----------------
mov edx,OFFSET msgMaxStr ;move Max String size message
call writestring ;write message to console
call crlf
call readint ;Read an integer value for Str_copyN
mov intVal,eax
mov ebx,eax
push OFFSET tString
push OFFSET sString
call Str_copyN
mov edx,OFFSET tString
call writestring
call crlf
;------------- Problem 1 -----------------
;------------- Problem 2 -----------------
push OFFSET targetStr ; push the offest of the target
push OFFSET sourceStr ; push the offset of the source
call Str_concat
mov edx,OFFSET targetStr
call writeString
call crlf
;------------- Problem 2 -----------------
;------------- Problem 7 -----------------
push count
push OFFSET sieve
call initArray
push count
push OFFSET sieve
call findSieve
push count
push OFFSET sieve
call displayArray
;------------- Problem 7 -----------------
exit
main ENDP
;--------------------------------------------------
Str_copyN PROC USES eax ebx ecx esi edi,
source: PTR BYTE,
target: PTR BYTE,
maxSz: DWORD
;
;Copy a string from a source to target.
;The user defines the maximum string size to copy.
;Requires: the target string must contain enough
; space to hold a copy of the source string.
;--------------------------------------------------
mov maxSz,ebx
INVOKE Str_length,source ; EAX = length of source
cmp maxSz,eax
jge L1
mov ecx,maxSz
dec ecx
jne L2
L1: mov ecx,eax ; REP count
L2: inc ecx
mov esi,source
mov edi,target
cld
rep movsb
ret
Str_copyN ENDP
;--------------------------------------------------
Str_concat PROC USES eax ecx esi edi,
source2: PTR BYTE,
target2: PTR BYTE,
;
;Append the source string to the target string
;Requires: the target string must contain enough
; space to hold a copy of the source string.
;--------------------------------------------------
INVOKE Str_length,source2 ; EAX = length of source
mov ecx,eax ; REP count
inc ecx ; add 1 for null byte
mov esi,source2
INVOKE Str_length,target2 ; EAX = length of targte
mov edi,target2
add edi,eax ; EDI = end of target
cld
rep movsb
ret
Str_concat ENDP
;--------------------------------------------------
initArray PROC USES eax ecx edi,
pArray: PTR DWORD,
CountTmp: PTR DWORD
;
; This procedure initializes an array with zeros
;--------------------------------------------------
mov al,0 ; AL = 0
mov edi,pArray ; EDI points to array
mov ecx,CountTmp ; loop counter
cld
rep stosb ; fill with zeros
ret
initArray ENDP
;--------------------------------------------------
findSieve PROC uses ebx ecx edi,
pArray: PTR DWORD,
CountTmp2: PTR DWORD
;
; Mark all non-primes with 1
;--------------------------------------------------
mov edx, pArray
push 2
pop eax
iLOOP:
mov ecx, eax
shl ecx, 1
jLOOP:
mov ebx, CountTmp2
cmp ecx, ebx
ja @F
mov DWORD PTR [edx+ecx-1], 4
add ecx, eax
jmp jLOOP
@@:
inc eax
shr ebx, 1
cmp eax, ebx
jb iLOOP
RET
findSieve ENDP
;--------------------------------------------------
displayArray PROC USES eax ecx edi esi,
pArray: PTR DWORD,
CountTmp: PTR DWORD
;
; This procedure displays the array
;--------------------------------------------------
cld
mov esi,pArray
mov edi,esi
mov ecx,CountTmp
mov al,0
zLOOP:
cmp [edi],al
je J1
lodsd
call writeInt ;display integer
mov al,','
call writeChar
J1:
loop zLOOP
ret
displayArray ENDP
END main