;FILENAME: UNIT17.TXT ; ; ASSEMBLY LANGUAGE COURSE, UNIT 17 ; By Don Stoner Revision: 2008.07.20 ; ORG 100h ; Code Starts at 100h ; ; This program also prints the segment ; load location from the CS register. ; But instead of returning to DOS the ; normal way, we will use the Terminate ; and Stay Resident (TSR) method. This ; means that each time we run this ; program, some of its code will be ; left in the computer's memory (we get ; to decide how much). ; MOV AX,CS ;get CS CALL AXOUT ;and print it ; ; Each time we run this program, it ; will be loaded into a higher memory ; location than the last time (because ; some of each old load is left in ; memory). Of course this uses up your ; memory, but rebooting the computer ; will clear it all out again. If you ; try to compare how much memory DOS ; uses up with each load to the amount ; you have requested that it save for ; you, you will notice that DOS also ; uses up a little extra bit more for ; its own information tracking, ; ; When you use the TSR exit, the DX ; register holds the number of 16-byte ; pages of memory to reserve. This ; includes both the number of bytes in ; your program, however much additional ; memory you might choose to reserve, ; and the 256 bytes (100h) of the PSP. ; ; If you load the DX register with ; 10h (16 decimal), this will reserve ; 16 pages (of 16 bytes each) or 256 ; bytes of memory. This is enough to ; save the PSP, but not enough to save ; any of your own code. We load DX ; with 17h below, which reserves 70h ; bytes of memory for our own code. ; ; Terminate and Stay Resident: ; MOV AH,31H ;31h = "TSR" Code MOV AL,0 ;Return error code ;of "0" (no error) MOV DX,17H ;reserve 17h 16-byte ;pages of memory (170h) ;This includes the 100H ;PSP (see UNIT17) and ;70h of our own code. INT 21H ;DOS TSR Exit ;-------------------------------------- ; I/O Subroutines: ; AXOUT: PUSH AX MOV AL,AH CALL ALOUT ;AH out POP AX ; Drop through ALOUT: PUSH AX ;AL out SHR AL,4 CALL LSNOUT ;MSN POP AX ; Drop through LSNOUT: AND AL,0FH ;LSN OR AL,30H CMP AL,3AH JC ASCOUT ADD AL,41H-3AH ; Drop through ASCOUT: MOV AH,0EH ;ASCII out, TTY mode MOV BH,0 ;Page MOV BL,6 ;Graphics color INT 10H ;BIOS Call RET ;-------------------------------------- ; Here is some Data that you can try to ; find in memory with a debug editor: ; DB "0---+---1---+---" DB "2---+---3---+---" DB "4---+---5---+---" DB "6---+---7---+---" DB "8---+---9---+---" DB "A---+---B---+---" DB "C---+---D---+---" DB "E---+---F---+---" ; ; You can tell where each of these ; bytes will get loaded by looking at ; the UNIT18.LST file which NASM ; generates. Remember the 4-digit hex ; number the program prints the first ; time you run TEST. Then run TEST a ; second time. (DOS will write some ; information on top of anything that ; you wasn't within the memory space ; you reserved.) Next, run DEBUG and ; look at the location where the first ; copy of the program was loaded. If ; that number was "12AB", then type ; "D12AB:0-180" (it won't all fit on ; the screen). Since you only saved ; 17h pages (170h bytes 0-16Fh), DOS ; will have written over address 170h ; with some other information relating ; to the second load.