English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 05 February 2020, 23:56   #1
Pixelfill
Registered User
 
Join Date: Dec 2019
Location: Preston
Posts: 100
Question noob question

So I'm just starting out, and fascinated by the 68000 optimisations thread

which got me wondering, I've seen numerous ways of clearing registers, and was wondering which of the following is the recommended way to clear a data register.

Code:
sub.l dn, dn 

moveq.l #0, dn 

eor dn, dn 

clr.l dn
similarly which is "best" for an address register.

Code:
movea.l #0, an 

suba.l an, an 

moveq.l #0, dn 
move.l dn, an 

eor dn, dn 
move.l dn, an 

clr.l dn 
move.l dn, an
I appreciate "best" is subjective, and affected by context, but I would appreciate some insight into the pros and cons of each approach, and what any side-effects may be (clobbered registers / flags etc.) I'm sure there are other alternative approaches too …

I'm very green when it comes to 68K assembler, so this is probably way too deep a topic for me, but as I said the optimisation thread piqued my interest.

thanks

Mike
Pixelfill is offline  
Old 06 February 2020, 01:13   #2
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,556
Code:
        moveq   #0,Dn
        suba.l  An,An
phx is offline  
Old 06 February 2020, 09:12   #3
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
This should be
Code:
        moveq   #0,Dn
        move.l  D0,An
But this is the same as
Code:
        suba.l  An,An

Last edited by sparhawk; 06 February 2020 at 12:40.
sparhawk is offline  
Old 06 February 2020, 10:37   #4
Cyprian
Registered User
 
Join Date: Jul 2014
Location: Warsaw/Poland
Posts: 195
@sparhawk
that's two separate instructions/aims:

1) clear data register
"moveq #0,Dn"

2)clear address register
"suba.l An,An"
Cyprian is offline  
Old 06 February 2020, 12:40   #5
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
Yes. That makes sense.
sparhawk is offline  
Old 07 February 2020, 14:42   #6
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,444
While the above is 100% correct (in that the examples for clearing registers are either the fastest/smallest or both), I'd argue that focusing on this kind of optimization might not be the best way to go about it.

In code that is either time or space critical, I would definitely recommend using the examples as given (moveq / suba.l). However, there are many places in which a simple clr.w/clr.l effectively is just as good. My suggestion then is to use "what you like"*, unless you are trying to get the fastest (or smallest) possible code. Which is something you probably should only really do if you need to

*) Within reason, of course. As an example: move.l #0,a0 will work but it needlessly wasteful in terms of space and performance. A suba.l a0,a0 or clr.l a0 (or a moveq #0,d0 vs a clr.l d0), however are both usually "good enough".

Note this isn't to say you should just waste cycles or space without limits or reason. It's more that you're probably better off choosing the correct algorithms and then optimizing as needed instead of trying to do everything optimally all the time - pick your battles and all that.
roondar is offline  
Old 07 February 2020, 15:00   #7
Pixelfill
Registered User
 
Join Date: Dec 2019
Location: Preston
Posts: 100
Quote:
Originally Posted by roondar View Post
While the above is 100% correct (in that the examples for clearing registers are either the fastest/smallest or both), I'd argue that focusing on this kind of optimization might not be the best way to go about it.

In code that is either time or space critical, I would definitely recommend using the examples as given (moveq / suba.l). However, there are many places in which a simple clr.w/clr.l effectively is just as good. My suggestion then is to use "what you like"*, unless you are trying to get the fastest (or smallest) possible code. Which is something you probably should only really do if you need to

*) Within reason, of course. As an example: move.l #0,a0 will work but it needlessly wasteful in terms of space and performance. A suba.l a0,a0 or clr.l a0 (or a moveq #0,d0 vs a clr.l d0), however are both usually "good enough".

Note this isn't to say you should just waste cycles or space without limits or reason. It's more that you're probably better off choosing the correct algorithms and then optimizing as needed instead of trying to do everything optimally all the time - pick your battles and all that.
thanks to all for chipping in, I’m fully on board with the suggestion to use what’s appropriate, I’m of the school that code should be easy to read and understand, rather than “clever” in my day to day life.

If I’m understanding you the main differences are down to assembled executable size, and cycle execution time. Is the best place to find cycle times the Motorola documentation, or is there a more accessible document?

The other consideration when starting out teaching myself is that I’m inspecting available sourcecode to learn. Most of this is demo/intro code which by definition is either trying to be size conscious or speed conscious, so being able to understand what some of these alternative optimisations do will help aid understanding.

Thanks again
Pixelfill is offline  
Old 07 February 2020, 15:04   #8
roondar
Registered User
 
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,444
The Motorola documentation (which, helpfully, is still on NXP's site) is awesome. Contains all you need to know. However, if all you want is a table of instructions and cycle uses I'd suggest looking at http://oldwww.nvg.ntnu.no/amiga/MC68...000timing.HTML

Which is perhaps easier to click through than reading the manual.
roondar 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
noob copper question jotd Coders. Asm / Hardware 4 10 January 2020 15:37
Noob Memory Question CHRIS-F support.Hardware 5 05 July 2016 18:45
Noob question, sorry. fitzsteve project.WHDLoad 3 23 August 2009 16:56
Noob Question but anyway. trackah123 Coders. General 17 30 October 2008 14:39
yet another noob question AliasXZ New to Emulation or Amiga scene 11 23 January 2008 23:28

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 03:37.

Top

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