News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Compute string length

Started by okch86, March 29, 2012, 12:08:36 PM

Previous topic - Next topic

okch86

Kindly i try the following code below to compute the length of string but it doesn't work
when i execute it the program hang and exit

.386
.model flat,stdcall
option casemap:none


include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib

strlo proto :DWORD
.data
msg db "Hello",0

.data?
pr dd ?

.code
start:

invoke strlo,addr msg
mov pr,eax
invoke StdOut,addr pr
invoke ExitProcess,0

strlo proc parm:DWORD
xor eax,eax
l1:
cmp parm,0
je l2
inc parm
inc eax
jmp l1
l2:
ret
strlo endp

end start

BogdanOntanu

#1
Apparently parm is a pointer to a string.
You are comparing the pointer with zero (wrong).

You should dereference the pointer. Read the value that is pointed at (the current string char) and not the pointer value.

Something like this:



mov esi,[param]
xor eax,eax

loop_len:
   mov dl,[esi]
   cmp dl,0
   jz done

  inc esi
  inc eax
  jmp loop_len

done:
  ret

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

dedndave

that is correct - you pass a pointer to the string
the StdOut function measures and returns the string length for you
(actually, it returns the number of bytes written to the console, which will be the same)
you can view the code by looking in masm32\m32lib\stdout.asm
you will see that the function calls StrLen

at any rate, i suspect what you are trying to do is to display the length of the string as a decimal value
        INVOKE  StrLen,offset msg
        print   ustr$(eax),13,10
        inkey
        exit

the print, ustr$, inkey, and exit macros may be found in masm32\macros\macros.asm
the StrLen function may be found in masm32\m32lib\strlen.asm

you can find descriptions in the masm32\help folder...
masmlib.chm
hlhelp.chm