Skip to content

Instantly share code, notes, and snippets.

@xatier
Created March 14, 2013 08:03
Show Gist options
  • Save xatier/5159666 to your computer and use it in GitHub Desktop.
Save xatier/5159666 to your computer and use it in GitHub Desktop.
TITLE cosine calculator (main.asm)
;;========================================================
;; Student Name:
;; Student ID:
;; Email: @cs.nctu.edu.tw
;;========================================================
;; Instructor: Sai-Keung WONG
;; Email: [email protected]
;; Room: 706
;; Assembly Language
;;========================================================
;; Description:
;;
;;
;; Compute the cos(x) using the Taylor Series
INCLUDE Irvine32.inc
INCLUDE macros.inc
.data
MAX = 10000 ;; maximum number of terms (array size)
NUM DWORD ? ;; user's input, to count n's iteration
CNT DWORD 1 ;; just a counter
TEMP DWORD ?
SIGN SDWORD 1 ;; (-1)^n
ZERO REAL8 0.0
ONE REAL8 1.0
VALUES REAL8 MAX DUP(?) ;; array for storing values
X_2n REAL8 MAX DUP(?) ;; an array of x^2n
Fact REAL8 MAX DUP(?) ;; an array of 2n!
X REAL8 0.2 ;; cos(X) that we compute
.code
main PROC
;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initialization
;;;;;;;;;;;;;;;;;;;;;;;;;;
finit ;; init FPU
fld ONE ;; X_2n[0] = 1.0
fstp X_2n[0]
fld ONE ;; Fact[0] = 1.0
fstp Fact[0]
fld ZERO ;; VALUES[0] = 0.0
fstp VALUES[0]
;; my student info.
mWriteLn "My Student Name: "
mWriteLn "My Student ID: "
mWriteLn "My Student Email: "
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Input an integer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mWrite "Please enter the number of terms (1-10000):"
call ReadInt ;; read an integer (to eax)
mov NUM, eax
mWriteLn "Computing cos(x)......"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initialize the array X_2n
;; X_2n[0] = 1
;; X_2n[i] = X^2i
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov ecx, NUM
mov eax, 0 ;; reset eax
L1:
finit ;; init FPU
fld X_2n[eax] ;; push X_2n[current]
fld X ;; push X
fld X ;; push X
fmul ;; X_2n[next] = X_2n[current] * X^2
fmul
fstp X_2n[eax+8] ;; store the result back
add eax, 8 ;; next REAL8
loop L1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initialize the array Fact
;; Fact[i] = i!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov ecx, NUM
mov eax, 0
mov CNT, 1
L2:
finit ;; init FPU
fld Fact[eax] ;; push Fact[current]
mov ebx, CNT
imul ebx, 2 ;; 2i
mov TEMP, ebx
fild TEMP ;; push 2i
sub ebx, 1 ;; 2i-1
mov TEMP, ebx
fild TEMP ;; push 2i-1
fmul ;; Fact[next] = Fact[current] * (2i) * (2i-1)
fmul
fstp Fact[eax+8] ;; store the result back
add eax, 8 ;; next REAL8
mov ebx, CNT
add ebx, 1 ;; count++
mov CNT, ebx
loop L2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; calculate cos(x) by Taylor Series
;; cos(x) => 1 - x^2/2! + x^4/4! - x^6/6! + ...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov ecx, NUM
mov eax, 0
mov SIGN, -1
L3:
finit ;; init FPU
fld VALUES[eax] ;; push VALUES[current]
fld X_2n[eax] ;; push X_2n[current]
fld Fact[eax] ;; push Fact[current]
mov ebx, SIGN ;; SIGN = -SIGN
neg ebx
mov SIGN, ebx
fild SIGN ;; push SIGN
fmul ;; VALUES[next] = VALUES[c] +
;; (X_2n[c] / (SIGN * Fact[c]))
fdiv
fadd
fstp VALUES[eax+8] ;; store it back
add eax, 8 ;; next REAL8
loop L3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; output
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
finit
mov eax, NUM
imul eax, 8
fld VALUES[eax] ;; push VALUES[NUM*8]
call ShowFPUStack ;; show ST(0)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dump the array
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COMMENT%
mov ecx, NUM ;; move value of eax to ecx
mov eax, 0 ;; set eax as 0
Lx:
finit ;; init FPU
fld VALUES[eax] ;; push VALUES[current]
call ShowFPUStack ;; show ST(0)
add eax, 8
loop Lx
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
%
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; pause & exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
call readint
exit
main ENDP
END main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment