TITLE hw5_ex1 (hw5_ex1.asm)
; this program prompts the user to enter 10 numbers between 0 and 9
; the program should print error message if the input invalid and
; prompt to enter a valid one, at the end the program should print
; only the valid input.
INCLUDE Irvine32.inc
.data
msg1 BYTE "Enter a number between 0 and 9: ",0
msg2 BYTE " invalid number!",0
.code
main PROC
call Clrscr
mov edx,OFFSET msg1
mov numbers, edx
call Writestring
call readInt
call crlf
mov ebx, numbers ; point to the first of the numbers
add ebx, 9 ; point to the last of the numbers
data_entry_loop:
cmp ebx, numbers ; compare ebx and number
jb end_data_entry_loop ; if ebx < number, jump
data_entry:
mov eax, msg1 ; print msg1
call writestring ; print msg1
call readint ; read the number
cmp eax, 9 ; compare integer and 9
ja int_not_ok ; if > 9 jump to int_not_ok
cmp eax, 0 ; compare integer and 0
jb int_not_ok ; if >= 0 jump to int_not_ok
jmp int_ok ; jump to int_ok
int_not_ok:
mov eax, msg2 ; print msg2
call writestring ; print msg2
call crlf ; print a new line
jmp data_entry ; retry to get an integer
int_ok:
mov byte [ebx], al ; store the integer
dec ebx ; ebx = ebx - 1 (points to the previous integer)
jmp data_entry_loop ; continue the loop
end_data_entry_loop:
mov ebx, numbers ; point to the first of the numbers
add ebx, 9 ; point to the last of the numbers
print_loop:
cmp ebx, numbers ; compare ebx and number
jb end_print_loop ; if ebx < number, jump
mov al, [ebx] ; ax = current stored integer
movzx eax, al ; zero-extend ax into eax for printing
call writestring ; print the number
call crlf ; print a new line
dec ebx ; ebx = ebx - 1 (points to the previous integer)
jmp print_loop ; continue the loop
end_print_loop:
main ENDP
END main