我在TASM汇编语言上编写DOS命令行参数解析器。这是我的代码:
.model tiny
.386
.code
org 100h
start:
mov ax, ax
mov bp, 80h
mov si, 81h
mov ch, 0
mov cl, ds:[bp]
push 0
cmp cx, 0
je FINISH
CYCLE_BEGIN:
lodsb
cmp al, 2Fh
je SLASH
cmp al, 31h
je ONE
cmp al, 32h
je TWO
cmp al, 33h
je THREE
cmp al, 68h
je HELP
cmp al, 20h
je SPACE
mov bx, 6
jmp CHANGE_STATE
SLASH:
mov bx, 0
jmp CHANGE_STATE
ONE:
mov bx, 1
jmp CHANGE_STATE
TWO:
mov bx, 2
jmp CHANGE_STATE
THREE:
mov bx, 3
jmp CHANGE_STATE
HELP:
mov bx, 4
jmp CHANGE_STATE
SPACE:
mov bx, 5
CHANGE_STATE:
mov ax, 7
mul dx
add ax, bx
mov dh, 0
push si
mov si, offset AU
add si, ax
lodsb
mov dl, al
pop si
cmp dx, 6
JE ERROR
cmp dx, 1
je HAVEONE
cmp dx, 2
je HAVETWO
cmp dx, 3
je HAVETHREE
cmp dx, 4
je HAVEHELP
jmp CYCLE_END
HAVEONE:
mov ax, 0001h
jmp SETFLAGS
HAVETWO:
mov ax, 0010h
jmp SETFLAGS
HAVETHREE:
mov ax, 0100h
jmp SETFLAGS
HAVEHELP:
mov ax, 1000h
SETFLAGS:
pop bx
or bx, ax
push bx
CYCLE_END:
dec cx
jne CYCLE_BEGIN
jmp SUCCESS
ERROR:
pop bx
mov dx, offset merror
mov ah, 09h
int 21h
jmp FINISH
SUCCESS:
pop bx
mov dx, bx
and dx, 1000h
je MESSAGES_CHECK
mov dx, offset mhelp
mov ah, 09h
int 21h
jmp FINISH
MESSAGES_CHECK:
mov dx, bx
and dx, 0001h
je CHECK2
mov dx, offset m1
mov ah, 09h
int 21h
mov ah, 02h
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
CHECK2:
mov dx, bx
and dx, 0010h
je CHECK3
mov dx, offset m2
mov ah, 09h
int 21h
mov ah, 02h
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
CHECK3:
mov dx, bx
and dx, 0100h
je FINISH
mov dx, offset m3
mov ah, 09h
int 21h
FINISH:
RET
m1 db 'You have used /1 key$'
m2 db 'You have used /2 key$'
m3 db 'You have used /3 key$'
mhelp db 'ad.asm [/1 | /2 | /3| /h]$'
merror db 'Wrong input$'
AU:; / 1 2 3 h *
db 5,6,6,6,6,0,6 ;
db 6,6,6,6,6,0,6 ; /1
db 6,6,6,6,6,0,6 ; /2
db 6,6,6,6,6,0,6 ; /3
db 6,6,6,6,6,0,6 ; /h
db 6,1,2,3,4,6,6 ; /
db 6,6,6,6,6,6,6 ; err
end start
如果有m1
,/1
,如果有m2
和/2
&#,则会打印m3
的内容39;如果/3
。问题在于此代码部分:
mov dx, offset m1
mov ah, 09h
int 21h
----
mov dx, offset m2
mov ah, 09h
int 21h
----
mov dx, offset m3
mov ah, 09h
int 21h
它不会打印任何东西。即使我在Turbo Debugger中看到代码已到达并且dx
中存在正确的偏移量,int 21h
之后的stdout中也没有文本。为什么呢?
答案 0 :(得分:2)
由于DX未事先初始化,因此CHANGE_STATE的乘法没有任何意义!
CHANGE_STATE:
mov ax, 7
mul dx
希望这会有所帮助。
另外,因为您正在使用字符串原语lodsb
,所以您可以确保清除方向标记cld
。
答案 1 :(得分:-2)
DOS .com应用程序仅将其所有代码和数据存储在一个段中,DS和ES已经初始化,并且在开始执行时等于CS。
...
为了获得一些命令行参数,建议之前获取PSP的段地址,因为当前PSP不一定是调用者的PSP,如果我们从父应用程序中启动子应用程序例如,使用int 21h的exec函数AX = 4B00h。
RBIL-> inter61b.zip-> INTERRUP.H
--------D-2162-------------------------------
INT 21 - DOS 3.0+ - GET CURRENT PSP ADDRESS
AH = 62h
Return: BX = segment of PSP for current process
Notes: this function does not use any of the DOS-internal stacks and may
thus be called at any time, even during another INT 21h call
the current PSP is not necessarily the caller's PSP
identical to the undocumented AH=51h
SeeAlso: AH=50h,AH=51h