-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.py
68 lines (55 loc) · 1.42 KB
/
arithmetic.py
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
from . import encodings
#
# Add two integers.
#
# Add two numbers contained in registers and store them in a register.
#
@encodings.R
@encodings.opcode(0x07)
def ADD(state, rs1, rs2, rd):
state.registers[rd] = state.registers[rs1] + state.registers[rs2]
@encodings.R
@encodings.opcode(0x0E)
def CMP(state, rs1, rs2, rd):
x = state.registers[rs1]
y = state.registers[rs2]
state.ccr = x - y
state.ccn = (state.ccr < 0)
state.ccz = (state.ccr == 0)
state.ccv = False
#
# Multiply two integers.
#
# Multiply two numbers contained in registers and store them in a register.
#
@encodings.R
@encodings.opcode(0x08)
def MUL(state, rs1, rs2, rd):
state.registers[rd] = state.registers[rs1] * state.registers[rs2]
#
# Shift Left Logical
#
# Shift the number in %rs1 left by the number of bits in %rs2.
#
@encodings.R
@encodings.opcode(0x04)
def SLL(state, rs1, rs2, rd):
state.registers[rd] = state.registers[rs1] << state.registers[rs2]
#
# Shift Right Logical
#
# Shift the number in %rs1 right by the number of bits in %rs2.
#
@encodings.R
@encodings.opcode(0x05)
def SRL(state, rs1, rs2, rd):
state.registers[rd] = state.registers[rs1] >> state.registers[rs2]
#
# Subtract two integers.
#
# Subtract two numbers contained in registers and store them in a register.
#
@encodings.R
@encodings.opcode(0x06)
def SUB(state, rs1, rs2, rd):
state.registers[rd] = state.registers[rs1] - state.registers[rs2]