home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / asm / wasm / dispatch.asm < prev    next >
Assembly Source File  |  1987-03-27  |  7KB  |  275 lines

  1.  List-
  2.  
  3. ;================================================
  4. ; The external dispatcher code. This file contains
  5. ; the code to allow a redirection table to interface
  6. ; with an external runtime library. Include this
  7. ; file in the application program along with the
  8. ; redirection tables (like IO.RED).
  9.  
  10. ;================================================
  11. ; Macro to set up the dispatch information and
  12. ; transfer control to the dispatcher. The
  13. ; redirection routines should push BX, load their
  14. ; own dispatch index to BL, and jump to this
  15. ; code. One of these macros is needed for each
  16. ; external library. This code assumes that it's
  17. ; in the same segment as the dispatch code below.
  18. ;
  19. ; LIB_NAME - file name of the library file: string.
  20.  
  21. Dispatch Macro Lib_Name
  22.  Push Si
  23.  Mov Si, Offset Dat     ;set control block offset
  24.  Jmp Library_Dispatcher ;branch to dispatch code
  25.  
  26. ;--- control block for this library
  27.  
  28. Dat Label Word
  29.  Dw 0                   ;initial status
  30.  Dw ?                   ;code offset (initialized when loaded)
  31.  Dw ?                   ;code segment (initialized when loaded)
  32.  Dw Offset Nam          ;location of library name
  33.  
  34. Nam Db Lib_Name, 0      ;name of library file
  35.  Endm
  36.  
  37. ;================================================
  38. ; Code to transfer request to library interface.
  39. ; The registers BX, SI, and DS should set the to
  40. ; the following: BL= dispatch index, SI= offset
  41. ; control block. The original values should be
  42. ; pushed on the stack. This code should be
  43. ; branched to, not called.
  44. ;
  45. ; On entry, the stack should look like the
  46. ; following:
  47. ;
  48. ;   |   SI   | - top of stack (TOS)
  49. ;   |   BX   |
  50. ;   |<return>| - original return address
  51. ;   |   ::   |   put there by first call
  52. ;
  53. ; If there is an error, the error code is displayed
  54. ; and also returned in the ERRORLEVEL variable. The
  55. ; error codes are:
  56. ;
  57. ;   01  could not allocate memory
  58. ;   02  could not open library file
  59. ;   03  incompatible library version
  60. ;   04  error in library file
  61. ;   05  unknown error
  62.  
  63. Library_Dispatcher
  64.  
  65. ;--- test if ready, and if so transfer to library (library does return)
  66.  
  67.  Cs:
  68.  Test Word [Si], 1      ;test if ready bit set
  69.  Jz DispatchNotReady    ;jump if not
  70.  
  71.  Push Cs                ;segment to return to
  72.  Cs:
  73.  Jmp Dword [Si+2]       ;transfer to library
  74.  
  75. ;--- ready bit not set, try to load
  76.  
  77. DispatchNotReady
  78.  Push Ax
  79.  
  80.  Mov Al, 5              ;error number, in case file has been read
  81.  Cs:
  82.  Test Word [Si], 10b    ;test if file has been read
  83.  Jnz DispatchCritErr    ;jump if so, unknown error
  84.  Call Load_Library      ;try to load library
  85.  Jc DispatchCritErr     ;jump if unsuccessful
  86.  
  87.  Pop Ax
  88.  Jmp Library_Dispatcher ;go back to the start otherwise
  89.  
  90. ;--- some kind of error, could not load library, error code should be in AL
  91.  
  92. DispatchCritErr
  93.  Push Ax
  94.  Push Cs
  95.  Pop Ds                 ;set data segment
  96.  
  97.  Sub Ah, Ah             ;error number is in AX
  98.  Mov Bl, 10             ;number base
  99.  Div Al, Bl             ;convert binary to decimal
  100.  Mov Bx, Offset DispatchCritMess ;message location
  101.  Add [Bx+14], Al        ;set tens
  102.  Add [Bx+15], Ah        ;set ones
  103.  
  104.  Mov Ah, 9              ;display function
  105.  Mov Dx, Bx             ;message
  106.  Int 21h                ;execute
  107.  
  108.  Pop Ax                 ;restore error number
  109.  Mov Ah, 4ch            ;exit function
  110.  Int 21h                ;execute
  111.  
  112. ;--- data
  113.  
  114. Dispatch_Mark Equ 128  ;library marker
  115. Dispatch_Ver  Db 1, 0  ;library version (1.00)
  116.  
  117. DispatchCritMess Db 'Runtime error 00$' ;error message
  118.  
  119. ;================================================
  120. ; Given the data block in DS:SI, an attempt is made
  121. ; to load the external library. If successful, the
  122. ; carry will be cleared, otherwise the carry will
  123. ; be set and AL will return the error code.
  124.  
  125. Load_Library Proc Near
  126.  Push Bx
  127.  Push Cx
  128.  Push Dx
  129.  Push Di
  130.  Push Ds
  131.  
  132.  Push Cs
  133.  Pop Ds                 ;set data segment to control block
  134.  
  135. ;--- open file
  136.  
  137.  Mov Ax, 3d00h          ;open file
  138.  Mov Dx, [Si+6]         ;location of file name
  139.  Int 21h                ;execute
  140.  Jnc LoadLibraryCont0   ;jump if ok
  141.  
  142.  Mov Al, 2              ;error code
  143.  Jmp LoadLibraryErr0
  144.  
  145. ;--- find size of file
  146.  
  147. LoadLibraryCont0
  148.  Mov Bx, Ax
  149.  Mov Ax, 4202h          ;move to end of file
  150.  Sub Cx, Cx
  151.  Mov Dx, Cx             ;offset 0000.0000 from end
  152.  Int 21h                ;execute
  153.  Or Dx, Dx
  154.  Jz LoadLibraryCont1    ;jump if fewer than 10000H bytes, ok
  155.  
  156.  Mov Al, 4              ;error code
  157.  Jmp LoadLibraryErr0
  158.  
  159. LoadLibraryCont1
  160.  Or Ax, Ax
  161.  Jnz LoadLibraryCont2   ;jump if more than zero bytes, ok
  162.  
  163.  Mov Al, 4              ;error code
  164.  Jmp LoadLibraryErr0
  165.  
  166. ;--- allocate memory
  167.  
  168. LoadLibraryCont2
  169.  Mov Cl, 4
  170.  Shr Ax, Cl             ;make paragraph form
  171.  Inc Ax                 ;account for partial paragraph
  172.  
  173.  Push Bx
  174.  Mov Bx, Ax
  175.  Mov Ah, 48h            ;allocate function
  176.  Int 21h                ;execute
  177.  Pop Bx
  178.  Jnc LoadLibraryCont3   ;jump if ok
  179.  
  180.  Mov Al, 1              ;error code
  181.  Jmp LoadLibraryErr0
  182.  
  183. ;--- save segment in library module and prepare for read
  184.  
  185. LoadLibraryCont3
  186.  Mov [Si+4], Ax         ;save segment in control block
  187.  
  188.  Push Ax
  189.  Pop Ds                 ;set data segment for read
  190.  
  191. ;--- move read pointer back to start of file
  192.  
  193.  Mov Ax, 4200h          ;function
  194.  Sub Cx, Cx
  195.  Mov Dx, Cx             ;offset 0000.0000
  196.  Int 21h                ;execute
  197.  
  198. ;--- read file into newly allocated memory
  199.  
  200.  Mov Ah, 3fh            ;function number
  201.  Mov Cx, 0ffffh         ;read all bytes (size was checked earlier)
  202.  Sub Dx, Dx             ;start of segment
  203.  Int 21h                ;execute
  204.  
  205. ;--- check checksum (total checksum of library should be zero)
  206.  
  207.  Mov Cx, Ax             ;set count (number of bytes read)
  208.  Sub Al, Al             ;initial sum
  209.  Sub Di, Di             ;start of segment
  210.  
  211. LoadLibraryLoop
  212.  Add Al, [Di]           ;add next byte
  213.  Inc Di                 ;advance pointer
  214.  Loop LoadLibraryLoop   ;loop back if more
  215.  
  216.  Or Al, Al              ;check if zero
  217.  Jz LoadLibraryCont4    ;jump if so, ok
  218.  
  219.  Mov Al, 4              ;error code
  220.  Jmp LoadLibraryErr0
  221.  
  222. ;--- library marker
  223.  
  224. LoadLibraryCont4
  225.  Cmp Byte [0], Dispatch_Mark  ;check library marker
  226.  Je LoadLibraryCont5    ;jump if ok
  227.  
  228.  Mov Al, 4              ;error code
  229.  Jmp LoadLibraryErr0
  230.  
  231. ;--- version number
  232.  
  233. LoadLibraryCont5
  234.  Cs:
  235.  Mov Ax, Word Dispatch_Ver ;get highest compatible version
  236.  Mov Dx, [1]            ;get library version
  237.  Xchg Al, Ah
  238.  Xchg Dl, Dh            ;make most significant byte high
  239.  Cmp Ax, Dx             ;compare version numbers
  240.  Jae LoadLibraryCont6   ;jump if loading lower or equal version, ok
  241.  
  242.  Mov Al, 3              ;error code
  243.  Jmp LoadLibraryErr0
  244.  
  245. ;--- loaded successfully
  246.  
  247. LoadLibraryCont6
  248.  Mov Ax, [3]            ;get starting offset
  249.  
  250.  Cs:
  251.  Mov [Si+2], Ax         ;save offset
  252.  Cs:
  253.  Mov Word [Si], 11b     ;set loaded and ready bits
  254.  
  255.  Pop Ds
  256.  Pop Di
  257.  Pop Dx
  258.  Pop Cx
  259.  Pop Bx                 ;restore regs
  260.  Clc
  261.  Ret
  262.  
  263. ;--- did not load correctly, error code in AL
  264.  
  265. LoadLibraryErr0
  266.  Pop Ds
  267.  Pop Di
  268.  Pop Dx
  269.  Pop Cx
  270.  Pop Bx                 ;restore regs
  271.  Stc
  272.  Ret
  273.  Endp
  274.  
  275.