; Author: Example ; Date: March 31, 2011 ; ; Description: ; The program is a simple guessing game for a number between 0 and 9. ; The user gets 10 tries to guess the number, if their guess is high ; display to high, if their guess is low display to low, and if they don't ; right in 10 tries, display fail. ; If they guess correct display the number of attempts ; ; Variables: ; R0: display number of tries (ascii) ; R1: ; R2: guess counter ; R3: compares guess to value ; R4: ; R5: ascii mask (x30) ; R6: ascii target value (negative) ; R7: ; ; Initialization ; .ORIG x3000 LD R6, val ;r6 will hold the # to guess LD R5, ascii ;get the ascii mask ADD R6, R6, r5 ;ascii mask to display guess target value AND R2, R2, #0 ;clear r2, counter NOT R6, R6 ;prep R6 for comparsion (-R6) ADD R6, R6, #1 ;2's complement of R6 ;;;;;;;; ; Main loop for guessing game ;;;;;;;; loop LEA R0, guess ; load guess string PUTS ; display "?" to console (string must be in R0) GETC ; get guess value (ascii), does not echo to display, guess value in R0 OUT ; echo guess value, R0 ADD r2, r2, #1 ; increment counter ADD r1, r2, #-10 ; compare attempts, max 10. BRz fail ; max attempts reached (10), print results (fail) and halt ADD R3, R6, R0 ; compare guess to value (guess - value) BRn low ; negative, guess too small, branch low BRp high ; positive, guess too big, branch high ; zero, guess correct, fall thru, and print results LEA R0, tries ; load the tries string PUTS ADD R0, R2, R5 ; prep number of tries (R2) for display by adding ascii mask (R5) OUT ; display number of tries HALT ; halt program ;;;;;;;; ; Display feedback ;;;;;;;; ;;;;;;;; ; Failure to guess ;;;;;;;; fail LEA R0, noob ;load noob string puts HALT ;exit ;;;;;;;; ; Guess too high ;;;;;;;; high LEA R0, big ; load too big string PUTS BRnzp loop ;get back in the loop ;;;;;;;;; ; Guess to low ;;;;;;;;; low LEA R0, small ; load too small strng PUTS BRnzp loop ;get back in the loop ;;;;;; ; Variables ;;;;;; val .FILL #6 ascii .FILL 1 x0030 guess .STRINGZ "? " small .STRINGZ "\nHigher\n" big .STRINGZ "\nLower\n" tries .STRINGZ "\nTries: " noob .STRINGZ "\nFail" .END