Program that reads five numbers from keyboard and then displays the maximum and minimum number on the screen.
UNIVERSITY OF THE PUNJAB
Third Semester 2014
Examination: B.S. 4 Years Programme
Paper: Computer Organization and Assembly Language
Course Code: IT-203 / 21402
Question # 3:
Write an assembly language program that reads five numbers from keyboard and then displays the maximum and minimum number on the screen. Your program should implement proper checks that only a number can be input.
Solution:
INCLUDE Irvine32.inc
count=5
.data
msg BYTE "Enter a number : ",0
error BYTE "Wrong Input!, Please enter a number : ",0
msgMin BYTE "Minimum integer is : ",0
msgMax BYTE "Maximum integer is : ",0
array DWORD count DUP(?)
.code
main PROC
mov esi, offset array
mov ecx, count
L1:
mov edx, offset msg
call WriteString
call ReadInt
call ValidateInput
mov [esi], eax
add esi, 4
loop L1
mov ecx, count
mov esi, offset array
mov eax, [esi] ;minimum in eax
mov ebx, [esi] ;minimum in ebx
L2:
.IF([esi]<eax)
mov eax, [esi]
.ELSEIF([esi]>ebx)
mov ebx, [esi]
.ENDIF
add esi, 4
loop L2
mov edx, offset msgMin
call WriteString
call WriteDec
call crlf
mov edx, offset msgMax
call WriteString
mov eax, ebx
call WriteDec
quit:
call crlf
call WaitMsg
exit
main ENDP
ValidateInput PROC
validate:
jno next
next:
jns quit
BadInput:
mov edx, offset error
call WriteString
call ReadInt
jmp validate
quit:
ret
ValidateInput ENDP
END main
count=5
.data
msg BYTE "Enter a number : ",0
error BYTE "Wrong Input!, Please enter a number : ",0
msgMin BYTE "Minimum integer is : ",0
msgMax BYTE "Maximum integer is : ",0
array DWORD count DUP(?)
.code
main PROC
mov esi, offset array
mov ecx, count
L1:
mov edx, offset msg
call WriteString
call ReadInt
call ValidateInput
mov [esi], eax
add esi, 4
loop L1
mov ecx, count
mov esi, offset array
mov eax, [esi] ;minimum in eax
mov ebx, [esi] ;minimum in ebx
L2:
.IF([esi]<eax)
mov eax, [esi]
.ELSEIF([esi]>ebx)
mov ebx, [esi]
.ENDIF
add esi, 4
loop L2
mov edx, offset msgMin
call WriteString
call WriteDec
call crlf
mov edx, offset msgMax
call WriteString
mov eax, ebx
call WriteDec
quit:
call crlf
call WaitMsg
exit
main ENDP
ValidateInput PROC
validate:
jno next
next:
jns quit
BadInput:
mov edx, offset error
call WriteString
call ReadInt
jmp validate
quit:
ret
ValidateInput ENDP
END main
Let me know in the comment section if you have any question.
Previous Post:
Program that calculates the factorial on an integer number.
Comments
Post a Comment