Why am I getting a segmentation fault in my assembly code?

18 Views Asked by At

My code is trying to find the word 'hello' in a given string, and return 'Found' if it is there, and 'Not found' if not.

When I debug it using gdb, I get a segmentation fault when it branches back to search after the first iteration.

Here is my code.

.data

input:  .asciz "hello world"
f:      .asciz "Found!"
nf:     .asciz "Not found!"
find:   .asciz "hello"

.text
.global main
main:
    adr x19, input
    mov x22, #0
    adr x21, find
    mov x23, #0

search:
    ldrb w21, [x21, x23, lsl #0]
    ldrb w19, [x19, x22, lsl #0]    
    cmp w19, #0         
    beq not             
    
    cmp w21, #0     
    beq found           

    cmp w21, w19            
    bne cont            

    add x22, x22, #1
    add x23, x23, #1

    b search


cont:
    add x22, x22, #1        
    mov x23, #0         

    b search

found:
    adr x20, f
    mov x0, x20
    bl printf

    b end

not:
    adr x20, nf
    mov x0, x20
    bl printf

    b end

end:
    mov x0, #0
    ret
0

There are 0 best solutions below