RosettaCodeData/Task/Generic-swap/Z80-Assembly/generic-swap-5.z80
2023-07-01 13:44:08 -04:00

20 lines
700 B
Z80 Assembly

ld hl,&C000
push bc
ldi a,(hl) ;equivalent of "ld a,(hl) inc hl" but is faster than the two separately. Some assemblers call this "ld a,(hl+)"
ld c,a
ld a,(hl) ;we don't need to increment hl this time. There's no wasted time or bytecode if we did however.
ld b,a ;on Zilog Z80 we would have just done "LD BC,(&C000)" but Game Boy can't do that.
; now we do the swap.
ld hl,&D000
ld a,(hl) ;get the byte at &D000
ld (&C000),a ;store it into &C000
ld (hl),c ;store the old byte at &C000 into &D000
inc hl ;inc HL to &D001
ld a,(hl) ;get the byte at &D001
ld (&C001),a ;store it at &C001
ld (hl),b ;store the old byte at &C001 into &D001
pop bc