TOOLKIT\CCS.ASM  ·  ASM  ·  3.7 KB  ·  1988-10-01  ·  from PersonalComputing_Oct-1988_Disk-1
public exit,_chkstack,display,dispnl,errno,_osmajor
extrn main:near,strlen:near
 
pgroup group aaa,prog,_codetop
dgroup group data,udata,stack
 
aaa segment byte public 'prog'
aaa ends
 
prog segment byte public 'prog'
assume cs:pgroup,ds:dgroup
 
org 100h
 
entry:
  jmp short start
 
_chkstack:
  cmp sp,offset datatop
  jbe BADSTACK
  cmp watermark,0d6d6h
  jnz BADSTACK
  ret
BADSTACK:
  mov dx,offset overflow
ERROR:
  mov ah,9
  int 21h
  mov ax,1
  push ax
  call exit
exit:
  mov bp,sp
  mov al,[bp+2]
  mov ah,4ch
  int 21h
 
start:
  cli
  mov ax,offset lastbyte
  mov cx,4
  shr ax,cl
  push cs
  pop cx
  add ax,cx
  mov ds,ax                  ; set up segment for DATA
  mov dx,es:2                ; get total memory paragraph count
  sub dx,ax
  cmp dx,1000h
  jb L1                      ; grab as much memory as we can for data segment
  mov dx,1000h               ; up to 64K limit for single segment
L1:
  mov bx,ax
  mov cx,es
  sub bx,cx
  mov cx,bx
  add bx,dx
  mov dx,ax
  mov ah,4ah
  int 21h                    ; inform DOS of new limit
  sub bx,cx
  mov cl,4
  shl bx,cl
  mov ss,dx
  mov sp,bx                  ; initialise stack
  sub bx,offset datatop
  cmp bx,512                 ; check for minimum stack of 512 bytes
  mov dx,offset nomem
  jb ERROR
  sub sp,128                 ; space for command line copy
  mov ah,30h
  int 21h
  mov _osmajor,al            ; save DOS major VERsion for external use
  cmp al,2
  mov dx,offset wrongdos
  jb ERROR
  sti                        ; interrupts back on now stack stable
  push ds
  push es
  pop ds
  pop es
  mov si,80h
  mov di,sp
  lodsb
  xor ah,ah
  mov cx,ax
  rep movsb                  ; copy command line above stack
  xor al,al
  stosb                      ; zero terminate so know where end is
  push es
  pop ds                     ; es=ds=data segment
  mov si,sp
  mov bx,2
round:
  lodsb
  or al,al
  jz argdone
  cmp al,20h
  jbe round
  cmp bx,10
  ja argdone
  dec si
  mov argv[bx],si            ; analyse comline and setup argv[]
  add bx,2
round2:
  lodsb
  or al,al
  jz argdone
  cmp al,20h
  ja round2
  xor al,al
  mov [si-1],al
  jmp round
argdone:
  shr bx,1
  mov argc,bx
  mov ax,offset argv
  push ax
  push bx                    ; argc and argv on stack for main
  mov cx,offset udatatop
  mov di,offset udatabase
  sub cx,di
  jz L2
  xor al,al
  rep stosb                  ; clear all udata to zero
L2:
  call main
  xor ax,ax
  push ax
  call exit
 
dispnl:
  push bp
  mov bp,sp
  push [bp+4]
  call strlen                ; get length of string passed
  mov sp,bp
  mov cx,ax
  mov ah,40h
  mov dx,[bp+4]
  mov bx,1                   ; handle for console output
  int 21h
  pop bp
  ret
 
display:
  push bp
  mov bp,sp
  push [bp+4]
  call dispnl
  mov sp,bp
  mov ax,offset crlf
  push ax
  call dispnl
  mov sp,bp
  pop bp
  ret
 
prog ends
 
_codetop segment para public 'prog'
lastbyte equ $               ; MUST be PARAGRAPH aligned to match data start
_codetop ends
 
data segment para public 'data'
zeronull      dw 0
overflow      db 0dh,0ah,"Stack Overflow",0dh,0ah,24h
nomem         db 0dh,0ah,"Insufficient Memory",0dh,0ah,24h
wrongdos      db 0dh,0ah,"DOS 2 or Higher",0dh,0ah,24h
crlf          db 0dh,0ah,0
even
argc          dw 0
argv          dw offset zeronull
              dw 4 dup (?)
errno         dw 0
_osmajor      db 0
data ends
 
udata segment para public 'data'
udatabase equ $
udata ends
 
stack segment word 'data'
udatatop equ $
watermark    dw 0d6d6h
datatop equ $                ; end of all data - bottom of stack
stack ends
 
end entry