[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pgubook-readers] solution ch. 4 power.s
From: |
Nick Gantt |
Subject: |
[Pgubook-readers] solution ch. 4 power.s |
Date: |
Fri, 31 Dec 2004 04:54:54 -0800 (PST) |
try adding a third call to the power function and add
it's result back in:
#PURPOSE: Program to illustrate how functions work
# This program will compute the value of
# 2^3 + 5^2 + 4^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 2nd argument
pushl $2 #push 1st argument
call power #call the function
addl $8, %esp #move the stack point back
pushl %eax #save the first answer before
#calling the next function
pushl $2 #push the 2nd argument
pushl $5 #push first argument
call power #call the function
addl $8, %esp #move stack pointer back
push %eax
push $2 #push 2nd argument
push $4 #push 1st argument
call power #call the function
addl $8, %esp #move stack pointer back
popl %ebx #The third answer is already in %eax. We
#saved the second answer onto the stack,
#so now we can just pop it out into %ebx
addl %eax, %ebx #add 2nd and 3rd together, the result
is in %ebx
popl %ecx #pop the first answer into %ecx
addl %ecx, %ebx #add 1st to sum of 2nd and 3rd, store
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 base to
#
#OUTPUT: Will give the result as a return value
#
#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
movl 8(%ebp), %ebx #put first argument in %ebx
movl 12(%ebp), %ecx #put second arugment 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 to %eax
imull %ebx, %eax #multiply the current result by the
base number
movl %eax, -4(%ebp) #store 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
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pgubook-readers] solution ch. 4 power.s,
Nick Gantt <=