English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. Asm / Hardware (https://eab.abime.net/forumdisplay.php?f=112)
-   -   Fast switch between '1' and '0' (https://eab.abime.net/showthread.php?t=108828)

DanielAllsopp 10 November 2021 18:33

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.

DanScott 10 November 2021 18:34

bchg #0,d0 ?


although if it doesn't matter if your "1" value is actually -1, you could use "not.w d0" for example

DanielAllsopp 10 November 2021 18:43

Quote:

Originally Posted by DanScott (Post 1516408)
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.

mcgeezer 10 November 2021 20:20

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.

jotd 10 November 2021 21:41

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.

mcgeezer 10 November 2021 22:57

Quote:

Originally Posted by jotd (Post 1516442)

@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.

DanielAllsopp 11 November 2021 00:59

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.

meynaf 11 November 2021 08:07

Quote:

Originally Posted by jotd (Post 1516442)
Careful with to-memory operations, as they don't set the status flags.

Typo ? They do set the status flags.

hooverphonique 11 November 2021 10:34

Quote:

Originally Posted by jotd (Post 1516442)
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.

mcgeezer 11 November 2021 10:39

Quote:

Originally Posted by hooverphonique (Post 1516499)
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.

jotd 11 November 2021 10:43

@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 :)

hooverphonique 11 November 2021 15:50

Quote:

Originally Posted by mcgeezer (Post 1516500)
Jotd was right - it's just I modified the code and placed a note in the edit.

Quote:

Originally Posted by jotd (Post 1516503)
@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 :D

DanScott 12 November 2021 15:54

Quote:

Originally Posted by DanielAllsopp (Post 1516412)
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 (Post 1516433)
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


All times are GMT +2. The time now is 17:36.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.08406 seconds with 11 queries