IsoKilo

Needle Haystack 68k ASM Solution

Jan 16th, 2026 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
M68000 Assembler 1.07 KB | Source Code | 0 0
  1. ; Context - A friend on Discord linked a basic leetcode problem, where in the code must return 0 if it can match the needle string in the haystack string, and if it fails, return -1. Did this 68k ASM version for funsies.
  2. entry:
  3.     lea     haystack, a0        ; Get string we're comparing.
  4.     lea     needle, a1          ; Get key we're looking for.
  5.  
  6. loop:
  7.     cmp.b   (a0)+, (a1)+        ; Compare characters in key based on correct chars so far.
  8.     bne.s   @resetcount         ; If they don't match, don't increment
  9.     bra.s   @cont
  10.  
  11. @resetcount:
  12.     lea     needle, a1          ; Reload needle string from start.
  13.  
  14. @cont:
  15.     cmp.b   #$FF, (a1)          ; Have we hit the end of the needle string?
  16.     beq.s   @pass               ; If so then we pass.
  17.     cmp.b   #$FF, (a0)          ; Have we hit the end of the haystack string?
  18.     beq.s   @fail               ; If so then we fail.
  19.     bra.s   loop                ; Loop until we either pass or fail.
  20.  
  21. @fail:
  22.     moveq   #0, d7              ; Report no match.
  23.     bra.s   freeze
  24.  
  25. @pass:
  26.     moveq   #-1, d7             ; Report success.
  27.  
  28. freeze:
  29.     bra.s   freeze              ; Freeze program to allow memory examination.
  30.  
  31. haystack:
  32.     dc.b    "sadbutsad", $FF
  33.  
  34. needle:
  35.     dc.b    "sad", $FF
  36.     end
Tags: basic 68k
Advertisement
Add Comment
Please, Sign In to add comment