assembly - JMP instruction in NASM gives segmentation fault -


this code concatenate 2 strings in nasm. got segmentation fault core dumped error. started commenting find source of error. can see in code used %ifdef , %endif create block of comment. when excluded line "jmp l1" comment, gave segmentation fault. me telling me why jmp giving segmentation fault?

extern scanf extern printf  section .bss str1: resb 20 str2: resb 10  section .data msg1: db "enter data:",0  msg2: db "%s",0 msg3: db "you entered %s",10,0  msg4: db "%s",0  test: db "test",0  section .text global main main: push msg1 call printf add esp,4  push str1 push msg4 call scanf add esp,8  push msg1 call printf add esp,4  push str2 push msg4 call scanf add esp,8   mov ecx,0 mov edx,0 l1: mov al,byte[str1+ecx]  cmp al,13  je next  inc ecx %ifdef jmp l1  next:cmp byte[str2+edx],13 je finish mov al,byte[str2+edx] mov byte[str1+ecx],al inc ecx inc edx jmp next  finish: %endif next: push str1 push msg3 call printf add esp,8  mov eax,1 mov ebx,0 int 80h 

most likely, problem string doesn't contain carriage return character (ascii value 13).

after each failed check (i.e. cmp al, 13), ecx register incremented , jmp l1 instruction creates loop. you're going through string looking value 13, if 13 doesn't exist in string loop never terminates. @ point try access memory process not have permissions access. seg fault.

most need terminating condition stop loop if reach end of string, null character (value 0). like:

l1: mov al,byte[str1+ecx] cmp al,13 je next ; added next 2 lines check end of string cmp al,0   ; if value 0, @ end of string je notfound  inc ecx jmp l1 

(for nitpickers: realize there faster ways of checking al==0. chose cmp because it's more clear, , easier beginner understand.)