English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 10 May 2018, 01:45   #21
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by Akira View Post
Is there any guide anywhere that explains how to make an OBJ and how to link together?
Most assemblers generate object files by default. Export symbols with XDEF. Import them with XREF. Then link all objects into the final executable:
Code:
linker a.o b.o c.o -o myprogram
Note that program execution starts in the first object on the command line.

Quote:
Also keep in mind I use Devpac, not a newfangled cross assembler
Makes no difference.

Quote:
Originally Posted by StingRay View Post
Includes can and should contain any kind of code. For example, I include the complete system-kill/startup code at the top of my source which makes it much more readable as the startup code is neatly hidden and not interesting to look at anyway when coding effects.
Exactly. That's the same reason for putting these routines into object files. The only difference is: with the object file I have a clearly defined interface (xdef-symbols), while you are cluttering your symbol name space and therefore have to remember all symbol names from your included sources.

Quote:
Why bloat the source with "standard" code when it can be easily included?
Nobody wants to bloat the source with standard code!

Quote:
Originally Posted by meynaf View Post
ReadJoypad.s is probably too small to count for anything in assembly time...
Sure. But this will change when the project grows.

Quote:
Using a linker has a small issue, it is that you'll generate slightly suboptimal code because some information (some of the offsets) will not be known at assembly time and therefore the assembler can't use its full peephole optimization power
Depending on what you put into the objects this may happen, but usually an object file will be a more or less autonomous part of your code. For example trackdisk-routines, protracker-player, joystick/kbd-handler, BOB-routines, etc.. There shouldn't be much references between them and optimization works as usual.

Otherwise I see no suboptimal code at all. You can still call these routines with BSR, or do PC-relative or small-data references, as if the exported symbols had been "included".

Your only issue will be that you might lose 2 bytes between object from time to time, because AmigaDOS hunk sizes are always a multiple of 4.
phx is offline  
Old 10 May 2018, 10:34   #22
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Quote:
Originally Posted by phx View Post
Exactly. That's the same reason for putting these routines into object files. The only difference is: with the object file I have a clearly defined interface (xdef-symbols), while you are cluttering your symbol name space and therefore have to remember all symbol names from your included sources.
Cluttering the symbol name space is easy to fix by simple prefixing of symbols.
On the other hand, in object files you can not make adaptative code (i.e. code that will open only the libs the program really uses, etc). I mean, no if/endc depending on something defined in the main program.


Quote:
Originally Posted by phx View Post
Sure. But this will change when the project grows.
Not when the program grows ; only when the included file itself grows in a significative manner, which is unlikely to happen in this case (and with a fast enough asm on a fast enough machine, it needs to be really big to count : PhxAss on WinUae JIT can assemble a 1MB asm source in less than 1 second !).


Quote:
Originally Posted by phx View Post
Depending on what you put into the objects this may happen, but usually an object file will be a more or less autonomous part of your code. For example trackdisk-routines, protracker-player, joystick/kbd-handler, BOB-routines, etc.. There shouldn't be much references between them and optimization works as usual.
This is why i wrote "slightly" before "suboptimal"


Quote:
Originally Posted by phx View Post
Otherwise I see no suboptimal code at all. You can still call these routines with BSR, or do PC-relative or small-data references, as if the exported symbols had been "included".

Your only issue will be that you might lose 2 bytes between object from time to time, because AmigaDOS hunk sizes are always a multiple of 4.
If the linker generates different hunks for different objects, then it's more than 2 bytes from time to time. There is no possible BSR anymore, only JSR with a reloc for every call. If the included file is, say, a memory manager for example, every alloc/free will generate a long call with a reloc...

In addition, attempts to have them called with PC-relative modes may fail in big projects, where the called module is at the limit of the allowed range (a likely story in a large program where the included code can be called from anywhere). What happens if the linker wants to write 16-bit PC offset but the distance is >32kb ? A good asm can adapt to this. But even the best linker in the world can't do better than generating an error.
meynaf is offline  
Old 10 May 2018, 13:01   #23
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
Quote:
Originally Posted by meynaf View Post
On the other hand, in object files you can not make adaptative code (i.e. code that will open only the libs the program really uses, etc). I mean, no if/endc depending on something defined in the main program.
No, because there is an even better way: it's called linker-library!

Just put your routines, which you want to link on demand, in separate object files. Then put all these objects into a library - e.g. myroutines.lib. When linking your main program with -lmyroutines only those objects will be included where the linker has seen references to its symbols.

And for automatically opening/closing libraries there is a linker-feature called constructor/destructor lists. For example, your program references GfxBase and the linker pulls an object into the linking process which adds a constructor pointer for opening graphics.library. The only thing you have to do is processing the constructor/destructor function pointer lists in your startup code.


Quote:
If the linker generates different hunks for different objects, then it's more than 2 bytes from time to time.
The linker only does what you tell it to do. Sections with the same name from two different objects will be merged.

Quote:
There is no possible BSR anymore, only JSR with a reloc for every call.
Not true. See above. Just give all your code sections the same name and happily use BSR and PC-relative addressing between the objects.

Quote:
In addition, attempts to have them called with PC-relative modes may fail in big projects, where the called module is at the limit of the allowed range (a likely story in a large program where the included code can be called from anywhere). What happens if the linker wants to write 16-bit PC offset but the distance is >32kb ?
The linker will generate an error. You will have the same error in a monolithic assembler source, as long as your assembler doesn't do automatic PC-relative to abs32 translations. But usually you want to know about these cases and fix them.

Quote:
A good asm can adapt to this.
Right. Often it will be sufficient to shuffle some objects around on the command line.

As a conclusion for Akira I can only say he has to make his own experiences and find his personal best approach. As we can see there are several options, and as they are used by excellent coders like Stingray and Meynaf since many decades they cannot be all that wrong.

Everybody has to find his personal priorities.

Last edited by phx; 10 May 2018 at 13:02. Reason: typo
phx is offline  
Old 10 May 2018, 13:39   #24
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Quote:
Originally Posted by phx View Post
No, because there is an even better way: it's called linker-library!

Just put your routines, which you want to link on demand, in separate object files. Then put all these objects into a library - e.g. myroutines.lib. When linking your main program with -lmyroutines only those objects will be included where the linker has seen references to its symbols.

And for automatically opening/closing libraries there is a linker-feature called constructor/destructor lists. For example, your program references GfxBase and the linker pulls an object into the linking process which adds a constructor pointer for opening graphics.library. The only thing you have to do is processing the constructor/destructor function pointer lists in your startup code.
It's not only about routines put on demand and init/exit code. It's about real adaptative code, with constants set in the main program and used in the included part, that change something there. F.e. you call some of your internal API but don't use one of its feature - the entry point is there but the routine's code will change.


Quote:
Originally Posted by phx View Post
The linker only does what you tell it to do. Sections with the same name from two different objects will be merged.
That may spare some relocs but won't solve the problem for routines that end up out of reach with relative modes.


Quote:
Originally Posted by phx View Post
Not true. See above. Just give all your code sections the same name and happily use BSR and PC-relative addressing between the objects.
But I don't want to do that. I want the asm to choose between BSR and absolute JSR automatically.


Quote:
Originally Posted by phx View Post
The linker will generate an error. You will have the same error in a monolithic assembler source, as long as your assembler doesn't do automatic PC-relative to abs32 translations. But usually you want to know about these cases and fix them.
You don't need PC-relative to abs32 translations, actually, quite the opposite. Just write JSR and let the asm decide if it's BSR.S, BSR.W, BSR.L, or JSR abs32. This is what i'm doing, and in a macro. A linker can't replace that.


Quote:
Originally Posted by phx View Post
Right. Often it will be sufficient to shuffle some objects around on the command line.
May work for a project that's right at the limit. But not for very large projects. I prefer solutions that work everywhere.


Quote:
Originally Posted by phx View Post
As a conclusion for Akira I can only say he has to make his own experiences and find his personal best approach. As we can see there are several options, and as they are used by excellent coders like Stingray and Meynaf since many decades they cannot be all that wrong.

Everybody has to find his personal priorities.
Sure everyone has personal preferences.
But someone like Akira needs the simplest solution to start with. Fiddling with linkers is too much to learn at once i'm afraid - and looks overkill for simple ReadJoypad.s.

There is something to learn here though : no two programmers will ever agree on how things must be done
meynaf is offline  
Old 10 May 2018, 15:59   #25
Amiga1992
Registered User
 
Join Date: May 2001
Location: ?
Posts: 19,645
Quote:
Originally Posted by phx View Post
As a conclusion for Akira I can only say he has to make his own experiences and find his personal best approach. As we can see there are several options, and as they are used by excellent coders like Stingray and Meynaf since many decades they cannot be all that wrong.

Everybody has to find his personal priorities.
Yes, and I appreciate the discussion you guys have had here over this matter. Truly each one has their way of doing things and despite most of this going over my head at the moment because my level of knowledge in the matter is very low, I like to read the conversation and will be able to reference back to it when I need it!

Maybe unlike other people, I don't like to get offered "the simplest solution" only, I like to read all the options and then do a comfortable average of what I researched as my solution. So this thread has been very educational with the perspective of three experienced programmers.

For the time being I will probably not deal with linking because I can barely compile :P but it seems like a powerful tool to consider in the near future.
Amiga1992 is online now  
Old 03 June 2018, 20:14   #26
Crank
Registered User
 
Join Date: Mar 2018
Location: Prague
Posts: 35
I decided to learn how to use joystick. It required reading lots of manuals and hunting down some example codes before I managed to get anything working. To make life easier for the other newbies like me, here's a simple example how to read joystick:

------

#include <stdio.h>

#define REG_CIAA_PRA_R *((volatile const unsigned char*) 0xBFE001)
#define REG_CIAA_DDRA *((volatile unsigned char*) 0xBFE201)
#define REG_JOY1DAT_R *((volatile const unsigned short*) 0xDFF00C)


int main()
{
// Clear bits 6 & 7 in CIAA DDRA --> PRA0 bits 6 & 7 are in input mode (fire buttons)
REG_CIAA_DDRA &= ~((1 << 6) | (1 << 7));

while (1)
{
const unsigned short joystick_bits = REG_JOY1DAT_R;
const unsigned short joystick_vert_bits = 2*(joystick_bits & 0x0101);
const unsigned short joystick_up_down = (joystick_bits & 0x0202) ^ joystick_vert_bits;

if (joystick_up_down & 0x0200)
printf("UP ");

if (joystick_up_down & 0x0002)
printf("DOWN ");

if (joystick_bits & 0x0200)
printf("LEFT ");

if (joystick_bits & 0x0002)
printf("RIGHT ");

if (!(REG_CIAA_PRA_R & 0x80))
printf("FIRE ");

printf("\n");
}

return 0;
}
Crank is offline  
Old 03 June 2018, 23:03   #27
OmegaMax
Knight Of The Kingdom
 
OmegaMax's Avatar
 
Join Date: Feb 2016
Location: It's a bald world!
Posts: 179
I'm handling JoyStick Pressed very simply in a tutorial I'm writing


Code:
  move.w $dff00c,d0
  move.w d0,d1
JoyStickRight:  
  btst #1,d0
  beq JoyStickLeft
  add.w #2,Bob_XPosition
JoyStickLeft:
  btst #9,d0
  beq JoyStickUp
  sub.w #2,Bob_XPosition
JoyStickUp:
  lsr.w #1,d1
  eor.w d1,d0
  btst #0,d0
  beq JoyStickDown 
  add.w #2,Bob_YPosition
JoyStickDown:
  btst #8,d0
  beq JoyPressedDone
  sub.w #2,Bob_YPosition
JoyPressedDone:
  rts
OmegaMax is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
implement others 68000 machines ? turrican3 support.WinUAE 28 23 November 2017 00:54
Joystick reading code borchen Coders. Asm / Hardware 57 23 November 2014 14:08
Req: Tanks 'n' Stuff Src Code kevingpo request.Old Rare Games 1 07 July 2003 12:07
Should I implement PM download now ? RCK project.EAB 12 08 January 2002 01:21

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 15:52.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.10601 seconds with 13 queries