×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# New to assembly help with x^nth power

## New to assembly help with x^nth power

(OP)
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..

### RE: New to assembly help with x^nth power

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."

### RE: New to assembly help with x^nth power

(OP)
Thanks alot! :)

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!