;FILENAME: UNIT9.TXT ; ; ASSEMBLY LANGUAGE COURSE, UNIT 9 ; By Don Stoner Revision: 2008.06.29 ; ORG 100h ; Code Starts at 100h ; ; This program will output a ; string of ASCII characters ; which is terminated by a byte ; containing the number zero. ; An ASCII string termintaed ; with a zero is called an ; "ASCIIZ" string. We will use ; a subroutine to do this. To ; tell the subroutine which ; ASCII string to display, we ; put its address in the SI ; register like this: ; MOV SI,STRING ;LOAD SI WITH ;THE ASCIIZ ADDRESS ; ; Then, we call the subroutine: ; CALL ASCIIZ_OUT ; (The string ; "STRING1" and ; the subroutine are ; both found below.) ; ; Here is another way we could ; do the same thing, but ; without having to load the ; SI register first. This ; inline subroutine gets its ; string pointer from its ; subroutine return address! ; CALL INLINE_ASCIIZ_OUT DB "And the second string",0 ; INT 20h ;Back to DOS STRING: DB "Here is the first string",0 ; ; This will cause the two strings to ; run together. You could change the ; string's end into: ...string",13,10,0 ; if you want to add a carriage return ; (ASCII 13) and line feed (ASCII 10) ; between the two strings. ; ; End of Main code ;-------------------------------------- ; Subroutines: ; ; Output an ASCIIZ string (from [SI]) ; ASCIIZ_OUT: LODSB ;Load byte into AL from ;[SI] and incriment SI ; This instruction does the ; same thing as: ; MOV AL,[SI] ; ; INC SI ; LODS is a special single-byte ; instruction that takes care ; of both operations at the ; same time. In this case, we ; load the first byte of the ; string into AL, while we cue ; SI to point to the second. ; ; Next we see if we are at the ; end of the string by testing ; the byte to see if it is =0. ; AND AL,AL ;AND AL with itself ; ; We could have used the more ; obvious: ; CMP AL,0 ; here, but "AND" effectively ; does the same thing while ; useing less of the computer's ; resources. ; ; Next, we exit if we are ; already at the final zero. ; JZ END_ASCIIZ ;end if at zero ; ; Otherwise, we output the ; ASCII character: ; CALL ASCII_OUT ; ; And loop back to get another: ; JMP ASCIIZ_OUT ;Loop back ; END_ASCIIZ: RET ;return to main code ; ; Output an inline ASCIIZ string ; (using the subroutine return address) ; INLINE_ASCIIZ_OUT: POP SI ;LOAD return address ;(it's saved on the stack) ;into the SI register CALL ASCIIZ_OUT ;output string PUSH SI ;Get new return address ; (just past the final "0") RET ;And return to it ; ; Output AL in ASCII ; 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