返回指向字符串中字母的指针
Returning pointer to the letter in string
这是我的代码,用于搜索一些由一个字母组成的字符串:
selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'
cld
mov ecx, LENGTHOF selectedWords
mov edi, offset selectedWords
mov al, inputLetter ;Load character to find
repne scasb ;Search
jne notfound
但是如何return指向字符串中字母的指针呢?
如果我想把一个字母改成另一个字母。如果你有指向字符串中字母的指针,这很容易做到。
如果您阅读 REP
and SCASB
的文档,您将看到 SCAS 更新 edi
。因此匹配字符的位置存储在 EDI
.
您所要做的就是 return EDI
if ZF=1
和 return 0 if ZF<>1
。
cld
mov ecx, LENGTHOF selectedWords
mov edi, offset selectedWords
mov al, inputLetter ;Load character to find
repne scasb ;Search
jne notfound
found:
mov eax,edi ;return the address of the match.
ret
notfound:
xor eax,eax ;return 0 aka not found as address.
ret
如果 repne scasb
找到元素,EDI
指向元素 在 第一个匹配项之后。您必须递减它以获得指向所需元素的指针。
您不需要清除方向标志(cld
)。在您没有任何参与的情况下设置方向标志的可能性非常小。如果是这样,你应该把它恢复到以前的状态。
INCLUDE Irvine32.inc
.DATA
selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'
err_msg BYTE "Not found.", 0
.CODE
main PROC
mov ecx, LENGTHOF selectedWords
mov edi, offset selectedWords
mov al, inputLetter ; Load character to find
repne scasb ; Search
jne notfound
dec edi
mov al, [edi]
call WriteChar ; Irvine32: Write a character in AL
exit ; Irvine32: ExitProcess
notfound:
lea edx, err_msg
call WriteString ; Irvine32: Write a null-terminated string pointed to by EDX
exit ; Irvine32: ExitProcess
main ENDP
END main
如果你不喜欢repne scasb
你可以用"normal"比较循环扫描这个词
INCLUDE Irvine32.inc
.DATA
selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'
err_msg BYTE "Not found.", 0
.CODE
main PROC
mov edi, offset selectedWords
mov ecx, LENGTHOF selectedWords
mov al, inputLetter
@@:
cmp [edi], al ; Compare memory/immediate value
je found ; JE = jump if equal
inc edi ; Increment pointer
dec ecx ; Decrement counter
jne @B ; Jump back to the last @@, if ECX == 0
jmp notfound
found:
mov al, [edi]
call WriteChar ; Irvine32: Write a character in AL
exit ; Irvine32: ExitProcess
notfound:
lea edx, err_msg
call WriteString ; Irvine32: Write a null-terminated string pointed to by EDX
exit ; Irvine32: ExitProcess
main ENDP
END main
这是我的代码,用于搜索一些由一个字母组成的字符串:
selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'
cld
mov ecx, LENGTHOF selectedWords
mov edi, offset selectedWords
mov al, inputLetter ;Load character to find
repne scasb ;Search
jne notfound
但是如何return指向字符串中字母的指针呢?
如果我想把一个字母改成另一个字母。如果你有指向字符串中字母的指针,这很容易做到。
如果您阅读 REP
and SCASB
的文档,您将看到 SCAS 更新 edi
。因此匹配字符的位置存储在 EDI
.
您所要做的就是 return EDI
if ZF=1
和 return 0 if ZF<>1
。
cld
mov ecx, LENGTHOF selectedWords
mov edi, offset selectedWords
mov al, inputLetter ;Load character to find
repne scasb ;Search
jne notfound
found:
mov eax,edi ;return the address of the match.
ret
notfound:
xor eax,eax ;return 0 aka not found as address.
ret
如果 repne scasb
找到元素,EDI
指向元素 在 第一个匹配项之后。您必须递减它以获得指向所需元素的指针。
您不需要清除方向标志(cld
)。在您没有任何参与的情况下设置方向标志的可能性非常小。如果是这样,你应该把它恢复到以前的状态。
INCLUDE Irvine32.inc
.DATA
selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'
err_msg BYTE "Not found.", 0
.CODE
main PROC
mov ecx, LENGTHOF selectedWords
mov edi, offset selectedWords
mov al, inputLetter ; Load character to find
repne scasb ; Search
jne notfound
dec edi
mov al, [edi]
call WriteChar ; Irvine32: Write a character in AL
exit ; Irvine32: ExitProcess
notfound:
lea edx, err_msg
call WriteString ; Irvine32: Write a null-terminated string pointed to by EDX
exit ; Irvine32: ExitProcess
main ENDP
END main
如果你不喜欢repne scasb
你可以用"normal"比较循环扫描这个词
INCLUDE Irvine32.inc
.DATA
selectedWords BYTE "BICYCLE"
inputLetter BYTE 'Y'
err_msg BYTE "Not found.", 0
.CODE
main PROC
mov edi, offset selectedWords
mov ecx, LENGTHOF selectedWords
mov al, inputLetter
@@:
cmp [edi], al ; Compare memory/immediate value
je found ; JE = jump if equal
inc edi ; Increment pointer
dec ecx ; Decrement counter
jne @B ; Jump back to the last @@, if ECX == 0
jmp notfound
found:
mov al, [edi]
call WriteChar ; Irvine32: Write a character in AL
exit ; Irvine32: ExitProcess
notfound:
lea edx, err_msg
call WriteString ; Irvine32: Write a null-terminated string pointed to by EDX
exit ; Irvine32: ExitProcess
main ENDP
END main