home *** CD-ROM | disk | FTP | other *** search
/ D!Zone (Collector's Edition) / D_ZONE_CD.ISO / programs / editors / dhtk100 / swapa.asm < prev    next >
Assembly Source File  |  1994-12-06  |  27KB  |  711 lines

  1. ;**************************************************************************;
  2. ;*                           S W A P A . A S M                            *;
  3. ;*------------------------------------------------------------------------*;
  4. ;*    Task           : Assembler module for the SWAP unit                 *;
  5. ;*------------------------------------------------------------------------*;
  6. ;*    Author         : MICHAEL TISCHER                                    *;
  7. ;*    developed on   :  06/01/1989                                        *;
  8. ;*    last update    :  02/28/1990                                        *;
  9. ;*------------------------------------------------------------------------*;
  10. ;*    assembly       : TASM - /MX SWAPA                              *;
  11. ;*                     ... bind to the SWAP unit                          *;
  12. ;**************************************************************************;
  13.  
  14. ;== Constants ===============================================================
  15.  
  16. STACK_LEN  equ 64                 ;Number of words in the internal stack
  17.  
  18. ;== Structures =============================================================
  19.  
  20. ExecStruc struc                   ;Data structure for EXEC function
  21. EsSegEnv  dw ?                    ;Segment address of environment blocks
  22. EsCmdPAdr dd ?                    ;Pointer to command line parameters
  23. EsFCB1Adr dd ?                    ;Pointer to FCB #1
  24. EsFCB2Adr dd ?                    ;Pointer to FCB #2
  25.  
  26. ExecStruc ends
  27.  
  28. public     SwapOutAndExec         ;Gives a Turbo Pascal program the
  29.                                              ;ability to pass the address of
  30.                                              ;the assembler handler
  31. public     InitSwapa              ;Initialization procedure
  32.  
  33. ;== Data segment ===========================================================
  34.  
  35. DATA   segment word public
  36.  
  37. extrn  PrefixSeg : word           ;Segment address of PSP in Turbo variables
  38.  
  39. DATA   ends
  40.  
  41. ;== Program ===============================================================
  42.  
  43. CODE       segment byte public    ;Program segment
  44.  
  45.  
  46. ;== Variables in code segment ===============================================
  47.  
  48. CodeStart  equ this word          ;Code begins here which is copied to
  49.                                              ;the Turbo program
  50.  
  51. ;-- Variables needed by the Swap routines for uploading and downloading ----
  52.  
  53. CoStAddr   dd ?                   ;Orig. address of PARA(CodeStart)
  54. CoStLen    dw ?                   ;Number of words swapped w/ CoStAddr
  55. StackPtr   dw ?                   ;Gets old stack pointer
  56. StackSeg   dw ?                   ;Gets old stack segment
  57. TurboSeg   dw ?                   ;Segment address - Turbo code segment
  58.  
  59. ;-- Variables needed for program configuration and command execution -------
  60.  
  61. NewStack   dw STACK_LEN dup (?)   ;New stack
  62. EndStack   equ this word          ;End of stack
  63.  
  64. Command    dd ?                   ;Pointer to command
  65. CmdPara    dd ?                   ;Pointer to command line parameters
  66. ToDisk     db ?                   ;True when disk swapping occurs
  67. Handle     dw ?                   ;Disk or EMS handle
  68. Len        dd ?                   ;Number of bytes saved
  69.  
  70. FCB1       db  16 dup ( 0 )       ;FCB #1 for PSP
  71. FCB2       db  16 dup ( 0 )       ;FCB #2 for PSP
  72. CmdBuf     db 128 dup ( 0 )       ;Commands following prg. name
  73. PrgName    db  64 dup ( 0 )       ;Program name
  74. ExecData   ExecStruc < 0, CmdBuf, FCB1, FCB2 >   ;Data structure for EXEC
  75.  
  76. OldPara    dw ?                   ;Number of previously reserved paragraphs
  77. FrameSeg   dw ?                   ;Segment address of EMS page frame
  78. Error_Code db 0                   ;Error code for caller
  79.  
  80. TerMes     db 13,10,13,10
  81.               db "╔═══════════════════════════════════════════════════╗",13,10
  82.               db "║ Done_Swap: Stored Turbo Pascal program could      ║",13,10
  83.               db "║            not be reloaded back into memory.      ║",13,10
  84.               db "║            Program execution terminated!          ║",13,10
  85.               db "╚═══════════════════════════════════════════════════╝"
  86.               db 13,10,13,10,"$"
  87.  
  88. Msg1       db 13,10,13,10
  89.               db "╔════════════════════════════╗",13,10
  90.               db "║ Init_Swap: Swapping out    ║",13,10
  91.               db "╚════════════════════════════╝"
  92.               db 13,10,13,10,"$"
  93.  
  94.  
  95. ;== Procedures =============================================================
  96.  
  97. ;---------------------------------------------------------------------------
  98. ;-- StartSwap : Coordinate swapping of Turbo Pascal program
  99.  
  100. StartSwap  proc far
  101.  
  102.               assume cs:code, ds:nothing
  103.  
  104.               ;-- Store current stack and initialize new stack ----------------
  105.  
  106.               cli                    ;Suppress interrupts
  107.               mov   StackPtr,sp      ;Mark current stack
  108.               mov   StackSeg,ss
  109.               push  cs               ;Install new stack
  110.               pop   ss
  111.               mov   sp,offset EndStack - 2
  112.               sti                    ;Re-enable interrupts
  113.  
  114.               push  cs               ;Set DS to CS
  115.               pop   ds
  116.               assume cs:code, ds:code
  117.  
  118.               ;-- Overwrite unnecessary memory --------------------------------
  119.  
  120.               cmp   ToDisk,0         ;Write to EMS memory?
  121.               je    Ems              ;Yes ---> Ems
  122.  
  123.               call  Write2File       ;No ---> Write to file
  124.               jnc   ShrinkMem        ;No error ---> ShrinkMem
  125.  
  126.               mov   Error_Code, 1    ;File output error?
  127.               jmp   short GetBack    ;return to Turbo
  128.  
  129. Ems:       mov   ah,41h           ;Pass segment address of the page frame
  130.               int   67h              ;Call EMM
  131.               mov   FrameSeg,bx      ;Place result in variables
  132.  
  133.               call  Write2Ems        ;Write program to EMS
  134.  
  135.               ;-- Provide number of currently allocated paragraphs ------------
  136.  
  137. ShrinkMem: mov   ax,TurboSeg      ;Segment address of Turbo code segment
  138.               sub   ax,11h           ;Allocate 10 paragraphs for PSP and 1
  139.                                              ;for MCB
  140.               mov   es,ax            ;ES now pointer to Turbo prog. MCB
  141.               mov   bx,es:[3]        ;Get number of paragraphs allocated
  142.               mov   OldPara,bx       ;and place in variable
  143.  
  144.               ;-- Calculate the number of paragraphs needed and reduce  --
  145.               ;-- memory requirements by this amount                    --
  146.  
  147.               inc   ax               ;AX now points to the PSP
  148.               mov   es,ax            ;for function call to ES
  149.               mov   bx,CostLen       ;Number of words needed by Swap routine
  150.               add   bx,128+7         ;Recalculate and round off PSP
  151.               mov   cl,3             ;Divide by 8 words (per paragraph)
  152.               shr   bx,cl
  153.  
  154.               mov   ah,4Ah           ;Function number for "change size"
  155.               int   21h              ;Call DOS interrupt
  156.  
  157.               ;-- Execute specified command line using the EXE function ------
  158.  
  159.               mov   bp,ds            ;Store DS 
  160.  
  161.               mov   ax,cs            ;Set ES and DS to CS
  162.               mov   es,ax
  163.               mov   ds,ax
  164.  
  165.               ;-- Enter segment address of code segments in the pointer -----
  166.               ;-- to the EXEC structure
  167.  
  168.               mov   word ptr ExecData.EsFCB1Adr + 2,ax
  169.               mov   word ptr ExecData.EsFCB1Adr + 2,ax
  170.               mov   word ptr ExecData.EsCmdPAdr + 2,ax
  171.  
  172.               mov   bx,offset ExecData  ;ES:BX point to parameter block
  173.               mov   dx,offset PrgName   ;DS:DX point to command string
  174.  
  175.               mov   ax,4B00h         ;Function number for "EXEC"
  176.               int   21h              ;Call DOS interrupt
  177.               mov   ds,bp            ;Move DS
  178.               jnc   ReMem            ;No error ---> ReMem
  179.  
  180.               mov   Error_Code,ah    ;Note ErrorCode
  181.  
  182.               ;-- Return memory to original size ------------------------------
  183.  
  184. ReMem:     mov   ax,TurboSeg      ;Set Turbo code segment address
  185.               sub   ax,10h           ;to start of PSP
  186.               mov   es,ax            ;and load into ES
  187.               mov   bx,OldPara       ;Old number of paragraphs
  188.  
  189.               mov   ah,4Ah           ;Function number for "change size"
  190.               int   21h              ;Call DOS interrupt
  191.               jnc   GetBack          ;No error ---> GetBack
  192.  
  193.               jmp   Terminate        ;Error in ReMem --> End program
  194.  
  195.               ;-- Return to program -------------------------------------------
  196.  
  197. GetBack:   cmp   ToDisk,0         ;Write to EMS memory?
  198.               je    Ems1             ;Yes ---> Ems1
  199.  
  200.               call  GetFromFile      ;No, reload as file
  201.               jnc   CloseUp          ;No error ---> CloseUp
  202.  
  203.               jmp