Q:

Searching a string in a text

I have to write a program using x86 assembly that can search for a string in a text. I am trying really hard but I am just a beginner in assembly and just can not make anything that works. This is what I tried to write, it is probably realy messed up but I will be glad for any advices. Is at least anything good in it? 
%include 'rw32.inc'     ; library

[segment .data use32]   

sMessage1 db  "Hello World!",0      ;main text
sMessage2 db  "llo",0               ;searched string
sMessage3 db  "Searched string was found in main string",0
sMessage4 db  "Searched string was not found in main string",0 

[segment .code use32]   

prologue                ; macro

         mov di,sMessage1     
         mov si,sMessage2
         mov al,[si]
HOP:     cmp di,0
         je ENDOFTEXT
         cmp si,0
         je FINISHED    
         cld
         scasb
         jz HOP
         add si,1
         jmp HOP
ENDOFTEXT:
         mov si,sMessage3
         call WriteString
         jmp END
FINISHED:
         mov si,sMessage4
         call WriteString
END:          

epilogue                ; macro  

A:

Why don't we start by giving your variables some reasonable names. I don't know about you, but sMessage1, sMessage2, etc. don't convey any information to me.
StringToSearch  db  "Hello World!",0      ;main text
StringToLookFor db  "llo",0               ;searched string
FoundMessage    db  "Searched string was found in main string",0
NotFoundMessage db  "Searched string was not found in main string",0 

That makes things a little easier for me.
You are on the right track, but there are a few problems with your code. First, the instruction cmp di,0 compares the value in the DI register to see if it's 0. What you really want to do is see if the byte pointed to by DI is equal to 0. What you want is:
mov ah,[di]
cmp ah,0
je ENDOFTEXT

That loads the value pointed to by DI into a register and compares it with 0.
You have the same error with the cmp si,0 line, and you need to fix it the same way.
Now, for the actual searching, you have the right idea, but the algorithm is overly simplified. I won't give you a full working program,  because I assume that you're doing this for a class assignment. But I can point you in the right direction.
The basic idea is this:
    textPosition = 0    // start of text
FindFirstCharacter:
    targetPosition = 0  // start of string to search for
    targetChar = stringToSearchFor[0]
    if text[textPosition] == 0 then done (failure)
    if text[textPosition] != targetChar    // scasb
        textPosition = textPosition + 1
        goto FindFirstCharacter

; At this point, textPosition is pointing to the position in the text
; that has the first character of the search string.
; Increment it, and save the position
    textPosition = textPosition + 1
    savePosition = textPosition

; now, we're looking for the following characters of the search string in the text.
SearchLoop:
    targetPosition = targetPosition + 1
    targetChar = stringToSearchFor[targetPosition]
    if targetChar == 0 then done (success)
    if text[textPosition] == 0 then done (failure)
    if text[textPosition] != targetChar    // scasb
        ; characters didn't match
        ; start over at savePosition
        textPosition = savePosition
        goto FindFirstCharacter
    ; Characters matched. Go for the next one
    textPosition = textPosition + 1
    goto SearchLoop

You should be able to convert that to assembly language. Just remember that where I have text[textPosition], you want to put [di], and searchString[targetPosition] is [si].
Note that you need the inner loop because you might match part of the string. For example, consider this text:
Text = abcabdfe
SearchFor = abd

If you just had a single loop, then it would find a and b, but then would fail on c and give up. The algorithm shown above will fail on c and then go back to looking for the first character starting at the c.

