Hello!

I have a piece of code here that I just can't get to work. I get segmentation fault and now I'm trying to apply a ddd tool to understand the problem. I haven't used ddd before but there seem to be some kind of a problem around the subroutines fgets or gettext. I have been putting a breakpoint at various places but I guess I'm doing some mistake here.

fgets reads what comes from the keyboard and then I'm supposed to transfer that piece of text(in this case, for the moment, 2 numbers ) into a buffer (INBUFFERT ) with gettext. But I'm obviously doing something wrong here.

Putting a breakpoint at fgets and then stepi I get the message "a syntax error in expression, near 0x123d30"
Here's the code:

.equ 	MAXPOS, 40
.equ 	POS, 44
.equ 	MINPOS, 48

.data 
Head:		.asciz	"Start of testprogram.Put in 2 numbers!"
Show_Integ:	.asciz	"%d\n"
Buf:		.skip	64
N:		.long	0
T:		.long	0
Show:		.asciz	"%s\n"

Intg:		.long	0

UTBUFFERT:	.skip 128
INBUFFERT:	.skip 128

.text
.globl main, puttext, outimage, inimage, getint


main:		
		pushl		$Head		/*Head lagras på stacken*/
		call		puttext		/*Denna text läggs nu in i UTBUFFERT*/
		call		outimage	/*Texten tas från UTBUFFERT och skrivs nu ut på skärmen*/
		call		inimage
		movl		$5, N
		call		getint
		
		call		printgetint_end
		call		exit

printgetint_end:	
		pushl	%eax
		pushl	$Show_Integ
		call	printf		
		addl	$8, %esp	
		movl	$0, %eax
		ret

puttext:
		pushl	%ebp
		movl	%esp, %ebp
		movl	$UTBUFFERT, %ecx
		movl	8(%ebp), %ebx

next:		movb	(%ebx), %al
		cmpb	$0, %al
		je	puttextLoopEnd
		movb	%al, (%ecx)
		incb	%cl
		incb	%bl
		jmp	next

puttextLoopEnd:
		movl	%ebp, %esp
		popl	%ebp
		ret

outimage:	
		pushl	$UTBUFFERT
		pushl	$Show
		call 	printf
		addl	$8, %esp 
		ret

inimage:	
		pushl	stdin
		pushl	$79
		pushl	Buf
		call	fgets
		addl	$12,%esp
		xorl	%ecx, %ecx		/*position = 0*/

gettext:	pushl	%ebp
		movl	%esp, %ebp
		movl	$INBUFFERT, %ebx

gettext_loop:
		movb	(%eax), %dl
		cmpb	$13, %dl
		je	end_gettext
		movb	%dl, (%ebx)
		incl	%eax
		incl	%ebx
		incl	%ecx
		jmp	gettext_loop

end_gettext:
		movl	%ecx, MAXPOS(%ebp)
		movl	$0, %ecx
		movl	%ebp, %esp
		popl	%ebp
		ret

getint:	
		popl	%ebp
		movl	%esp, %ebp
		movl	$1, %edi
		call	getinpos

getchar:	
		movl	$INBUFFERT, (%ebx)
		cmpb	$45, (%ebx)		/*check to see if the character = '-'*/
		je	increase_charN		/*if so, jump to increase_charN*/
		jmp	getchar_loop		/*if not, jump to getchar_loop*/

getchar_loop:
		cmpb	$32, (%ebx)
		je	increase_char	
		movzx	(%ebx), %eax
		pushl	%eax
		incl	%ecx
		incl	%ebx
		jmp	getchar_loop

increase_char:	incl	%ecx
		incl	%ebx
		jmp	setinpos

getint_loop:
		popl	%eax
		subl	$48, %eax
		imul	%edi, %eax
		addl	%eax, Intg
		imul	$10, %edi
		decl	%ecx
		cmpl	$0, %ecx
		je	getint_end
		jmp	getint_loop
getint_end:
		xorl	%eax, %eax
		movl	Intg, %eax
		movl	%ebp, %esp
		popl	%ebp
		ret

setinpos:
		cmpl	$0, %ecx
		jb	setinpos_zero
		cmpl	MAXPOS(%ebp), %ecx
		jg	setinpos_max	
		movl	%ecx, POS(%ebp)
		jmp	getint_loop

setinpos_zero:	
		movl	$0, %ecx
		movl	%ecx, MINPOS(%ebp)
		jmp	getint_loop

setinpos_max:	
		movl	MAXPOS(%ebp), %ecx
		movl	%ecx, POS(%ebp)
		jmp	getint_loop

getinpos:	
		cmpl	$0, %ecx
		je	getchar
		movl	POS(%ebp), %ecx
		movl	%ecx, %ebx
		ret


getint_loopN:
		popl	%eax
		subl	$48, %eax
		imul	%edi, %eax
		addl	%eax, Intg
		imul	$10, %edi
		decl	%ecx
		cmpl	$0, %ecx
		je	getint_end
		jmp	getint_loopN
getint_endN:
		xorl	%eax, %eax
		movl	Intg, %eax
		movl	%ebp, %esp
		popl	%ebp
		ret

setinposN:
		cmpl	$0, %ecx
		jb	setinpos_zeroN
		cmpl	MAXPOS(%ebp), %ecx
		jg	setinpos_maxN	
		movl	%ecx, POS(%ebp)
		jmp	getint_loopN

setinpos_zeroN:	
		movl	$0, %ecx
		movl	%ecx, MINPOS(%ebp)
		jmp	getint_loopN

setinpos_maxN:	
		movl	MAXPOS(%ebp), %ecx
		movl	%ecx, POS(%ebp)
		jmp	getint_loopN

increase_charN:
		incl	%ecx
		incl	%ebx
		jmp	setinposN

getchar_loopN:
		cmpb	$32, (%ebx)
		je	increase_charN	
		movzx	(%ebx), %eax
		pushl	%eax
		incl	%ecx
		incl	%ebx
		jmp	getchar_loopN

With the code I just want to take the first number and print it out. If it's a negative number there are subroutines to take care of that. At least I hope they do.
Since I get the error message "Segmentation fault" I thought that with the ddd tool it should be easy to find out this problem but I don't think so. I understand that there is some kind of problem with the stack so I'm looking up the source - backtrace in order to see the stack but it doesn't help me. What shall I do in order to solve this problem?
Can anyone please help me out here?

Anders

Hi,
Please, forgive me if I am wrong, but I think that in the next part of your code:
getint:
popl %ebp
movl %esp, %ebp
movl $1, %edi
call getinpos
You really want to write at the first line:
pushl %ebp

I am wrong?

Yes you are right. I changed that, but still, it is the same kind of problem(Segmentation fault). Any more suggestions in what to do here?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.