Link to home
Start Free TrialLog in
Avatar of garisoain
garisoain

asked on

Strings and Arrays on Assembler

Hi there, well, i'm programming a little (and very basic) Pascal-to-Assembler compiler on Pascal (Delphi), but i'm kinda lost about string and 2-Dimensional array management...

what i need is to know how to 'translate':

-simple string operations like:
x:='string';   or
x[y]:='<character>';

-simple 2D array operations like:
x[y,z]:=<value>;

to Assembler Code.

i'm done now with lexic, sintactic, and semantic part of the compiler, and can manage 'for','while','repeat', and basic integer, real, and byte operations, but still lost with strings and arrays. (well, read(string) and write(string) is working too.)

step-by-step instructions are welcome since i'm not an assembler expert, and reffering to simple tutorials too.

Thanx in Advance...
Avatar of SunBow
SunBow
Flag of United States of America image

?
Basically, string is fixed size (possible)
You give it address, name, number of bytes,
possibly initializing to a specific value (either for default or debug)
That is data part.
Code part is using that address to access that string.
Good ides to have another item to size the string (number) and/or limit it (nul at end).

Depending on language and platform, quite often we'll toss the 'string' onto a 'stack' form. Difference is how you approach the definition for limitation of string. Defining up front with names and preassigned bytes, the length is limited by predesignation. For flexibility, the string is not defined until runtime, and all strings get to use a common store. This means of course, less space used for small names, longer strings permitted, but a lot of overhead trying to keep track of who has what space, managing free space, and garbage collection.

Another method would allocate with linked list. More overhead, but easier to manage in coding it up.

Any sense?

A lot depends on the underlying platform and the needs of the programmer.

For running dimensions and not caring on space or efficiency, consider linked list {a,b,c,d,link}{x,y,z,link}. Have pointers off on the side.

For efficiency, decide early which goes first (rotates). So 0,0,0,3  has rotation on right, so you run it up to end, then begin on next column.

Anything variable length gets either special character termination (<nul>) or neat trick, 1st datum entry (zero-eth) has the length. This is managed at runtime.
Avatar of garisoain
garisoain

ASKER

=/ sorry, but i don't get it...

any explained example???
ASKER CERTIFIED SOLUTION
Avatar of Neutron
Neutron

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A string in Pascal is always 255 unless declared otherwise ( string[n] ).
in:
s:='Hello';
(s is a variable with reserved place)
simply copy 'Hello' to the offset of s.
another tip: the first byte of pascal strings ( s[0] ) is they're size.
for example, 'hello' in memory is chr(5)+'hello'.

As for 2 dimentional array or more...
do the same u do on 1dim arrays, only multiply the values:
a[10] is offset a+10
a[10,5] is offset a+10*5

I hope I helped
  Erez Sh.
In example of assigning a single char at given index in string, it should be

mov   si, OFFSET x_LENGTH


instead of


mov   si, OFFSET x_DATA


because Pascal (at least BP/Delphi) allows access to element 0 just like any other character in a string.

If you use any of this, please make this correction.

Greetings,
    Ntr:)
Thanx to you all!!!

Now I know how to deal with this. =)