Search code examples
assemblyx86masmmasm32

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


I am searching to solve an exercise that i found online to prepare me to the next exam about Microsoft Assembler. So the text say that I need to create an .asm file that work with string:

#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();
}

The function (in MASM) must work on the first string 's' and the word 'word1' puting the result on the second string 'd'... And the output must be (with this 's' and 'word1'):

“Sopra la panca la capra campa”
“            capra             ”

So saveworddisplacement() put a space in 'd' when it not find 'word1' in 's'... I try it a lot of times but it doesnt work! So now i try asking to you... Thank you very much!

PLEASE ONLY ASM or MASM!

EDIT: this is what i tryed to do:

.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

Solution

  • You're on the right path. You have to handle the "found" case:

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