6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu May 02, 2024 8:33 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Thu Jan 25, 2007 4:39 pm 
Offline

Joined: Thu Jan 25, 2007 4:16 pm
Posts: 3
Hello
I know how to write in asm when I decide where to store variables etc.
The easiest way for me is to write directly with POKE, I am not experienced in assemblers.
Now I want examples of asm code for modular programming using relocatable code.
for instance I need examples howto define hidden and exported variables in .data and .bss segments, I also want to have example of pointer to struct.
In other words I am very interested in examples of howto hide and export variables
encapsulated in .o relocatable. And how to use (import) that variables from another relocatable.
How to pass variable to subroutine in another .o by reference.
May be it will be easier for someone just translate the following oberon source to xa65 or ca65 asm syntax so I could understand

MODULE M1;
VAR a*,b,e- : SHORTINT; (* signed byte*)
(* note that a is exported and could be seen rw from other module *)
(* b is hidden*)
(* e is exported read only*)
PROCEDURE p*( VAR l : INTEGER); (*this procedure is exported*)
(*it could be called from other module *)
(* l is passed by reference or via pointer if you like to call it that way *)
VAR a, f : INTEGER; (* signed int *)
BEGIN
a := 5; (*assigning to local variable*)
b := 7;

END p;

PROCEDURE p1; (* hidden procedure, cannot be called from outside *)
BEGIN

END;

BEGIN

a := 7;
e := 9;
END M1.

------------------------------
MODULE M2;
IMPORT M1;

VAR a : INTEGER;
BEGIN

a := M1.a;
M1.p(a);

END M2


Thank you very much


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jan 25, 2007 4:48 pm 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
This just reaks of "HOMEWORK." SO I'm not going to translate the code. But if you read the ca65 online documentation, you'll see how to expose symbols. Relocation is handled at load (or link, depending on your environment) time.

You can find the ca65 documentation here:

http://www.cc65.org/doc/ca65.html

I will say this though: the assembler and linker are designed to support the C programming environment, so there is no explicit two-level symbol naming system in place. You have to put this in yourself. I personally use underscores in my coding (e.g., Module1_procedure1 or Module2_someVariable2).

I also follow a strict programming style that clearly documents the conventions for naming variables and procedures, etc. This is good no matter WHAT programming language you use. But it's particularly important for assembly language, because you thoroughly lack type safety features at this level.


Thanks.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jan 25, 2007 8:44 pm 
Offline

Joined: Thu Jan 25, 2007 4:16 pm
Posts: 3
kc5tja wrote:
This just reaks of "HOMEWORK."

I had to suppose that someone will think about reaks.
No, honestly 6502 programming is just my hobby since childhood.
I am probably not a master in it, but I definitely can write working code and know all 6502 instructions and part of corresponding hexadecimal numbers by heart.
What I do not understand (or what seems somewhat hard to understand) is assemblers.
I am used to write in assembly language directly coding in hexadecimal instructions.
But I am still fail to understand howto use modern assemblers.
When I am writing 6502 code then I decide where to put all variables and/or pointers.
For instance I am often using zero page when do not want to return to host machine os.
I decide how to deal with procedures, functions, howto use records, I am using something like my own calling conventions.
Besides I better understand x86 assembly for gas rather than xa65 because there are for instance .comm directive which is used to allocate variables in the memory.
How to do the same with xa65 without .comm and without defining its address myself to have relocatable .o file?
How to pass variable by reference to function residing in another object?
I am not sure is it possible on the level of modern assemblers di\efine read only variable in .data segment and where is that data segment allocated anyway.
Please, forget about translation, just give me understandable examples.
Thanks ;)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jan 25, 2007 9:27 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8430
Location: Southern California
Starting out coding by hand is good! I wish more people did that, since it gives a better understanding of the insides of software and even hardware. It makes the assembler a tool to help get the job done, instead of a substitute for learning and something to do the job for you.

If you do much assembly by hand, you will soon run into the frustration of having to adjust branch lengths, jump addresses, addresses referring to variables, etc. every time you need to insert or delete a segment. What I did when I started out was to put a string of three NOPs ($EA) frequently in the code, so I could insert extra lines more easily later on even with a JSR to the new piece if necessary, without moving things around so much.

The assembler makes all this unnecessary and also makes your code more manageable is several other ways. You can still decide where to put your variables or start your code. ".ORG" for "origin" is a common directive used in different assemblers. It is followed by a parameter telling what address you want the following lines to start at. In the case of your ZP variables, you tell it where to start, and then give each a name and tell the assembler how many bytes' space to leave for the variable before assigning the next one.

In the case of the records you mention, you will specify the array similarly, and probably have names for the various records, each name being a constant that tells how many bytes into the array to go to get to the beginning of the particular record. The constant should probably be defined in terms of previous records' lengths so that if you decide to change the length of a previous record, all the ones after it get moved up or down automatically.

All kinds of data structures can be made, depending on how elaborate you want to go. Don't neglect macros. They will enable you to effectively raise the level of the language and make it much more concise, clear, and manageable, without adding any runtime penalty or taking away any control over the internal details.

The exact way it is done will vary from one assembler to another. I have used 2500AD and Universal Cross-Assemblers and also written a couple myself, but am not familiar with the particular ones you mention.

It does seem strange however that you would be trying to jump into relocatable code when you haven't made the transition from hand assembly to using and taking advantages of the various features of a nice macroassembler, let alone a linker.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


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 post attachments in this forum

Search for:
Jump to: