flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > Arrays?

Author
Thread Post new topic Reply to topic
THEWizardGenius



Joined: 14 Jan 2005
Posts: 382
Location: California, USA
THEWizardGenius 15 Mar 2005, 19:23
I know it isn't too difficult to make an array, but it's rather awkward. Couldn't we have a simple tool built in to FASM for making arrays? Like this would become legal:

mov ax, [myarray(3)]

if an array called "myarray" existed with at least 4 items (starting with 0) or 3 (starting with 1). Just an idea that would make FASM less awkward, easier to use, etc - because arrays are used so often for many things, it would be nice if this were possible. There could be arrays of bytes, words, dwords, or single, double, or double extended, and SSE2 128-bits. I don't imagine it being too difficult, and it would make some things much easier, for example modifying portions of a string:

mov [mystrarray(0)],'J'
mov [mystrarray[6)],'uH'
;Jello, Hurld!
mystring db 'Hello World!'
virtual at mystring
mystrarray array ?
end

_________________
FASM Rules!
OS Dev is fun!
Pepsi tastes nasty!
Some ants toot!
It's over!
Post 15 Mar 2005, 19:23
View user's profile Send private message AIM Address Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8358
Location: Kraków, Poland
Tomasz Grysztar 15 Mar 2005, 19:52
First of all: in your sample it's enough to write [mystring+0] and [mystring+6] instead of [mystrarray(0)] and [mystrarray(6)], so it's a bit unfortunate example. And generally it should be something like [array_address+size_of_element*index] - this is straightforward assembly way and this is what I personally find less confusing and easy to use, really.

But if you want some HLL-ish syntax, you might try making some macros. For example:
Code:
macro darray name,type,[value]
 { common virtual
            name#.addr type ?
            name#.itemlen = $-name#.addr
            name equ name#.addr+name#.itemlen*
          end virtual
   forward type value }

macro rarray name,type,size
 { virtual
     name#.addr type ?
     name#.itemlen = $-name#.addr
     name equ name#.addr+name#.itemlen*
   end virtual
   if ~ size eq ?
    repeat size
     type ?
    end repeat
   end if }    

First one defines an initialized array, second one uninitialized. The only limitation is that you have to declare an array before you use it in code. But the usage is just like you wanted:
Code:
; data definition:
darray myarray,dd, 10,7,400,98,321

; data accessing:
mov eax,[myarray(3)] ; gets 98 into eax    

and something like your sample:
Code:
mystring db 'Hello World!'

virtual at mystring
 rarray mystrarray,db,?
end virtual

; data accessing:
mov [mystrarray(6)],'U'    

The only problem can occur, when you want an index from some other variable or register (though in cases of element sizes being powerd of two register will work even with above macros) - then you have to do the address calculations manually - this is assembly, not HLL.
Post 15 Mar 2005, 19:52
View user's profile Send private message Visit poster's website 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.