08 September 2020, 11:08 | #1 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
shortest way to set z=0 in ccr
Hello, just some small question.
Very often i return ccr's z bit as boolean in my routines. This spares tst instructions. Of course i could just use moveq but there is not always a data register available. For setting z=1 no problem : cmp.b d0,d0 does the trick. Changing other ccr bits is no big deal in most cases. If i didn't care about code size i could use simple andi.b #$fb,ccr but code size is important to me and this instruction is large (and not very readable either). No assumption can be made about other register contents, as it needs to be a macro used everywhere. So we can't count on D0-D7/A0-A6 being nonzero. So to set z=0 with single code word the only solution i found is tst.l a7 but this is 020+ and it looks real weird (even if a7 can hardly ever be 0, can it ?). Any other idea ? Any coding trick i missed ? |
08 September 2020, 11:43 | #2 |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
|
08 September 2020, 11:52 | #3 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,408
|
Fascinating, I completely forgot about RTR
Gotta remember that one - I immediately thought of ways to use it |
08 September 2020, 12:00 | #4 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
Could be used in some cases... but too dependent of the situation.
Two problems arise : - we can have to save/restore registers and there it doesn't work - we could use moveq but this makes it unsuitable for a simple macro - we need to return with rtr - so that's one extra instruction because we also need rts for the other case (when the routine returns z=1) Nice try |
08 September 2020, 12:05 | #5 | |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
Quote:
Code:
tst.l (sp) |
|
08 September 2020, 12:25 | #6 |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
|
08 September 2020, 12:42 | #7 | |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
Quote:
But I have not found any way with the required restrictions. The only one is the ugly and ccr.. |
|
08 September 2020, 13:32 | #8 | |
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
Quote:
Of course i could try with another flag, such as c. But i can only clear it with single code word, not set it, so the situation is the same. We do not have simple clear/set single flag instructions and yes it's a 68k shortcoming. I guess i will keep my tst.l a7. |
|
08 September 2020, 14:21 | #9 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,215
|
Frankly, I suggest to change the coding style. I know returning conditions in ccr was kind of iconic in the 8-bit ages because there were not so many registers left, and when I moved to 68K, I tried (incorrectly) to adapt this style as well. But this only works with assembler, and no other language, and it does not necessarily make things faster or better. Within a function, it is acceptable, but I would not do that nowadays anymore as part of a function interface.
andi/ori ccr is the most direct way how to deal with the condition codes, but if that is not readable, I had (back then) a couple of macros defined that set or cleared individual flags. clc/sec (carry bit), clz/sez (zero flag), or ... well, you know how the macro looks like that sets the x bit, right? (-: |
08 September 2020, 14:42 | #10 | ||
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
Quote:
Of course this only works with assembler, and ? Do i care about other languages ? In the unlucky case i have to mess with them, i provide a few wrappers, job done. But change the coding style to look like compiler output, what a joke. It's not this way to make things faster, even if it's certainly not slower, it's just to avoid putting instructions where not needed. Often the condition is ready without anything special to do and i just have to return. Setting a register would just add extra instructions. Quote:
|
||
08 September 2020, 15:50 | #11 |
Registered User
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 55
Posts: 1,959
|
On Amiga tst.l 4.W can be used, but can be slow and its 4 bytes long.
|
08 September 2020, 15:54 | #12 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,215
|
Yes, you better do. The problem is that "secondary return codes" do not scale. As soon as the program and its functions grow larger, it becomes quite easy (I speak from experience) to forget setting the return code, or branching out of a subroutine without setting condition codes correctly. This then means bug-huntig fun for hours.
Take ARexx as a (negative) example: This is all assembly, in one big pile, and is almost impossible to maintain as it contains and lives from such techniques. It means that about 80% of the rexxsyslib.library is not callable from anything but assembler, and undocumented. It also meant that ARexx was never ported to anything, like AmigaOs 4, because it was basically impossible to read or impossible to follow. I know, I tried, and gave up on it. |
08 September 2020, 16:34 | #13 |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
|
08 September 2020, 17:06 | #14 | |
Registered User
|
Quote:
tst.l (sp) directly before a rts will always do the job, because (sp) contains the return address in this case, which is always non-zero. Only in the case you saved the registers, you must restore them before using the trick (but you must do this before rts anyway). Or am I misunderstanding you? |
|
08 September 2020, 17:17 | #15 | |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,215
|
Quote:
But does a needless memory read and hence requires a bus cycle to potentially slow memory. Seriously, compare that to a tst.l <return register> (probably in cache), which does not require to read from memory, or an andi #..,ccr, which does not need that either. All this looks like analyzing the toe of the elephant. There are far more important problems to be solved in software, and there are far more important tricks to be played to create fast software - than this. |
|
08 September 2020, 17:35 | #16 |
Registered User
Join Date: Jul 2015
Location: The Netherlands
Posts: 3,408
|
If I read the OP correctly, I do believe the goal here wasn't so much speed as it was size. I do kind of agree it seems to be a very small thing, but then again I don't know why meynaf needs this code. It may be that he really does need something that is as small as possible.
One thing I've learned is that assuming a problem is small or unimportant tends to only work for the general case and not the specific. Anyway, enough about this. |
08 September 2020, 17:49 | #17 | |||||||
son of 68k
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
|
Quote:
Quote:
Quote:
Now, is porting ARexx to something else a good idea ? Maybe not. Quote:
Quote:
So if the trick is done after restoring the regs, how can i restore the regs and return the other value ? Restoring them in another place would mean larger code. Quote:
You have more important things to do. Quote:
And yes, the problem is small. It is more academic than anything else, to let me know if my current solution is the most adequate. |
|||||||
08 September 2020, 20:00 | #18 | |
Registered User
Join Date: Jan 2008
Location: Warsaw/Poland
Age: 55
Posts: 1,959
|
Quote:
lea EndBuffy,A0 move.l A7, StackPtr sub.l A7,A7 moveg #0,d0 ... etc movem.l D0-D7/A1-A7,-(A0) movem.l D0-D7/A1-A7,-(A0) movem.l D0-D7/A1-A7,-(A0) etc move.l StackPtr,A7 |
|
08 September 2020, 20:41 | #19 | |
Defendit numerus
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
|
Quote:
And yes, I've also seen a7 set to zero in some "extreme" routines. |
|
09 September 2020, 09:41 | #20 |
Registered User
Join Date: Dec 2014
Location: germany
Posts: 439
|
Hmm, would a
Code:
btst Dn,#$FF It's two words, unfortunately, but that's still a word less than andi to ccr. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Aminet Set 1 to 10 | wizard66 | request.Other | 3 | 21 August 2022 02:31 |
ccr behaviour of div for v=1 | meynaf | Coders. Asm / Hardware | 9 | 02 December 2017 22:20 |
here are my computers set up | mcbone | Amiga scene | 3 | 17 July 2013 20:00 |
move.w ccr,d0 on 68030 | BlankVector | support.WinUAE | 11 | 05 February 2013 16:46 |
Tosec Set 0.29 | Ahamay | New to Emulation or Amiga scene | 9 | 15 November 2004 10:03 |
|
|