home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 2 / shell.asm
Encoding:
Assembly Source File  |  1988-08-11  |  4.4 KB  |  122 lines

  1. ; SHELL.ASM   A simple program to run an application as an
  2. ;             MS-DOS shell program. The program name and
  3. ;             startup parameters must be adjusted before
  4. ;             SHELL is assembled.
  5. ;
  6. ; Written by William Wong
  7. ;
  8. ; To create SHELL.COM:
  9. ;
  10. ;             C>MASM SHELL;
  11. ;             C>LINK SHELL;
  12. ;             C>EXE2BIN SHELL.EXE SHELL.COM
  13.  
  14. stderr  equ 2                ; standard error
  15. cr      equ 0dh              ; ASCII carriage return
  16. lf      equ 0ah              ; ASCII linefeed
  17. cseg    segment para public 'CODE'
  18. ;
  19. ; ----  Set up DS, ES, and SS:SP to run as .COM  ----
  20. ;
  21.         assume  cs:cseg
  22. start   proc    far
  23.         mov     ax,cs           ; set up segment registers
  24.         add     ax,10h          ; AX = segment after PSP
  25.         mov     ds,ax
  26.         mov     ss,ax           ; set up stack pointer
  27.         mov     sp,offset stk
  28.         mov     ax,offset shell
  29.         push    cs              ; push original CS
  30.         push    ds              ; push segment of shell
  31.         push    ax              ; push offset of shell
  32.         ret                     ; jump to shell
  33. start   endp
  34. ;
  35. ; ----  Main program running as .COM  ----
  36. ;
  37. ; CS, DS, SS = cseg
  38. ; Original CS value on top of stack
  39. ;
  40.         assume cs:cseg,ds:cseg,ss:cseg
  41. seg_size equ (((offset last) - (offset start)) + 10fh)/16
  42. shell   proc    near
  43.         pop     es              ; ES = segment to shrink
  44.         mov     bx,seg_size     ; BX = new segment size
  45.         mov     ah,4ah          ; AH = modify memory block
  46.         int     21h             ; free excess memory
  47.         mov     cmd_seg,ds      ; setup segments in
  48.         mov     fcb1_seg,ds     ; parameter block for EXEC
  49.         mov     fcb2_seg,ds
  50.         mov     dx,offset main_loop
  51.         mov     ax,2523h        ; AX = set Control-C handler
  52.         int     21h             ; set handler to DS:DX
  53.         mov     dx,offset main_loop
  54.         mov     ax,2524h        ; AX = set critical error handler
  55.         int     21h             ; set handler to DS:DX
  56.                                 ; Note: DS is equal to CS
  57. main_loop:
  58.         push    ds              ; save segment registers
  59.         push    es
  60.         mov     cs:stk_seg,ss   ; save stack pointer
  61.         mov     cs:stk_off,sp
  62.         mov     dx,offset pgm_name
  63.         mov     bx,offset par_blk
  64.         mov     ax,4b00h        ; AX = EXEC/run program
  65.         int     21h             ; carry = EXEC failed
  66.         mov     ss,cs:stk_seg   ; restore stack pointer
  67.         mov     sp,cs:stk_off
  68.         pop     es              ; restore segment registers
  69.         pop     ds
  70.         jnc     main_loop       ; loop if program run
  71.         mov     dx,offset load_msg
  72.         mov     cx,load_msg_length
  73.         call    print           ; display error message
  74.         mov     ah,08h          ; AH = read without echo
  75.         int     21h             ; wait for any character
  76.         jmp     main_loop       ; execute forever
  77. shell   endp
  78. ;
  79. ; ----  Print string  ----
  80. ;
  81. ; DS:DX = address of string
  82. ; CX    = size
  83. ;
  84. print   proc    near
  85.         mov     ah,40h          ; AH = write to file
  86.         mov     bx,stderr       ; BX = file handle
  87.         int     21h             ; print string
  88.         ret
  89. print   endp
  90. ;
  91. ; ----  Message strings  ----
  92. ;
  93. load_msg db cr,lf
  94.          db 'Cannot load program.',cr,lf
  95.          db 'Press any key to try again.',cr,lf
  96. load_msg_length equ $-load_msg
  97. ;
  98. ; ----  Program data area  ----
  99. ;
  100. stk_seg  dw     0               ; stack segment pointer
  101. stk_off  dw     0               ; save area during EXEC
  102. pgm_name db     '\NEWSHELL.COM',0 ; any program will do
  103. par_blk  dw     0               ; use current environment
  104.          dw     offset cmd_line ; command-line address
  105. cmd_seg  dw     0               ; fill in at initialization
  106.          dw     offset fcb1     ; default FCB #1
  107. fcb1_seg dw     0               ; fill in at initialization
  108.          dw     offset fcb2     ; default FCB #2
  109. fcb2_seg dw     0               ; fill in at initialization
  110. cmd_line db     0,cr            ; actual command line
  111. fcb1     db     0
  112.          db     11 dup (' ')
  113.          db     25 dup ( 0 )
  114. fcb2     db     0
  115.          db     11 dup (' ')
  116.          db     25 dup ( 0 )
  117.          dw     200 dup ( 0 )   ; program stack area
  118. stk      dw     0
  119. last     equ    $               ; last address used
  120. cseg     ends
  121.          end    start
  122.