News:

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

question

Started by falcon, May 21, 2005, 04:16:50 PM

Previous topic - Next topic

falcon

  ok...i want to ask that how will i save a string or a value for future use ? like forexample i made a edit box & the user typed something & i want to save what he typed in a place so that i can use it later ?..i think i can use something like this
in my .data ? section

.data?
value   dd    ?

            thanks in advance  :)

hutch--

Basically you write either the string or the value to memory, the question is what type of memory do you require and what scope does the write have to have.

Within a proc, you use a LOCAL otherwise you allocate space in the .DATA or .DATA? sections.


.data?
value dd ?
buffer db 128 dup (?)
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

falcon

     say if i want to save a string then what will i use ?
this
value dd ? or this ?
buffer db 128 dup (?)
          and whats the difference between the two ?...
       
ps:sorry for late reply :)

Robert Collins

I don't think you would normally save a string data in a double word (dd) so if your string data is 128 bytes or less then wouldn't you want to save it in a db 128 dup (?). Seems that way to me. Now, since you want to use it later then you should save it in an area defined globally in the .data or .data? sections, that is, if you want to make it simple and clear.

AeroASM

You will need to use the db 128 dup (0), or maybe more. Don't forget, a string is only a list of bytes. In C it gets confusing because strings are almost always handled as pointers which are size dword, hence th value dd 0.

Mark Jones

Falcon, the DD == a DWORD == a Double-Word == two sets of two bytes. DB is bytes, DW = words (two bytes), DD = double words (four bytes), DQ = quad word (eight bytes), etc. When you write:


.data
    myVar1  DD  0
    myVar2  DD  0


myVar1 and myVar2 are dword-sized pointers to dword-sized elements. myVar1 is actually a static value of 00403000 and since it is four bytes long, myVar2 is 00403004. These then are pointers to standard memory locations. If you put some immediate values into those memory locations like this:


    mov myVar1, 5678h
    mov myVar2, 1234h


then the resulting dump of memory would look like this:


address       myVar1        myVar2              unused
00403000:  78 56 00 00   34 12 00 00   00 00 00 00   00 00 00 00


The code produced is this:


00401001:  C705 00304000> MOV DWORD PTR DS:[403000],5678
0040100B:  C705 04304000> MOV DWORD PTR DS:[403004],1234


Therefore, data defined as "DD" are used as a pointers to DWORD-sized elements. Strings are also DWORD pointers, but are used much differently. Imagine this:


.data
    myStr1  DB "123",0
    myStr2  DB "Hello World!",0
    myStr3  DB "Hutch is a cool guy.",0


All strings must be defined as DB - bytes. The first string is 4 bytes long (ASCII 1,2,3, and a 00) so will appear to be DWORD in size. But the second and third strings are much larger. When we decompile the executable, this is the contents of the .data section:


00403000:  31 32 33 00  48 65 6C 6C    123.Hell
00403008:  6F 20 57 6F  72 6C 64 21    o World!
00403010:  00 48 75 74  63 68 20 69    .Hutch i
00403018:  73 20 61 20  63 6F 6F 6C    s a cool
00403020:  20 67 75 79  2E 00 00 00    guy....


We can see the first "string" is present from address 00403000 to 00403003, then myStr2 begins at 00403004, and myStr3 at 00403011.

So, if you want to put an arbitrary length of data into a "string" location, then the number of bytes reserved is of course important. It is possible to overwrite other "strings" if not careful. Often string space is reserved like this:


.data
    myStr5  DB  256 dup(0)


In this case, myStr5 is a pointer to 00403000, and the memory is blank up to 00403100, so 255 bytes can be placed in there. Because "myStr5" is just a pointer and has a literal value of 0040300h, we must use a prefix in MASM to indicate how it should access the string otherwise it will try to use the pointer's literal value and crash. That prefix is ADDR. If we wanted to display our strings in a messagebox:


.code
    invoke MessageBox, 0, addr myStr1, addr myStr2, MB_OK
    invoke ExitProcess, 0


Now to get a user-input string into myStr5, follow this:


.data
    szTemp  DB  64 dup(0)    ; a temporary buffer
    myStr5  DB  256 dup(0)   ; our big temporary buffer
.code
; fetch up to 63 bytes of text from control 101 into szTemp
    invoke GetDlgItemText,hWnd,101,addr szTemp,63
; copy the string data from szTemp to myStr5
    invoke lstrcpy,addr myStr5,addr szTemp


Please see the ASMINTRO.HLP file and download the WIN32.HLP file for more information.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

falcon

 thanks guys....i finally understood it  :bg.