forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiply.ibcm
106 lines (106 loc) · 5.36 KB
/
multiply.ibcm
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
102
103
104
105
106
c00b 000 jmp init // skip around variables
0fff 001 sptr // stack pointer: is decremented AFTER usage
0000 002 tmp // temporary variable
3000 003 load
4000 004 store
c000 005 jmp
0000 006 zero
0001 007 one
0002 008 two
0003 009 three
0000 00a retval
1000 00b readH // read first number
4002 00c store tmp // store in tmp
3001 00d load sptr // load the stack pointer
5004 00e add store // create a store instruction
4011 00f store pos1 // store the store instruction into pos2
3002 010 load tmp // load up the value to push onto the stack
b000 011 pos1 nop // will hold the push-to-stack instruction
3001 012 load sptr // load the stack pointer
6007 013 sub one // decrement it
4001 014 store sptr // store it back
1000 015 readH // read second number
4002 016 store tmp // store in tmp
3001 017 load sptr // load the stack pointer
5004 018 add store // create a store instruction
401b 019 store pos2 // store the store instruction into pos1
3002 01a load tmp // load up the value to push onto the stack
b000 01b pos2 nop // will hold the push-to-stack instruction
3001 01c load sptr // load the stack pointer
6007 01d sub one // decrement it
4001 01e store sptr // store it back
f027 01f brl mult // using the branch-and-link instruction
4002 020 store tmp // store return value into tmp
3001 021 load sptr // load the stack pointer
5008 022 add two // increment the stack pointer by two for the two parameters
4001 023 store sptr // store it back
3002 024 load tmp // load return value from tmp
1800 025 printH // print out the return value (in the acc)
0000 026 halt
4002 027 mult store tmp // store the return address into tmp
3001 028 load sptr // load the stack pointer
5004 029 add store // create a store instruction
402c 02a store pos3 // store the store instruction into pos3
3002 02b load tmp // load up the value to push onto the stack
b000 02c pos3 nop // will hold the push-to-stack instruction
3001 02d load sptr // load the stack pointer
6007 02e sub one // decrement it
4001 02f store sptr // store it back
3006 030 load zero // load zero into the accumulator
400a 031 store retval // if we are returning, store zero in the return value
3001 032 load sptr // load the stack pointer
5003 033 add load // create a load instruction
5009 034 add three // move to the position of the second parameter
4036 035 store pos4 // store the load instruction into pos4
b000 036 pos4 nop // will hold the instruction to load the second parameter
4002 037 store tmp // store it in tmp for later use in the recursive part
d03a 038 jmpe ret // if it's zero, we're done with the recursion
c046 039 jmp recurse // otherwise, we will call ourselves recursively
3001 03a ret load sptr // load the stack pointer
5003 03b add load // create a load instruction
5007 03c add one // move to the position of the return address
403e 03d store pos5 // store the load instruction into pos5
b000 03e pos5 nop // will hold the instruction to load the return address
5005 03f add jmp // create a jump instruction
4045 040 store pos6 // store the jmp instruction into pos6
3001 041 load sptr // load the stack pointer
5007 042 add one // add one to 'remove' the return address
4001 043 store sptr // store it back
300a 044 load retval // load the return value
b000 045 pos6 nop // will hold the jmp (return) instruction
3001 046 recurse load sptr // load the stack pointer
5004 047 add store // create a store instruction
404b 048 store pos7 // store the store instruction into pos2
3002 049 load tmp // the second parameter is already in tmp
6007 04a sub one // subtract one
b000 04b pos7 nop // will hold the push-to-stack instruction
3001 04c load sptr // load the stack pointer
6007 04d sub one // decrement it
4001 04e store sptr // store it back
3001 04f load sptr // load the stack pointer
5003 050 add load // create a load instruction
5009 051 add three // move to the position of the first parameter
4053 052 store pos8 // store the load instruction into pos4
b000 053 pos8 nop // will hold the instruction to load the first parameter
4002 054 store tmp // store it in tmp for use in the recursive part
3001 055 load sptr // load the stack pointer
5004 056 add store // create a store instruction
4059 057 store pos9 // store the store instruction into pos1
3002 058 load tmp // load up the value to push onto the stack
b000 059 pos9 nop // will hold the push-to-stack instruction
3001 05a load sptr // load the stack pointer
6007 05b sub one // decrement it
4001 05c store sptr // store it back
f027 05d brl mult // using the branch-and-link instruction
4002 05e store tmp // store the return value in tmp
3001 05f load sptr // load the stack pointer
5003 060 add load // create a load instruction
5007 061 add one // move to the position of the first parameter (one b/c the ret addr is not on the stack)
4063 062 store pos10 // store the load instruction into pos4
b000 063 pos10 nop // will hold the instruction to load the first parameter
5002 064 add tmp // add the returned value to the first parameter
400a 065 store retval // store it for later
3001 066 load sptr // load the stack pointer
5008 067 add two // increment the stack pointer by two for the two parameters
4001 068 store sptr // store it back
c03a 069 jmp ret // jump to the return part of this code (return value in retval)