English Amiga Board


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

 
 
Thread Tools
Old 10 November 2021, 18:33   #1
DanielAllsopp
Registered User
 
DanielAllsopp's Avatar
 
Join Date: Feb 2018
Location: Northumberland, UK
Posts: 272
Fast switch between '1' and '0'

Hey!

Quick, probably easy for you lot, question that I can't figure out.

I would I write a quick switch between 1 and 0 in ASM? In a higher level language like C I would write something like:

Code:
x = 1 - x
Cheers.
DanielAllsopp is offline  
Old 10 November 2021, 18:34   #2
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,212
bchg #0,d0 ?


although if it doesn't matter if your "1" value is actually -1, you could use "not.w d0" for example
DanScott is offline  
Old 10 November 2021, 18:43   #3
DanielAllsopp
Registered User
 
DanielAllsopp's Avatar
 
Join Date: Feb 2018
Location: Northumberland, UK
Posts: 272
Quote:
Originally Posted by DanScott View Post
bchg #0,d0 ?


although if it doesn't matter if your "1" value is actually -1, you could use "not.w d0" for example
Thanks Dan, that's the sort of little tip I'm missing. I think I need to invest in a decent reference for basic ASM instructions.
DanielAllsopp is offline  
Old 10 November 2021, 20:20   #4
mcgeezer
Registered User
 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 2,702
I would probably have used eor.b #1,d0

As an aside I tend to have to do this a lot in my stuff, so I always opt to use a frame counter when doing it.

Code:
loop:
  WAIT_FOR_FRAME

  addq.l #1,FRAME_COUNTER

  move.l FRAME_COUNTER,d0
  and.l #1,d0
  beq  do_something
  bra  do_something else

  bra loop

I really would look to learn the inner workings of 68k. When I was younger I didn't pay any attention to status flags, I also didn't pay attention to detail on not, neg and particularly the effects of ext. The set instructions are all useful too.

Last edited by mcgeezer; 10 November 2021 at 22:47. Reason: thanks jotd for pointing out silly code error
mcgeezer is offline  
Old 10 November 2021, 21:41   #5
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,233
status flags are useful to save cycles. Careful with to-memory operations, as they don't set the status flags. Operations to data registers (not address) do set the status flags.

bclr #0,d0 also sets D0 value before bit is cleared. Can be useful at times. In doubt: RTFM

note: in C, x ^= 1; (in-place xclusive or) is probably faster that x = 1 - x; but optimizers can detect such patterns and change the expression to the fastest one.

@mcgeezer your snippet has issues

Code:
  addq.l #1,FRAME_COUNTER

  move.l FRAME_COUNTER,d0  ; no  "#" there!!!
  and.l #1,d0
although I'd preload the counter value in D0 to save cycles, in this case

Code:
  move.l FRAME_COUNTER,d0
  addq.l #1,d0
  move.l  D0,FRAME_COUNTER
  and.b #1,d0
and and.b does the same there you only need byte value of D0 for the rest of your code probably, instruction fetch is faster (4 bytes instead of 6) although btst #0,d0 probably beats both in terms of speed unless you need the masked value.

Last edited by jotd; 10 November 2021 at 21:50.
jotd is offline  
Old 10 November 2021, 22:57   #6
mcgeezer
Registered User
 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 2,702
Quote:
Originally Posted by jotd View Post

@mcgeezer your snippet has issues

Code:
  addq.l #1,FRAME_COUNTER

  move.l FRAME_COUNTER,d0  ; no  "#" there!!!
  and.l #1,d0
although I'd preload the counter value in D0 to save cycles, in this case

Code:
  move.l FRAME_COUNTER,d0
  addq.l #1,d0
  move.l  D0,FRAME_COUNTER
  and.b #1,d0
and and.b does the same there you only need byte value of D0 for the rest of your code probably, instruction fetch is faster (4 bytes instead of 6) although btst #0,d0 probably beats both in terms of speed unless you need the masked value.

Indeed, thanks for pointer that error out. You're right about the byte op on "and" too but I guess I used longs for completeness.
mcgeezer is offline  
Old 11 November 2021, 00:59   #7
DanielAllsopp
Registered User
 
DanielAllsopp's Avatar
 
Join Date: Feb 2018
Location: Northumberland, UK
Posts: 272
Yeah, this is exactly the sort of direction I need from you pros. I'm probably so stuck in all of the high-level stuff that I do every day for my job that I tend to forget about all the low-level instructions such as not etc that make everything work.

I usually have a week of 68k coding, and then a week of iOS coding but lately I've just done 4 or 5 weeks of 68k as I'm so into it at the minute that I just want to keep cracking on in my spare time.

There's probably TONS of stuff I could optimise in my code, but I guess you learn as you go on.

Thanks for all of the help lads, it's very much appreciated.
DanielAllsopp is offline  
Old 11 November 2021, 08:07   #8
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Quote:
Originally Posted by jotd View Post
Careful with to-memory operations, as they don't set the status flags.
Typo ? They do set the status flags.
meynaf is offline  
Old 11 November 2021, 10:34   #9
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
Quote:
Originally Posted by jotd View Post
Code:
  addq.l #1,FRAME_COUNTER

  move.l FRAME_COUNTER,d0  ; no  "#" there!!!
  and.l #1,d0
There's no error there - he wants the value of the frame counter in d0, not the address.
hooverphonique is offline  
Old 11 November 2021, 10:39   #10
mcgeezer
Registered User
 
Join Date: Oct 2017
Location: Sunderland, England
Posts: 2,702
Quote:
Originally Posted by hooverphonique View Post
There's no error there - he wants the value of the frame counter in d0, not the address.
Jotd was right - it's just I modified the code and placed a note in the edit.
mcgeezer is offline  
Old 11 November 2021, 10:43   #11
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,233
@meynaf correct. I was wrong. conditions codes aren't set for move to address registers, that's all.

@hooverphonique setting # loads the address, so # must be removed. Adressing mode error (mistaking immediate for direct or reverse) that's a deadly mistake that happens to every one of us
jotd is offline  
Old 11 November 2021, 15:50   #12
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
Quote:
Originally Posted by mcgeezer View Post
Jotd was right - it's just I modified the code and placed a note in the edit.
Quote:
Originally Posted by jotd View Post
@hooverphonique setting # loads the address, so # must be removed. Adressing mode error (mistaking immediate for direct or reverse) that's a deadly mistake that happens to every one of us
I obviously misunderstood - I thought jotd suggested, a hash was missing
hooverphonique is offline  
Old 12 November 2021, 15:54   #13
DanScott
Lemon. / Core Design
 
DanScott's Avatar
 
Join Date: Mar 2016
Location: Tier 5
Posts: 1,212
Quote:
Originally Posted by DanielAllsopp View Post
Thanks Dan, that's the sort of little tip I'm missing. I think I need to invest in a decent reference for basic ASM instructions.

Quote:
Originally Posted by mcgeezer View Post
The set instructions are all useful too.

Yep, quite often you might (want to) do something like this to set a flag if d1 >= 100

Code:
     moveq #0,d0
     cmp.w #100,d1
     blt.s .LessThan
     moveq #1,d0
.LessThan
but you can do this:

Code:
     cmp.w #100,d1
     sge d0
which will set d0.b to 0 or -1

You have all these "set" instructions:

SCC set on carry clear
SCS set on carry set
SEQ set on equal
SGE set on greater than or equal
SGT set on greater than
SHI set on higher than
SLE set on less than or equal
SLS set on lower than or same
SLT set on less than
SMI set on minus (i.e., negative)
SNE set on not equal
SPL set on plus (i.e., positive)
SVC set on overflow clear
SVS set on overflow set
SF set on false (i.e., set never)
ST set on true (i.e., set always)


But yeah, get yourself a good reference guide for 68000

There's a few online.. this one is fine for most purposes:

http://wpage.unina.it/rcanonic/didat...docs/68000.pdf
DanScott 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
Fast switch statement code Jobbo Coders. Asm / Hardware 10 22 October 2021 19:13
Switch off / revert back VBR Move to fast alexh support.AmigaOS 9 05 September 2021 21:21
CD32 - Is NTSC/PAL switch and 50/60 switch possible? missyrelm Hardware mods 71 13 January 2019 05:21
Fast switch Port 1 mouse/ gamepad? jimmy2x2x support.WinUAE 3 04 January 2015 09:11
Use of 4MB PCMCIA Fast Flash Memory as Fast RAM in A1200 nkarytia support.Hardware 10 16 September 2011 13:37

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 07:55.

Top

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