home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / nor_asm / video_io.asm < prev   
Assembly Source File  |  1989-08-13  |  9KB  |  300 lines

  1. .MODEL    SMALL
  2.  
  3. ;-----------------------------------------------------------------------;
  4. ; This file contains various procedures that write to the screen:    ;
  5. ;                                    ;
  6. ; INIT_WRITE_CHAR        Initializes this module            ;
  7. ; WRITE_CHAR            Writes a character on the screen    ;
  8. ; WRITE_STRING            Writes a string to the screen        ;
  9. ; WRITE_HEX            Writes a two-digit hex number        ;
  10. ; WRITE_HEX_DIGIT        Writes a single hexadecimal digit    ;
  11. ; WRITE_DECIMAL            Writes a decimal number on the screen    ;
  12. ; WRITE_PATTERN            Writes a pattern on the screen        ;
  13. ; WRITE_CHAR_N_TIMES        Writes N copies of one character    ;
  14. ; WRITE_ATTRIBUTE_N_TIMES    Writes just the attribute to the screen    ;
  15. ;-----------------------------------------------------------------------;
  16.  
  17. .DATA
  18.     PUBLIC    SCREEN_PTR
  19.     PUBLIC    SCREEN_X, SCREEN_Y
  20. SCREEN_SEG    DW    0B800h        ;Segment of the screen buffer
  21. SCREEN_PTR    DW    0        ;Offset into screen memory of cursor
  22. SCREEN_X    DB    0        ;Position of the screen cursor
  23. SCREEN_Y    DB    0
  24.  
  25.  
  26. .CODE
  27.  
  28.     PUBLIC    WRITE_STRING
  29. ;-----------------------------------------------------------------------;
  30. ; This procedure writes a string of characters to the screen.  The    ;
  31. ; string must end with        DB    0                ;
  32. ;                                    ;
  33. ; On entry:    DS:DX    Address of the string                ;
  34. ;                                    ;
  35. ; Uses:        WRITE_CHAR                        ;
  36. ;-----------------------------------------------------------------------;
  37. WRITE_STRING    PROC
  38.     PUSH    AX
  39.     PUSH    DX
  40.     PUSH    SI
  41.     PUSHF                ;Save direction flag
  42.     CLD                ;Set direction for increment (forward)
  43.     MOV    SI,DX            ;Place address into SI for LODSB
  44. STRING_LOOP:
  45.     LODSB                ;Get a character into the AL register
  46.     OR    AL,AL            ;Have we found the 0 yet?
  47.     JZ    END_OF_STRING        ;Yes, we are done with the string
  48.     MOV    DL,AL            ;No, write character
  49.     CALL    WRITE_CHAR
  50.     JMP    STRING_LOOP
  51. END_OF_STRING:
  52.     POPF                ;Restore direction flag
  53.     POP    SI
  54.     POP    DX
  55.     POP    AX
  56.     RET
  57. WRITE_STRING    ENDP
  58.  
  59.  
  60.     PUBLIC    WRITE_HEX
  61. ;-----------------------------------------------------------------------;
  62. ; This procedure converts the byte in the DL register to hex and writes    ;
  63. ; the two hex digits at the current cursor position.            ;
  64. ;                                    ;
  65. ; On entry:    DL    Byte to convert to hex.                ;
  66. ;                                    ;
  67. ; Uses:        WRITE_HEX_DIGIT                        ;
  68. ;-----------------------------------------------------------------------;
  69. WRITE_HEX    PROC            ;Entry point
  70.     PUSH    CX            ;Save registers used in this procedure
  71.     PUSH    DX
  72.     MOV    DH,DL            ;Make a copy of byte
  73.     MOV    CX,4            ;Get the upper nibble in DL
  74.     SHR    DL,CL
  75.     CALL    WRITE_HEX_DIGIT        ;Display first hex digit
  76.     MOV    DL,DH            ;Get lower nibble into DL
  77.     AND    DL,0Fh            ;Remove the upper nibble
  78.     CALL    WRITE_HEX_DIGIT        ;Display second hex digit
  79.     POP    DX
  80.     POP    CX
  81.     RET
  82. WRITE_HEX    ENDP
  83.  
  84.     PUBLIC    WRITE_HEX_DIGIT
  85. ;-----------------------------------------------------------------------;
  86. ; This procedure converts the lower 4 bits of DL to a hex digit and    ;
  87. ; writes it to the screen.                        ;
  88. ;                                    ;
  89. ; On entry:    DL    Lower 4 bits contain number to be printed    ;
  90. ;            in hex.                        ;
  91. ;                                    ;
  92. ; Uses:        WRITE_CHAR                        ;
  93. ;-----------------------------------------------------------------------;
  94. WRITE_HEX_DIGIT        PROC
  95.     PUSH    DX            ;Save registers used
  96.     CMP    DL,10            ;Is this nibble <10?
  97.     JAE    HEX_LETTER        ;No, convert to a letter
  98.     ADD    DL,"0"            ;Yes, convert to a digit
  99.     JMP    Short WRITE_DIGIT    ;Now write this character
  100. HEX_LETTER:
  101.     ADD    DL,"A"-10        ;Convert to hex letter
  102. WRITE_DIGIT:
  103.     CALL    WRITE_CHAR        ;Display the letter on the screen
  104.     POP    DX            ;Restore old value of DX
  105.     RET
  106. WRITE_HEX_DIGIT        ENDP
  107.  
  108.     PUBLIC    INIT_WRITE_CHAR
  109. ;-----------------------------------------------------------------------;
  110. ; You need to call this procedure before you call WRITE_CHAR since    ;
  111. ; WRITE_CHAR uses information set by this procedure.            ;
  112. ;                                    ;
  113. ; Writes:    SCREEN_SEG                        ;
  114. ;-----------------------------------------------------------------------;
  115. INIT_WRITE_CHAR        PROC
  116.     PUSH    AX
  117.     PUSH    BX
  118.     MOV    BX,0B800h        ;Set for color graphics display
  119.     INT    11h            ;Get equipment information
  120.     AND    AL,30h            ;Keep just the video display type
  121.     CMP    AL,30h            ;Is this a monochrome display adapter?
  122.     JNE    SET_BASE        ;No, it's color, so use B800
  123.     MOV    BX,0B800h        ;Yes, it's monochrome, so use B000
  124. SET_BASE:
  125.     MOV    SCREEN_SEG,BX        ;Save the screen segment
  126.     POP    BX
  127.     POP    AX
  128.     RET
  129. INIT_WRITE_CHAR        ENDP
  130.  
  131.     PUBLIC    WRITE_CHAR
  132.     EXTRN    CURSOR_RIGHT:PROC
  133. ;-----------------------------------------------------------------------;
  134. ; This procedure outputs a character to the screen by writing directly    ;
  135. ; into screen memory, so that characters such as the backspace are    ;
  136. ; treated as any other characters and are displayed.            ;
  137. ;                                    ;
  138. ; This procedure must do a bit of work to update the cursor position.    ;
  139. ;                                    ;
  140. ; On entry:    DL    Byte to print on screen.            ;
  141. ;                                    ;
  142. ; Uses:        CURSOR_RIGHT                        ;
  143. ; Reads:    SCREEN_SEG, SCREEN_PTR                    ;
  144. ;-----------------------------------------------------------------------;
  145. WRITE_CHAR    PROC
  146.     PUSH    AX
  147.     PUSH    BX
  148.     PUSH    DX
  149.     PUSH    ES
  150.  
  151.     MOV    AX,SCREEN_SEG        ;Get segment for screen memory
  152.     MOV    ES,AX            ;Point ES to screen memory
  153.     MOV    BX,SCREEN_PTR        ;Pointer to character in screen memory
  154.  
  155.     MOV    DH,7            ;Use the normal attribute
  156.     MOV    ES:[BX],DX        ;Write character/attribute to screen
  157.     CALL    CURSOR_RIGHT        ;Now move to next cursor position
  158.  
  159.     POP    ES
  160.     POP    DX
  161.     POP    BX
  162.     POP    AX
  163.     RET
  164. WRITE_CHAR    ENDP
  165.  
  166.     PUBLIC    WRITE_DECIMAL
  167. ;-----------------------------------------------------------------------;
  168. ; This procedure writes a 16-bit, unsigned number in decimal notation.    ;
  169. ;                                    ;
  170. ; On entry:    DX    N : 16-bit, unsigned number.            ;
  171. ;                                    ;
  172. ; Uses:        WRITE_HEX_DIGIT                        ;
  173. ;-----------------------------------------------------------------------;
  174. WRITE_DECIMAL    PROC
  175.     PUSH    AX            ;Save registers used here
  176.     PUSH    CX
  177.     PUSH    DX
  178.     PUSH    SI
  179.     MOV    AX,DX
  180.     MOV    SI,10            ;Will divide by 10 using SI
  181.     XOR    CX,CX            ;Count of digits placed on stack
  182. NON_ZERO:
  183.     XOR    DX,DX            ;Set upper word of N to 0
  184.     DIV    SI            ;Calculate N/10 and (N mod 10)
  185.     PUSH    DX            ;Push one digit onto the stack
  186.     INC    CX            ;One more digit added
  187.     OR    AX,AX            ;N = 0 yet?
  188.     JNE    NON_ZERO        ;Nope, continue
  189. WRITE_DIGIT_LOOP:
  190.     POP    DX            ;Get the digits in reverse order
  191.     CALL    WRITE_HEX_DIGIT
  192.     LOOP    WRITE_DIGIT_LOOP
  193. END_DECIMAL:
  194.     POP    SI
  195.     POP    DX
  196.     POP    CX
  197.     POP    AX
  198.     RET
  199. WRITE_DECIMAL    ENDP
  200.  
  201.     PUBLIC    WRITE_CHAR_N_TIMES
  202. ;-----------------------------------------------------------------------;
  203. ; This procedure writes more than one copy of a character        ;
  204. ;                                    ;
  205. ; On entry:    DL    Character code                    ;
  206. ;        CX    Number of times to write the character        ;
  207. ;                                    ;
  208. ; Uses:        WRITE_CHAR                        ;
  209. ;-----------------------------------------------------------------------;
  210. WRITE_CHAR_N_TIMES    PROC
  211.     PUSH    CX
  212. N_TIMES:
  213.     CALL    WRITE_CHAR
  214.     LOOP    N_TIMES
  215.     POP    CX
  216.     RET
  217. WRITE_CHAR_N_TIMES    ENDP
  218.  
  219.  
  220.     PUBLIC    WRITE_ATTRIBUTE_N_TIMES
  221.     EXTRN    CURSOR_RIGHT:PROC
  222. ;-----------------------------------------------------------------------;
  223. ; This procedure sets the attribute for N characters, starting at the    ;
  224. ; current cursor position.                        ;
  225. ;                                    ;
  226. ;    CX    Number of characters to set attribute for        ;
  227. ;    DL    New attribute for characters                ;
  228. ;                                    ;
  229. ; Uses:        CURSOR_RIGHT                        ;
  230. ; Reads:    SCREEN_SEG, SCREEN_PTR                    ;
  231. ;-----------------------------------------------------------------------;
  232. WRITE_ATTRIBUTE_N_TIMES        PROC
  233.     PUSH    AX
  234.     PUSH    CX
  235.     PUSH    DI
  236.     PUSH    ES
  237.  
  238.     MOV    AX,SCREEN_SEG        ;Set ES to point to screen segment
  239.     MOV    ES,AX
  240.     MOV    DI,SCREEN_PTR        ;Character under cursor
  241.     INC    DI            ;Point to the attribute under cursor
  242.     MOV    AL,DL            ;Put attribute into AL
  243. ATTR_LOOP:
  244.     STOSB                ;Save one attribute
  245.     INC    DI            ;Move to next attribute
  246.     INC    SCREEN_X        ;Move to next column
  247.     LOOP    ATTR_LOOP        ;Write N attributes
  248.  
  249.     DEC    DI            ;Point to start of next character
  250.     MOV    SCREEN_PTR,DI        ;Remember where we are
  251.  
  252.     POP    ES
  253.     POP    DI
  254.     POP    CX
  255.     POP    AX
  256.     RET
  257. WRITE_ATTRIBUTE_N_TIMES    ENDP
  258.  
  259.  
  260.     PUBLIC    WRITE_PATTERN
  261. ;-----------------------------------------------------------------------;
  262. ; This procedure writes a line to the screen, based on data in the    ;
  263. ; form                                    ;
  264. ;                                    ;
  265. ;    DB    {character, number of times to write character}, 0    ;
  266. ; Where {x} means that x can be repeated any number of times        ;
  267. ;                                    ;
  268. ; On entry:    DS:DX    Address of the pattern to draw            ;
  269. ;                                    ;
  270. ; Uses:        WRITE_CHAR_N_TIMES                    ;
  271. ;-----------------------------------------------------------------------;
  272. WRITE_PATTERN    PROC
  273.     PUSH    AX
  274.     PUSH    CX
  275.     PUSH    DX
  276.     PUSH    SI
  277.     PUSHF                ;Save the direction flag
  278.     CLD                ;Set direction flag for increment
  279.     MOV    SI,DX            ;Move offset into SI register for LODSB
  280. PATTERN_LOOP:
  281.     LODSB                ;Get character data into AL
  282.     OR    AL,AL            ;Is it the end of data (0h)?
  283.     JZ    END_PATTERN        ;Yes, return
  284.     MOV    DL,AL            ;No, set up to write character N times
  285.     LODSB                ;Get the repeat count into AL
  286.     MOV    CL,AL            ;And put in CX for WRITE_CHAR_N_TIMES
  287.     XOR    CH,CH            ;Zero upper byte of CX
  288.     CALL    WRITE_CHAR_N_TIMES
  289.     JMP    PATTERN_LOOP
  290. END_PATTERN:
  291.     POPF                ;Restore direction flag
  292.     POP    SI
  293.     POP    DX
  294.     POP    CX
  295.     POP    AX
  296.     RET
  297. WRITE_PATTERN    ENDP
  298.  
  299.     END
  300.