News:

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

How to assign variables in My code ?(.code section)

Started by Rainstorm, April 04, 2007, 09:32:30 PM

Previous topic - Next topic

Rainstorm

hi.

how do i assign variables in my code itself (in the .code section) as & when needed ?
- on the fly so to speak while the app is running.
I just need a storage area for the words, which can be refrenced later.

for example    .data

data_string    db "hello there all of you its late",0
search_string  db "there you late",0

    .code
start:
   mov esi, offset search_string
   sub esi, 1   

   seperate_words:
   add esi, 1
   cmp byte ptr [esi], 32
   jne seperate_words
   if equal..   <------ here I would branch out to the section where I'd copy each
                     character of that word into a storage area after a space is found
                     the number or varriables i'd need would depend on the number of words.



in this example the input is from the .data section but it could be a line from a file or anything.
the number of variables i'd need would depend on what the app is proccessing at that time.
how do i do this ?

Thankyou

Rainstorm
-

BogdanOntanu


Use GlobalAlloc or Virtual Alloc to allocate some memory at runtime (dynamic).
Store the obtained pointer into a variable in .data section
Use this pointer to access many dynamic search strings
Parse command line and store options into this allocated area

Alternatively for a quick test you could simply reserve a huge area in your .data? section
my_storage db 1024*1024 dup(?) ; reserve 1Megabyte

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

Rainstorm

Bogdan,

thanks for the reply that was very helpful
have some more questions, - after each word is stored in memory how exactly do I quickly refrence each word ? - do i remember the length ? or do i cycle through the memory looking for a seperator between each word ? - for example if i want to access the 2nd or 3rd word

I tried this code with the alloc() function but there's something wrong with it as i get the win error msg at the end.
    .data

data_string    db "hello there al its latel",0
search_string  db "there all late",0
match_count    dd 0
hmem           dd 0

    .code
start:
   mov esi, offset search_string
   mov hmem, alloc(1048576)

   xor ecx, ecx
   xor esi, esi
   sub esi,1
   seperate_words:
   add esi, 1
   cmp byte ptr [esi], 0
   je exit_
   cmp byte ptr [esi], 32
   jne store_words
   xor ecx, ecx
   xor eax, eax
   jmp seperate_words

   store_words:
   mov bl, [esi]
   mov byte ptr [hmem+ecx], bl
   add ecx, 1
   jmp seperate_words

exit_:
    mov byte ptr [hmem+ecx], 0
    print "string - "
    print hmem,13,10
    inkey
    free hmem
    exit

end start


hutch--

If it is storing initialised string data at a procedure level it is only in fact a method of attributing a LOCAL variable to string data that is normally placed in the .DATA section.

In manual code it looks something like this.


.data
  mystring db "mystring data",0

.code

YourProc proc etc ....

    LOCAL pMyString:DWORD

    mov pMyString, OFFSET mystring



MASM32 has a macro "sas" that automates some of this for you.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm

thanks, but i was looking for a way to store words & being able to later access each of them where, at the time of writing the code i wouldn't know how many words there might be. - like tokens.

Rainstorm.
-

hutch--

Have a look at the "wtok proc pText:DWORD,pArray:DWORD" procedure in the latest version of the MASM32 library. If you don't already have it get the latest service pack in the masm32 sub forum. Its easy enough to use as long as you read the documentation in the help file.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm

 been away a while, didn't know about the service pack.
thanks.
-

Rainstorm

can someone tell me what's wrong with this code ?
it assembles proper but i get the windows 'end report' error msg

   

    .data
search_string  db "there all",0
hmem           dd 0

.code
start:
   mov esi, offset search_string
   mov hmem, alloc(1048576)
   mov edx, hmem

   xor esi, esi
   xor ecx, ecx
   sub esi,1

   seperate_words:
   add esi, 1
   cmp byte ptr [esi], 0        ; check for zero terminator
   je exit_
   cmp byte ptr [esi], 32
   jne store_words
   jmp seperate_words

   store_words:
   mov bl, [esi]
   mov byte ptr [hmem+ecx], bl
   add ecx, 1
   jmp seperate_words

exit_:
    mov byte ptr [hmem+ecx], 0
    print "string - "
    print hmem,13,10
    inkey

    free hmem
    exit
end start

ramguru

   mov esi, offset search_string
   mov hmem, alloc(1048576)
   mov edx, hmem

   xor esi, esi --> ALERT!!!!!!!!!! now esi point nowhere (delete this line)


Rainstorm

ramguru,
  I removed that line you mentioned & its still happening  : /

-
[EDIT] its to do with the print hmem,13,10 line since if i comment it out it doesn't happen.

ramguru

replace
mov byte ptr [hmem+ecx], bl
with
mov byte ptr [edx+ecx], bl

Rainstorm

ramguru,
thanks a lot, works proper now.

don't know the reason why though.

mov byte ptr [hmem+ecx], bl      is valid syntax too....right ?

rainstorm

Rainstorm

sry my mistake, i get it now, am destroying the pointer when i shift the value of bl into it.
was thinking in terms of the usual variable ;)

ramguru

...because hmem is already a pointer with mov byte ptr [hmem+ecx], you are making it pointer one more time (**), with search_string I gues this would be alright