Advertisement
luckytyphlosion

longsqrt

Feb 8th, 2016
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LongIntegerSqrt:
  2.     ld a, [H_QUOTIENT]
  3.     and a
  4.     jp z, .sqrt32bit
  5.     ld a, [H_QUOTIENT+1]
  6.     and a
  7.     jp z, .regularSqrt
  8.     ld de, 255 * 255
  9.     ld a, [H_QUOTIENT+3]
  10.     sub e
  11.     ld [H_QUOTIENT+3], a
  12.     ld a, [H_QUOTIENT+2]
  13.     sbc d
  14.     ld [H_QUOTIENT+2], a
  15.     ld a, [H_QUOTIENT+1]
  16.     sbc $0
  17.     ld [H_QUOTIENT+1], a
  18.     ld de, 509 ; (255 * 2) - 1
  19.     ld bc, 255
  20. .sqrtLongLoop_24Bit
  21.     inc bc
  22.     inc de
  23.     inc e
  24.     ld a, [H_QUOTIENT+3]
  25.     sub e
  26.     ld [H_QUOTIENT+3], a
  27.     ld a, [H_QUOTIENT+2]
  28.     sbc d
  29.     ld [H_QUOTIENT+2], a
  30.     ld a, [H_QUOTIENT+1]
  31.     sbc $0
  32.     ld [H_QUOTIENT+1], a
  33.     jr nc, .sqrtHLLoop
  34.     ld h, b
  35.     ld l, c
  36.     ret
  37. .sqrt32Bit
  38.     ld de, (4095 * 4095) & $ffff
  39.     ld a, [H_QUOTIENT+3]
  40.     sub e
  41.     ld [H_QUOTIENT+3], a
  42.     ld a, [H_QUOTIENT+2]
  43.     sbc d
  44.     ld [H_QUOTIENT+2], a
  45.     ld a, [H_QUOTIENT+1]
  46.     sbc (4095 * 4095) / $10000
  47.     ld [H_QUOTIENT+1], a
  48.     ld a, [H_QUOTIENT]
  49.     sbc $0
  50.     ld [H_QUOTIENT], a
  51.     ld c, 0
  52.     ld de, 8189 ; (4095 * 2) - 1
  53.     ld hl, 4095
  54. .sqrtLongLoop_32Bit
  55.     inc de
  56.     ld a, d
  57.     or e
  58.     jr nz, .noCarry
  59.     inc c
  60. .noCarry
  61.     inc e
  62.     inc hl
  63.     ld a, [H_QUOTIENT+3]
  64.     sub e
  65.     ld [H_QUOTIENT+3], a
  66.     ld a, [H_QUOTIENT+2]
  67.     sbc d
  68.     ld [H_QUOTIENT+2], a
  69.     ld a, [H_QUOTIENT+1]
  70.     sbc c
  71.     ld [H_QUOTIENT+1], a
  72.     ld a, [H_QUOTIENT]
  73.     sbc $0
  74.     ld [H_QUOTIENT], a
  75.     jr nc, .sqrtLongLoop_32Bit
  76.     ret
  77. .regularSqrt
  78.     ld a, [H_QUOTIENT+2]
  79.     ld h, a
  80.     ld a, [H_QUOTIENT+3]
  81.     ld l, a
  82.     ld a, -1
  83.     ld bc, $1
  84. .sqrtHLLoop
  85.     inc a
  86.     dec c
  87.     dec bc
  88.     add hl, bc
  89.     jr c, .sqrtHLLoop
  90.     ld l, a
  91.     ld h, $0
  92.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement