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

时间:2017-06-15 16:39:51

标签: assembly x86 masm masm32

我正在寻找解决我在网上找到的练习,以帮助我准备下一期关于Microsoft Assembler的考试。 所以文本说我需要创建一个使用string:./ / p>的.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'中找不到'word'时's'中有空格... 我尝试了很多次但它不起作用!所以现在我试着问你...... 非常感谢你!

请仅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

1 个答案:

答案 0 :(得分:1)

你正走在正确的道路上。你必须处理&#34;发现&#34;情况下:

    ...                                 ;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
    ...