hw8_ex1.asm

; Jade Yu Cheng
; ICS 312
; Assignment 7 Exercise 1
; April 24, 2009

; This program takes one command-line argument, which should be a floating
; point number. The program then calls a function called compute_exp that
; computes the exponential function.

%include "asm_io.inc"

%define x dword [ebp + 8]               ; define x to be the argument

segment .bss
        counter resq    1               ; a buffer space

segment .text
        global  compute_exp
compute_exp:
        push    ebp                     ; setup
        mov     ebp, esp                ; setup
        push    ebx                     ; setup reserve ebx

        fld1                            ; st0 is the previous x^n/n!
        fld1                            ; st1 is the previous summation
        mov     ebx, 0                  ; loop counter
add_loop:
        cmp     ebx, 40                 ; loop 40 times
        jz      add_loop_end
        inc     ebx                     ; increment the loop counter

        fxch    st1                     ; first work on updating x^n/n
        fmul    dword x                 ; x^(n-1)/(n-1) * x = x^n/(n-1)
        mov     [counter], ebx
        fidiv   dword [counter]         ; (x^n/(n-1))/n = x^n/n
        fxch    st1                     ; then work on updating the summation
        fadd    st1                     ; add the new item x^n/n
        jmp     add_loop
add_loop_end:

        pop     ebx                     ; clean up
        mov     esp, ebp                ; clean up
        pop     ebp                     ; clean up
        ret                             ; clean up
Valid HTML 4.01 Valid CSS