1 |
; |
2 |
; Name: Andrew Pollock |
3 |
; Student Number: 4137129 |
4 |
; Unit: COMP2300 |
5 |
;Assignment Number: 2 |
6 |
;Name of this file: log.ass |
7 |
; Lab Group: Tuesday 0900 |
8 |
; |
9 |
; |
10 |
; I declare that the material I am submitting in this file is entirely |
11 |
; my own work. I have not collaborated with anyone to produce it, nor |
12 |
; have I copied it, in part or in full, from work produced by someone else. |
13 |
; |
14 |
|
15 |
external Log10 ; #include <stdio.h> |
16 |
external ReadCard ; |
17 |
external WriteCard ; |
18 |
external Write ; |
19 |
|
20 |
a: block 1 ; int a |
21 |
l2: block 1 ; int l2 |
22 |
l10: block 1 ; int l10 |
23 |
|
24 |
; Blatant rip off of some of the macros from InOut.ass to make the blatant |
25 |
; rip off of Log10 (aka ilog2) work properly |
26 |
|
27 |
macro Set1 (x, e) ; perform x = e |
28 |
load e ; (AC = <value referred by e>) |
29 |
store x ; (Memory[x] = AC) |
30 |
endmacro |
31 |
|
32 |
macro Set2 (x, e1, op, e2) ; perform x = e1 + e2 |
33 |
load e1 ; (AC = <value referred by e1>) |
34 |
op e2 ; (AC = AC op <value referred by e2>) |
35 |
store x ; (Memory[x] = AC) |
36 |
endmacro |
37 |
|
38 |
macro Comp (e1, e2) ; compare e1, e2 |
39 |
; (use for evaluating conditions in IF, WHILE, etc) |
40 |
load e1 ; (AC = <value referred by e1>) |
41 |
cmp e2 ; (PSW(10:13) set to compare AC with e2) |
42 |
endmacro |
43 |
|
44 |
; Blatant rip off of Log10 from InOut.ass, rejigged for binary logs |
45 |
|
46 |
x = -2 |
47 |
RV = -3 |
48 |
Logx = 0 |
49 |
NumLocs = 1 |
50 |
ilog2: ; |
51 |
incsp #NumLocs ; /* allocate stack space for local vars. */ |
52 |
Comp (!x, #0) ; if (x != 0) { |
53 |
beq Lelse ; |
54 |
Set1 (!Logx, #0) ; Logx = 0; |
55 |
Set2 (!x, !x, dvd, #2); x = x / 2; |
56 |
Lwhile: Comp (!x, #0) ; while (x != 0) { |
57 |
beq Lendwh ; |
58 |
Set2 (!Logx, !Logx, add, #1); Logx = Logx + 1; |
59 |
Set2 (!x, !x, dvd, #2); x = x / 2; |
60 |
jmp Lwhile ; } /*while*/ |
61 |
Lendwh: ; |
62 |
jmp Lendif ; } else { |
63 |
Lelse: ; |
64 |
Set1 (!Logx, #-1) ; Logx = -1; /* illegal value for 0*/ |
65 |
Lendif: ; } /*if*/ |
66 |
Set1 (!RV, !Logx) ; return Logx; |
67 |
incsp #-NumLocs ; } /*Log2()*/ /*take local vars off stack*/ |
68 |
load #-1 ; /* break callers using AC to get RV */ |
69 |
ret ; |
70 |
|
71 |
log: ; int main() { |
72 |
; Read a number |
73 |
loada a ; /* Load address of a */ |
74 |
incsp #1 ; /* Make space for param on stack */ |
75 |
store !0 ; /* Store param on stack */ |
76 |
call ReadCard ; scanf("%d", &a); |
77 |
incsp #-1 ; /* Discard param after call */ |
78 |
; |
79 |
|
80 |
; Find the ilog2 of the number |
81 |
load a ; /* Load contents of a in AC */ |
82 |
incsp #1 ; /* Make space for return value on stack */ |
83 |
incsp #1 ; /* Make space for param on stack */ |
84 |
store !0 ; /* Store param on stack */ |
85 |
call ilog2 ; /* Call procedure */ |
86 |
incsp #-1 ; /* Discard param */ |
87 |
load !0 ; /* Load return value from stack to AC */ |
88 |
incsp #-1 ; /* Discard return value */ |
89 |
store l2 ; l2 = ilog2(a); |
90 |
; |
91 |
|
92 |
; Find the Log10 of the number |
93 |
load a ; /* Load value of a into AC */ |
94 |
incsp #1 ; /* Make space on stack for return value */ |
95 |
incsp #1 ; /* Make space on stack for param */ |
96 |
store !0 ; /* Save param on stack */ |
97 |
call Log10 ; /* Call procedure */` |
98 |
incsp #-1 ; /* Discard param */ |
99 |
load !0 ; /* Load return value from stack to AC */ |
100 |
incsp #-1 ; /* Discard return value */ |
101 |
store l10 ; l10 = Log10(a); |
102 |
; |
103 |
|
104 |
; Write l2 value |
105 |
load l2 ; /* Load l2 from memory to AC */ |
106 |
incsp #1 ; /* Make space on stack for param */ |
107 |
store !0 ; /* Store param on stack */ |
108 |
call WriteCard ; printf("%d", l2); |
109 |
incsp #-1 ; /* Discard param */ |
110 |
; |
111 |
|
112 |
load #10 ; /* Load NL into AC */ |
113 |
incsp #1 ; /* Make space on stack for param */ |
114 |
store !0 ; /* Save param on stack */ |
115 |
call Write ; printf("\n"); |
116 |
incsp #-1 ; /* Discard param */ |
117 |
|
118 |
; Write l10 value |
119 |
load l10 ; /* Load l10 into AC */ |
120 |
incsp #1 ; /* Make space on stack for param */ |
121 |
store !0 ; /* Save param on stack */ |
122 |
call WriteCard ; printf("%d", l10); |
123 |
incsp #-1 ; /* Discard param */ |
124 |
; |
125 |
|
126 |
load #10 ; /* Load NL into AC */ |
127 |
incsp #1 ; /* Make space on stack for param */ |
128 |
store !0 ; /* Save param on stack */ |
129 |
call Write ; printf("\n"); |
130 |
incsp #-1 ; /* Discard param */ |
131 |
|
132 |
; Show's over |
133 |
trap #1 ; } |
134 |
|
135 |
end log |