hi i just have a quick question, ill attach my source incase i've mistyped
anyway i'm doing the book's exercises in a vm of ubuntu mate, i was having
no issues until trying to run the first program in chapter 4, it compiles
and links just fine but when i try to run it i get this error: 'Cannot
execute binary file: Exec format error'
attached is my source, i believe that the problem may be that i am running
in a VM but if so im not sure what to do, any help would be appreciated.
regards,
Johnathan
#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 
doesnt need 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(after the function runs)
pushl %eax      #save the first answer before
                #calling the next function
pushl $2        #push second argument
pushl $5        #push 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 stored in the second one addl first number, 
second number which is also the destination register
movl $1, %eax   #exit (%ebx is returned)
int $0x80

#PURPOSE This function is used to computer 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 through eax
#
#notes: the power must be 1 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 by subtracting one word (4 
bytes) from the stack pointer

movl 8(%ebp), %ebx      #put the first agument in ebx
movl 12(%ebp),%ecx      #put the 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
movl -4(%ebp), %eax     #move the current result into eax(on first run this 
will be the first argument)
imull %ebx, %eax        #multiply the current result by the base number(it gets 
stored in the second register)
movl %eax, -4(%ebp)     #store the 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
_______________________________________________
Pgubook-readers mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/pgubook-readers

Reply via email to