LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2006, 02:35 PM   #1
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Rep: Reputation: 15
ASM Program Trouble


I have the following program. When I assemble and link it, it gives a segmentation error ... why?

Code:
#PURPOSE:	Program to illustrate how functions work.
#		This program will compute the value of
#		2^3 + 5^2
#

#Everything in the main program is stored in registers,
#so the data section doesn't have anything.

 .section data

 .section text

 .globl _start

_start:
 pushl $3	        #push second argument
 pushl $2               #push first argument
 call power	        #call the function
 addl $8, %esp	        #move the stack pointer back
 pushl %eax	        #save the first answer before calling the next function
 pushl $2	        #push the second argument
 pushl $5	        #push the first argument
 call power	        #call the function
 addl $8, %esp	        #move the stack pointer back
 popl %ebx	        #the second answer is already in %eax. We saved the first
		        #answer onto the stack, so now we can just pop it out into %ebx
 addl %eax, %ebx        #add them together, the result is in %ebx
 movl $1, %eax	        #exit, %ebx is returned
 int $0x80

#PURPOSE:	This function is used to compute
#		the value of a number raised to 
#		a power
#
#INPUT:		First argument - the base number
#		Second argument - the power to raise it to
#
#OUTPUT:	Will give the result as a return value
#
#NOTE: 		The power must be one or greater
#
#VARIABLES:	%ebx - holds the base number
#		%ecx - holds the power
#		-4(%ebp) - holds the current result
#		%eax is used for temporary storage
#

 .type power, @function
power:
 pushl %ebp	        #save old base pointer
 movl %esp, %ebp        #make stack pointer the base pointer
 subl $4, %esp          #get room for our local storage
 movl 8(%ebp), %ebx     #put first argument in %eax
 movl 12(%ebp), %ecx    #put second argument in %ecx
 movl %ebx, -4(%ebp)    #store current result

power_loop_start:
 cmpl $1, %ecx          #if the power is 1, we are done
 je end_power           #and we jump to end_power
 movl -4(%ebp), %eax    #move the current result into %eax
 imull %ebx, %eax       #multiply the current result by the base number
 movl %eax, -4(%ebp)    #store the current result
 decl %ecx	        #decrease the power
 jmp power_loop_start   #run for the next power
 
end_power:
 movl -4(%ebp), %eax    #return value goes in %eax
 movl %ebp, %esp        #restore the stack pointer
 popl %ebp		#restore the base pointer
 ret
 
Old 11-08-2006, 03:15 PM   #2
Shioni
Member
 
Registered: Mar 2006
Posts: 182

Rep: Reputation: 30
...............................

Last edited by Shioni; 11-08-2006 at 03:20 PM.
 
Old 11-08-2006, 03:18 PM   #3
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
Sorry, I meant Segmentation Fault. That is the error.
 
Old 11-08-2006, 03:21 PM   #4
Shioni
Member
 
Registered: Mar 2006
Posts: 182

Rep: Reputation: 30
what assembler are you using?
 
Old 11-08-2006, 03:23 PM   #5
Mercurius
Member
 
Registered: Jul 2005
Distribution: Slackware 11, Solaris 10
Posts: 143

Original Poster
Rep: Reputation: 15
"as" and then link it with "ld"
 
Old 11-08-2006, 08:10 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Not sure, but I copied the same program to NASM (linked w/ ld), because I'm familiar w/ it, and it ran fine. W/ as and ld it seg faulted like you said. Anyone who uses the GNU assembler want to chime in?
 
Old 11-08-2006, 09:18 PM   #7
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
here's the problem
Code:
#Everything in the main program is stored in registers,
#so the data section doesn't have anything.

 .section data

 .section text
the data and text segment must be prefixed with a "."
Code:
#Everything in the main program is stored in registers,
#so the data section doesn't have anything.

 .section .data

 .section .text
that should solve your problem
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I am having trouble installing a program chesterthefatp Linux - Newbie 2 01-10-2006 03:57 PM
having trouble with the do while loop in program mshinska Programming 2 10-08-2005 10:18 PM
trouble configuring a program minm Linux - Newbie 12 03-28-2005 04:33 PM
Using asm code in C program. grub Programming 2 04-03-2003 11:00 PM
calling all C, ASM in trouble orgcandman Programming 1 06-24-2002 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration