;FILENAME: UNIT5.TXT ; ; ASSEMBLY LANGUAGE COURSE, UNIT 5 ; By Don Stoner Revision: 2008.06.17 ; ; WARNING: Assembly language programs, ; like the ones you are going to be ; experimenting with here, can do ; absolutely anything! Eventually, as ; you progress through these units, ; YOU *WILL* CRASH THE HARD DRIVE OF ; THE COMPUTER YOU ARE USING! Make sure ; that there is ABSOLUTELY NOTHING on ; that computer that you can't afford ; to loose! In fact, having a dedicated ; computer would be a very good idea. ; ; As always, type: ; ; MAKE UNIT5 ; UNIT5 ; ; Since you will be experimenting with ; program loops in this unit, you may ; find that sometimes the program will ; not return to the DOS prompt when it ; is through running. When it doesn't, ; you may be able to exit by holding ; the "Ctrl" key down while pressing ; the "C" or "Break" keys. If this ; doesn't work hold the "Ctrl" and ; "Alt," keys down while pressing the ; "Del" key to reboot the computer. If ; this still doesn't work, reset the ; computer or turn its power off for a ; few seconds to reboot it. ; ; Code Starts at 100h ORG 100h ; This time we'll just echo ; all of the typed keys until ; an "x" (lowercase) is typed. ; ; This program contains a loop ; which means it will running ; the same code over and over. ; ; First we need to tell NASM ; where the loop-back point is. ; This is an arbitrary label ; (but it is case sensitive as ; before). LOOP_BACK_TO_HERE: ; Read a character and echo it: CALL ASCII_IN ;GET KEY (AL) CALL ASCII_OUT ;ECHO IT ; The echoed character is still ; supposed to be in AL, (except ; that we can't necessarily ; trust all BIOS programmers ; always to follow all of the ; rules they are supposed to ; follow). ; ; Here we will take our chances ; and assume the key is still ; in AL. This means we can ; compare AL to the ASCII ; code for an "x" (lower case). ; An upper case "X" won't cause ; an exit: CMP AL,"x" ; The "CMP" (compare) command ; sets some of the computer's ; "status" bits. Here we are ; wondering whether the "X" is ; or isn't equal to the key. JNE LOOP_BACK_TO_HERE ; We jump (J) back to our label ; if it isn't equal, (NE means ; not equal, but if it is equal ; we just drop through to the ; exit command: INT 20h ;back to DOS ; end of main program ;-------------------------------------- ; Subroutines: ; ; Subroutine to output an ASCII ; character from AL to the monitor: ; ASCII_OUT: MOV AH,0Eh ;select "output" MOV BL,0 ;select page and MOV BH,0 ;graphics color INT 10h ;BIOS routines RET ;return to main code ; ; Subroutine to input an ASCII ; character from the keyboard into AL: ; ASCII_IN: MOV AH,0 ;use normal ;keyboard system ;BIOS routine to INT 16H ;read a key into ;the AL register RET ;return to main code ; And this is the end of the code