flat assembler
Message board for the users of flat assembler.

Index > Main > Arrays in FASM

Author
Thread Post new topic Reply to topic
Killswitch



Joined: 21 Jan 2006
Posts: 20
Killswitch
Hey,

How do you create an array of bytes with 30,000 elements in FASM?

Cheers
Post 03 Dec 2007, 13:51
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Code:
buf rb 30000
    
Post 03 Dec 2007, 14:33
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
as above or, if more modularity needed:
Code:
array:
dd @f-$-4 ; size of the array
rb 30000  ;array
@@:        ;anonymous label, only for size purpose
    

this is an advanced array structure
the first dword, 4bytes, is for size.
if the array is allocated dynamically, using paging for it's segment descriptor, then it can be increased.
Code:
array:
dd @f-$-4
dd .xl    ;two dimentions array
dd .yl
rb ?
@@:
    


Code:
array:
dd @f-$-4
rd N  ;N dimention array
rb ?
@@:
    


an array can be created at compilation or while execution.
above it's for compile time creation.
for real time creation , you need to specify the size of each dimentions, then the routine will ask for memory allocation or create without any ask.


Last edited by edfed on 03 Dec 2007, 19:40; edited 1 time in total
Post 03 Dec 2007, 14:33
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
edfed: why would you store size of array in first dword? What for? It is compile time constant 30000, no need to save it in variable.
Post 03 Dec 2007, 14:56
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
first dword is a tag, a label, or else...usefull for memory management.
if the array is accessed with a ptr, if many arrays are defined for the same routine, you need to have the size value defined in memory, not only equates.
to resize the array too.
obtain an overview of the array by the dword size, etc etc...

accessing an array force to add 4 to the pointer.
Post 03 Dec 2007, 15:02
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
No anymore Wink
Code:
dd @f-array ; size of the array 
array: 
rb 30000  ;array 
@@:        ;anonymous label, only for size purpose 
    


Now you access its size from array-4 so the user don't worry about anything and the memory handling routines access the "secret" size field.
Post 03 Dec 2007, 15:34
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
it's a good idea, but no..

the array is complete when it's size is inside.
array is a file, a file must be easylly stored/loaded on disk/memory, it's label, "array", is for the full array, size and other field included.

as you want... now, killswich, you have many possibilities.

note:
if we want to make a "standard" fasm array definition, to easy share code, link it etc... we need to find a consensus.
who is for size inside array?
who is for size outside array?
who is for else?
what about aditional fields?

Code:
array:
dd @f-$-4 ;size of dim field, 32bits
rd Ndim
@@:
dd @f-$-4 ;size of full array,32bits
rb ?          ;array, can be in bytes, words dwords, as you want
@@:
    


user shall respect a minimum coding constraints in ASM!
if they want easy and fast coding ways, they just have to choose an other programming language.

personnaly, i always use this method to define arrays.
and it works very well

in adition to array, i "always" create an equate field.
Code:
mov esi,array
call arrayexplorer
...
arrayexplorer:
push esi ;save current array...
mov edx,[esi+array.size] ;how many dimensions*4?
add esi,[array.dims]
mov ecx,[esi+edx] ;how many bytes in array?
add esi,edx
add esi,[array.array]
;now esi point to array.
...
xxx [esi+?],x
xxx x,[esi+?]
...
pop esi  ;returns array
ret
array:
.size=0
.dims=4  ;same 
.array=4

dd @f-$-4
rd Ndim
@@:
dd @f-$-4
rb ?
@@:
    


i know it's not a very beautyfull code, but it's simple to write..
Post 03 Dec 2007, 16:10
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3056
Location: vpcmipstrm
bitRAKE
LocoDelAssembly wrote:
No anymore Wink
Code:
dd @f-array ; size of the array 
array: 
rb 30000  ;array 
@@:        ;anonymous label, only for size purpose 
    


Now you access its size from array-4 so the user don't worry about anything and the memory handling routines access the "secret" size field.
I use this method also. It makes array access in a loop easier:
Code:
    mov ecx,[esi-4]
.1: cmp byte [esi+ecx],80h
    je DoSomething
    dec ecx
    jns .1    
The logic for me was that the pointer to the actual data is used much more frequently, and the encoding uses a sign-extended byte - supporting up to 128 bytes of array header space. Nothing prevents subtracting to point to header address if needed, but that is rare.
Post 03 Dec 2007, 17:21
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
edfed: Yes, size of array in dword before array might be useful for generic routines that work with array. But that is only in case you decide to design your code for such (custom) style. It is one of many possibilities for type-safe(r) programming.

But why the hell do you put it as answer to "how to declare array in FASM"? You are just overcomplicating very simple stuff by adding your custom constructs to it as if it was default, normal and/or required way.

Your code example is NOT an array. It is some higher level wrapper for array. Please do not confuse newcomers by adding your own complexity to it.
Post 03 Dec 2007, 17:42
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
ok, but i want to know, why do you always contradict everybody?
to force them to reflect more? or simply because like me, you cannot accept datas from others?

all in all, i agree with you for all that you say, but i agree with me.
my custom array structure is to use in file systems, memory allocations, 3d universes, window( not MSwindows) structures, and 2d Sonic the hedgehogg levels if i code one.

but yes, simply defining array as a string of bytes, without any custom structure is possible and better for some static case, like a codepage for keyboard, a fixed size font, etc...

Wink
Post 03 Dec 2007, 18:00
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
edfed: in this particular case, i already told you reason. I think answer to his question was as simple as one line, and I think your extra info (which he didn't ask for) only confused him. And this isn't first time, something like that happened.

If you would write "this is my own wrapper structure for array, which I think is very useful", then it would be fine. But this time it appears, especially to someone new, that what you write is way to create arrays in ASM.

Rule of thumb: only answer what people ask you about. I am pretty sure that he didn't want to hear about paging or segment descriptors.

about "arguing with everyone": i think people should strive to be as good as possible. If I believe someone is wrong, I feel it as my duty to correct mistake. Yes, it might be annoying, but in the end, either he or I learn something new, and that's good, even if I become asshole in people's eyes.
Post 03 Dec 2007, 18:54
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
exactlly what i think.
it's a little confusing for newbee, but at least, he/she will see that the difference between simple array and complex array is minimal, as just some ptr.

i'm sorry! i've made a big mistake by reply 3211244 lines for this very simple question.

knowledge is like butter, the less you have, the more you spread it.
Post 03 Dec 2007, 19:08
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
i'm sorry! i've made a big mistake by reply 3211244 lines for this very simple question.

It would be fine, if you wrote that it is just an extra option, not "this is an array". Anyway, screw this, we both understand now.
Post 03 Dec 2007, 19:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3056
Location: vpcmipstrm
bitRAKE
I like the way discussions evolve to expand on a topic. So, if one were to search the board for "array" then several implementations and ideas about arrays can be explored. Often I search in just such a manner and am thankful others are able to share their discovery and the time they have taken to do so.
Post 03 Dec 2007, 19:54
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
by this way, we (all active fasm forumers) will forcelly arrive to a very good asm reference programming guide, and possibly a FASMOS will born, one day.
Post 03 Dec 2007, 20:03
View user's profile Send private message Visit poster's website Reply with quote
Hayden



Joined: 06 Oct 2005
Posts: 132
Hayden
at the end of the day an array is just a string of 1s and 0s, nothing more, nothing less!

_________________
New User.. Hayden McKay.
Post 07 Dec 2007, 11:20
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.