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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to assembly help with x^nth power 1

Status
Not open for further replies.

soas

Technical User
Aug 3, 2001
49
US
Hey, Im new to assembly language, trying to learn it on my own, comming along okay, I found plenty of information, but I was writing a program and I need to take x to the nth power... like 2^5 or 3^6 or whatever, is there a math command for powers? or some simple way to do it? or do I have to make my own macro or something?

I guess I could make my own.. but I thought it would be really inefficient especialy if there was a command for it already built in... I am using it inside a rather large loop so writing it out doesnt seem like something I want to do, but I will if thats all the choice I have..
 
He he there is NO command that will do something that complex!!! (and believe me, for a computer that is VERY complex).

So now what do you do? If you're working with integers, use a loop! There's no other way!

'Course you could actually cheat with your program, i.e. if your doing something like this:
soas' ultra-super-duper power program!!
5^1 = 5
5^2 = 25
5^3 = 125
.
.
.
etc
you can just store the previous result, and each time you output a line just multiply the base by the previous result, or something. Think about it.

If you need to work with floating point numbers, however, you can't do loops, because what if the idiot who's using your program wants, say, 2.5 ^ 0.75?

That's when you use logarithms and the weird things they do. (The math coprocessor also does NOT have a way of doing powers directly, by the way.)

Here's how:
we know that:
x = exp(log(x))
log(x^n)=n log x
thus:
x^n=exp(n log x)

So: do a log, then an exp. As it happens, the math coprocessor DOES have a way of getting logs and antilogs, although it uses 2 as the base (but that is acceptable anyway).

Take note also that for older coprocessors, there are limitations on both the log and antilog instructions, better follow them or your coprocessor will MELT DOWN. Just kidding. It will only crash.
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top