SHIAR Homepage   Home of
Wormy
   Home  ||  TI Calculators  ||  Programming  ||  Z80 OPTIMIZING 11.I.02 

 

THE Z80 FEELING

So you've learned a little z80. You know ld, cp and jp, and think that's all. As far as commands go, that's most of it indeed; you'll get the job done (usually). However, the thing with z80 is, with a little experience and feeling you can make everything twice as fast, half as big. That's what I love about it :)

Below's a short list of common mistakes, or impurities rather, by beginning programmers. Most more experienced programmers will (or should) already know better. But for those who can't feel the ugly code, just remember these few rules. (Note: of course there are a lot of exceptions, these are just basic guidelines.)


  call foo
  ret
>
  jp foo

  cp 0
  ret z
>
  or a
  ret z

  cp 1
>
  dec a

  ld a,0
>
  xor a         ;flags altered

  and 1
  cp 1
  jr z,foo
>
  and 1         ;and sets zf, no need for cp
  jr nz,foo

  and 1
  cp 1          ;a not needed after this
  jr z,foo
>
  rra
  jr c,foo

  bit 0,a
  call z,foo
>
  rra
  call nc,foo

  bit 7,a
  jr z,foo
>
  rla
  jr nc,foo

  bit 2,a
  ret nz
  xor a
>
  and %100
  ret nz

  ld a,42
  ld (hl),a
>
  ld (hl),42

  ld a,(foo)
  inc a
  ld (foo),a
>
  ld hl,foo
  inc (hl)

  ld hl,foo+(42*1)
  add a,(hl)
  ld hl,foo+(42*2)
  add a,(hl)
  ld hl,foo+(42*3)
  add a,(hl)
  ld hl,foo+(42*4)
  add a,(hl)
>
  ld hl,foo
  ld de,42
  ld b,4
bar:
  add hl,de
  add a,(hl)
  djnz bar

  ld hl,(foo)
  ld de,0
  call _cphlde
  ret z
>
  ld hl,(foo)
  ld a,h
  or l
  ret z

  ld a,h
  cp l
  ret nz
  ld a,1
>
  ld a,h
  sub l
  ret nz
  inc a

  ex de,hl
  pop hl
  push de
>
  ex (sp),hl

  cp 42
  jp z,foo
  cp 43
  jp z,bar
  cp 84
>
  sub 42
  jp z,foo
  dec a
  jp z,bar
  cp  84-42-1

  cp K_LEFT     ;likewise..
  jr z,foo
  cp K_RIGHT
  jr z,bar
  cp K_DEL
  jr z,quux
>
  sub K_LEFT
  jr  z,foo
  dec a
  jr  z,bar
  cp  K_DEL-K_RIGHT
  jr  z,quux

  sla l
  rl h
>
  add hl,hl

  xor %11111111
>
  cpl

  ld bc,42
  or a
  sbc hl,bc
>
  ld  bc,-42
  add hl,bc

  ld b,42
  ld c,84
>
  ld bc,$2A54

 

z80optim (28.XII.01) created in 0.92s ©1999-2002 by SHIAR -- Tous droits réservés