The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Hawker on January 08, 2007, 07:37:04 PM

Title: Problem with a very simple program
Post by: Hawker on January 08, 2007, 07:37:04 PM
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
Title: Re: Problem with a very simple program
Post by: u on January 08, 2007, 07:59:11 PM
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.
Title: Re: Problem with a very simple program
Post by: mnemonic on January 08, 2007, 07:59:55 PM
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 (http://msdn2.microsoft.com/en-us/library/ms645518.aspx) 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
Title: Re: Problem with a very simple program
Post by: Hawker on January 08, 2007, 08:27:51 PM
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?
Title: Re: Problem with a very simple program
Post by: mnemonic on January 08, 2007, 10:19:50 PM
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
Title: Re: Problem with a very simple program
Post by: Hawker on January 08, 2007, 10:52:57 PM
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