Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 x86 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 Irvine32.inc
ExitProcess proto, dwExitCode:dword
.DATA
message1 BYTE "Enter the number of primes to be displayed (1 to 200): ",0
errorMessage BYTE "Error: Please enter a number between 1 and 200.",0
primesMsg BYTE "The first ",0
primesMsg2 BYTE " prime numbers are:", 0
name1 BYTE "Programmer: ",0
title1 BYTE "Prime Number Calculation", 0
goodbye1 BYTE "Thank you for using the program!"
newline BYTE 0Dh,0Ah,0
threeSpaces BYTE "",0
inputBuffer BYTE 4 DUP(0)
primeBuffer DWORD 200 DUP(0)
; Constants
LOWER_BOUND =1
UPPER_BOUND =200
.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, 0
main ENDP
; Procedure: introduction
introduction PROC
mov edx, OFFSET name1
call WriteString
call Crlf
mov edx, OFFSET title1
call WriteString
call Crlf
call Crlf
ret
introduction ENDP
; Procedure: getUserData
getUserData PROC
mov n,0
mov primeCount, 0
inputLoop:
mov edx, OFFSET message1
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, 0
je inputLoop ; If invalid, loop again
ret
getUserData ENDP
; Procedure: atod (ASCII to DWORD conversion)
atod PROC
; Input: EDX points to a null-terminated string
; Output: EAX contains the integer value
xor eax, eax
xor ecx, ecx
mov ecx, 10
convertLoop:
movzx ebx, byte ptr [edx]
cmp bl,0
je doneConvert
sub bl,'0'
imul eax, ecx
add eax, ebx
inc edx
jmp convertLoop
doneConvert:
ret
atod ENDP
; Procedure: validate
validate PROC
mov eax, n
cmp eax, LOWER_BOUND
jl invalidInput
cmp eax, UPPER_BOUND
jg invalidInput
mov eax, 1 ; Valid input
ret
invalidInput:
mov edx, OFFSET errorMessage
call WriteString
call Crlf
mov eax, 0 ; Invalid input
ret
validate ENDP
; Procedure: showPrimes
showPrimes PROC
mov edx, OFFSET primesMsg
call WriteString
mov eax, n
call WriteInt
mov edx, OFFSET primesMsg2
call WriteString
call Crlf
mov primeCount, 0
mov candidate, 2
primeLoop:
mov eax, n
cmp eax, [primeCount]
jge done
mov eax, candidate
call isPrime
cmp eax, 1
jne notPrime
; Store the prime number
mov eax, candidate
mov ebx, primeCount
shl ebx, 2
mov primeBuffer[ebx], eax
inc primeCount
notPrime:
inc candidate
jmp primeLoop
done:
; Display primes 10 per line
mov i,0
displayLoop:
mov eax, i
cmp eax, primeCount
jge endDisplay
mov ebx, i
shl ebx, 2
mov eax, primeBuffer[ebx]
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, 10
idiv ebx
cmp edx, 0
jne displayLoop
call Crlf
jmp displayLoop
endDisplay:
call Crlf
ret
showPrimes ENDP
; Procedure: isPrime
isPrime PROC
mov eax, 1 ; Assume prime
mov ecx, 2
mov ebx, candidate
sqrtLoop:
cmp ecx, ebx
jge endSqrtLoop
mov edx, 0
div ecx
cmp edx, 0
je notPrime
inc ecx
jmp sqrtLoop
endSqrtLoop:
ret
notPrime:
mov eax, 0 ; Not a prime
ret
isPrime ENDP
; Procedure: farewell
farewell PROC
call Crlf
mov edx, OFFSET goodbye1
call WriteString
call Crlf
ret
farewell ENDP
END main

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

Students also viewed these Databases questions