nesdev.comhttp://forums.nesdev.com/ Simplify long, repetitive code.http://forums.nesdev.com/viewtopic.php?f=10&t=16187 Page 2 of 2

 Author: OmegaMax [ Thu Jul 13, 2017 1:52 pm ] Post subject: Re: Simplify long, repetitive code. Oziphantom wrote:Yeah but Garth, they are having trouble making an array of Words, lets keep the -1, stack push tricks to latter yeah Agreed and that's why I used your code example to help him.

 Author: Garth [ Thu Jul 13, 2017 3:08 pm ] Post subject: Re: Simplify long, repetitive code. What I presented is really basic stuff, not rocket science. Make the table say,Code:LvlFuncPtrTableLo   .byte  LOW(LEV01-1), LOW(LEV02-1), LOW(LEV03-1, LvlFuncPtrTableHi   .byte  HIGH(LEV01-1), HIGH(LEV02-1), HIGH(LEV03-1), Using the RTS for a jump simplifies things by eliminating the need for a variable and removing it from the constant battle for ZP space (since it was suggested to put it there). It also makes the program more efficient.

 Author: rainwarrior [ Thu Jul 13, 2017 3:18 pm ] Post subject: Re: Simplify long, repetitive code. Garth wrote:It also makes the program more efficient.Depends what you mean by "efficient". Using RTS takes 1 more cycle vs an indirect jump, but it saves some bytes of code at least?Code:STA zp +3STA zp +3JMP (addr) +5PHA +3PHA +3RTS +6The JMP pointer does not need to be on the zeropage, either (JMP's indirection is always from an absolute address, ZP is irrelevant to that instruction), though that would cost another 2 cycles from the STAs.

 Author: OmegaMax [ Thu Jul 13, 2017 3:25 pm ] Post subject: Re: Simplify long, repetitive code. This is becoming "use my way" instead,you left an example Garth so if sdm chooses to use it that's fine also.

 Author: Garth [ Thu Jul 13, 2017 3:30 pm ] Post subject: Re: Simplify long, repetitive code. Quote:Depends what you mean by "efficient".It saves several bytes; and if the indirect address is not in ZP, it's slightly faster, too.Quote:This is becoming "use my way" instead of helping another programmer.I believe it's always helpful to present a more elegant solution.

 Author: OmegaMax [ Thu Jul 13, 2017 3:39 pm ] Post subject: Re: Simplify long, repetitive code. I believe when your new the code Oziphantom showed is easier to grasp,that's why I choose his code to help sdm.Now that he understands and has it working he can explore other ways if he chooses,it's not really necessary though but it's up to him.

 Author: sdm [ Mon Jul 17, 2017 5:13 am ] Post subject: Re: Simplify long, repetitive code. I would still like to ask for help with a slightly different, large code. Rule similar, but slightly different. Each bit in the "GOLD_xx_xx_UnLock" byte slot is responsible for the collected (1) or not (0) gold. I tried to make the simplified code as above in posts, but this time instead of jump (JMP) to the label are operations on different bits. And now I have a problem, because I do not know if any one can do it in a similar way: /Collecting the "GOLD" item can be random, there is no pre-assigned order of collection order (one big map consisting of 64-nametables)GOLD_Checker - is the variable that depends on which "map" we enter, the number of this map is loaded (Specify: the value with the map number is uploaded exactly in the "collision" of the gold with the hero, and after that time is again zero)Code:GOLD_Checker      .ds 1GOLD_01_08_UnLock   .ds 1GOLD_09_16_UnLock   .ds 1GOLD_17_24_UnLock   .ds 1GOLD_25_32_UnLock   .ds 1GOLD_33_40_UnLock   .ds 1GOLD_41_48_UnLock   .ds 1GOLD_49_56_UnLock   .ds 1GOLD_57_64_UnLock   .ds 1GOLD_Check:   LDA GOLD_Checker   CMP #1   BNE NoGC_01   LDA GOLD_01_08_UnLock   ORA #%00000001   STA GOLD_01_08_UnLockNoGC_01:   LDA GOLD_Checker   CMP #2   BNE NoGC_02   LDA GOLD_01_08_UnLock   ORA #%00000010   STA GOLD_01_08_UnLockNoGC_02:   LDA GOLD_Checker   CMP #3   BNE NoGC_03   LDA GOLD_01_08_UnLock   ORA #%00000100   STA GOLD_01_08_UnLockNoGC_03:   LDA GOLD_Checker   CMP #4   BNE NoGC_04   LDA GOLD_01_08_UnLock   ORA #%00001000   STA GOLD_01_08_UnLockNoGC_04:   LDA GOLD_Checker   CMP #5   BNE NoGC_05   LDA GOLD_01_08_UnLock   ORA #%00010000   STA GOLD_01_08_UnLockNoGC_05:   LDA GOLD_Checker   CMP #6   BNE NoGC_06   LDA GOLD_01_08_UnLock   ORA #%00100000   STA GOLD_01_08_UnLockNoGC_06:   LDA GOLD_Checker   CMP #7   BNE NoGC_07   LDA GOLD_01_08_UnLock   ORA #%01000000   STA GOLD_01_08_UnLockNoGC_07:   LDA GOLD_Checker   CMP #8   BNE NoGC_08   LDA GOLD_01_08_UnLock   ORA #%10000000   STA GOLD_01_08_UnLockNoGC_08:   LDA GOLD_Checker   CMP #9   BNE NoGC_09   LDA GOLD_09_16_UnLock   ORA #%00000001   STA GOLD_09_16_UnLockNoGC_09:   LDA GOLD_Checker   CMP #10   BNE NoGC_10   LDA GOLD_09_16_UnLock   ORA #%00000010   STA GOLD_09_16_UnLockNoGC_10:   LDA GOLD_Checker   CMP #11   BNE NoGC_11   LDA GOLD_09_16_UnLock   ORA #%00000100   STA GOLD_09_16_UnLockNoGC_11:   LDA GOLD_Checker   CMP #12   BNE NoGC_12   LDA GOLD_09_16_UnLock   ORA #%00001000   STA GOLD_09_16_UnLockNoGC_12:   LDA GOLD_Checker   CMP #13   BNE NoGC_13   LDA GOLD_09_16_UnLock   ORA #%00010000   STA GOLD_09_16_UnLockNoGC_13:   LDA GOLD_Checker   CMP #14   BNE NoGC_14   LDA GOLD_09_16_UnLock   ORA #%00100000   STA GOLD_09_16_UnLockNoGC_14:   LDA GOLD_Checker   CMP #15   BNE NoGC_15   LDA GOLD_09_16_UnLock   ORA #%01000000   STA GOLD_09_16_UnLockNoGC_15:   LDA GOLD_Checker   CMP #16   BNE NoGC_16   LDA GOLD_09_16_UnLock   ORA #%10000000   STA GOLD_09_16_UnLockNoGC_16:   LDA GOLD_Checker   CMP #17   BNE NoGC_17   LDA GOLD_17_24_UnLock   ORA #%00000001   STA GOLD_17_24_UnLockNoGC_17:   LDA GOLD_Checker   CMP #18   BNE NoGC_18   LDA GOLD_17_24_UnLock   ORA #%00000010   STA GOLD_17_24_UnLockNoGC_18:   LDA GOLD_Checker   CMP #19   BNE NoGC_19   LDA GOLD_17_24_UnLock   ORA #%00000100   STA GOLD_17_24_UnLockNoGC_19:   LDA GOLD_Checker   CMP #20   BNE NoGC_20   LDA GOLD_17_24_UnLock   ORA #%00001000   STA GOLD_17_24_UnLockNoGC_20:   LDA GOLD_Checker   CMP #21   BNE NoGC_21   LDA GOLD_17_24_UnLock   ORA #%00010000   STA GOLD_17_24_UnLockNoGC_21:   LDA GOLD_Checker   CMP #22   BNE NoGC_22   LDA GOLD_17_24_UnLock   ORA #%00100000   STA GOLD_17_24_UnLockNoGC_22:   LDA GOLD_Checker   CMP #23   BNE NoGC_23   LDA GOLD_17_24_UnLock   ORA #%01000000   STA GOLD_17_24_UnLockNoGC_23:   LDA GOLD_Checker   CMP #24   BNE NoGC_24   LDA GOLD_17_24_UnLock   ORA #%10000000   STA GOLD_17_24_UnLockNoGC_24:   LDA GOLD_Checker   CMP #25   BNE NoGC_25   LDA GOLD_25_32_UnLock   ORA #%00000001   STA GOLD_25_32_UnLockNoGC_25:   LDA GOLD_Checker   CMP #26   BNE NoGC_26   LDA GOLD_25_32_UnLock   ORA #%00000010   STA GOLD_25_32_UnLockNoGC_26:   LDA GOLD_Checker   CMP #27   BNE NoGC_27   LDA GOLD_25_32_UnLock   ORA #%00000100   STA GOLD_25_32_UnLockNoGC_27:   LDA GOLD_Checker   CMP #28   BNE NoGC_28   LDA GOLD_25_32_UnLock   ORA #%00001000   STA GOLD_25_32_UnLockNoGC_28:   LDA GOLD_Checker   CMP #29   BNE NoGC_29   LDA GOLD_25_32_UnLock   ORA #%00010000   STA GOLD_25_32_UnLockNoGC_29:   LDA GOLD_Checker   CMP #30   BNE NoGC_30   LDA GOLD_25_32_UnLock   ORA #%00100000   STA GOLD_25_32_UnLockNoGC_30:   LDA GOLD_Checker   CMP #31   BNE NoGC_31   LDA GOLD_25_32_UnLock   ORA #%01000000   STA GOLD_25_32_UnLockNoGC_31:   LDA GOLD_Checker   CMP #32   BNE NoGC_32   LDA GOLD_25_32_UnLock   ORA #%10000000   STA GOLD_25_32_UnLockNoGC_32:   LDA GOLD_Checker   CMP #33   BNE NoGC_33   LDA GOLD_33_40_UnLock   ORA #%00000001   STA GOLD_33_40_UnLockNoGC_33:   LDA GOLD_Checker   CMP #34   BNE NoGC_34   LDA GOLD_33_40_UnLock   ORA #%00000010   STA GOLD_33_40_UnLockNoGC_34:   LDA GOLD_Checker   CMP #35   BNE NoGC_35   LDA GOLD_33_40_UnLock   ORA #%00000100   STA GOLD_33_40_UnLockNoGC_35:   LDA GOLD_Checker   CMP #36   BNE NoGC_36   LDA GOLD_33_40_UnLock   ORA #%00001000   STA GOLD_33_40_UnLockNoGC_36:   LDA GOLD_Checker   CMP #37   BNE NoGC_37   LDA GOLD_33_40_UnLock   ORA #%00010000   STA GOLD_33_40_UnLockNoGC_37:   LDA GOLD_Checker   CMP #38   BNE NoGC_38   LDA GOLD_33_40_UnLock   ORA #%00100000   STA GOLD_33_40_UnLockNoGC_38:   LDA GOLD_Checker   CMP #39   BNE NoGC_39   LDA GOLD_33_40_UnLock   ORA #%01000000   STA GOLD_33_40_UnLockNoGC_39:   LDA GOLD_Checker   CMP #40   BNE NoGC_40   LDA GOLD_33_40_UnLock   ORA #%10000000   STA GOLD_33_40_UnLockNoGC_40:   LDA GOLD_Checker   CMP #41   BNE NoGC_41   LDA GOLD_41_48_UnLock   ORA #%00000001   STA GOLD_41_48_UnLockNoGC_41:   LDA GOLD_Checker   CMP #42   BNE NoGC_42   LDA GOLD_41_48_UnLock   ORA #%00000010   STA GOLD_41_48_UnLockNoGC_42:   LDA GOLD_Checker   CMP #43   BNE NoGC_43   LDA GOLD_41_48_UnLock   ORA #%00000100   STA GOLD_41_48_UnLockNoGC_43:   LDA GOLD_Checker   CMP #44   BNE NoGC_44   LDA GOLD_41_48_UnLock   ORA #%00001000   STA GOLD_41_48_UnLockNoGC_44:   LDA GOLD_Checker   CMP #45   BNE NoGC_45   LDA GOLD_41_48_UnLock   ORA #%00010000   STA GOLD_41_48_UnLockNoGC_45:   LDA GOLD_Checker   CMP #46   BNE NoGC_46   LDA GOLD_41_48_UnLock   ORA #%00100000   STA GOLD_41_48_UnLockNoGC_46:   LDA GOLD_Checker   CMP #47   BNE NoGC_47   LDA GOLD_41_48_UnLock   ORA #%01000000   STA GOLD_41_48_UnLockNoGC_47:   LDA GOLD_Checker   CMP #48   BNE NoGC_48   LDA GOLD_41_48_UnLock   ORA #%10000000   STA GOLD_41_48_UnLockNoGC_48:   LDA GOLD_Checker   CMP #49   BNE NoGC_49   LDA GOLD_49_56_UnLock   ORA #%00000001   STA GOLD_49_56_UnLockNoGC_49:   LDA GOLD_Checker   CMP #50   BNE NoGC_50   LDA GOLD_49_56_UnLock   ORA #%00000010   STA GOLD_49_56_UnLockNoGC_50:   LDA GOLD_Checker   CMP #51   BNE NoGC_51   LDA GOLD_49_56_UnLock   ORA #%00000100   STA GOLD_49_56_UnLockNoGC_51:   LDA GOLD_Checker   CMP #52   BNE NoGC_52   LDA GOLD_49_56_UnLock   ORA #%00001000   STA GOLD_49_56_UnLockNoGC_52:   LDA GOLD_Checker   CMP #53   BNE NoGC_53   LDA GOLD_49_56_UnLock   ORA #%00010000   STA GOLD_49_56_UnLockNoGC_53:   LDA GOLD_Checker   CMP #54   BNE NoGC_54   LDA GOLD_49_56_UnLock   ORA #%00100000   STA GOLD_49_56_UnLockNoGC_54:   LDA GOLD_Checker   CMP #55   BNE NoGC_55   LDA GOLD_49_56_UnLock   ORA #%01000000   STA GOLD_49_56_UnLockNoGC_55:   LDA GOLD_Checker   CMP #56   BNE NoGC_56   LDA GOLD_49_56_UnLock   ORA #%10000000   STA GOLD_49_56_UnLockNoGC_56:   LDA GOLD_Checker   CMP #57   BNE NoGC_57   LDA GOLD_57_64_UnLock   ORA #%00000001   STA GOLD_57_64_UnLockNoGC_57:   LDA GOLD_Checker   CMP #58   BNE NoGC_58   LDA GOLD_57_64_UnLock   ORA #%00000010   STA GOLD_57_64_UnLockNoGC_58:   LDA GOLD_Checker   CMP #59   BNE NoGC_59   LDA GOLD_57_64_UnLock   ORA #%00000100   STA GOLD_57_64_UnLockNoGC_59:   LDA GOLD_Checker   CMP #60   BNE NoGC_60   LDA GOLD_57_64_UnLock   ORA #%00001000   STA GOLD_57_64_UnLockNoGC_60:   LDA GOLD_Checker   CMP #61   BNE NoGC_61   LDA GOLD_57_64_UnLock   ORA #%00010000   STA GOLD_57_64_UnLockNoGC_61:   LDA GOLD_Checker   CMP #62   BNE NoGC_62   LDA GOLD_57_64_UnLock   ORA #%00100000   STA GOLD_57_64_UnLockNoGC_62:   LDA GOLD_Checker   CMP #63   BNE NoGC_63   LDA GOLD_57_64_UnLock   ORA #%01000000   STA GOLD_57_64_UnLockNoGC_63:   LDA GOLD_Checker   CMP #64   BNE NoGC_64   LDA GOLD_57_64_UnLock   ORA #%10000000   STA GOLD_57_64_UnLockNoGC_64:   RTS

 Author: thefox [ Mon Jul 17, 2017 6:52 am ] Post subject: Re: Simplify long, repetitive code. Something like this:- A = GOLD_Checker-1. This is your 0-based level index 0..63.- A/8 (range 0..7) tells you which byte to modify. You can divide by 8 by shifting right (LSR) 3 times.- A & %111 (range 0..7) tells you which bit to modify (& = bitwise AND). You can use a lookup table with 8 elements (%1, %10, %100, ..., %10000000) to retrieve a mask for ORA.

 Author: tokumaru [ Mon Jul 17, 2017 9:08 am ] Post subject: Re: Simplify long, repetitive code. Here's one way to do it:Code:BitMasks:   .byte %00000001   .byte %00000010   .byte %00000100   .byte %00001000   .byte %00010000   .byte %00100000   .byte %01000000   .byte %10000000;************************   sec   lda GOLD_Checker   sbc #\$01   tay   and #%00000111   tax   tya   lsr   lsr   lsr   tay   lda GOLD_01_08_UnLock, y   ora BitMasks, x   sta GOLD_01_08_UnLock, yEDIT: Whenever you find yourself doing lots of comparisons against consecutive numbers, you should probably be using indexing instead. And when you're indexing stuff, the bits in the index usually have meanings that you can exploit, so when you realize what the bit pattern of the index/address is, you'll be able to find/manipulate the target data after a few bit manipulations rather than tons of comparisons for specific cases. For example, the way that the layout of a name table address is %0010NNYY YYXXXXX (NN = name table, XXXXX = horizontal tile coordinate, YYYYY = vertical tile coordinate) or an attribute table address is %0010NN11 11YYYXXX (NN = name table, XXX = horizontal 32x32 block coordinate, YYY = vertical 32x32 block coordinate) while the highest unused X and Y bits can be combined to index bit masks for manipulating the 16x16 blocks inside the 32x32 blocks (the lower X and Y bits are discarded for good, since you can't set palettes for individual tiles).

 Page 2 of 2 All times are UTC - 7 hours Powered by phpBB® Forum Software © phpBB Grouphttp://www.phpbb.com/