Skip to content

Commit d3ee495

Browse files
committed
Minor fixes to value parsing errors and sign extension logic
1 parent 174e43d commit d3ee495

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

lc2200.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def sign_extend(binary, target):
7676
if binary.startswith('0b'):
7777
binary = binary[2:]
7878

79-
return binary[0] * (target - len(binary)) + binary
79+
sign = binary[0] if len(binary) > 1 else '0'
80+
return sign * (target - len(binary)) + binary
8081

8182
def bin2hex(binary):
8283
return '%0*X' % ((len(binary) + 3) // 4, int(binary, 2))
@@ -90,31 +91,31 @@ def dec2bin(num, bits):
9091

9192
def parse_value(offset, size, pc=None, unsigned=False):
9293
bin_offset = None
93-
94+
9495
if type(offset) is str:
9596
if pc is not None and offset in SYMBOL_TABLE:
96-
offset = SYMBOL_TABLE[offset] - (pc + 1)
97+
offset = SYMBOL_TABLE[offset] - pc - 1
9798
elif offset.startswith('0x'):
9899
try:
99100
bin_offset = hex2bin(offset)
100101
except:
101102
raise RuntimeError("'{}' is not in a valid hexadecimal format.".format(offset))
102-
103+
103104
if len(bin_offset) > size:
104105
raise RuntimeError("'{}' is too large for {}.".format(offset, __name__))
105-
106+
106107
bin_offset = zero_extend(bin_offset, size)
107108
elif offset.startswith('0b'):
108109
try:
109110
bin_offset = bin(int(offset))
110111
except:
111112
raise RuntimeError("'{}' is not in a valid binary format.".format(offset))
112-
113+
113114
if len(bin_offset) > size:
114115
raise RuntimeError("'{}' is too large for {}.".format(offset, __name__))
115-
116+
116117
bin_offset = zero_extend(bin_offset, size)
117-
118+
118119
if bin_offset is None:
119120
try:
120121
offset = int(offset)
@@ -123,21 +124,25 @@ def parse_value(offset, size, pc=None, unsigned=False):
123124
raise RuntimeError("'{}' cannot be resolved as a label or a value.".format(offset))
124125
else:
125126
raise RuntimeError("'{}' cannot be resolved as a value.".format(offset))
126-
127+
127128
if unsigned:
128129
bound = (2**size)
129130

130131
# >= bound because range is [0, 2^n - 1]
131-
if offset >= bound:
132-
raise RuntimeError("'{}' is too large (values) or too far away (labels) for {}.".format(offset, __name__))
132+
if offset < 0:
133+
raise RuntimeError("'{}' cannot be a negative value for {}.".format(offset, __name__))
134+
elif offset >= bound:
135+
raise RuntimeError("'{}' is too large (as a value) or too far away (as a label) for {}.".format(offset, __name__))
133136
else:
134137
bound = 2**(size - 1)
135138

136-
if offset < -bound or offset >= bound:
137-
raise RuntimeError("'{}' is too large (values) or too far away (labels) for {}.".format(offset, __name__))
138-
139+
if offset < -bound:
140+
raise RuntimeError("'{}' is too small (as a value) or too far away (as a label) for {}.".format(offset, __name__))
141+
elif offset >= bound:
142+
raise RuntimeError("'{}' is too large (as a value) or too far away (as a label) for {}.".format(offset, __name__))
143+
139144
bin_offset = dec2bin(offset, size)
140-
145+
141146
return bin_offset
142147

143148

lc32200a.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def sign_extend(binary, target):
8888
if binary.startswith('0b'):
8989
binary = binary[2:]
9090

91-
return binary[0] * (target - len(binary)) + binary
91+
sign = binary[0] if len(binary) > 1 else '0'
92+
return sign * (target - len(binary)) + binary
9293

9394
def bin2hex(binary):
9495
return '%0*X' % ((len(binary) + 3) // 4, int(binary, 2))
@@ -102,31 +103,31 @@ def dec2bin(num, bits):
102103

103104
def parse_value(offset, size, pc=None, unsigned=False):
104105
bin_offset = None
105-
106+
106107
if type(offset) is str:
107108
if pc is not None and offset in SYMBOL_TABLE:
108-
offset = SYMBOL_TABLE[offset] - (pc + 1)
109+
offset = SYMBOL_TABLE[offset] - pc - 1
109110
elif offset.startswith('0x'):
110111
try:
111112
bin_offset = hex2bin(offset)
112113
except:
113114
raise RuntimeError("'{}' is not in a valid hexadecimal format.".format(offset))
114-
115+
115116
if len(bin_offset) > size:
116117
raise RuntimeError("'{}' is too large for {}.".format(offset, __name__))
117-
118+
118119
bin_offset = zero_extend(bin_offset, size)
119120
elif offset.startswith('0b'):
120121
try:
121122
bin_offset = bin(int(offset))
122123
except:
123124
raise RuntimeError("'{}' is not in a valid binary format.".format(offset))
124-
125+
125126
if len(bin_offset) > size:
126127
raise RuntimeError("'{}' is too large for {}.".format(offset, __name__))
127-
128+
128129
bin_offset = zero_extend(bin_offset, size)
129-
130+
130131
if bin_offset is None:
131132
try:
132133
offset = int(offset)
@@ -135,21 +136,25 @@ def parse_value(offset, size, pc=None, unsigned=False):
135136
raise RuntimeError("'{}' cannot be resolved as a label or a value.".format(offset))
136137
else:
137138
raise RuntimeError("'{}' cannot be resolved as a value.".format(offset))
138-
139+
139140
if unsigned:
140141
bound = (2**size)
141142

142143
# >= bound because range is [0, 2^n - 1]
143-
if offset >= bound:
144-
raise RuntimeError("'{}' is too large (values) or too far away (labels) for {}.".format(offset, __name__))
144+
if offset < 0:
145+
raise RuntimeError("'{}' cannot be a negative value for {}.".format(offset, __name__))
146+
elif offset >= bound:
147+
raise RuntimeError("'{}' is too large (as a value) or too far away (as a label) for {}.".format(offset, __name__))
145148
else:
146149
bound = 2**(size - 1)
147150

148-
if offset < -bound or offset >= bound:
149-
raise RuntimeError("'{}' is too large (values) or too far away (labels) for {}.".format(offset, __name__))
150-
151+
if offset < -bound:
152+
raise RuntimeError("'{}' is too small (as a value) or too far away (as a label) for {}.".format(offset, __name__))
153+
elif offset >= bound:
154+
raise RuntimeError("'{}' is too large (as a value) or too far away (as a label) for {}.".format(offset, __name__))
155+
151156
bin_offset = dec2bin(offset, size)
152-
157+
153158
return bin_offset
154159

155160
class Instruction:

lc32200b.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def sign_extend(binary, target):
8383
if binary.startswith('0b'):
8484
binary = binary[2:]
8585

86-
return binary[0] * (target - len(binary)) + binary
86+
sign = binary[0] if len(binary) > 1 else '0'
87+
return sign * (target - len(binary)) + binary
8788

8889
def bin2hex(binary):
8990
return '%0*X' % ((len(binary) + 3) // 4, int(binary, 2))
@@ -97,31 +98,31 @@ def dec2bin(num, bits):
9798

9899
def parse_value(offset, size, pc=None, unsigned=False):
99100
bin_offset = None
100-
101+
101102
if type(offset) is str:
102103
if pc is not None and offset in SYMBOL_TABLE:
103-
offset = SYMBOL_TABLE[offset] - (pc + 1)
104+
offset = SYMBOL_TABLE[offset] - pc - 1
104105
elif offset.startswith('0x'):
105106
try:
106107
bin_offset = hex2bin(offset)
107108
except:
108109
raise RuntimeError("'{}' is not in a valid hexadecimal format.".format(offset))
109-
110+
110111
if len(bin_offset) > size:
111112
raise RuntimeError("'{}' is too large for {}.".format(offset, __name__))
112-
113+
113114
bin_offset = zero_extend(bin_offset, size)
114115
elif offset.startswith('0b'):
115116
try:
116117
bin_offset = bin(int(offset))
117118
except:
118119
raise RuntimeError("'{}' is not in a valid binary format.".format(offset))
119-
120+
120121
if len(bin_offset) > size:
121122
raise RuntimeError("'{}' is too large for {}.".format(offset, __name__))
122-
123+
123124
bin_offset = zero_extend(bin_offset, size)
124-
125+
125126
if bin_offset is None:
126127
try:
127128
offset = int(offset)
@@ -130,21 +131,25 @@ def parse_value(offset, size, pc=None, unsigned=False):
130131
raise RuntimeError("'{}' cannot be resolved as a label or a value.".format(offset))
131132
else:
132133
raise RuntimeError("'{}' cannot be resolved as a value.".format(offset))
133-
134+
134135
if unsigned:
135136
bound = (2**size)
136137

137138
# >= bound because range is [0, 2^n - 1]
138-
if offset >= bound:
139-
raise RuntimeError("'{}' is too large (values) or too far away (labels) for {}.".format(offset, __name__))
139+
if offset < 0:
140+
raise RuntimeError("'{}' cannot be a negative value for {}.".format(offset, __name__))
141+
elif offset >= bound:
142+
raise RuntimeError("'{}' is too large (as a value) or too far away (as a label) for {}.".format(offset, __name__))
140143
else:
141144
bound = 2**(size - 1)
142145

143-
if offset < -bound or offset >= bound:
144-
raise RuntimeError("'{}' is too large (values) or too far away (labels) for {}.".format(offset, __name__))
145-
146+
if offset < -bound:
147+
raise RuntimeError("'{}' is too small (as a value) or too far away (as a label) for {}.".format(offset, __name__))
148+
elif offset >= bound:
149+
raise RuntimeError("'{}' is too large (as a value) or too far away (as a label) for {}.".format(offset, __name__))
150+
146151
bin_offset = dec2bin(offset, size)
147-
152+
148153
return bin_offset
149154

150155

0 commit comments

Comments
 (0)