The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Dasar on June 13, 2006, 10:49:37 PM

Title: how to assign a string to var
Post by: Dasar on June 13, 2006, 10:49:37 PM
hi

i know how to define an initialized string , like:

.data
MyString db "hi, how are you ? ",0



but i want to delcare a var, and then in the code assign some value to it...

for example:

.data?

MyUnKownString db 255 dup(?)  ; string of 255 char

.code
  begin:
    mov MyUnkownString, "hi how are you ?"
    invoke MessageBox, 0, Addr MyUnkownString, addr SomeCaptoin, MB_OK
end begin


i know that i can't mov from memory to memory location, and i have to solutions, using stack(push & pop), or using registers....


and i tested some other things like

mov eax, MyUnknowString

&

mov edi, MyUnkownString

etc...

i tried alot , but can't get it to work   : '(


any help is appreciated ...
Title: Re: how to assign a string to var
Post by: mnemonic on June 13, 2006, 11:42:53 PM
If you want to get text into your prog at runtime there are two simple Methods:
Then
Title: Re: how to assign a string to var
Post by: Mark Jones on June 14, 2006, 12:36:43 AM
Quote from: Dasar on June 13, 2006, 10:49:37 PM
.code
  begin:
    mov MyUnkownString, "hi how are you ?"
    invoke MessageBox, 0, Addr MyUnkownString, addr SomeCaptoin, MB_OK
end begin

Hi Dasar. Mnemonic is on the right track here. Remember that in assembly, all memory is only bytes and pointers. MOV is an elementary instruction, and its only operands types are an immediate value, a memory pointer, or a register. MOV is of course, not smart enough to understand "hi how are you ?" as an operand :bg

If you're writing a Windows GUI application, a commonly used method of getting a string of text is with the GetDlgItemText API. Ex:


    invoke GetDlgItemText,hWnd,1002,addr szSearchQuery,255


This fetches up to 255 bytes from an edit box with the ID of 1002 and stores those bytes at string pointer szSearchQuery. Lookup GetDlgItemText in the Win32.hlp file or at MSDN for more info. :U
Title: Re: how to assign a string to var
Post by: dsouza123 on June 14, 2006, 01:05:27 AM
This uses the szCopy function in masm32.lib, look at masm32.inc for a list of string functions.
Example code for many string functions are in string.asm, one of the examples from MASM32.


  .486                      ; create 32 bit code
  .model flat, stdcall      ; 32 bit memory model
  option casemap :none      ; case sensitive

  include \masm32\include\windows.inc
  include \masm32\include\masm32.inc
  include \masm32\include\gdi32.inc
  include \masm32\include\user32.inc
  include \masm32\include\kernel32.inc

  includelib \masm32\lib\masm32.lib
  includelib \masm32\lib\gdi32.lib
  includelib \masm32\lib\user32.lib
  includelib \masm32\lib\kernel32.lib

.data
  MyString    db "hi, how are you ? ",0
  SomeCaption db "Caption text",0
.data?
  MyUnknownString db 255 dup(?)  ; string of 255 char

.code
begin:
  invoke szCopy,addr MyString,addr MyUnknownString   ; source,destination
  invoke MessageBox, 0, Addr MyUnknownString, addr SomeCaption, MB_OK
  invoke ExitProcess, 0
end begin
Title: Re: how to assign a string to var
Post by: Dasar on June 14, 2006, 08:33:18 AM
Thank you all for your replies : )

really, i found a great forum !    : D