Advertisement
Cthutu

Some simple utilities

Feb 19th, 2020
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;----------------------------------------------------------------------------------------------------------------------
  2. ;; 16-bit compare
  3.  
  4. compare16:
  5.         ; Input:
  6.         ;       HL = 1st value
  7.         ;       DE = 2nd value
  8.         ; Output:
  9.         ;       CF, ZF = results of comparison:
  10.         ;
  11.         ;               CF      ZF      Result
  12.         ;               -----------------------------------
  13.         ;               0       0       HL > DE
  14.         ;               0       1       HL == DE
  15.         ;               1       0       HL < DE
  16.         ;               1       1       Impossible
  17.         ;
  18.                 push    hl
  19.                 and     a
  20.                 sbc     hl,de
  21.                 pop     hl
  22.                 ret
  23.  
  24. ;;----------------------------------------------------------------------------------------------------------------------
  25. ;; 16-bit negate
  26.  
  27. negate16:
  28.         ; Input:
  29.         ;       HL = value
  30.         ; Output:
  31.         ;       HL = -value
  32.         ; Destroys:
  33.         ;       AF
  34.         ;
  35.         xor a
  36.         sub l
  37.         ld l,a
  38.         sbc a,a
  39.         sub h
  40.         ld h,a
  41.         ret
  42.  
  43. ;;----------------------------------------------------------------------------------------------------------------------
  44. ;; max
  45.  
  46. Max:
  47.         ; Input:
  48.         ;       HL = 1st value
  49.         ;       DE = 2nd value
  50.         ; Output:
  51.         ;       HL = maximum value
  52.         ;       DE = minimum value
  53.         ;       CF = 1 if DE was maximum
  54.         ;
  55.                 and     a
  56.                 sbc     hl,de
  57.                 jr      c,.choose_2nd   ; HL < DE?  Choose DE!
  58.                 add     hl,de           ; Restore HL
  59.                 ret
  60. .choose_2nd     add     hl,de
  61.                 ex      de,hl
  62.                 scf
  63.                 ret
  64.  
  65. ;;----------------------------------------------------------------------------------------------------------------------
  66. ;; alignUp8K
  67. ;; align a 16-bit value up to the nearest 8K
  68. ;;
  69. ;; Input:
  70. ;;      HL = value
  71. ;;
  72. ;; Output:
  73. ;;      HL = aligned value
  74.  
  75. alignUp8K:
  76.         push    af
  77.         ld      a,l
  78.         and     a               ; LSB == 0?
  79.         jr      nz,.alignup
  80.         ld      a,h
  81.         and     $1f
  82.         jr      nz,.alignup
  83.  
  84.         ; Value is already aligned
  85. .end    pop     af
  86.         ret
  87.  
  88. .alignup:
  89.         ld      l,0
  90.         ld      a,h
  91.         and     $e0
  92.         add     a,$20
  93.         ld      h,a
  94.         jr      .end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement