; Program to print a message on an IBM PC       *
; Accesses the LPT1 printer ports directly      *

stk        segment     stack
           dw          16 dup (?)
stk        ends

data       segment
num        dw          42
msg        db          'Hello world. I control the printer', 0dh, 0ah
dataport   dw          ?
statport   dw          ?
strobport  dw          ?
data       ends

code       segment
           assume      cs:code,ds:data
main       proc        far
           push        ds             ;bookkeeping
           sub         ax,ax
           push        ax
           mov         ax,data
           mov         ds,ax

           mov         ax,40h         ;get the printer data port address
           mov         es,ax
           mov         bx,8
           mov         si,es:[bx]
           mov         dataport,si    ;save it
           inc         si             ;status port address
           mov         statport,si
           inc         si             ;strobe port address
           mov         strobport,si   

           call        prtmsg         ;print out the msg string

main       endp

prtmsg     proc        near           ;sends characters of msg to printer
           mov         cx,num         ;length of output message
           mov         bx,offset msg  ;point to first character
top:       mov         al,[bx]        ;get next character
           call        prtchr         ;send to printer
           inc         bx             ;point to next character
           loop        top            ;do it again
prtmsg     endp

;**** prtchr--sends the character in al to the printer interface when ready
prtchr     proc        near
           push        dx
           push        ax             ;save character on stack
           mov         dx,statport
wait1:     in          al,dx          ;input status byte--start of polling loop
           test        al,10000000b   ;test ready bit
           jz          wait1          ;if not ready, do it again
           pop         ax             ;restore character
           mov         dx,dataport    ;output to data port
           out         dx,al
           mov         dx,strobport   ;control port
           mov         al,00001101b   ;begin strobe pulse
           out         dx,al
           mov         al,00001100b   ;end strobe pulse
           out         dx,al
           pop         dx
prtchr     endp

code       ends
           end        main