git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/asm/z80/reverse.s

      1 ;--------------------------------------------------------------------------
      2 ;  reverse.s
      3 ;
      4 ;  Copyright (C) 2026, Phidias618
      5 ;
      6 ;--------------------------------------------------------------------------
      7 
      8         .module reverse
      9 
     10         .area   _CODE
     11 
     12 _reverse::
     13         ld d, h
     14         ld e, l
     15 
     16         xor a
     17         cpi
     18         ret z      ; return if length == 0
     19         cpi
     20         ret z      ; return if length == 1 
     21 
     22         ; determine the middle of the string
     23         ld b, a
     24         ld c, a
     25         cpir
     26         add hl, de
     27         rr h
     28         rr l
     29         
     30         ld b, h
     31         ld c, l
     32 
     33         jr c, 0$
     34         dec hl
     35 0$:
     36         ; swap pairs of characters starting from the middle of the string until the 0 terminator is found
     37         ld a, (bc)
     38 1$:
     39         dec hl
     40         ld e, (hl)
     41         ld (hl), a
     42         ld a, e
     43         ld (bc), a
     44 
     45         inc bc
     46         ld a, (bc)
     47         or a
     48         jp nz, 1$        ; exit if 0 terminator
     49 
     50         ; return the address of the string in de
     51         ex de, hl
     52         ret

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.