home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / advos2 / ch12 / beeper.asm next >
Encoding:
Assembly Source File  |  1988-12-12  |  5.1 KB  |  137 lines

  1.         title   BEEPER -- simple multithreaded application
  2.         page    55,132
  3.         .286
  4.  
  5. ; BEEPER.ASM  --- A simple multithreaded application which
  6. ;                 creates a second thread to issue beeps
  7. ;                 at one-second intervals, while the first
  8. ;                 thread waits for a any keypress.
  9. ; Copyright (C) 1987 Ray Duncan 
  10. ;
  11. ; Build:        MASM BEEPER;
  12. ;               LINK BEEPER,,,OS2,BEEPER;
  13. ;
  14. ; Usage:        BEEPER
  15.  
  16. stdin   equ     0                       ; standard input handle
  17. stdout  equ     1                       ; standard output handle
  18. stderr  equ     2                       ; standard error handle
  19.  
  20. cr      equ     0dh                     ; ASCII carriage return
  21. lf      equ     0ah                     ; ASCII line feed
  22.  
  23. stksiz  equ     4096                    ; stack size for 2nd thread
  24.  
  25.         extrn   DosAllocSeg:far         ; references to OS/2 API
  26.         extrn   DosBeep:far
  27.         extrn   DosCreateThread:far
  28.         extrn   DosExecPgm:far
  29.         extrn   DosExit:far
  30.         extrn   DosSleep:far
  31.         extrn   DosWrite:far
  32.         extrn   KbdCharIn:far
  33.  
  34. DGROUP  group   _DATA
  35.  
  36. _DATA   segment word public 'DATA'
  37.  
  38. cdata   db      10 dup (0)              ; receives character data
  39. wlen    dw      ?                       ; receives bytes written        
  40. selector dw     ?                       ; receives segment selector
  41. beepID  dw      ?                       ; receives thread ID
  42.  
  43. msg1    db      cr,lf
  44.         db      'Press any key to end program...'
  45. msg1_len equ $-msg1
  46.  
  47. msg2    db      cr,lf
  48.         db      'Unexpected OS/2 Error'
  49.         db      cr,lf
  50. msg2_len equ $-msg2
  51.  
  52. _DATA   ends
  53.  
  54.  
  55. _TEXT   segment word public 'CODE'
  56.  
  57.         assume  cs:_TEXT,ds:DGROUP
  58.  
  59. main    proc    far                     ; entry point for primary thread
  60.  
  61.                                         ; allocate stack for 2nd thread...
  62.         push    stksiz                  ; size of new segment
  63.         push    ds                      ; receives selector
  64.         push    offset DGROUP:selector
  65.         push    0                       ; 0=not sharable
  66.         call    DosAllocSeg             ; transfer to OS/2
  67.         or      ax,ax                   ; allocation successful?
  68.         jnz     error                   ; jump if failed
  69.         
  70.                                         ; start new beeper thread...
  71.         push    cs                      ; thread entry point
  72.         push    offset _TEXT:Beeper
  73.         push    ds                      ; receives Thread ID
  74.         push    offset DGROUP:beepID
  75.         push    selector                ; stack for new thread
  76.         push    stksiz
  77.         call    DosCreateThread         ; transfer to OS/2
  78.         or      ax,ax                   ; new thread created?
  79.         jnz     error                   ; jump if failed
  80.  
  81.                                         ; display message,
  82.                                         ; 'Press any key to end program'...
  83.         push    stdout                  ; standard output handle
  84.         push    ds                      ; address of message
  85.         push    offset msg1
  86.         push    msg1_len                ; length of message
  87.         push    ds                      ; receives bytes written 
  88.         push    offset wlen
  89.         call    DosWrite                ; transfer to OS/2
  90.  
  91.                                         ; now wait for key...
  92.         push    ds                      ; receives data packet
  93.         push    offset DGROUP:cdata
  94.         push    0                       ; 0=wait for char.
  95.         push    0                       ; keyboard handle
  96.         call    KbdCharIn               ; transfer to OS/2
  97.  
  98.                                         ; final exit to OS/2...
  99.         push    1                       ; terminate all threads
  100.         push    0                       ; exit code=0 (success)
  101.         call    DosExit                 ; transfer to OS/2
  102.  
  103. error:                                  ; display error message...
  104.         push    stdout                  ; standard output handle
  105.         push    ds                      ; message address
  106.         push    offset msg1
  107.         push    msg1_len                ; message length
  108.         push    ds                      ; receives bytes written
  109.         push    offset wlen
  110.         call    DosWrite                ; transfer to OS/2
  111.  
  112.         push    1                       ; terminate all threads
  113.         push    1                       ; return error code
  114.         call    DosExit                 ; exit program
  115.  
  116. main    endp
  117.  
  118.  
  119. beeper  proc    far                     ; thread entry point
  120.  
  121.                                         ; sound a tone...
  122.         push    440                     ; 440 Hz
  123.         push    100                     ; 100 milliseconds
  124.         call    DosBeep                 ; transfer to OS/2
  125.  
  126.         push    0                       ; now suspend thread
  127.         push    1000                    ; for 1 second...
  128.         call    DosSleep                ; transfer to OS/2
  129.  
  130.         jmp     beeper                  ; do it again...
  131.  
  132. beeper  endp
  133.  
  134. _TEXT   ends
  135.  
  136.         end     main
  137.