INCLUDE Irvine32.inc
.data
marksArray DWORD 100 DUP(0) ; Array to hold marks
count DWORD ?
average DWORD ?
gradeMsg BYTE 0 ; Define gradeMsg as a null-terminated string
.code
main PROC
mov count, 0 ; Initialize count to 0
inputLoop:
mov eax, count
mov edx, OFFSET marksArray
add edx, eax ; Calculate the offset for the current input
; Prompt user for input
call ReadInt
mov esi, eax ; Integer value
; Check for termination condition
cmp esi, -1
je calculateAndDisplay ; Jump to calculation and display if -1 entered
; Check for valid input
cmp esi, 0
jl invalidEntry ; Jump if negative (invalid)
cmp esi, 100
jg invalidEntry ; Jump if greater than 100 (invalid)
; Store valid input in the array
mov [edx], esi
inc count
jmp inputLoop
invalidEntry:
mov edx, OFFSET invalidMsg
call WriteString
jmp inputLoop
calculateAndDisplay:
; Calculate average of all entered marks
mov edx, OFFSET marksArray
mov ecx, count
call marks_avg
; Determine grade based on the average
mov eax, average
lea edx, gradeMsg ; Use lea to load the address of the string
call grad_marks
; Display the values stored in the array
mov edx, OFFSET marksArray
mov ecx, count
call DisplayArray
; Exit the program
call DumpRegs
exit
main ENDP
; Procedure to calculate the average of all entered marks
marks_avg PROC
mov eax, 0
mov ecx, count
avgLoop:
add eax, [edx]
add edx, 4
loop avgLoop
mov ebx, count
cdq
idiv ebx
mov average, eax
ret
marks_avg ENDP
; Procedure to determine the grade based on the average
grad_marks PROC
cmp eax, 40
jl fail
cmp eax, 81
jg distinction
mov edx, OFFSET passMsg
jmp done
fail:
mov edx, OFFSET failMsg
jmp done
distinction:
mov edx, OFFSET distinctionMsg
done:
call WriteString
ret
grad_marks ENDP
; Procedure to display the values stored in the array
DisplayArray PROC
mov eax, 0
mov ecx, count
displayLoop:
mov edx, [OFFSET marksArray + eax \* 4] ; Load the current element
call WriteInt
call Crlf
add eax, 1
loop displayLoop
ret
DisplayArray ENDP
.data
invalidMsg BYTE “Invalid Entry”, 0
passMsg BYTE “Grade = Pass”, 0
failMsg BYTE “Grade = Fail”, 0
distinctionMsg BYTE “Grade = Distinction”, 0
END main