ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.