home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_01 / memrite.asm < prev    next >
Assembly Source File  |  1989-10-29  |  6KB  |  269 lines

  1.  
  2.         TITLE   MEMRITE
  3.         PAGE    60,132
  4.  
  5.         ;MEMRITE.ASM
  6.  
  7.     COMMENT     @
  8.  
  9.     This sub-routine writes directly to video ram on the specified
  10.     video page. It first checks for mono text mode, then whether
  11.     a VGA or EGA is currently active. If neither is the case
  12.     it defaults to CGA.
  13.  
  14.  extern void far pascal memrite(
  15.      int atr,int row,int col,int page,int dir,char far *str
  16.      );
  17.  
  18.     Can easily be modified for Turbo Pascal 4.0+ by accomodating
  19.     Turbo Pascal string format.  In fact I debugged this in TP 4.0
  20.     and changed the string handling code for C, the " pascal "
  21.     function modifier saves me from moving stuff around on the stack
  22.     and messing up working code.
  23.  
  24.     Will link with any memory model except tiny
  25.  
  26.     @       Modified    **  10/29/89  **     Michael Kelly
  27.  
  28.  
  29.     MAXSTR          EQU     8192
  30.  
  31.     VMODE           EQU     BYTE PTR ES:[0049h]
  32.     VCOLS           EQU     WORD PTR ES:[004Ah]
  33.     VPAGE_SIZE      EQU     WORD PTR ES:[004Ch]
  34.     VROWS           EQU     BYTE PTR ES:[0084h]
  35.     INFO_BYTE       EQU     BYTE PTR ES:[0087h]
  36.  
  37.     BYTES_PER_ROW   EQU     WORD PTR [BP-2]     ;local variables
  38.     IS_CGA_ADAPT    EQU     WORD PTR [BP-4]
  39.  
  40.     ATR_PARAM       EQU     [BP+18]    ;proc arguments
  41.     ROW_PARAM       EQU     [BP+16]
  42.     COL_PARAM       EQU     [BP+14]
  43.     PAGE_PARAM      EQU     [BP+12]
  44.     DIR_PARAM       EQU     [BP+10]
  45.     STR_PARAM       EQU     [BP+6]
  46.  
  47.     CSEG    SEGMENT BYTE    PUBLIC  'CODE'
  48.         ASSUME  CS:CSEG
  49.         PUBLIC  MEMRITE
  50.     MEMRITE     PROC    FAR
  51.     START:  PUSH    BP
  52.         MOV     BP,SP
  53.         SUB     SP,4
  54.         PUSH    DS
  55.         PUSH    SI
  56.         PUSH    ES
  57.         PUSH    DI
  58.         PUSHF
  59.         CLD
  60.         XOR     CX,CX
  61.         MOV     IS_CGA_ADAPT,0        ;check the video hardware
  62.         MOV     DX,0B000h
  63.         XOR     DI,DI                 ;assume mono video segment
  64.         MOV     AX,40h                ;and memory ofset of 0 for now.
  65.         MOV     ES,AX
  66.         MOV     AL,VMODE
  67.         CMP     AL,7                  ;check video mode
  68.         JE      MRITE_02
  69.         MOV     DX,0B800h
  70.         CMP     AL,3
  71.         JBE     MRITE_01
  72.         JMP     EXIT
  73.  
  74.     MRITE_01:
  75.         MOV     AX,1A00h
  76.         INT     10h
  77.         CMP     AL,1Ah
  78.         JNE     MRITE_011
  79.         CMP     BL,2
  80.         JNE     MRITE_02
  81.         MOV     IS_CGA_ADAPT,1
  82.         JMP     SHORT MRITE_02
  83.     MRITE_011:
  84.         MOV     AH,12h
  85.         MOV     BL,10h
  86.         INT     10h
  87.         CMP     BL,10h
  88.         JNE     MRITE_012
  89.         MOV     IS_CGA_ADAPT,1
  90.         JMP     SHORT MRITE_02
  91.     MRITE_012:
  92.         MOV     AX,40h
  93.         MOV     ES,AX
  94.         TEST    INFO_BYTE,8
  95.         JZ      MRITE_02
  96.         MOV     IS_CGA_ADAPT,1
  97.     MRITE_02:   MOV     AX,40h
  98.         MOV     ES,AX
  99.         MOV     AX,VCOLS
  100.         SHL     AX,1
  101.         MOV     BYTES_PER_ROW,AX
  102.  
  103.         MOV     BX,PAGE_PARAM       ;Calc offset of video page
  104.         CMP     BX,0                ;using shifts and adds.
  105.         JE      MRITE_03
  106.         MOV     AX,VPAGE_SIZE
  107.         OR      BX,BX
  108.         JZ      MRITE_03
  109.         CMP     BX,7
  110.         JBE     MRITE_021
  111.         JMP     EXIT                ;Assume Max page # is 7.
  112.     MRITE_021:  CMP     BX,1
  113.         JA      MRITE_022
  114.         MOV     DI,AX
  115.         JMP     SHORT MRITE_03
  116.     MRITE_022:  CMP     BX,2
  117.         JA      MRITE_023
  118.         SHL     AX,1
  119.         MOV     DI,AX
  120.         JMP     SHORT MRITE_03
  121.     MRITE_023:  CMP     BX,3
  122.         JA      MRITE_024
  123.         MOV     DI,AX
  124.         SHL     AX,1
  125.         ADD     DI,AX
  126.         JMP     SHORT MRITE_03
  127.     MRITE_024:  CMP     BX,4
  128.         JA      MRITE_025
  129.         SHL     AX,1
  130.         SHL     AX,1
  131.         MOV     DI,AX
  132.         JMP     SHORT MRITE_03
  133.     MRITE_025:  CMP     BX,5
  134.         JA      MRITE_026
  135.         MOV     DI,AX
  136.         SHL     AX,1
  137.         SHL     AX,1
  138.         ADD     DI,AX
  139.         JMP     SHORT MRITE_03
  140.     MRITE_026:  CMP     BX,6
  141.         JA      MRITE_027
  142.         SHL     AX,1
  143.         MOV     DI,AX
  144.         SHL     AX,1
  145.         ADD     DI,AX
  146.         JMP     SHORT MRITE_03
  147.     MRITE_027:  PUSH    AX
  148.         SHL     AX,1
  149.         MOV     DI,AX
  150.         SHL     AX,1
  151.         ADD     DI,AX
  152.         POP     AX
  153.         ADD     DI,AX
  154.  
  155.     MRITE_03:
  156.         MOV     AX,ROW_PARAM    ;get screen row and sub 1
  157.         DEC     AX
  158.         JZ      MRITE_04
  159.         MOV     BX,BYTES_PER_ROW
  160.         MUL     BL                  ;then mul to get memory offset.
  161.  
  162.     MRITE_04:
  163.         MOV     BX,COL_PARAM        ;throw in video column calc.
  164.         XOR     BH,BH
  165.         DEC     BX
  166.         SHL     BX,1
  167.  
  168.  
  169.         ADD     AX,BX
  170.         ADD     DI,AX           ;sum page row and col to get
  171.         MOV     ES,DX           ;right place in screen memory.
  172.         MOV     AX,ATR_PARAM
  173.         MOV     AH,AL
  174.         LDS     SI,STR_PARAM
  175.         MOV     CX,MAXSTR
  176.  
  177.     MRITE_05:
  178.         CMP     IS_CGA_ADAPT,0  ;now we jump to routines depending
  179.         JNE     MRITE_09        ;on screen writing direction
  180.                     ;and wether we must hassle with a
  181.                     ;Cga video adapter to avoid the
  182.                     ;abominable snow man.
  183.  
  184.         OR      BYTE PTR DIR_PARAM,0
  185.         JNZ     MRITE_07
  186.  
  187.     MRITE_06:   LODSB               ;horizontal write until
  188.         OR      AL,AL       ;'\0' char found.
  189.         JZ      EXIT
  190.         STOSW
  191.         LOOP    MRITE_06
  192.         JMP     EXIT
  193.  
  194.  
  195.     MRITE_07:   LODSB               ;vertical write until
  196.         OR      AL,AL       ;'\0' char found.
  197.         JZ      EXIT
  198.         MOV     WORD PTR ES:[DI],AX
  199.         ADD     DI,BYTES_PER_ROW
  200.         LOOP    MRITE_07
  201.         JMP     EXIT
  202.  
  203.                         ;same thing but with
  204.                         ;Cga sync hassle.
  205.  
  206.     MRITE_09:   MOV     DX,03DAh                ;Crt status reg
  207.         OR      BYTE PTR DIR_PARAM,0    ;to detect retrace sync.
  208.         JNZ     MRITE_13
  209.  
  210.     MRITE_10:   LODSB
  211.         OR      AL,AL
  212.         JZ      EXIT        ;done if we've found '\0' char.
  213.         MOV     BX,AX
  214.         CLI                 ;turn off interrupts and use two
  215.     MRITE_11:   IN      AL,DX       ;loop to snag start of horiz video sync.
  216.         AND     AL,1
  217.         JNZ     MRITE_11
  218.  
  219.     MRITE_12:   IN      AL,DX
  220.         AND     AL,1        ;if bit 0 = 1, we're in horiz sync.
  221.         JZ      MRITE_12
  222.  
  223.         XCHG    AX,BX       ;get char/attr pair and stuff in memory
  224.         STOSW               ;during horiz sync.
  225.         STI
  226.         LOOP    MRITE_10    ;repeat until '\0' char found.
  227.         JMP     EXIT
  228.  
  229.     MRITE_13:   ADD     DI,2        ;same as above but writes vertcally
  230.     MRITE_14:   DEC     DI          ;down the screen.
  231.         DEC     DI
  232.         LODSB
  233.         OR      AL,AL
  234.         JZ      EXIT
  235.     MRITE_15:   MOV     BX,AX
  236.         CLI
  237.     MRITE_16:   IN      AL,DX
  238.         AND     AL,1
  239.         JNZ     MRITE_16
  240.  
  241.     MRITE_17:   IN      AL,DX
  242.         AND     AL,1
  243.         JZ      MRITE_17
  244.         XCHG    AX,BX
  245.         STOSW
  246.         STI
  247.         ADD     DI,BYTES_PER_ROW
  248.         LOOP    MRITE_14
  249.  
  250.  
  251.     EXIT:
  252.         POPF              ;restore flags & regs
  253.         POP     DI
  254.         POP     ES
  255.         POP     SI
  256.         POP     DS
  257.         MOV     SP,BP
  258.         POP     BP
  259.         RET     14              ;discard params & return
  260.     MEMRITE     ENDP
  261.  
  262.     CSEG    ENDS
  263.  
  264.         END     START
  265.  
  266.  
  267.  
  268.  
  269.