Hello,
Can someone please help me out, im pretty new with ASM and i want to know how to create a loop wich checks a string letter by letter.
So if eax contains the word "computer" for example, then i want to create a loop wich checks if the letter "u" is in it.
But if someone could show me how to do a simple loop and how to see the characters one by one.
In c++ you can do something like
cout << bla[5];
that would give the 6th letter that the char bla contains.
I want to know the same with asm, i hope anyone can help me out with this.
Thanks for any help.
Posted on 2005-08-15 05:32:47 by Davidos
I think C++ is pampering you.

In C, you would do something like

int isAinstring(char *c){
  int ii;
  for (ii=0;c!=0;ii++)
    if (c == 'A')
      return 1;
  return -1;
}


It is something similar in asm.

isAinstring proc string:DWORD
  mov ecx, string
@@:
  mov al,
  inc ecx
  test al, 'A'
  jz found
  test al, al
  jnz @B
  or eax, -1
  ret
found:
  xor eax, eax
  inc eax
  ret
endp
Posted on 2005-08-15 07:11:25 by roticv

I think C++ is pampering you.

In C, you would do something like

int isAinstring(char *c){
? int ii;
? for (ii=0;c!=0;ii++)
? ? if (c == 'A')
? ? ? return 1;
? ?return -1;
}


It is something similar in asm.

isAinstring proc string:DWORD
? mov ecx, string
@@:
? mov al,
? inc ecx
? test al, 'A'
? jz found
? test al, al
? jnz @B
? or eax, -1
? ret
found:
? xor eax, eax
? inc eax
? ret
endp



Thanks ill try the asm code when i get home. Im no asm guru so forgive me if im wrong but doesnt your asm code only checks once if it see's the letter A and does it end immidiatly when it see the letter A ?? Cause it should keep looking untill the whole sentence/word is finished. Once again im not sure cause im not that good yet with asm.
Posted on 2005-08-15 13:14:22 by w00
Hi,

Davidos just wanted a loop to check whether a character is found in the string (Oops I should have used 'u' in my example). So my code does just that. If a 'A' is found, it would quit the loop and return with 1. Else, if 'A' is not found, it would scan till then null terminator and return -1.

If you want to return the address or the index of the found character, it is simple too. All you need to do is modify some part of the asm codes.

Actually, my point about posting the C code is to show how similar the asm codes is to C codes. ;)
Posted on 2005-08-15 13:36:55 by roticv
test al, 'A' <-- This is wrong, use cmp al,CHAR
the test instruction will trigger the zero flag whenever the <and al,'A'> is 0, example ' ','0','2','4'...' '
that is, it works if you are sure the character array won't contain any char other than lower/upper letters
...and it should first check for zero byte? ;) (null string)

generally use cmp, you can use <repnz scasb> too

or simply something like this? ;)

StrChr proc pString:DWORD,dbChr:DWORD
mov eax,pString
mov dl,byte ptr dbChr
@@: mov cl,
inc eax
test cl,cl
jz @F
cmp cl,dl
jnz @B
dec eax;; return pointer in string
ret
@@: or eax,-1;; return -1 if not found
ret
StrChr endp

invoke StrChr,addr mystring,'u'
Posted on 2005-08-15 21:46:34 by drizz
Oh yes, you are correct. I'm rusty  ;)
Posted on 2005-08-16 01:46:11 by roticv