Objectives: 1) Designing and implementing procedures 2) Designing and implementi
ID: 3795374 • Letter: O
Question
Objectives:
1) Designing and implementing procedures
2) Designing and implementing loops
3) Writing nested loops
4) Understanding data validation
Problem Definition:
Write a program using MASM to calculate composite numbers. First, the user is instructed to enter the number of composites to be displayed, and is prompted to enter an integer in the range [1 .. 400]. The user enters a number, n, and the program verifies that 1 n 400. If n is out of range, the user is re-prompted until s/he enters a value in the specified range. The program then calculates and displays all of the composite numbers up to and including the nth composite. The results should be displayed 10 composites per line with at least 3 spaces between the numbers.
Requirements:
1) The programmer’s name must appear in the output.
2) The counting loop (1 to n) must be implemented using the MASM loop instruction.
3) The main procedure must consist (mostly) of procedure calls. It should be a readable “list” of what the program will do.
4) Each procedure will implement a section of the program logic, i.e., each procedure will specify how the logic of its section is implemented. The program must be modularized into at least the following procedures and sub-procedures :
• introduction
• getUserData
• validate
• showComposites
• isComposite
• farewell
5) The upper limit should be defined and used as a constant.
6) Data validation is required. If the user enters a number outside the range [1 .. 400] an error message should be displayed and the user should be prompted to re-enter the number of composites.
7) The usual requirements regarding documentation, readability, user-friendliness, etc., apply.
8) Submit your text code file (.asm) to Canvas by the due date.
Notes:
1) For this program, you may use global variables instead of passing parameters. This is a one-time relaxation of the standards so that you can get accustomed to using procedures.
2) A number k is composite if it can be factored into a product of smaller integers. Every integer greater than one is either prime or composite. Note that this implies that
a. 1 is not composite.
b. The number must be positive.
3) There are several ways to make your isComposite procedure efficient. (I recommend discussing this in your groups!)
4) See next page for an example execution
Example:
Composite Numbers Programmed by Euclid
Enter the number of composite numbers you would like to see.
I’ll accept orders for up to 400 composites.
Enter the number of composites to display [1 .. 400]: 501
Out of range. Try again.
Enter the number of composites to display [1 .. 400]: 0
Out of range. Try again.
Enter the number of composites to display [1 .. 400]: 31
4 6 8 9 10 12 14 15 16 18
20 21 22 24 25 26 27 28 30 32
33 34 35 36 38 39 40 42 44 45
46
Results certified by Euclid. Goodbye.
Extra Credit:
1) Align the output columns.
2) Display more composites, but show them one page at a time. The user can “Press any key to continue …” to view the next page. Since length of the numbers will increase, it’s OK to display fewer numbers per line.
3) One way to make the program more efficient is to check against only prime divisors, which requires saving all of the primes found so far (numbers that fail the composite test). It’s easy in a high-level language, but you will have to look ahead in the textbook to figure out how to do it in assembly language.
To ensure you receive credit for any extra credit options you did, you must add one print statement to your program output PER EXTRA CREDIT which describes the extra credit you chose to work on. You will not receive extra credit points unless you do this. The statement must be formatted as follows...
--Program Intro--
**EC: DESCRIPTION
--Program prompts, etc—
Explanation / Answer
INCLUDE Irvine32.inc
.data
welcome BYTE "Welcome to Composites by your name.", 0
instructions_1 BYTE "Please enter a number between [1, 400] to see all ",0
instructions_2 BYTE "of the composites up to and including the number you entered", 0
instructions_3 BYTE "Please enter a number between 1 and 400.", 0
belowError BYTE "The number you entered was too small. ", 0
aboveError BYTE "The number you entered was too big. ", 0
spaces BYTE " ", 0
goodbye BYTE "Tchau for now!", 0
number DWORD ?
count DWORD 1
userNumber DWORD ?
userNumberTemp DWORD ?
innerLoopCount DWORD ?
outerLoopCount DWORD ?
underScore BYTE " _ ", 0
barr BYTE " | ", 0
outerCompare DWORD ?
innerCompare DWORD ?
writeCount DWORD 0
tenn DWORD 10
;constants
LOWERLIMIT = 1
UPPERLIMIT = 400
;change text color, because white text is a little boring after a while
val1 DWORD 11
val2 DWORD 16
.code
main PROC
call changeColor
call introduction
call getUserData
;validate
call showComposites
;validate is composite
call farewell
exit
main ENDP
changeColor PROC
; Set text color to teal
mov eax, val2
imul eax, 16
add eax, val1
call setTextColor
ret
changeColor ENDP
introduction PROC
; Programmer name and title of assignment
call CrLf
mov edx, OFFSET welcome
call WriteString
call CrLf
; assignment instructions
mov edx, OFFSET instructions_1
call WriteString
mov edx, OFFSET instructions_2
call WriteString
call CrLf
mov ecx, 0
ret
introduction ENDP
getUserData PROC
; loop to allow user to continue entering negative numbers
userNumberLoop:
mov eax, count
add eax, 1
mov count, eax
mov edx, OFFSET instructions_3
call WriteString
call CrLf
call ReadInt
mov userNumber, eax
cmp eax,LOWERLIMIT
jb errorBelow
cmp eax, UPPERLIMIT
jg errorAbove
jmp continue
;validation
errorBelow:
mov edx, OFFSET belowError
call WriteString
call CrLf
jmp userNumberLoop
errorAbove:
mov edx, OFFSET aboveError
call WriteString
call CrLf
jmp userNumberLoop
continue:
; prep the loop
mov ecx, 4
mov userNumberTemp, ecx
cmp ecx, userNumber
ja farewell
ret
getUserData ENDP
showComposites PROC
; for inner loop
mov eax, userNumber
sub eax, 2
mov innerLoopCount, eax
; for outer loop
mov eax, userNumber
sub eax, 3
mov outerLoopCount, eax
mov ecx, outerLoopCount
mov eax, 4
mov outerCompare, eax
; reset inner loop after each complete inner loop cycle
mov eax, 2
mov innerCompare, eax
call CrLf
outerLoop:
skipCarry:
mov eax, 2
mov innerCompare, eax
mov eax, outerCompare
push ecx
push eax
mov ecx, innerLoopCount
isComposite:
mov eax, outerCompare
mov edx, 0
div innerCompare
cmp edx, 0
jne skipPrint
; print out Composites
mov eax, outerCompare
call WriteDec
mov edx, OFFSET spaces
call WriteString
mov ebx, writeCount
inc ebx
mov writeCount, ebx
cmp ebx, 10
jne exitInnerLoop
call CrLf
mov writeCount,esi
jmp exitInnerLoop
skipPrint:
mov ebx, innerCompare
sub eax, 1
cmp eax, ebx
jae skipIncrement
add eax, 1
mov innerCompare, eax
skipIncrement:
loop isComposite
exitInnerLoop:
pop eax
pop ecx
inc eax
mov outerCompare, eax
loop outerLoop
ret
showComposites ENDP
farewell PROC
; say goodbye
call CrLf
mov edx, OFFSET goodbye
call WriteString
call CrLf
call CrLf
exit
farewell ENDP
END main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.