hi all,

total noob here with assembly. I'm working on a project for one my classes and for some reason we all keep getting segmentation faults when attempting to run the program.

I can't for the life of me figure out what the heck is going on and why this keep happening. I have tried to go though the program and remove things little by little to see if I could find the cancerous lines but I was unable.. As a last resort I've come to Daniweb to see if anyone can spot it for me... (My instructor was not able to...)

.section .data

	Sregs:
		.long 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
	OPtbl:
		.long meth_exit
		.long meth_nim
		.long meth_nim
		.long meth_nim
		.long meth_ldr	 
		.long meth_ldri 
		.long meth_ldrm  
		.long meth_ldrt  

		.rept 256-((. - OPtbl)/4) 
		.long meth_nim
		.endr

	Smem:
		.byte 0x04, 0x33, 0x30 

.section .text

.globl _start

_start:
	movl $0, %edi

	loop_fetch:
	
		movb Smem(,%edi,1),%al

		#//And the OP code in %eax with
		#//dec(255) or bin(11111111)
		#//This clears any garbage left 
		#//in the top portion of register eax
		andl $0xff,%eax

		#//Now that %eax is clean and we have 
		#//our OP code we will move the instructions
		#//for that operation into register %eax
		#//killing the OP code but we have the instructions
		#//for that OP code instead now
		movl OPtbl(,%eax,4),%eax

		#//increment %edi to the next memory location
		incl %edi
	
		#//call the operation code we put
		#//into %eax register.
		call *%eax
	
		#//Jump to beginning of the loop
		#//and repeat the process
		jmp loop_fetch

		#//END OF loop_fetch


		#//Begin not implemented method
	meth_nim:
	
		#//Increment register edi to move to the
		#//next memory location
		incl %edi

		#//Return to the call location
		ret

		#//End nim method

		#//Begin loader method
	meth_ldr:

		#//Get next byte in Smem
		#//which contains the Rd
		#//mov Rd to %eax
		movb Smem(,%edi,1),%al
	
		#//[%eax] = | 0000 0000 | 0000 0000 | 0000 0000| 0011 0011 | // hex: 33

		#//Convert from string "3" to int 3
		#//by anding off the first 3 in hex 33
		andl $0x0f,%eax #//1 f saves 4bits

		#//[%eax] = | 0000 0000 | 0000 0000 | 0000 0000| 0011 0011 | // hex: 33
		#//[andl] = | 0000 0000 | 0000 0000 | 0000 0000| 0000 1111 | // hex: 0F
		#//	----------------------------------------------------------------
		#//[%eax] = | 0000 0000 | 0000 0000 | 0000 0000| 0000 0011 | // hex: 03

		#//^Now we have Rd
	
		#//Inc index to next byte of Smem
		incl %edi

		//Get the data from Smem and place
		#//it into %ebx
		movb Smem(%edi), %bl

		#//Convert from string to int
		andl $0x0f, %ebx
	
		#//^Now we have both Rd & Rs
		#//[%eax] = Rd
		#//[%ebx] = Rs

		#//Get whats in the Sregs[Rs]
		#//store it in %ebx
		movl Sregs(%ebx), %ebx

		#//Now move it from %ebx into
		#//Sregs[Rd]
		movl %ebx, Sregs(%eax)

		#//Increment %edi to point to 
		#//next byte of Smem
		incl %edi

		#//Return to the call location
		ret

		#//End loader method

		#//Begin loader i method[Rd,$i]
	meth_ldri:

		#//Get next byte in Smem
		#//which contains the Rd
		#//mov Rd to %eax
		movb Smem(, %edi,1), %al
	
		#//[%eax] = | 0000 0000 | 0000 0000 | 0000 0000| 00011 0011 | // hex: 33

		#//Convert from string "3" to int 3
		#//by anding off the first 3 in hex 33
		andl $0x0f, %eax #//1 f saves 4bits

		#//[%eax] = | 0000 0000 | 0000 0000 | 0000 0000| 0011 0011 | // hex: 33
		#//[andl] = | 0000 0000 | 0000 0000 | 0000 0000| 0000 1111 | // hex: 0F
		#//	----------------------------------------------------------------
		#//[%eax] = | 0000 0000 | 0000 0000 | 0000 0000| 0000 0011 | // hex: 03

		#//^Now we have Rd

		#//Increment %edi to get next
		#//byte of Smem 
		incl %edi

		#//Get the $i value and place
		#//it into %ebx
		movb Smem(%edi), %bl

		#//Clean off high end of %ebx
		#//No conversion here; saving whole byte
		andl $0xff, %ebx	

		#//^Now we have both Rd & $i
		#//[%eax] = Rd
		#//[%ebx] = $i

		#//Place the $i value into Sregs[Rd]
		movl %ebx, Sregs(%eax)

		#//Increment the index to the next byte
		#//of Smem
		incl %edi

		#//Return to the call location
		ret

		#//End loader i method

		#//Begin loader m method 
	meth_ldrm:

		jmp meth_nim
	
		#//End loader m method

		#//Begin loader t method
	meth_ldrt:

		jmp meth_nim

		#//End loader t method

	meth_exit:

		movl $1, %ebx

		movl $42, %ebx

		int $0x80

Thanks all for your suggestions/tips..

~eff

I believe I have figured out the problem.. it seems that infinite loops are not liked very well, after playing with some other programs that I knew worked I figured out that the real difference between them was that one program specifically had an exit routine and a "terminator" was placed in the data being processed. Once it was hit the program fired its exit routine. So just for kicks I implemented the same situation into my broken program and Eureka.

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.