Advertisement
Cthutu

String comparison routine

Mar 10th, 2020
943
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;----------------------------------------------------------------------------------------------------------------------
  2. ;; strcmp
  3. ;; Test a string until a difference occurs.  A will be the difference (1st different char - 2nd different char).
  4. ;; This means that A < 0 (signed 8-bit) then first string comes before second string.  If A == 0 (or Z flag is set), strings are the
  5. ;; same up until they are terminated (with a 0).
  6. ;;
  7. ;; Input:
  8. ;;      DE = 1st string (signed characters)
  9. ;;      HL = 2nd string (signed characters)
  10. ;;
  11. ;; Output:
  12. ;;      A = difference of first different character (0 if strings are the same)
  13. ;;      ZF = 1 if strings are the same
  14. ;;
  15.  
  16. strcmp:
  17.                 push    bc
  18.                 push    de
  19.                 push    hl
  20.  
  21. .l1             ld      a,(de)
  22.                 cpi                     ; ZF affected by comparison
  23.                 jr      nz,.no_match    ; If ZF=0, then a difference is discovered
  24.                 inc     de
  25.  
  26.                 ; Both characters are the same, so check for null termination for both
  27.                 and     a
  28.                 jr      nz,.l1
  29.  
  30.                 jr      .end
  31.  
  32. .no_match       ; A = difference (or 0 if both terminated)
  33.                 dec     hl
  34.                 sub     (hl)
  35. .end            pop     hl
  36.                 pop     de
  37.                 pop     bc
  38.                 ret
Advertisement
RAW Paste Data Copied
Advertisement