1; Substitute for 'sbc hl, sp' instruction
2; CPU   Min T Max T
3; 8080  156   156
4; 8085  154   154
5; gbz80 232   232
6; r2k   111   111
7; z180  146   146
8; z80   151   151
9; z80n  151   151
10
11      SECTION  code_crt0_sccz80
12      PUBLIC   __z80asm__sub_hl_sp
13
14__z80asm__sub_hl_sp:
15      push  bc
16      ld    b, a
17      push  de
18      ex    de, hl            ; subtrahed to de
19
20IF __CPU_GBZ80__
21      ld    hl, sp+6          ; minuend to hl, compensate for return address, DE and BC in stack
22ELSE
23      push  af
24      ld    hl, 8             ; minuend to hl, compensate for return address, DE, BC and AF in stack
25      add   hl, sp
26      pop   af
27ENDIF
28      ld    a, e
29      sub   a, l
30      ld    e, a
31
32      ld    a, d
33      sbc   a, h
34      ld    d, a
35
36      ex    de, hl
37      pop   de
38      ld    a, b
39      pop   bc
40      ret
41