News:

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

Problem with a very simple program

Started by Hawker, January 08, 2007, 07:37:04 PM

Previous topic - Next topic

Hawker

Hello everyone,

I am new to the forum -only discovered it yesterday-and I have only started learning asm programming.

i am making a very simple program which takes the users name and displays the number of characters it has (i know it's not a useful program  :red but i am just trying to put some of the things i have learned into practice)

invoke GetDlgItemText,hWnd,IDC_EDITNAME,ADDR NameBuffer,32
invoke lstrlen,ADDR NameBuffer
mov edi,OFFSET OutputBuffer
mov BYTE ptr[edi],al
invoke SetDlgItemText,hWnd,IDC_EDITOUTPUT, ADDR OutputBuffer

this is where the problem is, the program instead of displaying the number of characters displays some strange symbol like an arrow or a square. I'd appreciate if you would nudge me in the right direction on this.

thanks

u

You have to convert the number to ANSI. If the number will be 0..9 , simply add 48 to it. And don't forget to zero-terminate.

add al,48
mov BYTE ptr[edi],al
mov byte ptr[edi+1],0 ; zero-termination
invoke SetDlgItemText,hWnd,IDC_EDITOUTPUT, ADDR OutputBuffer


If the number can be greater than 9, you have to use an integer-to-string convertion proc.
Please use a smaller graphic in your signature.

mnemonic

This is a common mistake that beginners usually do - don't worry.

Realize, that "text" always means a zero terminated string.
A DWORD value as returned by "lstrlen" is just a 32bit number and never ever zero terminated. It contains 32 bits that make up the value. Therefore you have to use SetDlgItemInt which takes a DWORD value and converts it internaly into a string which is then written into the textfield.
However you could write a own function that takes a DWORD value and then converts it to a zero terminated string for exercise purpose. :)

HTH
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

Hawker

Thanks for the replies!

Ultrano:
I tried your method and it is working! but like you said it only works for 0-9 and displays letters for anything larger. and i noticed that it worked even if i don't zero terminate by leaving out

mov BYTE ptr[edi+1],0

mnemonic:
I replaced
invoke SetDlgItemText,hWnd,IDC_EDITOUTPUT, ADDR OutputBuffer
with:
invoke SetDlgItemInt,hWnd,IDC_EDITOUTPUT,ADDR OutputBuffer,FALSE

but now the output is always 4206652 no matter what i enter. where could i be going wrong?

mnemonic

There is no need for a buffer, just use the return value of lstrlen:
invoke lstrlen,ADDR NameBuffer
; string length is in EAX now
invoke SetDlgItemInt,hWnd,IDC_EDITOUTPUT, EAX, FALSE

Always have a look at the documentation of the functions, that is why I link to them. ;)

You always printed the address of OutputBuffer, that's why it was always the same number.

HTH
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

Hawker

Thanks very much! It works like a charm.

I did look up the function and i thought that ADDR OutputBuffer had the interger since i moved al into it so i would use that, but really EAX should have been the first thing to come to my mind. But thanks for the help i'll surely keep visiting these forums for more help.  :U