Question: Masm x 8 6 assembly code working but does not display the prime numbers when user inputs from the specified range, What do i need
Masm x assembly
code working but does not display the prime numbers when user inputs from the specified range, What do i need to do to make it so it displays the prime numbers
INCLUDE Irvineinc
ExitProcess proto, dwExitCode:dword
DATA
message BYTE "Enter the number of primes to be displayed to :
errorMessage BYTE "Error: Please enter a number between and
primesMsg BYTE "The first
primesMsg BYTE prime numbers are:",
name BYTE "Programmer:
title BYTE "Prime Number Calculation",
goodbye BYTE "Thank you for using the program!"
newline BYTE DhAh
threeSpaces BYTE
inputBuffer BYTE DUP
primeBuffer DWORD DUP
; Constants
LOWERBOUND
UPPERBOUND
DATA?
n DWORD
primeCount DWORD
candidate DWORD
i DWORD
j DWORD
isPrimeFlag DWORD
CODE
main PROC
call introduction
call getUserData
call showPrimes
call farewell
; Exit the process
INVOKE ExitProcess,
main ENDP
; Procedure: introduction
introduction PROC
mov edx, OFFSET name
call WriteString
call Crlf
mov edx, OFFSET title
call WriteString
call Crlf
call Crlf
ret
introduction ENDP
; Procedure: getUserData
getUserData PROC
mov n
mov primeCount,
inputLoop:
mov edx, OFFSET message
call WriteString
mov edx, OFFSET inputBuffer
call ReadString
call atod ; convert ASCII to integer
mov n eax ; store the integer in n
call validate
cmp eax,
je inputLoop ; If invalid, loop again
ret
getUserData ENDP
; Procedure: atod ASCII to DWORD conversion
atod PROC
; Input: EDX points to a nullterminated string
; Output: EAX contains the integer value
xor eax, eax
xor ecx, ecx
mov ecx,
convertLoop:
movzx ebx, byte ptr edx
cmp bl
je doneConvert
sub bl
imul eax, ecx
add eax, ebx
inc edx
jmp convertLoop
doneConvert:
ret
atod ENDP
; Procedure: validate
validate PROC
mov eax, n
cmp eax, LOWERBOUND
jl invalidInput
cmp eax, UPPERBOUND
jg invalidInput
mov eax, ; Valid input
ret
invalidInput:
mov edx, OFFSET errorMessage
call WriteString
call Crlf
mov eax, ; Invalid input
ret
validate ENDP
; Procedure: showPrimes
showPrimes PROC
mov edx, OFFSET primesMsg
call WriteString
mov eax, n
call WriteInt
mov edx, OFFSET primesMsg
call WriteString
call Crlf
mov primeCount,
mov candidate,
primeLoop:
mov eax, n
cmp eax, primeCount
jge done
mov eax, candidate
call isPrime
cmp eax,
jne notPrime
; Store the prime number
mov eax, candidate
mov ebx, primeCount
shl ebx,
mov primeBufferebx eax
inc primeCount
notPrime:
inc candidate
jmp primeLoop
done:
; Display primes per line
mov i
displayLoop:
mov eax, i
cmp eax, primeCount
jge endDisplay
mov ebx, i
shl ebx,
mov eax, primeBufferebx
call WriteInt
mov edx, OFFSET threeSpaces
call WriteString
inc i
; Check if we need to move to a new line
mov eax, i
cdq
mov ebx,
idiv ebx
cmp edx,
jne displayLoop
call Crlf
jmp displayLoop
endDisplay:
call Crlf
ret
showPrimes ENDP
; Procedure: isPrime
isPrime PROC
mov eax, ; Assume prime
mov ecx,
mov ebx, candidate
sqrtLoop:
cmp ecx, ebx
jge endSqrtLoop
mov edx,
div ecx
cmp edx,
je notPrime
inc ecx
jmp sqrtLoop
endSqrtLoop:
ret
notPrime:
mov eax, ; Not a prime
ret
isPrime ENDP
; Procedure: farewell
farewell PROC
call Crlf
mov edx, OFFSET goodbye
call WriteString
call Crlf
ret
farewell ENDP
END main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
