Author Topic: Mad DJNZ Hacks  (Read 8475 times)

0 Members and 1 Guest are viewing this topic.

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Mad DJNZ Hacks
« on: October 21, 2011, 05:53:11 pm »
I was looking through starfox when I saw some code that used DJNZ in an unconventional way, this made me think about how useful it is, so here are some things I just thought of.

On this first one, you're going to say, "well duh," but I bet you don't do it.

Let's say you want to see if A==1:
Code: [Select]
dec a
jr nz, AWasnt1

You could replace it with this for only 1 extra t-state:
Code: [Select]
ld b, a
djnz AWasnt1

One benefit of this is that you don't destroy A. Now before you start complaining about that 1 t-state, just remember, if you manage to put your result into B rather than A, this routine becomes 3 t-states faster.


Here is another common code structure that I run into a lot:
Code: [Select]
ld a, (hl)
dec a
jr nz, not1
...
jr final
not1:
dec a
jr nz, not2
...
jr final
not2:
dec a
jr nz, not3
...
jr final
not3:
dec a
jr nz, not4
...
jr final
not4:
...
final:

Why not replace it with this, which is both faster and smaller (you don't need the jumps if you can leave b at 0):
Code: [Select]
ld b, (hl)
djnz not1
...
not1:
djnz not2
...
not2:
djnz not3
...
not3:
djnz not4
...
not4:
djnz not5
...
not5:

final:


Finally, this one is a bit more of a stretch, but you can use B as a flag byte, take for instance this byte plotter:
Code: [Select]
;######################################
;plot a byte
;a = byte
;b = inverse 1 = yes, 0 = no
;de = xy

plotAByte:
ld c, d
ld d, 0
ld h, d
ld l, e
add hl, de
add hl, de
add hl, hl
add hl, hl
srl c
srl c
srl c
ld e, c
add hl, de
ld de, plotSScreen
add hl, de

djnz inversing

or (hl)
ld (hl), a
ret

inversing:
cpl
and (hl)
ld (hl), a
ret


Also, while it's not a new idea today, I have used DJNZ in weird ways before, I typically do it when unpacking bytes from storage. This routine is from zStart and pulls a name out of storage from 6 bytes to 8 bytes. I use B as a bit counter.

Code: [Select]
pop hl
ld ix, op1+1
ld bc, 8*256+6
ld d, 8
xor a
unpackNameLoop:
rr (hl)
rra
djnz notNextUnpackByte
inc hl
ld b, 8
notNextUnpackByte:
dec c
jr nz, unpackNameLoop
ld c, 6
rra
rra
ld (ix), a
inc ix
xor a
dec d
jr nz, unpackNameLoop
« Last Edit: October 21, 2011, 06:05:58 pm by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline boot2490

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 607
  • Rating: +54/-36
    • View Profile
    • Boot2490's Stuff
Re: Mad DJNZ Hacks
« Reply #1 on: October 21, 2011, 06:07:29 pm »
What
seems cool though
I'm not worried about SOPA creating censorship, that will not stand for long. I'm worried that they'll succeed in stopping piracy!

Spoiler For Signature, updated march 23, 11:28 PM EST:















An useful tool!

PM me if you need some help. I am glad to be of assistance and part of the TI Communnity.

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Mad DJNZ Hacks
« Reply #2 on: October 26, 2011, 04:48:06 pm »
Why not replace it with this, which is both faster and smaller (you don't need the jumps if you can leave b at 0):
Code: [Select]
ld b, (hl)
djnz not1
...
not1:
djnz not2
...
not2:
djnz not3
...
not3:
djnz not4
...
not4:
djnz not5
...
not5:

final:
Wow!  That's great stuff!  :D
Plus you can keep your original value unchanged in A.  
« Last Edit: October 26, 2011, 04:48:16 pm by ralphdspam »
ld a, 0
ld a, a

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Mad DJNZ Hacks
« Reply #3 on: October 29, 2011, 07:43:36 pm »
I use djnz to jump forward very often, actually, and it is a trick I figured out and exploited when I created my first asm library all the way up to BatLib :) I even made a note of it in my opcode chart:
Quote from: Zeda's Opcode Chart
DJNZ * decrements B. If B is   not 0, it pretends to be a jr. If B is 0, the code continues. This is mostly used for loops because it uses only 2 bytes, so * is usually negative. Because of this, people seem to forget that you can jump forward with this. :P   

EDIT: and I use it for things like selecting a sprite method to draw with, rectangle method, pixel plotting method, et cetera.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Mad DJNZ Hacks
« Reply #4 on: January 17, 2012, 10:34:01 pm »
Another DJNZ hack is that you can use DJNZ $+2 to decrease B without affecting any flags. It's 4-9 cycles slower than normal DEC B, though.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Mad DJNZ Hacks
« Reply #5 on: January 17, 2012, 10:35:11 pm »
Brilliant

(And useless in 99% of cases)
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Mad DJNZ Hacks
« Reply #6 on: January 17, 2012, 10:36:29 pm »
It is one of those things that may not be used for a long time, but when it happens, somebody is going to feel special XD

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Mad DJNZ Hacks
« Reply #7 on: January 17, 2012, 10:40:43 pm »
That looks very nice, the djnz bundles dec, jr, nz nicely, saving space and adding efficiency. Could be useful, +1.
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Mad DJNZ Hacks
« Reply #8 on: January 18, 2012, 05:19:49 pm »
It's amazing how creative you all are. For the djnz list i usually just use a jump table and maybe push a return value onto the stack, but that looks like it'd be smaller (if not faster), though you'd have to be careful with 'b'.

Offline All_¥our_Bass

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 17
  • Rating: +1/-0
  • Spins Leeks
    • View Profile
Re: Mad DJNZ Hacks
« Reply #9 on: January 28, 2012, 09:05:32 pm »
 ld b,0
for_loop:
;stuff here
 djnz for_loop
;will repeat 256 times
SOPA = Sons Of the Patriots Act

Stop SOPA

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Mad DJNZ Hacks
« Reply #10 on: February 02, 2012, 12:21:57 am »
I think the best DJNZ is to ONLY use DJNZ :D I was reading about Single Instruction Set Computers and learned that using only DJNZ you can produce a turring complete language!

Offline All_¥our_Bass

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 17
  • Rating: +1/-0
  • Spins Leeks
    • View Profile
Re: Mad DJNZ Hacks
« Reply #11 on: February 03, 2012, 07:37:34 pm »
I think the best DJNZ is to ONLY use DJNZ :D I was reading about Single Instruction Set Computers and learned that using only DJNZ you can produce a turring complete language!
Mind = Blown
SOPA = Sons Of the Patriots Act

Stop SOPA

Offline Jonius7

  • python! Lua!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1918
  • Rating: +82/-18
  • Still bringing new dimensions to the TI-nspire...
    • View Profile
    • TI Stadium
Re: Mad DJNZ Hacks
« Reply #12 on: February 19, 2012, 02:58:22 pm »
What does turring complete language mean? especially the turring part.
Do you mean Turing, as for example, the Turing test?
« Last Edit: February 19, 2012, 02:59:17 pm by Jonius7 »
Programmed some CASIO Basic in the past
DJ Omnimaga Music Discographist ;)
DJ Omnimaga Discography
My Own Music!
My Released Projects (Updated 2015/05/08)
TI-nspire BASIC
TI-nspire Hold 'em
Health Bar
Scissors Paper Rock
TI-nspire Lua
Numstrat
TI-nspire Hold 'em Lua
Transport Chooser
Secret Project (at v0.08.2 - 2015/05/08)
Spoiler For Extra To-Be-Sorted Clutter:

Spoiler For Relegated Projects:
TI-nspire BASIC
Battle of 16s (stalled) | sTIck RPG (stalled) | Monopoly (stalled) | Cosmic Legions (stalled)
Axe Parser
Doodle God (stalled while I go and learn some Axe)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Mad DJNZ Hacks
« Reply #13 on: February 19, 2012, 03:09:22 pm »
Turing Completeness is a computer science term referring to the ability of a language to complete problems.  If a language is Turing Complete, it is able to solve any problem a Turing computer could solve.