186e TITLE ErrLvl.ASM ;==== Executes a program and shows the errorlevel afterwards ;---------------------------------------------------------------------- INCLUDE SCREEN.MAC INCLUDE KEYB.MAC B EQU DS:BYTE PTR 0 W EQU DS:WORD PTR 0 CSEG SEGMENT ASSUME CS:CSEG,DS:CSEG ORG 100h ;--------------------------------------------------------------------- Start: jmp Begin fcb1 db 40 dup (0) fcb2 db 40 dup (0) parmblk dw 0 ; Segment address of environment string dd 0 ; Pointer to command line dd 0 ; Pointer to default FCB to be passed at PSP+5CH dd 0 ; Pointer to default FCB to be passed at PSP+6CH cmdline db 128 dup (0) path_str db 65 dup (0) stacksp db 32 dup ('STACK ') newstack dw 0 ;------------------------------------------------------------------------- Mshrink PROC NEAR mov ax,OFFSET Codesize ;Calculate number of bytes to keep mov cx,16 div cl and ax,0ffh ;Remove Remainder in AH add ax,100h ;Just to be sure... mov bx,ax mov ah,04aH ;Mshrink int 21H ret Mshrink ENDP ;-------------------------------------------------------------------------- Param_Read PROC NEAR xor cx,cx mov cl,byte ptr ds:[80h] cmp cl,0 jnz Not_Empty call Show_Usage ret Not_Empty: xor cx,cx mov cl,ds:byte ptr[80h] mov si,81h call get_char ;Search for first non-space or non-tab ch. cmp cx,0 ;Only spaces or tabs? jnz go_on call Show_Usage ;True, show usage ret go_on: push si ;save temporarily push cx call chg_path ;Extract path and change it, if exists pop cx pop si push si ;Save start of filename string inc si dec cx fn_lp: ;Loop as long as "real" characters cmp b[si],20h jbe go_on2 inc si loop fn_lp go_on2: mov al,[si] mov b[si],0 ;Mark end of filename string with '0' cmp cx,00h ;End of string? jz no_params ;Yes, finished inc si mov cx,00h ;Clear char counter mov di,OFFSET cmdline+1 par_lp: movsb inc cx cmp b[si],0dh jz ready jmp par_lp ready: mov b[cmdline],cl no_params: clc pop si ret Param_Read ENDP ;---------------------------------------------------------------------- get_char PROC NEAR nxt: cmp b[si],020h ja rdy inc si loop nxt rdy: ret get_char ENDP ;---------------------------------------------------------------------- chg_path PROC NEAR xor cx,cx mov cl,byte ptr ds:[80h] mov bl,cl mov di,81h mov al,'\' cld repne scasb cmp cx,0 jz not_found inc cx dec di dec di cmp byte ptr[di],':' jnz no_drive dec di mov dl,byte ptr[di] and dl,0dfh ;Make it Upper Case cmp dl,41h ;Check for valid jb not_found ;drive letter cmp dl,60h ja not_found push ax sub dl,061h mov ah,0eh ;Select Disk int 21h pop ax no_drive: mov di,81h add di,bx mov dx,bx sub dx,cx mov cx,bx std repne scasb inc di inc cx sub cx,dx sub di,cx mov si,di mov di,OFFSET path_str cld rep movsb mov ah,3bh ;Change directory mov dx,OFFSET path_str int 21h not_found: clc ret chg_path ENDP ;------------------------------------------------------------------------- show_usage PROC NEAR Print 'Executes a program and shows the ERRORLEVEL on return' call crlf Print 'usage: ERRLVL [drive:][path]filename.ext [command parameters]' call crlf mov al,01h stc ret show_usage ENDP ;-------------------------------------------------------------------------- Exec PROC NEAR push ds mov bp,OFFSET parmblk mov [bp+2],OFFSET cmdline mov [bp+4],CS mov [bp+6],OFFSET fcb1 mov [bp+8],CS mov [bp+10],OFFSET fcb2 mov [bp+12],CS mov ah,4BH ; EXEC mov al,00H ; Load and Execute mov dx,si mov bx,OFFSET parmblk int 21H jnc Exec_OK Print 'File not found' call crlf stc mov al,01h jmp Exec_Error Exec_OK: clc Exec_Error: pop ds ret Exec ENDP ;----------------------------------------------------------------------- Get_ErrLvl PROC NEAR mov ah,04dh int 21h call crlf Print '===> ERRORLEVEL = ' mov dl,al Wr_Byte call crlf mov al,00h ret Get_ErrLvl ENDP ;------------------------------------------------------------------------ crlf PROC NEAR cr_lf ret crlf ENDP ;------------------------------------------------------------------------ Begin: mov sp,OFFSET newstack ;New stack call Param_Read jc Exit call Mshrink call Exec jc Exit call Get_ErrLvl Exit: mov ah,04ch int 21h ;--------------------------------------------------------------- Codesize: CSEG ENDS END Start . 0