My decision statement operates okay, but I'm having a difficulty time figuring out why the decision statement that control the number of trys not responding.
I can I create this?
INCLUDE Irvine32.inc
ARRAY_SIZE = 20
.data
str1 BYTE "Enter a signed integer: ", 0
str2 BYTE "The sum of the integers is: ", 0
array DWORD ARRAY_SIZE DUP( ? )
; I code below is what I added.
numberOfInt BYTE "How many integers will be added? ", 0
errorMsg BYTE "Number of integer should be 20 or less!", 0dh, 0ah, 0
exitProgramMsg BYTE "Sorry only three attempt is allowed.", 0dh, 0ah
BYTE "This program will now close.", 0dh, 0ah
BYTE "Goodbyu!", 0
.code
main PROC
call Clrscr
mov esi, OFFSET array
;mov ecx, INTEGER_COUNT ; Comment out this line of code to make the modification work.
call NumberOfIntegers
call PromptForIntegers
call ArraySum
call DisplaySum
exit
main ENDP
; The procedure below is what I added to modify this program.
NumberOfIntegers PROC
COMMENT !
Prompts the user for the number of integers that will be add
and check if the integer is valid. If the integer is not valid
after three attempt the program close.
Receives: nothing
Returns: ECX = number of times the user will enter an integer.
!
mov ecx, 0 ; Set the ECX register to zero
.REPEAT
; Ask for an input ffrom the user.
mov edx, OFFSET numberOfInt ; "How many integers will be added? "
call WriteString ; Display string
call ReadInt ; Read integer into EAX
call Crlf ; Go to next output line
; Check if the input is invalid
.IF eax > 20 ; Check if the input is invalid
mov edx, OFFSET errorMsg ; "Number of integer should be 20 or less!"
call WriteString ; Display error msg
call Crlf ; Go to next output line
inc ecx ; increment ecx counter
.ENDIF
; Check how meny time the user attempt to input a valid input.
.IF ecx == 3 ; Check if the uers attempt an input 3 times.
mov edx, OFFSET exitProgramMsg ; "Sorry only three attempt is allowed."
call WriteString ; Display string
call Crlf ; Go to next output line
exit
.ENDIF
; End the REPEAT UNTIL loop if the user input is valid or goes back to the REPEAT statement.
.UNTIL eax <= 20 ; Check if the input is valid or go back to the REPEAT statement.
mov ecx, eax ; Set the loop counter
ret
NumberOfIntegers ENDP
; Every procedure below this line is from the book.
PromptForIntegers PROC USES ecx edx esi
COMMENT !
Prompts the user for an arbitrary number of integers
and inserts the integers into an array.
Receives: ESI points to array, ECX = array size
Returns: nothing
!
mov edx, OFFSET str1 ; "Enter a signed integer"
L1:
call WriteString ; display string
call ReadInt ; read integer into EAX
call Crlf ; go to next output line
mov [ esi ], eax ; store in array
add esi, TYPE DWORD ; next integer
loop L1
ret
PromptForIntegers ENDP
ArraySum PROC USES esi ecx
COMMENT !
Calculates the sum of an array of 32-bit integers.
Receives: ESI points to array, ECX = number
Returns: EAX = sum of the array elements
!
mov eax, 0 ; set the sum to zero
L1:
add eax, [ esi ] ; add each integer to sum
add esi, TYPE DWORD ; point to next integer
loop L1 ; repeat for array size
ret ; sum is in EAX
ArraySum ENDP
DisplaySum PROC USES edx
COMMENT !
Display the sum on the screen
Receives: EAX = the sum
Returns: nothing
!
mov edx, OFFSET str2 ; "The sum of the..."
call WriteString
call WriteInt ; display EAX
call Crlf
ret
DisplaySum ENDP
END main