-
Notifications
You must be signed in to change notification settings - Fork 0
/
trig.asm
94 lines (84 loc) · 2.15 KB
/
trig.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
; #########################################################################
;
; trig.asm - Assembly file for CompEng205 Assignment 3
;
;
; #########################################################################
.586
.MODEL FLAT,STDCALL
.STACK 4096
option casemap :none ; case sensitive
include trig.inc
.DATA
;; These are some useful constants (fixed point values that correspond to important angles)
PI_HALF = 102943 ;; PI / 2 0x1 921F
PI = 205887 ;; PI
TWO_PI = 411774 ;; 2 * PI
PI_INC_RECIP = 5340353 ;; Use reciprocal to find the table entry for a given angle 0x51 7CC1
;; (It is easier to use than divison would be)
;; If you need to, you can place global variables here
.CODE
;;theta <= pi, sine is +
;;theta > pi, sine is -
FixedSin PROC USES edi ebx ecx edx esi angle:FXPT
;;MODULO DIVIDE ANGLE BY 2PI
;; i = theta*256/pi
;;I'M NOT ACTUALLY GETTING MY SINE ANGLES CORRECT FOR ANYTHING > |PI/2|
xor ecx, ecx
xor esi, esi
mov eax, angle ;;load angle into eax
;mov eax, PI_HALF
cdq ;;sign extends eax into edx
mov ebx, TWO_PI
idiv ebx
mov eax, edx
cmp eax, 0
jl NEG_ANGLE ;;makes the angle positive, knowing that we'll invert result
je ZERO
;;DEFINITELY 100% WORKS
L0:
cmp eax, PI
jge PI_2PI ;;is theta > PI? If so, theta -= PI and negate output
L1:
cmp eax, PI_HALF ;;is angle > pi/2? Great. Now we say theta = Pi - theta
jg PI_PI_2 ;;result isn't negated.
L2:
shl eax, 8 ;; eax *= 256
mov edx, 0
mov ebx, PI
idiv ebx ;;eax now holds our index (angle/(pi/256)) = i
L3:
lea edi, SINTAB ;;edi now holds the address of SINTAB
movzx eax, WORD PTR [edi + eax * 2] ;;should move array entry into eax
cmp ecx, 0
jne NEGATE_OUT
FIN:
ret
NEG_ANGLE:
not ecx
neg eax
jmp L0
PI_2PI:
not ecx
sub eax, PI
jmp L1
NEGATE_OUT:
neg eax
jmp FIN
ZERO:
mov eax, 0
ret
PI_PI_2:
mov ebx, PI
sub ebx, eax
mov eax, ebx ;;eax = Pi - eax
jmp L2
ret
FixedSin ENDP
FixedCos PROC angle:FXPT
mov eax, angle
add eax, PI_HALF
invoke FixedSin, eax
ret ; Don't delete this line!!!
FixedCos ENDP
END