-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbigthinker.asm
More file actions
101 lines (83 loc) · 2.28 KB
/
bigthinker.asm
File metadata and controls
101 lines (83 loc) · 2.28 KB
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
95
96
97
98
99
100
101
####################################################
#This CPU-bound program simply runs for a while and exits
###################################################
#Initialize the loop variables
SET r1 0 #counter
SET r2 1 #increment amount
SET r3 4 #limit
#Main Loop
:cpuloop
ADD r1 r2 r1
#Save the loop variables
PUSH r1
PUSH r2
PUSH r3
#This program calculates C(R0,R1) as per
#discrete mathematics. Initialize R0 = R1 + 3
SET R2 3
ADD R0 r1 r2
#STEP 1: Calculate m! and put it onto the stack
#Setup some initial variables
SET R2 1 #increment amount
SET R3 0 #counter (this will count backwards)
ADD R3 R3 R0
SET R4 1 #result
#Calculate m!
:factm
MUL R4 R4 R3 #multiply the counter into R4
SUB R3 R3 R2 #decrement the counter
BLT R2 R3 factm #repeat until R3==0
#Push the result
PUSH R4 #push the result
#STEP 2: Calculate n! and put it onto the stack
#Setup some initial variables
SET R2 1 #increment amount
SET R3 0 #counter (this will count backwards)
ADD R3 R3 R1
SET R4 1 #result
#Calculate
:factn
MUL R4 R4 R3 #multiply the counter into R4
SUB R3 R3 R2 #decrement the counter
BLT R2 R3 factn #repeat until R3==0
#Push the result
PUSH R4 #push the result
#STEP 3: Calculate (m-n)! and put it onto the stack
#Setup some initial variables
SET R2 1 #increment amount
SUB R3 R0 R1 #counter (this will count backwards)
SET R4 1 #result
#Calculate
:factmn
MUL R4 R4 R3 #multiply the counter into R4
SUB R3 R3 R2 #decrement the counter
BLT R2 R3 factmn #repeat until R3==0
#Push the result
PUSH R4 #push the result
#STEP 4: Calculate the and print the result using the
# formula: m! / ((n!)(m-n!))
#Pop off our values into registers
POP R4 #R4 = (m-n)!
POP R3 #R3 = n!
POP R2 #R2 = m!
#Calculate the result
MUL R1 R3 R4
SET R0 0
BNE R0 R1 dodiv
BRANCH skipdiv
:dodiv
DIV R1 R2 R1
:skipdiv
#Restore the main loop variables
POP r3
POP r2
POP r1
#loop test
BNE r1 r3 cpuloop
#========================================
#This is a normal exit from the program
#========================================
:exit
SET r4 0 #EXIT system call id
PUSH r4 #push sys call id on stack
TRAP #exit the program