【问题标题】:how to find a word in a string and save the displacement如何在字符串中查找单词并保存位移
【发布时间】:2017-06-15 16:39:51
【问题描述】:

我正在寻找解决我在网上找到的一个练习,以便为下一次有关 Microsoft 汇编程序的考试做好准备。 所以文本说我需要创建一个使用字符串的 .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' 时,它会在 '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

【问题讨论】:

  • 删除了 C-tag(与 C 无关)。
  • 展示您尝试过的示例,以便了解您的思维过程。
  • 好的,非常感谢!现在有一个例子:)
  • 能否请您提供英文的示例代码(这将有助于更好地理解它)
  • @JensBaitinger 现在看看......但我认为这没有什么改变:D

标签: assembly x86 masm masm32


【解决方案1】:

你走在正确的道路上。你必须处理“找到”的情况:

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

【讨论】:

  • 哇!现在它工作了!你是最棒的rkhb!!!非常感谢,您总是帮助我解决我的代码问题...谢谢!真的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-05
  • 2018-04-01
  • 1970-01-01
相关资源
最近更新 更多