Author Topic: Segmentation Fault(Core Dumped)  (Read 3921 times)

Offline amitprasad53121

  • Jr. Member
  • *
  • Posts: 2
Segmentation Fault(Core Dumped)
« on: April 13, 2014, 09:13:41 PM »
I have tried making this simple program for multiplication using successive addition alogrithm
But my program is not accepting the second number
and then it terminates and displays a message segmentation fault(core dumped)
please Help me
Code: [Select]
section .data
welcum db 10,"Welcome To successive addition"
welcum_len equ $-welcum

msg1 db 10,"Enter First Number",10
msg1_len equ $-msg1

msg2 db 10,"Enter Second Number",10
msg2_len equ $-msg2

resultmsg db 10,"The result is",10
result_len equ $-resultmsg

test1 db 10, "Test Message!!!!!",10
test_len equ $-test1


section .bss

result resw 1
num1 resb 2
num2 resb 2
x resb 1
count resb 1

res_buff resb 4

%macro disp 2
mov eax,04
mov ebx,01
mov ecx,%1
mov edx,%2
int 80h

%macro accept 2
mov eax,03
mov ebx,01
mov ecx,%1
mov edx,%2
int 80h


section .text
global _start

disp welcum,welcum_len

disp msg1,msg1_len
accept num1,2
mov esi,num1
call packnum2
mov byte[x],bl

disp msg2,msg2_len
accept num2,2
mov esi,num2
call packnum2
mov byte[count],bl

mov dl,byte[x]
xor ecx,ecx
mov cl,byte[count]
mov word[result],0000h

add [result],dl
loop loop1

disp resultmsg,result_len
call disp16

mov eax,01
mov ebx,00
int 80h


mov ecx,2
mov bl,00

rol bl,4
mov al,[esi]
cmp al,39h
jbe sub30
sub al,07h
sub al,30h
add bl,al
inc esi
loop l1

mov edx,[result]
mov edi,res_buff
mov cx,4

rol edx,4
mov bl,dl
and bl,0fh
cmp bl,09h
jbe add30
add bl,07h
add bl,30h
mov [edi],bl
inc edi
loop back

disp res_buff,4
« Last Edit: April 13, 2014, 09:33:48 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2358
  • Country: us
Re: Segmentation Fault(Core Dumped)
« Reply #1 on: April 13, 2014, 11:32:57 PM »
The segfault is from loading just cx with your loopcount (in disp16). "loop" works with ecx. It is possible to force "loop" to use just cx - easier to just use ecx.

Refusing to accept your second number is because sys_read (on stdin) won't return until "enter" is hit. If it won't fit in the buffer, it remains in the OS's input queue ("keyboard buffer"?) to screw up our next read (which may be the shell, if we exit in that state).. If you can guarantee a "well behaved user", you can just add an extra byte to the buffer and to edx. If the pesky user types too much, we're still messed up. Better to flush the buffer. I just learned a neat trick over on stack overflow... but I didn't get it to work with your code. I may get into it and figure out what I was doing wrong. Meanwhile, here's an extremely crude method:
Code: [Select]

%macro accept 2
mov eax,03
mov ebx,01
mov ecx,%1
mov edx,%2
int 80h

push eax
cmp byte [ecx + eax - 1], 10 ;LF
jz %%goodread
mov eax, 3 ; sys_read
mov ebx, 0
mov ecx, dummybuf
mov edx, 1
int 80h
jmp %%checklf
pop eax

I hate to introduce such cruft, especially in a macro that's going o be duplicated all over the code. However, it seems to "work" and is fairly easy to understand(?).

Still doesn't appear to produce correct results (I'm not entirely certain what you're trying to do), but may get you closer. At least it doesn't segfault...


Offline amitprasad53121

  • Jr. Member
  • *
  • Posts: 2
Re: Segmentation Fault(Core Dumped)
« Reply #2 on: April 14, 2014, 03:33:52 AM »
Dude you are are genius!!!!
Its not giving the correct output though... there's a problem with my logic part i think so. Ill check it. But finally the segmentation fault has gone.
Could you please tell me in which cases this segmentfault occurs. And what should we check for inorder to remove it.

Do you have any tips for me to become a good programmer like you??