Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help to get output from my program after its done the calculation

Status
Not open for further replies.

redwing26

Programmer
Feb 16, 2006
21
0
0
GB
Hi I typed a program in from a newbie assembly language tutorial ........i saved the file as pwrbookex.s then did the following

as pwrbookex.s -o pwrbookex.o
ld pwrbookex.o -o pwrbookex
./pwrbookex
argent777@localhost ~/assembly $ <-------nothing

echo $?
displays 0


......the above was the books way to compile and run files ^ ......my program doesnt error but well it seems to do nothing .......im sure its doing what its supposed to do as I copied and pasted the example from the book in the end ......the thing is the person in the book says to try stuff with it but I dont see how I can learn much if it isnt displaying any output in my terminal ......can someone show me how I would actually display the results of the 2 powers calculations in the terminal so I can see if the calculations are working out ok......at least if they come out completely wrong i no ive made a mistake but I get nothing ?

heres the program .......any advice greatly appreciated
redwing :) ....ow I am using gentoo linux with as ...i think its AT&T syntax


.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 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
#result 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
#
#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), %eax #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
movl -4(%ebp), %eax #move the current result into %eax
imul %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
 
Yet another post of code without code tags [sad]

--
 
Hi sorry about the tags .......is my first time on this forum , I will tag the next one however thats done ........anyway now that you have told me about tags how about helping me with my actual question as well?
 
Your program does some calculation, outputs nothing and then exits.
What were you expecting to see?

Though I see you're trying to return the result in %ebx.
You should know that you're only going to see the LSB of the result, so if it's large (and a multiple of 256), then you'll see 0.

Code:
 movl $2,%ebx
 movl $1,%eax
 int $0x80
Simple test, when I echo $?, I get 2 as expected.

Either that, or your calculation is broken.

--
 
I basically copied this out a book that was supposed to be for newbies ......Its supposed to add 2 to the power of 5 +
3 to the power of 2 ......im thinking it should be doing it since I copied the example from this book I have found the book really good except that the author says to change stuff to see how the program works but well they havent covered how I can print a registers contents to the screen at certain times through the programs execution .....this is all I need to do so I can fully understand my programs and step through them using an assembly printf equivalent but I can find this anywhere ......im ok with mistakes but I just want to print my registers to the screen at all different points so I can see which numbers are where and at which time. I just cant figure how I can take my assermbly further if I cant find a method of debugging my code .......unless ive screwed up some line in my current code which puts output to a terminal as it happens .........I dont think the guide im reading does this its like im writing programs and have just to assume there working lol without any output ......I have put this point on a few forums prior top this 1 and Ive had no joy with an answer

for example I used to code c :

say I was writting a program to add to variables

int var1;
int var2;
int answer;

var1 = 21;
var2 = 32;
answer = var1 + var2;
printf("The answer is : %d ",answer); #basically I want to be able to use a simple assembly command throughout my program that will do the above ^ except print the contents of say the %ebx register to my konsole terminal for example

Im really stuck with this what im sure is a simple concept :) .....any help is much needed & greatly appreciated .

thanks
redwing
 
im going to try and run this on its own

movl $2,%ebx
movl $1,%eax
int $0x80

 
ok going over my newb notes :) and I may be wrong here but

movl $2,%ebx <-----moves the actual number 2 into %ebx
movl $1,%eax <-----moves the actual number 1 into %eax
int $0x80 <------interrupt 80 which tells linux to exit
the program ....it knows the call we want by what is in eax (we assigned eax 1 here which is exit call) also sytemcall 1 requires the status code to be put in $ebx which is why we get 2 later???

echo $? <------is used to display a status code .....read from ebx??

I get 2 here ......ok im fine with getting status codes but what if I want to display output from registers as my program runs in a terminal e.g konsole ?

thanks for the help
redwing
 
cool I managed to get my problem fixed from someone in the gentoo forums the guy took the time to explain some basic stuff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top