          ; from ASSEMBLY LANGUAGE PRIMER FOR THE IBM PC & XT
          ; by Robert LaFore
          ;    modified to be compilable by CHASM  (disk # 37)
          ;GUN--makes a machine gun sound
          ;   fires fixed number of shots
          ;------------------------------------------
main      proc      far       ;main part of program
          mov       cx,20     ;set # of shots
new_shot
          push      cx        ;save count
          call      shoot     ;sound of shot
          mov       cx,4000h  ;set up silent delay
silent    loop      silent    ;silent delay
          pop       cx        ;get back shots count
          loop      new_shot  ;loop til done
          int       20h       ;back to DOS
          endp
          ;------------------------------------------
          ;subroutine to make brief noise
          ;------------------------------------------
shoot     proc      near      ;
          mov       dx,140h   ;initial value of wait
          mov       bx,20h    ;set count
          in        al,61h    ;get port 61
          and       al,0FCh   ;"and off" bits 0 and 1
sound     xor       al,2      ;toggle bit #1 in al
          out       61h,al    ;output to port 61
          add       dx,9248h  ;add random pattern
          mov       cl,3      ;get set to rotate 3 bits
          ror       dx,cl     ;rotate it
          mov       cx,dx     ;put it in cx
          and       cx,1FFh   ;mask off upper 7 bits
          or        cx,10     ;ensure not too short
wait      loop      wait      ;time delay
          dec       bx        ;done enough?
          jnz       sound     ;if not, back to sound
          and       al,0FCh   ;and off bits 0 and 1
          out       61h,al    ;turn off bits o and 1
          ret                 ;return from subroutine
          endp
          ;------------------------------------------