Author Topic: segmentation fault core dump  (Read 5063 times)

Offline ravi0807

  • Jr. Member
  • *
  • Posts: 9
segmentation fault core dump
« on: April 15, 2014, 04:03:18 AM »
this program is to
1 calculate string length
2 print reverse string
3 check palindrome

program gives segmentation fault after calculation string length
if i run only run Reverse procedure then also it gives segmentation error just after printing reverse string

it only run 1 procedure ....display its result, then hang there only till i press enter.After pressing enter it display Segmentation Fault core dump

Plz tell me which are the various condition that leads to segmentation fault
i even try to change registers then also having same error
Code: [Select]
section .data
length db 10,'length of string is::'
lengths equ $-length
msgp db 10,'palindrome'
msgps equ $-msgp
msgnp db 10,'not palindrome'
msgnps equ $-msgnp

section .bss
str12 resb 20
str1 resb 20
str1s equ $-str1
result resb 3
resv resb 20
resvs equ $-resv
len resb 6


%macro print 2
mov eax,4
mov ebx,00
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro read 2
mov eax,3
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro

section .text
global _start
_start:

lengh:
read str12,20
dec eax
mov [len],eax
mov ebx,eax
call disp
print length,lengths
print result,2

reverse:
read str1,20
dec eax
mov [len],eax
mov esi,str1
mov ecx,[len]
add esi,ecx
dec esi
mov edi,resv

again:
mov al,[esi]
mov [edi],al
dec esi
inc edi
dec ecx
jnz again

print resv,10

palin:

mov esi,str1
mov edi,resv
mov ecx,[len]

pln:mov al,[esi]
cmp [edi],al
jne ntp
inc esi
inc edi
loop pln

print msgp,msgps
mov eax,1
mov ebx,1
int 80h
ntp:print msgnp,msgnps
mov eax,1
mov ebx,1
int 80h


disp:mov ecx,04
mov esi,result
ck:rol bl,4
mov al,bl
and al,0fh
cmp al,09
jbe dn
add al,07h
dn:add al,30h
mov [esi],al
inc esi
loop ck
ret
« Last Edit: April 15, 2014, 05:27:56 AM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: segmentation fault core dump
« Reply #1 on: April 15, 2014, 07:01:13 AM »
Please use "code tags". The word "code" in square brackets at the start of your code and "/code" in square brackets at the end. Makes it easier to cut and paste. :)
Code: [Select]
section .data
length db 10,'length of string is::'
lengths equ $-length
msgp db 10,'palindrome'
msgps equ $-msgp
msgnp db 10,'not palindrome'
msgnps equ $-msgnp

section .bss
str12 resb 20
str1 resb 20
str1s equ $-str1
result resb 3 ; only three bytes, but you write 4 to it!
resv resb 20
resvs equ $-resv
len resb 6


%macro print 2
mov eax,4
mov ebx,00 ; stdin!
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro read 2
mov eax,3
mov ebx,1 ; stdout!
mov ecx,%1
mov edx,%2
int 80h
%endmacro

section .text
global _start
_start:

lengh:
read str12,20
dec eax
; what if the pesky user didn't enter anything?
; gonna loop a lot of times!
mov [len],eax
mov ebx,eax
call disp
print length,lengths
print result,2

reverse:
read str1,20
dec eax
; what if the pesky user didn't enter anything?
; gonna loop a lot of times!
mov [len],eax
mov esi,str1
mov ecx,[len]
add esi,ecx
dec esi
mov edi,resv

again:
mov al,[esi]
mov [edi],al
dec esi
inc edi
dec ecx
jnz again

print resv,10

palin:

mov esi,str1
mov edi,resv
mov ecx,[len]

pln:mov al,[esi]
cmp [edi],al
jne ntp
inc esi
inc edi
loop pln

print msgp,msgps
mov eax,1
mov ebx,1
int 80h
ntp:print msgnp,msgnps
mov eax,1
mov ebx,1
int 80h


disp:mov ecx,04 ; result is only 3 bytes!
mov esi,result
ck:rol bl,4
mov al,bl
and al,0fh
cmp al,09
jbe dn
add al,07h
dn:add al,30h
mov [esi],al
inc esi
loop ck
ret

Curiously, writing to stdin and reading from stdout works. That's not your problem. What if the pesky user just hits "enter"? (you didn't prompt us!)  sys_read returns 1, you decrement that and put it in "[len]". Then you put that in ecx and decrement it until it becomes zero (again). Long before that you run off the end of your allotted memory and segfault. I don't know what you want to do about this. Spank the user and make 'em enter it again? Assume user is done and just jump to exit? (that's what I did to test it) There's a "jecxz" instruction you could use to skip the loop if ecx is already zero. Do something!

There may be more, but that's all I saw on first glance. The actual "work" seems to be pretty good. "A man a plan a canal Panama" is not a palindrome because of the spaces, and "case" (it's a little too long, also). Would you want to "improve" your program to handle that? Get the "simple" version running first, I guess...

Best,
Frank