将新输入的文本与asm中的可用命令列表进行比较



如何比较buffer中的字符串与'--help'之类的命令如何比较缓冲区中新输入的文本与命令这是我的代码

xor  ax, ax
mov  ds, ax
mov  es, ax
cld
;bg
MOV AH, 06h   
XOR AL, AL    
XOR CX, CX    
MOV DX, 184FH  
MOV BH, 1Eh  
INT 10H
;cursor
mov dh, 1
mov dl, 30
mov bh, 0
mov ah, 2
int 10h
;text
mov  si, msg     
mov  bh, 0      
lodsb
More:
mov  ah, 0Eh     ; BIOS.Teletype
int  10h
lodsb            
cmp  al, 0
jnz  More
;cursor
mov dh, 3
mov dl, 2
mov bh, 0
mov ah, 2
int 10h
;commandline
Key:
mov  di, buf   
Next:
mov  ah, 00h   ;GetKeyboardKey
int  16h       
stosb          
mov  bh, 0     
mov  ah, 0Eh   ; BIOS.Teletype
int  10h
cmp  al, 13
jne  Next      
mov  al, 10   
int  10h

buf  db '........'
help db '--help'
msg  db 'WELCOME TO DARSHOS', 0
times 510-($-$$) db 0
dw 0xAA55```
help db '--help'

不清楚为什么要在命令前加上'——'。

对于该任务的实际方法,请考虑下一个可用命令列表:

list  db  'cls', 13
db  'help', 13
db  'date', 13
db  13

下面是搜索以carriagreturn结尾的ASCII字符串列表的一种方法,其中字符串不超过7个文本字符加上一个结束字节(13)。总共是8个,因为这是我们在输入要定位的命令时选择的限制。

代码片段使用了一对字符串指令:

  • lodsb字符串指令将DS:SI字节加载到AL寄存器中。这将自动移动指针。如果方向标志DF是清除的,它将增加1。
  • scasb字符串指令将比较AL寄存器和ES:DI的字节。这将修改我们可以查询的标志,它也会自动移动指针。
cld
mov  si, list
NextItemOnList:
mov  di, buf          ; Contains the command we want to find (terminated by 13)
NextChar:
lodsb
scasb
jne  NotThisOne
cmp  al, 13
jne  NextChar
Found:
...
NotThisOne:
dec  si               ; Back to where the mismatch occured
SkipRemainder:
lodsb                 ; Skip remainder of this list item
cmp  al, 13
jne  SkipRemainder
cmp  byte [si], 13    ; EndOfList ?
jne  NextItemOnList   ; No
NotFound:
...
使用上面的代码,我们可以找出该命令是否在列表中,但它是哪个?
  • 我们可以选择将索引与列表中的每个命令相关联,如:
cld
xor  cx, cx           ; cls=0, help=1, date=2
mov  si, list
NextItemOnList:
mov  di, buf          ; Contains the command we want to find (terminated by 13)
NextChar:
lodsb
scasb
jne  NotThisOne
cmp  al, 13
jne  NextChar
Found:
...
NotThisOne:
dec  si               ; Back to where the mismatch occured
SkipRemainder:
lodsb                 ; Skip remainder of this list item
cmp  al, 13
jne  SkipRemainder
inc  cx               ; Next index
cmp  byte [si], 13    ; EndOfList ?
jne  NextItemOnList   ; No
NotFound:
...
  • 或者我们可以在列表中添加将要处理命令的实际地址,如:
list  dw  xCls
db  'cls', 13
dw  xHelp
db  'help', 13
dw  xDate
db  'date', 13
dw  xFail
...
cld
mov  si, list
NextItemOnList:
lodsw                 ; Address of command's execution
mov  bx, ax
mov  di, buf          ; Contains the command we want to find (terminated by 13)
NextChar:
lodsb
scasb
jne  NotThisOne
cmp  al, 13
jne  NextChar
Found:
...
NotThisOne:
dec  si               ; Back to where the mismatch occured
SkipRemainder:
lodsb                 ; Skip remainder of this list item
cmp  al, 13
jne  SkipRemainder
cmp  word [si], xFail ; EndOfList ?
jne  NextItemOnList   ; No
NotFound:
...

最新更新