返回指向字符串中字母的指针 [英] Returning pointer to the letter in string

查看:88
本文介绍了返回指向字符串中字母的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我搜索的代码,其中某个字符串包含一个字母:

This is my code to search ih some string consists of one letter:

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

但是如何将指针返回到字符串中的字母呢?

But how to return the pointer to the letter in string?

如果我想换一个字母.如果您有指向字符串中字母的指针,那么这样做很容易.

If I want after to change one leter with some other. Its easy to do if you have pointer to the letter in string.

推荐答案

如果repne scasb找到了元素,则EDI指向元素在第一个匹配项之后.您必须对其进行递减以获取指向所需元素的指针.

If repne scasb finds the element, EDI points to the element after the first match. You have to decrement it to get the pointer to the desired element.

您不需要清除方向标记(cld).在没有您的任何参与的情况下设置方向标志是非常不可能的.如果是这样,您应该将其恢复为以前的状态.

You don't need to clear the direction flag (cld). It's very very unlikelikely that the direction flag is set without any involvement of your part. And if so, you should seit it back to the former status.

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,则可以使用正常"比较循环来扫描单词

If you don't like repne scasb you can scan the word with a "normal" comparison loop

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

这篇关于返回指向字符串中字母的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆