; Shawn Nematbakhsh ; login: snematbakhsh, sec 22 ; as2 - This program prompts the user for a number and tells the user ; if it is even or odd. If the number is negative then an error is ; displayed. .ORIG x3000 ; offsets to check input LD R6,NEG_ASCII_ENTER LD R5,NEG_ASCII_HYPHEN LD R4,NEG_ASCII_ZERO LD R3,NEG_ASCII_Y ; put -1 into R2 to check if any input was taken later START AND R2, R2, #0 ADD R2, R2, #-1 ; print an input prompt and get input JSR PRINT_PROMPT JSR GET_AND_OUTPUT ; check to see if a negative sign was the first character ADD R1,R0,R5 ; if so, jump down BRnp POSITIVE ; if not, continue taking input until enter is hit and then go again ; if the user wants JSR DUMMY_LOOP BRnzp AGAIN ; check to see if enter is hit POSITIVE ADD R1,R0,R6 ; if so then calculate the parity BRz CALCULATE ; otherwise store the last input into R2 and get input again AND R2,R0,R0 JSR GET_AND_OUTPUT BRnzp POSITIVE ; check for the case that the user hit enter without entering ; anything CALCULATE AND R2,R2,R2 BRn NOTHING_ERROR ; determine the parity and print the proper message then go again ; again if the user chooses to JSR DETERMINE_PARITY BRnzp AGAIN ; if just an enter was hit then print an error NOTHING_ERROR JSR PRINT_ERROR ; see if the user wants to go again and if so then go again AGAIN JSR CHECK_AGAIN BRnzp START ; ; this sub-routine asks the user if they want to go again. If ; they don't answer "Y" then the program is halted ; ; store the return address so we can get back CHECK_AGAIN AND R1,R7,R7 ; print the prompt, then get and output a character LEA R0,AGAIN_PROMPT PUTS GETC OUT ; if the user enters "Y" then halt the program ADD R0,R0,R3 BRnp STOP ; otherwise restore the return address and return AND R7,R1,R1 RET STOP HALT ; ; this sub-routine determines the parity of the number stored ; in R2 as a character ; ; store return address DETERMINE_PARITY AND R1,R7,R7 ; subtract ascii zero from R2 ADD R2,R2,R4 ; and R2 with one to check the last bit AND R2,R2,#1 ; if it's odd then print the odd message otherwise print the ; even message and return BRp ODD LEA R0,EVEN_MSG PUTS ; restore the return address AND R7,R1,R1 RET ; odd case ODD LEA R0,ODD_MSG PUTS AND R7,R1,R1 RET ; ; This sub-routine gets a character and outputs it to the screen ; ; ; store return address GET_AND_OUTPUT AND R1,R7,R7 ; get and output a character GETC OUT ; restore the return address and return AND R7,R1,R1 RET ; ; This sub-routine is called when the user enters a negative ; sign. It just takes characters until an enter is hit. ; ; store return address DUMMY_LOOP AND R1,R7,R7 ; get a character and output it GET_ANOTHER GETC OUT ; check if enter was hit, and if it is then print an error ADD R2,R0,R6 BRz CONT ; otherwise just get another character BRnzp GET_ANOTHER ; print an error message CONT LEA R0,ERROR_MSG PUTS ; restore return address and return AND R7,R1,R1 RET ; ; This sub-routine prints the input prompt ; ; load prompt PRINT_PROMPT LEA R0,INPUT_PROMPT ; store return address AND R1,R7,R7 ; output prompt PUTS ; restore return address and return AND R7,R1,R1 RET ; ; This sub-routine prints the error prompt. ; PRINT_ERROR LEA R0,ERROR_MSG ; store return address AND R1,R7,R7 PUTS ; restore and return AND R7,R1,R1 RET ; these constants are used for checking the user input NEG_ASCII_Y .fill #-89 NEG_ASCII_ZERO .fill #-48 NEG_ASCII_ENTER .fill #-10 NEG_ASCII_HYPHEN .fill #-45 ; these are the various prompts that need be outputed INPUT_PROMPT .stringz "\nInput a Number> " EVEN_MSG .stringz "This Number is even\n\n" ODD_MSG .stringz "This Number is odd\n\n" ERROR_MSG .stringz "Error!\n\n" AGAIN_PROMPT .stringz "Enter \"Y\" to go again: " .END