Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for negative immediate values #61

Merged
merged 1 commit into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions cocoasm/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ def __init__(self, value, size_hint=None, mode=ExplicitAddressingMode.NONE):
self.int = value
if self.int > 65535:
raise ValueTypeError("integer value cannot exceed 65535")
if self.int < 0:
self.negative = True
self.int *= -1
self.post_init_direct_check()
return

Expand Down Expand Up @@ -424,6 +427,7 @@ def __init__(self, value, size_hint=None, mode=ExplicitAddressingMode.NONE):
if self.int > 32768:
raise ValueTypeError("integer value cannot be below -32768")
self.negative = True
self.post_init_direct_check()
return

raise ValueTypeError("[{}] is not valid integer, character literal, or hex value".format(value))
Expand All @@ -432,7 +436,7 @@ def get_negative(self):
if not self.negative:
return self.int

return self.int | 0x80 if self.int < 128 else self.int | 0x8000
return 0x100 - self.int if self.int < 256 else 0x10001 - self.int

def hex(self, size=0):
if self.size_hint and size == 0:
Expand All @@ -441,7 +445,7 @@ def hex(self, size=0):
size = self.hex_len()
size += 1 if size % 2 == 1 else 0
format_specifier = "{{:0>{}X}}".format(size)
return format_specifier.format(self.int)
return format_specifier.format(self.get_negative())

def hex_len(self):
if self.size_hint is not None:
Expand Down
18 changes: 18 additions & 0 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,24 @@ def test_string_in_lhs_of_extended_indexed_expression(self):
program.translate_statements()
self.assertEqual([0xA6, 0x95], program.get_binary_array())

def test_negative_immediate_8_bit(self):
statements = [
Statement(" CMPB #-2"),
]
program = Program()
program.statements = statements
program.translate_statements()
self.assertEqual([0xC1, 0xFE], program.get_binary_array())

def test_negative_immediate_16_bit(self):
statements = [
Statement(" CMPX #-258"),
]
program = Program()
program.statements = statements
program.translate_statements()
self.assertEqual([0x8C, 0xFE, 0xFF], program.get_binary_array())

# M A I N #####################################################################


Expand Down
26 changes: 21 additions & 5 deletions test/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,33 @@ def test_is_16_bit_correct(self):
self.assertFalse(result.is_8_bit())
self.assertTrue(result.is_16_bit())

def test_negative_value_is_negative(self):
def test_negative_string_value_is_negative(self):
result = NumericValue("-1")
self.assertTrue(result.is_negative())

def test_negative_int_value_is_negative(self):
result = NumericValue(-1)
self.assertTrue(result.is_negative())

def test_negative_value_get_negative_correct_8_bit(self):
result = NumericValue("-1")
self.assertEqual(0x81, result.get_negative())
self.assertEqual(0xFF, result.get_negative())

def test_negative_string_value_get_negative_correct_8_bit(self):
result = NumericValue("-1")
self.assertEqual(0xFF, result.get_negative())

def test_negative_int_value_get_negative_correct_8_bit(self):
result = NumericValue(-1)
self.assertEqual(0xFF, result.get_negative())

def test_negative_string_value_get_negative_correct_16_bit(self):
result = NumericValue("-258")
self.assertEqual(0xFEFF, result.get_negative())

def test_negative_value_get_negative_correct_16_bit(self):
result = NumericValue("-256")
self.assertEqual(0x8100, result.get_negative())
def test_negative_int_value_get_negative_correct_16_bit(self):
result = NumericValue(-258)
self.assertEqual(0xFEFF, result.get_negative())

# M A I N #####################################################################

Expand Down