NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: bugnoob on January 08, 2014, 03:20:32 AM

Title: Euler number in NASM only
Post by: bugnoob on January 08, 2014, 03:20:32 AM
Hello
this is my first post here, i am NASM noob so please forgive me any stupid errors.

I need to make program in NASM only (without coprocessor instructions) which will take as input two numbers from the user and write on the screen e^x with the accuracy given bu user. As far i figure out that probably the easiest way to calculate e^x is this
http://img197.imageshack.us/img197/2302/2zov.png (http://img197.imageshack.us/img197/2302/2zov.png)
So i would need a loop that repeats n times and then adds the result of (x^n)/n! to a register.

My problem is i have no idea how to fit all these numbers into registers and how to calculate n! for example, how to fit it all in this program. No idea how to print on the screen so long number.

Thanks for the help

J.
Title: Re: Euler number in NASM only
Post by: avcaballero on January 08, 2014, 03:16:20 PM
Wow, many subjects, eh? :)

Take a look to this chapter (http://abreojosensamblador.net/Productos/AOE/html/Pags_en/Chap13.html). There's an example here for factorial.

If you don't know even how to get a number into a register, I guess that you need first begin to study asm from the beginning.

How to print a number to the screen (http://forum.nasm.us/index.php?topic=1103.0).

Regards
Title: Re: Euler number in NASM only
Post by: Frank Kotler on January 09, 2014, 09:36:13 AM
Hi J./bugnoob,

There's a common mistake, which you might have made - not giving us enough information. :)

You don't mention "What OS?", for example. I guess it doesn't matter too much. The way we get input from the user and print the result differs greatly, but that's the easy part. Doing the calculation won't differ much between OSen. 64-bit code might delay overflow, but you'll still have to watch out for it.

My math is really rusty, but I think I can read the formula you link to. From n=0 to infinity... Well, I don't have time to get to infinity, but until we've got the accuracy the user asked for. I hope they're not going to be allowed to enter too large a number! We do x^n (x is going to be an integer... I hope!) and n! and divide.

I believe in starting really simple, so I started with x=1. 1^0 is 1, right? And 0! is 1(?). So 1, and increasing n gives 1/1 again, then 1/2 + 1/6 + 1/24 + 1/120 + 1/720... Yeah, that looks like it might be converging on e. The problem is that I resorted to a hand-held calculator to do it. An integer "div" is going to give a quotient of 0 and a remainder of 1 (half), then 0 and 1 (sixth), etc. Doesn't seem useful - we can't just add 'em into a register and call it our integral! Those coprocessor instructions would sure come in handy!

What does "only Nasm" mean? Not even allowed to use a linker? No libraries? Are you expected to write your own "bignum" routines? How much of this do you know how to do? Can you show us what you've got?

In any case, thanks for an interesting question!

Best,
Frank

Title: Re: Euler number in NASM only
Post by: Mathi on January 12, 2014, 11:54:59 AM
The formula .. summation(1/n!)  might not be suited for this purpose.

I think what you need is a digit extraction algorithm for calculating e.

The below alternate representation has been used to devise some digits extraction algorithms for e.
e = 1 + 1/1(1+ 1/2(1 + 1/3(1+ 1/4(1+ 1/5 .....1+ 1/n)))))))

http://codereview.stackexchange.com/questions/33015/why-is-my-c-program-for-calculating-eulers-constant-of-poor-quality/33019#33019 (http://codereview.stackexchange.com/questions/33015/why-is-my-c-program-for-calculating-eulers-constant-of-poor-quality/33019#33019)

has a c Program.

Code: [Select]
#include <stdio.h>
#define DIGITS 9000 /* decimal places (not including the '2') */
int main() {
    int N = DIGITS+9, a[DIGITS+9], x = 0;
    a[0] = 0;
    a[1] = 2;
    for (int n = 2; n < N; ++n) {
        a[n] = 1;
    }
    for ( ; N > 9; --N) {
        for (int n = N - 1; n > 0; --n) {
            a[n] = x % n;
            x = 10 * a[n-1] + x/n;
        }
        printf("%d", x);
    }
    return 0;
}

I think this is easier to translate to asm.
Title: Re: Euler number in NASM only
Post by: bugnoob on January 14, 2014, 08:00:03 AM
Thank You Mathi. I am not familiar with C so much but i will try.

Actually, will the use of coprocessor instructions make the task much easier? If yes, can You please suggest how to make this  in ASM? -> (http://img197.imageshack.us/img197/2302/2zov.png)