DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
MSG2 DB "NOT FOUND$"
SE DB 34H
DATA ENDS
PRINT MACRO MSG
MOV AH, 09H
LEA DX, MSG
INT 21H
INT 3
ENDM
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AL, SE
LEA SI, STRING1
MOV CX, 04H
UP:
MOV BL,[SI]
CMP AL, BL
JZ FO
INC SI
DEC CX
JNZ UP
PRINT MSG2
JMP END1
FO:
PRINT MSG1
END1:
INT 3
CODE ENDS
END START
程序在序列11H,22H,33H,44H中搜索字节'34H'。
START部分执行以下操作:
循环部分执行以下操作:
打印宏:
MOV AH, 09H
INT 21H
是打印的MS-DOS调用,直到“$”INT 3
表示退出程序。
计数器CX设置为4是可疑的,但STRING1序列包含5个字节。没有真正调试程序我会说CX = 5会是正确的吗?
我的投票相当于memchr(s,c,n)
,其中s
在SI
(每次迭代递增),c
在BL
(与从'SI得到的'物品被发现'退出条件得到的东西相比)和n
在CX
(如果在BL
中则减少,如果数组元素等于http://man7.org/linux/man-pages/man3/memchr.3.html未找到,这是其他退出条件)加上友好输出(包含int21h调用的宏)。见33h
它在字节数组11H,22H,33H,44H,55H
中搜索值DATA SEGMENT
# Data where we will look up for value
STRING1 DB 11H,22H,33H,44H,55H
# message to be printed out
MSG1 DB "FOUND$"
# yet one message
MSG2 DB "NOT FOUND$"
# program will look up for this value
SE DB 33H
DATA ENDS
# this is PRINT macro- will be used later like a function
PRINT MACRO MSG
# 09H - is a function of interrupt 21h - print to console http://spike.scu.edu.au/~barry/interrupts.html
MOV AH, 09H
LEA DX, MSG
# print message using interrupt 21h
INT 21H
# I think it's exit - check it in the x86 interrupts table if you need
INT 3
ENDM # end of print macro
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
# this is the first line of code
MOV AX, DATA
MOV DS, AX
# copy value of SE (33h) to the AL register so later we can compare it
MOV AL, SE
LEA SI, STRING1
# save in CX length of the data to be looked up for value (actually num or iterations)
MOV CX, 04H
# the main loop starts here
UP:
# copy byte from buffer STRING1 to the BL register
MOV BL,[SI]
# check if AL==BL -> value found
CMP AL, BL
# if AL==BLjump to FO (will print FOUND)
JZ FO
# if not found,
# move to next byte in STRING1
INC SI
# decrement loop constraint (loop should end before reaching end of string)
DEC CX
# if CX <> 0 loop again
JNZ UP
# if CX==0 print message NOT FOUND
PRINT MSG2
JMP END1
FO:
PRINT MSG1
END1:
INT 3
CODE ENDS
END START
qazxswpoi