Author Topic: Cheating on the Z80  (Read 4048 times)

0 Members and 1 Guest are viewing this topic.

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Cheating on the Z80
« on: October 07, 2011, 12:46:22 am »
Most instructions on the Z80 processor were designed with a purpose, and you can't do anything except what the instruction was created for.  For example, you can't do anything with ld a, imm8 except store a byte value into register A.  Similarily, you can't use ld a, e to pop hl off the stack  :P

However, there are those rare times that you can use an instruction creatively for a purpose that the Z80 designers did not intend!  Please share your stories

Mine deals with LDD.  For a routine I'm working on for Elimination, I always need BC to point to one byte before HL, where HL holds the data from plotsscreen to transfer to a buffer.  This is because I need access to both (HL) and (HL-1) every time a byte is transferred to the buffer.   By using LDD, I can decrease HL and BC at the same time, even though the Z80 designers meant for BC to be decreased as a Byte Counter.

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: Cheating on the Z80
« Reply #1 on: October 07, 2011, 03:48:14 pm »
XOR A where would we be without it.

POP AF/PUSH AF is a great way to move SP 2 places.

Similar to yours, CPD not only decreases DE and BC but checks to see if BC is zero.

EX (SP), HL \ EX (SP), HL - 38 t-states in only 2 bytes.

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 calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Cheating on the Z80
« Reply #2 on: October 07, 2011, 05:06:42 pm »
Similar to yours, CPD not only decreases DE and BC but checks to see if BC is zero.
Decreases HL and BC, you mean.

Well, let's see... SP is meant to be used as a stack pointer, but in TI-Boy I use it to read 2 bytes from the Game Boy instruction stream at once.

EXX and EX AF,AF' are meant to be used in interrupt handlers, but I often use them for extra registers.

I'm sure it's possible to use I as an extra register when you don't have IM 2 interrupts enabled, though I haven't personally done so.

I've used DAA to optimize conversion between a raw hex digit and an ASCII hex digit.

JR nz/z/nc/c, $+1 can be used as a conditional RST 38h (because it jumps to the $FF in the JR encoding)

Also, in TI-Boy I use a 257-byte buffer of $FD bytes for my interrupt table, which doubles as a nop-slide (I throw the handlers for invalid opcodes into this buffer)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Cheating on the Z80
« Reply #3 on: October 08, 2011, 10:31:17 pm »
I've used the upper bit of R for some flags.

For a pseudo-random number I use ld a,r \ rrca \ ld r,a

I use some of the 8-bit index register halves when other registers are tied up.  Those were undocumented instructions at first.  (Darn Nspire.)
ld a, 0
ld a, a

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Cheating on the Z80
« Reply #4 on: October 10, 2011, 02:21:49 am »
Here is an incredibly inefficient and slow way to do an "ex h,l" but its impressive that it uses no extra registers or memory.  Even the carry flag is preserved:
Code: [Select]
ExhHL:
  adc hl,hl
  adc hl,hl
  adc hl,hl
  adc hl,hl
  adc hl,hl
  adc hl,hl
  adc hl,hl
  adc hl,hl
  rl  l
You could do the same strategy with any 2 registers, although none as efficiently.  I guess this could be theoretically useful in a non-time sensitive situation when you need to do an exchange but all registers are in use including SP (so no stacks) and there is no more free ram, but I doubt anyone would be in that situation.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: Cheating on the Z80
« Reply #5 on: October 12, 2011, 01:14:30 am »
Here is an incredibly inefficient and slow way to do an "ex h,l" but its impressive that it uses no extra registers or memory.  Even the carry flag is preserved.
Woah!  It even saves the carry flag too!  That's awesome!  +1
« Last Edit: October 12, 2011, 01:14:45 am by ralphdspam »
ld a, 0
ld a, a