Skip to main content
Code Review

Return to Answer

section .data
 ; the string
 string: db "this is the string we are searching in"
 stringlength: equ $-stringlengthstring
 ; the substring
 substring: db "string we are searching"
 substringlength: equ $-substring 
 
 mov rsi, string ; pointer to string in RSI
 mov rdx, stringlength ; length of string in RDX
 ; Subtract substring length to prevent looking beyond the string length,
 ; We can also check here if the substring fits in the string.
 ; If not we never can find the substring in the string
 sub rdx, substringlength
 cmp rdx, 0
 jl .@@notfound
 ; enter the compare loop
.@@repeat: 
 mov rdi, substring ; pointer to substring in RDI
 mov rcx, substringlength ; length substring in RCX (loop counter)
 cld
 repe cmpsb ; compare string at rdi with length rcx with string in rsi
 jz .@@found ; if zero flag then substring is found within string, exit loop
 ; substring is not found yet, put substring pointer at begin of substring
 dec rdx ; decrement length of string
 and rdx, rdx ; check remaining length to search in
 jnz .@@repeat ; remaining length non-zero, repeat
.@@notfound:
 ; else, substring wasn't found, exit loop
 ; substring not found actions
 
.@@found:
 ; substring found actions
 ; rsi has address to start of substring+the length of the substring
 ; subtracting the start of the string we can calculate the offset (or index) in the string where substring starts
 sub rsi, substringlength
section .data
 ; the string
 string: db "this is the string we are searching in"
 stringlength: equ $-stringlength
 ; the substring
 substring: db "string we are searching"
 substringlength: equ $-substring 
 
 mov rsi, string ; pointer to string in RSI
 mov rdx, stringlength ; length of string in RDX
 ; Subtract substring length to prevent looking beyond the string length,
 ; We can also check here if the substring fits in the string.
 ; If not we never can find the substring in the string
 sub rdx, substringlength
 cmp rdx, 0
 jl .@@notfound
 ; enter the compare loop
.@@repeat: 
 mov rdi, substring ; pointer to substring in RDI
 mov rcx, substringlength ; length substring in RCX (loop counter)
 cld
 repe cmpsb ; compare string at rdi with length rcx with string in rsi
 jz .@@found ; if zero flag then substring is found within string, exit loop
 ; substring is not found yet, put substring pointer at begin of substring
 dec rdx ; decrement length of string
 and rdx, rdx ; check remaining length to search in
 jnz .@@repeat ; remaining length non-zero, repeat
.@@notfound:
 ; else, substring wasn't found, exit loop
 ; substring not found actions
 
.@@found:
 ; substring found actions
 ; rsi has address to start of substring+the length of the substring
 ; subtracting the start of the string we can calculate the offset (or index) in the string where substring starts
 sub rsi, substringlength
section .data
 ; the string
 string: db "this is the string we are searching in"
 stringlength: equ $-string
 ; the substring
 substring: db "string we are searching"
 substringlength: equ $-substring 
 
 mov rsi, string ; pointer to string in RSI
 mov rdx, stringlength ; length of string in RDX
 ; Subtract substring length to prevent looking beyond the string length,
 ; We can also check here if the substring fits in the string.
 ; If not we never can find the substring in the string
 sub rdx, substringlength
 cmp rdx, 0
 jl .@@notfound
 ; enter the compare loop
.@@repeat: 
 mov rdi, substring ; pointer to substring in RDI
 mov rcx, substringlength ; length substring in RCX (loop counter)
 cld
 repe cmpsb ; compare string at rdi with length rcx with string in rsi
 jz .@@found ; if zero flag then substring is found within string, exit loop
 ; substring is not found yet, put substring pointer at begin of substring
 dec rdx ; decrement length of string
 and rdx, rdx ; check remaining length to search in
 jnz .@@repeat ; remaining length non-zero, repeat
.@@notfound:
 ; else, substring wasn't found, exit loop
 ; substring not found actions
 
.@@found:
 ; substring found actions
 ; rsi has address to start of substring+the length of the substring
 ; subtracting the start of the string we can calculate the offset (or index) in the string where substring starts
 sub rsi, substringlength
Notice removed Needs detailed answers by Jamal
deleted 103 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Maybe next code snippet can be useful? (perhaps I am a bit to late but anyway) weWe are lookingdetermining if a substring appears in a string. This This algorithm/code snippet is written for x86_64 intelIntel on linuxLinux with nasmNASM. It's quitquite shorter but because of the repe cmpsbrepe cmpsb, it's certainly not the fastest.

I hope this will be helpful

Maybe next code snippet can be useful? (perhaps I am a bit to late but anyway) we are looking if a substring appears in a string. This algorithm/code snippet is written for x86_64 intel on linux with nasm. It's quit shorter but because of the repe cmpsb certainly not the fastest.

I hope this will be helpful

We are determining if a substring appears in a string. This algorithm/code snippet is written for x86_64 Intel on Linux with NASM. It's quite shorter but because of the repe cmpsb, it's certainly not the fastest.

deleted 66 characters in body
Source Link
Agguro
  • 131
  • 3
section .data
 ; the string
 string: db "this is the string we are searching in"
 stringlength: equ $-stringlength
 ; the substring
 substring: db "string we are searching"
 substringlength: equ $-substring 
 
 mov rsi, string ; pointer to string in RSI
 mov rdx, stringlength ; length of string in RDX
 ; Subtract substring length to prevent looking beyond the string length,
 ; We can also check here if the substring fits in the string.
 ; If not we never can find the substring in the string
 sub rdx, substringlength
 cmp rdx, 0
 jl .@@notfound
 ; enter the compare loop
.@@repeat: 
 mov rdi, substring ; pointer to substring in RDI
 mov rcx, substringlength ; length substring in RCX (loop counter)
 cld
 repe cmpsb ; compare string at rdi with length rcx with string in rsi
 jz .@@found ; if zero flag then substring is found within string, exit loop
 ; substring is not found yet, moveput tosubstring nextpointer characterat inbegin theof string
substring
 inc rsi ; next character address
 dec rdx ; decrement length of string
 and rdx, rdx ; check remaining length to search in
 jnz .@@repeat ; remaining length non-zero, repeat
.@@notfound:
 ; else, substring wasn't found, exit loop
 ; substring not found actions
 
.@@found:
 ; substring found actions
 ; rsi has address to start of substring+the length of the substring
 ; subtracting the start of the string we can calculate the offset (or index) in the string where substring starts
 sub rsi, substringlength
section .data
 ; the string
 string: db "this is the string we are searching in"
 stringlength: equ $-stringlength
 ; the substring
 substring: db "string we are searching"
 substringlength: equ $-substring 
 
 mov rsi, string ; pointer to string in RSI
 mov rdx, stringlength ; length of string in RDX
 ; Subtract substring length to prevent looking beyond the string length,
 ; We can also check here if the substring fits in the string.
 ; If not we never can find the substring in the string
 sub rdx, substringlength
 cmp rdx, 0
 jl .@@notfound
 ; enter the compare loop
.@@repeat: 
 mov rdi, substring ; pointer to substring in RDI
 mov rcx, substringlength ; length substring in RCX (loop counter)
 cld
 repe cmpsb ; compare string at rdi with length rcx with string in rsi
 jz .@@found ; if zero flag then substring is found within string, exit loop
 ; substring is not found yet, move to next character in the string

 inc rsi ; next character address
 dec rdx ; decrement length of string
 and rdx, rdx ; check remaining length to search in
 jnz .@@repeat ; remaining length non-zero, repeat
.@@notfound:
 ; else, substring wasn't found, exit loop
 ; substring not found actions
 
.@@found:
 ; substring found actions
 ; rsi has address to start of substring+the length of the substring
 ; subtracting the start of the string we can calculate the offset (or index) in the string where substring starts
 sub rsi, substringlength
section .data
 ; the string
 string: db "this is the string we are searching in"
 stringlength: equ $-stringlength
 ; the substring
 substring: db "string we are searching"
 substringlength: equ $-substring 
 
 mov rsi, string ; pointer to string in RSI
 mov rdx, stringlength ; length of string in RDX
 ; Subtract substring length to prevent looking beyond the string length,
 ; We can also check here if the substring fits in the string.
 ; If not we never can find the substring in the string
 sub rdx, substringlength
 cmp rdx, 0
 jl .@@notfound
 ; enter the compare loop
.@@repeat: 
 mov rdi, substring ; pointer to substring in RDI
 mov rcx, substringlength ; length substring in RCX (loop counter)
 cld
 repe cmpsb ; compare string at rdi with length rcx with string in rsi
 jz .@@found ; if zero flag then substring is found within string, exit loop
 ; substring is not found yet, put substring pointer at begin of substring
 dec rdx ; decrement length of string
 and rdx, rdx ; check remaining length to search in
 jnz .@@repeat ; remaining length non-zero, repeat
.@@notfound:
 ; else, substring wasn't found, exit loop
 ; substring not found actions
 
.@@found:
 ; substring found actions
 ; rsi has address to start of substring+the length of the substring
 ; subtracting the start of the string we can calculate the offset (or index) in the string where substring starts
 sub rsi, substringlength
error is index calculation, modified to "sub rsi, substringlength" which is the right value.
Source Link
Agguro
  • 131
  • 3
Loading
moderator ask me to add explanation
Source Link
Agguro
  • 131
  • 3
Loading
moderator ask me to add explanation
Source Link
Agguro
  • 131
  • 3
Loading
Notice added Needs detailed answers by Jamal
Source Link
Agguro
  • 131
  • 3
Loading
lang-lisp

AltStyle によって変換されたページ (->オリジナル) /