如何在字符串中查找单词并保存位移

how to find a word in a string and save the displacement

我正在搜索解决我在网上找到的练习,为我准备下一次关于 Microsoft Assembler 的考试做准备。 所以文字说我需要创建一个使用字符串的.asm文件:

#include <stdio.h>
#include <string.h>

int saveworddisplacement(char *s, char *d, char *word1);

int main(){
    char s[25] = { "sopra la panca la capra campa" };
    char d[25] = {""};
    char word1[] = { "capra" };
    saveworddisplacement(s, d, word1);
    //printf("%s\n", s);
    printf("%s", d);

    getchar();
}

函数(在 MASM 中)必须处理第一个字符串 's' 和单词 'word1' 并将结果放在第二个字符串 'd'... 并且输出必须是(使用 's' 和 'word1'):

“Sopra la panca la capra campa”
“            capra             ”

所以当 saveworddisplacement() 在 's' 中找不到 'word1' 时,将 space 放入 'd'... 我尝试了很多次,但它不起作用!所以现在我试着问你... 非常感谢!

请仅使用 ASM 或 MASM!

编辑:这是我尝试做的:

.586
.model flat
.code

_saveworddisplacement proc

;pre
push ebp
mov ebp,esp
push ebx
push edi
push esi

;clean registers
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
xor esi, esi ;index source-dest
xor edi, edi ;index word

mov eax, dword ptr[ebp+8] ;source
mov ebx, dword ptr[ebp+12] ;dest
mov ecx, dword ptr[ebp+16] ;word

begins:                     ;slide source
mov dl, byte ptr[eax+esi*1]
cmp dl,0
je finishs ;source finish

    begind: ;slide word
    mov dh, byte ptr[ecx+edi*1]
    cmp dh,0
    je finishd
    cmp dh,dl
    jne nofound
                    ;if it arrive here letters are equals
    push edi        ;save index value in the stack
    push esi


    found: 
    mov byte ptr[ebx+esi*1],dh
    inc edi
    inc esi
    mov dh, byte ptr[ecx+edi*1]
    mov dl, byte ptr[eax+edi*1]
    cmp dh,dl
    je found    ;again increasing index esi,edi
    cmp dh,dl
    jne nofound2 ;oh shit thats not the word i am searching

    nofound2: ;pop the index and puting "space" in 'dest' 
    pop esi
    pop edi
    mov byte ptr[ebx+esi*1],32
    inc esi
    xor edi,edi
    jmp begins

    nofound: ;at the first step (extern label) letters are differents
    mov byte ptr[ebx+esi*1],32
    inc esi
    jmp begins

    finishd: ;finished the word
    mov dl, byte ptr[eax+esi*1] 
    cmp dl,0
    je finishs
    mov byte ptr[ebx+esi*1],32 ;put space in the right of the word
    inc esi
    jmp finishd

finishs:
mov byte ptr[ebx+esi*1],0 ;puting "end of file" in dest

;post
pop esi
pop edi
pop ebx
mov esp,ebp
pop ebp

ret

_saveworddisplacement endp
end

你走在正确的道路上。您必须处理 "found" 案例:

    ...                                 ;if it arrive here letters are equals
    push edi                            ;save index value in the stack
    push esi

    found:
    mov byte ptr[ebx+esi*1],dh
    inc edi
    inc esi
    mov dh, byte ptr[ecx+edi*1]
;    mov dl, byte ptr[eax+edi*1]        ; EDI? -> typo
    mov dl, byte ptr[eax+esi*1]
    cmp dh, dl
    je found

    test dh, dh                         ; DH == 0?
    jne nofound2                        ;oh shit thats not the word i am searching

    add esp, 8                          ; Adjust the stack without poping anything
    xor edi,edi
    jmp begins

    nofound2:                           ;pop the index and puting "space" in 'dest'
    pop esi
    pop edi
    ...