flat assembler
Message board for the users of flat assembler.

Index > Main > Array of strings,how to implement?

Author
Thread Post new topic Reply to topic
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 12 Feb 2013, 19:00
I am looking for design and implementation of array of strings in assembly. How do you implement it in fasm? I am looking for examples with null-strings and fixed-length.

From nasm source,I found an example using fixed-strings(see below). But how Tomasz really use it?

I think that something like this?

Code:
mov esi,[preprocessor_directives]
movzx eax,byte [esi] ;length of string
    


Another implementations put the values on heap and I define pointers to it,and accesses using something like this:

Code:
mov eax,[esp+4]
    


etc

Code:
preprocessor_directives:
 db 6,'define'
 dw define_symbolic_constant-directive_handler
 db 7,'include'
 dw include_file-directive_handler
 db 3,'irp'
 dw irp_directive-directive_handler
 db 4,'irps'
 dw irps_directive-directive_handler
 db 5,'macro'
 dw define_macro-directive_handler
 db 5,'match'
 dw match_directive-directive_handler
 db 5,'purge'
 dw purge_macro-directive_handler
 db 4,'rept'
 dw rept_directive-directive_handler
 db 7,'restore'
 dw restore_equ_constant-directive_handler
 db 7,'restruc'
 dw purge_struc-directive_handler
 db 5,'struc'
 dw define_struc-directive_handler
 db 0    
Post 12 Feb 2013, 19:00
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 12 Feb 2013, 20:11
I wouldn't say that in your example it is an array - it's more a table.

In an array all elements have the same (maximum) size.
The table created from Tomasz saves space due to a defined structure.
So the first byte is the length of the following token in bytes, followed by an address of the preprocessor-directive handler.

So if there is a token found and to be processed it is checked with a simple string compare and if found the handler is called. And there is some more restriction, all items have to be in alphabetical order.
Post 12 Feb 2013, 20:11
View user's profile Send private message Send e-mail Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777 12 Feb 2013, 21:52
Text array macros...

Code:
; TEXTS t[]='abc', 'xyz', '123'
; TEXTS t[](4)='ann', 'kim', 'sue'

; create array of literal 'text' + addresses
; example: TEXTA colors, 'Black', 'White'
; access: let ecx=[index], eax=[colors+ecx*4]

macro TEXTA name, [t] {
?nta=0
common
label name dword
forward local l
dd l
?nta=?nta+1
forward l db t, 0
name#.$=?nta
}

; same but align each to size

macro TEXTAA name, size, [t] {
common
?nta=0
label name dword
forward
local l
dd l
?nta=?nta+1
forward
l db t, 0
times (size-($-l)) db 0 ; align
common
name#.$=?nta
}

macro TEXTS [p] {
common
define ?s 0
match name[](size)==t, p \{
 TEXTAA name, size, t
 define ?s 1
\}
match =0 name[]==t, ?s p \{
 TEXTA name, t
 define ?s 1
\}
if ?s eq 0
 'Syntax error'
end if
}    
Post 12 Feb 2013, 21:52
View user's profile Send private message Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 13 Feb 2013, 00:57
shutdownall wrote:
I wouldn't say that in your example it is an array - it's more a table.

In an array all elements have the same (maximum) size.
The table created from Tomasz saves space due to a defined structure.
So the first byte is the length of the following token in bytes, followed by an address of the preprocessor-directive handler.

So if there is a token found and to be processed it is checked with a simple string compare and if found the handler is called. And there is some more restriction, all items have to be in alphabetical order.


Thanks! I get it now. The Tomasz don't use pointers in anywhere really,right?
ie,esp pointing to a array of integers,that are memory address as compilers(C/C++) generate? I am newbie in assembly. May be wrong.

A code example is:
Code:
mov [ebp- 12],DWORD PTR [STR0]
mov [ebp- 8], DWORD PTR [STR1]
mov [ebp -4], DWORD PTR [STR2]
    


This is not fasm compilable,is just an example. gcc compilers generate somethings like this when working with strings. STR0,STR1,STR2 are labels,like this:

Code:
STR0:
.string "etc"
    


in Fasm,I belive that it is equivalent to:

Code:
str0: db "etc"
    


Also,why all items have to be in alphabetical order? because your search algorithm? well,how is it called really? I thought Tomasz use hash in somewhere..

Sorry for too many questions. Embarassed Razz

uart777, Thank you too. Very Happy Woow,the macros in fasm are very powerful. I will learn more about macros in fasm to understand you code and back here.


Last edited by Bob++ on 13 Feb 2013, 01:05; edited 1 time in total
Post 13 Feb 2013, 00:57
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 13 Feb 2013, 01:04
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:32; edited 1 time in total
Post 13 Feb 2013, 01:04
View user's profile Send private message Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 13 Feb 2013, 01:05
HaHaAnonymous wrote:
Quote:

I will learn more about macros in fasm to understand you code and back here.

Then you may want to read this: http://bos.asmhackers.net/docs/FASM%20tutorial/preproc.html
and this http://flatassembler.net/docs.php

Maybe.


Sure. Thanks.
Post 13 Feb 2013, 01:05
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 13 Feb 2013, 01:37
Bob++ wrote:

Also,why all items have to be in alphabetical order? because your search algorithm? well,how is it called really? I thought Tomasz use hash in somewhere..


The alphabetical order is just to use an easy search algorithm I think.
Tomasz uses hash tables for dynamic symbols (labels) but not for static symbols placed in TABLES.INC.
Post 13 Feb 2013, 01:37
View user's profile Send private message Send e-mail Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4352
Location: Now
edfed 13 Feb 2013, 13:58
an array of anything is just:

Code:
array_of_anything:
.size dd @f-$-4 ;size in bytes
.elements:
 dd element1
 dd element2
 dd element3
 dd element4
 dd element1 ;you can point multiple times to the same element
 dd element4
 ...
@@:

element1 db "this is a string",0
element2 db "this is another string",0
element4 db "this string is another element",0
element3 db 0;and this string is empty
    

if is relativelly easy to implement the methods to exploit theses kind of lists.
Post 13 Feb 2013, 13:58
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 13 Feb 2013, 20:42
Well it's not really on area of the data - just an array of pointers.
For me an array is something you defined in BASIC with DIM which holds real data.

DIM A$(100,50)
or
DIM B(16)
or
DIM XY(10,10,10,10)
a four dimensional array ...
Post 13 Feb 2013, 20:42
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20422
Location: In your JS exploiting you and your system
revolution 14 Feb 2013, 02:30
shutdownall wrote:
Well it's not really on area of the data - just an array of pointers.
For me an array is something you defined in BASIC with DIM which holds real data.

DIM A$(100,50)
or
DIM B(16)
or
DIM XY(10,10,10,10)
a four dimensional array ...
What makes you think BASIC doesn't just use an array of pointers like in edfed's post above? Different versions of BASIC interpreters will do things in different ways anyway, there is no one-true-way to do anything. HLL array constructs are not magic things, they also have to use the underlying memory in similar ways to how an assembly programmer will do it.
Post 14 Feb 2013, 02:30
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 14 Feb 2013, 17:27
Yes you are right but you are talking about an array of pointers which is not really an array of data. It's more a directory.

How would you define a two or three dimensional array and how can the members easily accessed ?
Very Happy
Post 14 Feb 2013, 17:27
View user's profile Send private message Send e-mail Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 17 Feb 2013, 03:19
edfed wrote:
an array of anything is just:

Code:
array_of_anything:
.size dd @f-$-4 ;size in bytes
.elements:
 dd element1
 dd element2
 dd element3
 dd element4
 dd element1 ;you can point multiple times to the same element
 dd element4
 ...
@@:

element1 db "this is a string",0
element2 db "this is another string",0
element4 db "this string is another element",0
element3 db 0;and this string is empty
    

if is relativelly easy to implement the methods to exploit theses kind of lists.


Remove .size,.elements and assume this code.

Is array_of_anything's address same as element1? if not,to where is the label pointing really?
Post 17 Feb 2013, 03:19
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1660
Location: Toronto, Canada
AsmGuru62 17 Feb 2013, 12:27
@Bob++: so you need an array, which can dynamically change?
Like add some elements and remove some as needed?
I would use the private heap for this - WinAPI provides Heapxxx functions for this purpose.
Also the # of heaps created for a process is limited only by memory left within a process 2Gb room.
Post 17 Feb 2013, 12:27
View user's profile Send private message Send e-mail Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++ 21 Feb 2013, 01:27
AsmGuru62 wrote:
@Bob++: so you need an array, which can dynamically change?
Like add some elements and remove some as needed?
I would use the private heap for this - WinAPI provides Heapxxx functions for this purpose.
Also the # of heaps created for a process is limited only by memory left within a process 2Gb room.


No,it's static.I did the question because I was thinking in a different search-algorithm. I was confusing with label address..
Thaks for your answer!
Post 21 Feb 2013, 01:27
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.