; CIS-261
; M08.ASM
; @topic W080110 IO.H and IO.ASM usage demo
; @brief Lab M08, <a href="http:;www.c-jump.com/bcc/c261c/MLabs/M08console/M08console.html" target="_blank">Console Input/Output</a>, validate range 0 to 255

.386                ; Tells MASM to use Intel 80386 instruction set.
.MODEL FLAT         ; Flat memory model
option casemap:none ; Treat labels as case-sensitive

INCLUDE IO.H        ; header file for input/output

.CONST              ; Constant data segment
SZ_PROMPT_8BIT_UNSIGNED BYTE "Input 8-bit unsigned integer: ", 0
SZ_PROMPT_BAD_INPUT     BYTE "***BAD INPUT*** try again!", 0
SZ_RESULT               BYTE "The result is: ", 0
SZ_NEW_LINE             BYTE 13, 10, 0
SZ_PAUSE                BYTE "Please type x and hit Enter to exit: ", 0

.DATA               ; Begin initialized data segment
buffer              BYTE    12 DUP (?)
dtoa_buffer         BYTE    11 DUP (?)
atoa_buffer         BYTE    6  DUP (?)

.CODE               ; Begin code segment
_main PROC          ; Beginning of code

input_first_8_bit:
    ;--------------------------------------------------------
    ; Input first 8-bit unsigned integers from the user
    ;--------------------------------------------------------
    output SZ_PROMPT_8BIT_UNSIGNED
    input buffer, SIZEOF buffer ; Get user input
    ;--------------------------------------------------------
    ; Validate input length and make sure it's 0 < input < 4
    ;--------------------------------------------------------
    szlen buffer                ; Get input size in EAX
    or EAX, EAX                 ; Is EAX zero?
    jnz @F                      ; if not, then move on
    output SZ_PROMPT_BAD_INPUT
    output SZ_NEW_LINE
    jmp input_first_8_bit       ; repeat the input
@@:
    cmp EAX, 3                  ; Make sure no more than 3 characters
    jle @F                      ; if everything okay move on
    output SZ_PROMPT_BAD_INPUT
    output SZ_NEW_LINE
    jmp input_first_8_bit       ; repeat the input
@@:
    ;--------------------------------------------------------
    ; Validate range from 0 to 255
    ;--------------------------------------------------------
    atod buffer                 ; convert text to number in EAX
    jno @F                      ; if it worked, then move on
    output SZ_PROMPT_BAD_INPUT
    output SZ_NEW_LINE
    jmp input_first_8_bit       ; repeat the input
@@:
    ;--------------------------------------------------------
    ; Now EAX contains our first number
    ;--------------------------------------------------------
    cmp EAX, 0                  ; See if EAX >= 0
    jge @F                      ; EAX is >= 0, which is good 
    output SZ_PROMPT_BAD_INPUT
    output SZ_NEW_LINE
    jmp input_first_8_bit       ; repeat the input
@@:
    cmp EAX, 255                ; See if EAX > 255
    jle @F                      ; EAX <= 255, which is good
    output SZ_PROMPT_BAD_INPUT
    output SZ_NEW_LINE
    jmp input_first_8_bit       ; repeat the input
@@:
    
    ;--------------------------------------------------------
    ; TODO:
    ;--------------------------------------------------------
    ; Input second 8-bit unsigned integers from the user
    ; Validate range from 0 to 255
    ; Calculate the sum of the two numbers
    ; Check for the overflow; if yes, repeat the input

    ;--------------------------------------------------------
    ; Print result in EAX on the screen
    ;--------------------------------------------------------
    dtoa dtoa_buffer, EAX
    output SZ_RESULT
    output dtoa_buffer
    output SZ_NEW_LINE

    ;--------------------------------------------------------
    ; Pause before exiting
    ;--------------------------------------------------------
    output SZ_PAUSE
    input buffer, SIZEOF buffer
    ret
    
_main ENDP
END _main        ; Marks the end of the module and sets the program entry point label