2017-06-15 81 views
2

我正在寻找解决我在网上找到的练习,以便为下一次关于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”和词“字1”工作puting结果关于第二个字符串“d” ... 和输出必须是(本“s”和“字1”):

“Sopra la panca la capra campa” 
“   capra    ” 

所以saveworddisplacement()把一个空间“d”,当它找不到's'中的'word1'... 我尝试了很多次,但它不起作用!所以现在我试着问你... 非常感谢!

请仅使用ASM或MASM!

编辑:这是我tryed做:

.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

C标记已删除(与C无关)。 –

+0

显示你试过的例子,以便了解你的思维过程。 –

+0

非常感谢你!现在有一个例子:) –

回答

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 
    ... 
+0

哇!现在它工作了!你是最棒的rkhb!非常感谢,你总是帮我解决我的代码问题...谢谢!真! –

相关问题