-3

A byte in memory at address $9000.

Write a program to clear bits 7 and 6, set bits 5 and 4, and toggle bits 3, 2, 1, and 0.

This is what I have, and it does not work.

     ORG    $9000
    
        MOVE.B  #00, D0
   
        MOVE    #7, D1
        BCLR    #7, D0

NEXT    BTST    D1,D0

        BEQ     ZERO
        BCHG    #7,D0

ZERO    SUB.B   #1,D1

        BCC     NEXT
        
EXIT    TRAP    #14

        END     $9000

Any help would be much appreciated

Community
  • 1
  • 1
user2188946
  • 35
  • 1
  • 1
  • 4

1 Answers1

4

I don't know 68k assembly but why such a long program, and why do you need to compare and jump while it's just some simple bitwise operations. Here is an implementation in C

a &= 0x3f; // 0011 1111 clear bit 7 and 6
a |= 0x30; // 0011 0000 set bit 5 and 4
a ^= 0x0f; // 0000 1111 toggle bit 3~0

When converting to assembly it needs just 3 simple operations (not including a variable load and 3 constant load if needed), no jumping and comparing at all

Demo on brownbot compiler explorer. Browncc compiles to

bitwise:
        move.b 7(%sp),%d0
        and.b #63,%d0
        or.b #48,%d0
        eor.b #15,%d0
        rts
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    What you show in pseudocode translates easily and perfectly to 68K, just load a register with the value, and execute the operations (move somewhere,d0 and.b #$3f,d0 or.b #$30,d0 eor.b #$0f,d0). – Durandal Nov 11 '13 at 17:24
  • Actually `a = (((a & x) | y ) ^ z)` can always be expressed with just two logic operations (if `x, y, z` are constants): `a &= (x & ~y); a ^= (z ^ y);` Also, `move.b MEM,d0; and.b #x,d0;` can be done slightly more efficient: `moveq #x,d0; and.b MEM,d0;` (compiler writers seem to not optimize their 68k backend anymore ...) – chtz Nov 27 '20 at 07:29
  • @chtz I've always believed that it's possible to optimize this but never bothered to do that. That's just clever, but writing separate expressions like this may be more readable. And if you look at the output from Clang in the link above you'll be surprised by the amount of code it generates – phuclv Nov 27 '20 at 08:11
  • 1
    Adding `-fomit-frame-pointer` seems to help (no idea why this is not part of `-O2` ...) – chtz Nov 27 '20 at 09:15