Advertisement
tari

Untitled

Sep 19th, 2010
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;Inputs:
  2. ;; HL=16-bit number to check palindrome status
  3. ;; IX=pointer to end of string buffer
  4. ;;Outputs:
  5. ;;  Condition NZ if number is not a palindrome, Z otherwise.
  6. isPalindrome:
  7.     ld b,5
  8. _hl2String:
  9.     push bc         ;Save loop counter
  10.     call divideHL10
  11.     ld a,l
  12.     add a,'0'
  13.     ld (ix),a       ;Write char
  14.     dec ix
  15.     ld l,c
  16.     ld h,b          ;Result of division
  17.     pop bc          ;Recover counter
  18.     djnz _hl2String
  19.     ld b,5
  20. _palChk:
  21.     dec b
  22.     ld a,b
  23.     ld (_palChk_offs+2),a   ;SMC magic
  24.     ld a,4
  25.     sub a,b
  26.     ld (_palChk_offs2+2),a  ;SMC magic
  27. _palChk_offs2:
  28.     ld a,(ix+0)             ;SMC
  29. _palChk_offs:
  30.     cp (ix+0)               ;SMC
  31.     ret nz
  32.     inc ix
  33.     djnz _palChk
  34.     ret
  35.    
  36. ;;divideHL10: divides HL by 10
  37. ;;Inputs:
  38. ;;  HL=divisor  
  39. ;;Outputs:
  40. ;;  HL=HL%10
  41. ;;  BC=HL/10
  42. ;;Modifies:
  43. ;;  BC,DE,HL
  44. divideHL10:
  45.     ld bc,0
  46.     or a            ;Reset carry for sbc
  47. _divHL10l:
  48.     ld de,10
  49.     sbc hl,de
  50.     jr c,_divHL10r
  51.     inc bc
  52.     jr _divHL10l
  53. _divHL10r:
  54.     add hl,de
  55.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement