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/sm83/reverse.s

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

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