汇编语言命令参数程序的编写

  、 引言:

  如果大家用过TurboC2.0/3.0 or BorlandC3.X等编译器编写DOS应用编写个命令行参数形式应用对大家来说是件非常容易事情只要在主函中加几个参数就OK( ( argc,char *argv,char *env){})相对汇编语言来说编写个命令行参数就比较艰难它要用到DOS段前缀PSP(Program Segment Prefix)知识以及其他相关DOS知识(本文只对参数介绍环境块不作讨论)

   2、相关知识:

  在DOS提示符下键入个命令(内/外部命令)或名字,DOS SHELL(COMMAND.COM)首先根据名字判别其是内部命令还是外部命令或用户若是内部命令则COMMAND.COM暂驻在内存中部分DOS内部命令代码若是外部命令或用户DOS SHELL则在当前目录和搜索路径中搜索匹配文件名找到了就加载加载出错显示信息找不到则显示Bad command or file name

  用户载DOS提示符下输入DOS SHELL把以回车(0Dh)为结束这以串作为个命令和参数进行解释个空格以前串为命令名(必须符合DOS命名规则)个空格(包括空格)到回车的间作为命令或参数

  段前缀--PSP是DOS加载个外部命令或用户(扩展名为COM or EXE)时段前设置个以节为边界固定长度为10H(即256字节节为16字节)存储块PSP和段共有个内存控制块(MCB)PSP位于每个开始部分无论是COM还是EXEPSP数据结构是相同PSP是和DOS接口DOS利用PSP管理进程DOS用户进程指个已被装入内存可执行或已被调入内存但未执行COMMAND.COM是个最早被装入内存因而可被看作祖先进程外部命令或用户作为子进程被DOS通过INT 21H4BH号子功能来加载用户也可以通过INT 21H4BH号子功能来加载自己子进程控制子进程执行并通过4DH号子功能获取子进程运行状况

  PSP中存有许多有关启动、执行、结束以及进程调度、进程环境地址和进程标志等重要信息利用PSP还可以控制父子进程间通信至于PSP数据结构详细内容请参考有关书籍本文不详细给出

  DOS加载个COM或EXE段寄存器DSES都指向PSP段址(PSP段址是进程标志符)而不是指向数据段和附加段COM文件CSSS也指向PSP段址EXE文件CSSSIP和SP需要进行重定位

  DOS加载个外部命令或用户把文件名的后到回车符的间最多可达127个作为参数并把这些串送到PSP位移81H开始区域位移80H个字节存放参数串长度(回车符不算在内)大家可用DEBUG.EXE加载个带参数然后用D DS:80子命令查看加载参数命令行参数般以空格(20H)为开始回车符(0DH)为结束但命令行中重定向管道符以及有关信息不作为参数传递给PSP

   3、举例:

  本例PARATEST.ASM在没有参数(参数为连串空格也视为无参数)情况下显示提示信息'/'作为参数标志'/'后是参数根据区别参数显示区别并忽略'/'前空格还把非法参数显示出来由于中保存参数单元只设了两BYTES如果'/'后是合法参数不管'/'后有多少个都认为是合法

  顺便介绍个汇编编程窍门技巧文后附带举例源(PARATEST.ASM)中DEBUG子是利用了INT 21H07H号子功能等待用户键盘输入相当于TURBOC中getch作为断点我们还可以利用其显示断点调试信息(包括各寄存器值)但注意保存现场并进行现场恢复

   4、结束语:

  个月以前我还只是会看别人汇编自从自己动手写自己汇编编程水平有了很大进步过程中遇到问题然后自己看书自己解决问题这样学习汇编编程比光看书更有效汇编编程水平还很菜鸟我会不断提高自己水平同时也希望和广大编程爱好者交流

  附:源(PARATEST.ASM)

; ************************************************
; * Program:Use asm language to creat a command *
; *     line and parameter program.     *
; **
; * Designer:Howard  Original Place:Wuhan    *
; * Creat Date:09/30/1999            *
; * Modication Date:10/05/1999         *
; * Now Version:1.0               *
; * Pass:Tasm 5.0,Tlink 3.1           *
; **
; *    Modication History          *
; *----------------------------------------------*
; * Version 1.0 1.Command line and parameter   *
; * 09/30/1999  test program.         *
; *----------------------------------------------*
; * Version 1.1 2.Add the spaces parameters jud- *
; * 10/05/19999  gement.            *
; ************************************************
;
.model small
.386
.code
 org 100h
start:
proc far
     push cs
 pop ds ;ds=psp seg address
 cld       ;cf=0
 mov si,81h ;psp+81h is the first parameter char    
 lea bx,parameter ;parameter address(off) saved to bx
    ;unit parameter is used to save the parameter
     lodsb   ;load a from [si] to al,and si=si+1
 cmp al,0dh   ; Enter?
 jz  scanexit   ; yes then scan parameter end
 cmp al,' '
 jz  judgespace
 lodsb
continue:
     push si
 cmp al,'/'
 jz  parascanloop
     jmp error    ;wrong parameter
judgespace:
     lodsb
;    call debug     ; po
 cmp al,0dh
 je  scanexit
 cmp al,' '
 je  judgespace
 jne continue
parascanloop:   ;saved the parameter to unit parameter
     mov [bx],al   ;save al to [bx]just save the parameters
 lodsb
 cmp al,0dh   ;Enter?
 jz  choise   ; yes then jump choise
 inc bx
 jnb parascanloop ;the next char
scanexit:
     lea dx,noparametermsg
 call disp
 call rettodos
choise:
     lea si,parameter
 mov al,[si+1]   ;load the parameter to al
 cmp al,'?'   ;judge the parameter and choose                       ;the dferent process
 jz  help
 cmp al,'p'
 jz  pr
 cmp al,'P'
 jz  pr
 jmp error   ;wrong parameter
pr:
     lea dx,message
 call disp
 call rettodos
help:            ;pr the help message
     lea dx,helpmsg
 call disp
 call rettodos
error:           ;pr the error parameter message
     lea dx,wrongparamsg
 call disp
 mov ax,0200h
     pop si
 dec si
prnwrongparameter: 
     lodsb
 cmp al,0dh
 jz  retdos
 mov dl,al
  21h
 loop prnwrongparameter
retdos:
     mov dl,'"'
  21h
 mov dl,'!'
  21h
 call rettodos
endp
disp proc near
     mov ah,09h
  21h
 ret
disp endp
rettodos proc near
     mov ah,4ch
  21h
rettodos endp
;
; a po
;debug proc near
;    push ax dx
; mov ax,0900h
; mov dx,off debugmsg
;  21h
; mov ax,0700h
;  21h
; pop dx
; pop ax
;debug endp
;debugmsg db 0dh,0ah,'Program stop here,press any key to continue...','$' 
noparametermsg db 0ah,0dh,'There is no parameter,enter paratest /? for help.','$'
message  db 0dh,0ah,'This is a parameter test program.','$'
wrongparamsg db 0dh,0ah,'The wrong parameter:"','$'
helpmsg  db 0dh,0ah,'1.Paratest /?'
     db 0dh,0ah,' Pr the help message.'
  db 0dh,0ah,'2.Paratest /p'
  db 0dh,0ah,' Pr the test message.','$'
parameter db 2 dup(?)
end start


Tags: 

延伸阅读

最新评论

发表评论