Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.8KB

  1. ; Fill B bytes at (HL) with A
  2. fill:
  3. push bc
  4. push hl
  5. .loop:
  6. ld (hl), a
  7. inc hl
  8. djnz .loop
  9. pop hl
  10. pop bc
  11. ret
  12. ; Increase HL until the memory address it points to is equal to A for a maximum
  13. ; of 0xff bytes. Returns the new HL value as well as the number of bytes
  14. ; iterated in A.
  15. ; If a null char is encountered before we find A, processing is stopped in the
  16. ; same way as if we found our char (so, we look for A *or* 0)
  17. ; Set Z if the character is found. Unsets it if not
  18. findchar:
  19. push bc
  20. ld c, a ; let's use C as our cp target
  21. ld b, 0xff
  22. .loop: ld a, (hl)
  23. cp c
  24. jr z, .match
  25. or a ; cp 0
  26. jr z, .nomatch
  27. inc hl
  28. djnz .loop
  29. .nomatch:
  30. inc a ; unset Z
  31. jr .end
  32. .match:
  33. ; We ran 0xff-B loops. That's the result that goes in A.
  34. ld a, 0xff
  35. sub b
  36. cp a ; ensure Z
  37. .end:
  38. pop bc
  39. ret
  40. ; Compares strings pointed to by HL and DE up to A count of characters. If
  41. ; equal, Z is set. If not equal, Z is reset.
  42. strncmp:
  43. push bc
  44. push hl
  45. push de
  46. ld b, a
  47. .loop:
  48. ld a, (de)
  49. cp (hl)
  50. jr nz, .end ; not equal? break early. NZ is carried out
  51. ; to the called
  52. cp 0 ; If our chars are null, stop the cmp
  53. jr z, .end ; The positive result will be carried to the
  54. ; caller
  55. inc hl
  56. inc de
  57. djnz .loop
  58. ; We went through all chars with success, but our current Z flag is
  59. ; unset because of the cp 0. Let's do a dummy CP to set the Z flag.
  60. cp a
  61. .end:
  62. pop de
  63. pop hl
  64. pop bc
  65. ; Because we don't call anything else than CP that modify the Z flag,
  66. ; our Z value will be that of the last cp (reset if we broke the loop
  67. ; early, set otherwise)
  68. ret
  69. ; Transforms the character in A, if it's in the a-z range, into its upcase
  70. ; version.
  71. upcase:
  72. cp 'a'
  73. ret c ; A < 'a'. nothing to do
  74. cp 'z'+1
  75. ret nc ; A >= 'z'+1. nothing to do
  76. ; 'a' - 'A' == 0x20
  77. sub 0x20
  78. ret