Skip to content

Commit 446736d

Browse files
list-of-bytes sections, byte offsets
1 parent eef5685 commit 446736d

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

demo.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ dataend:
5858

5959
.bss
6060
bss0: .skip 4
61-
bss1: .skip 4
61+
bss1: .skip 2
6262
bssend:

esp32_ulp/assemble.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ def append_section(self, value, expected_section=None):
6161
self.offsets[s] += value
6262
else:
6363
self.sections[s].append(value)
64-
self.offsets[s] += 1
64+
self.offsets[s] += len(value)
6565

6666
def dump(self):
6767
print("Symbols:")
6868
for label, section_offset in sorted(self.symbols.items()):
6969
print(label, section_offset)
7070
print("%s section:" % TEXT)
7171
for t in self.sections[TEXT]:
72-
print("%08x" % t)
72+
print("%08x" % int.from_bytes(t, 'little'))
7373
print("size: %d" % self.offsets[TEXT])
7474
print("%s section:" % DATA)
7575
for d in self.sections[DATA]:
76-
print("%08x" % d)
76+
print("%08x" % int.from_bytes(d, 'little'))
7777
print("size: %d" % self.offsets[DATA])
7878
print("%s section:" % BSS)
7979
print("size: %d" % self.offsets[BSS])
@@ -88,20 +88,15 @@ def d_bss(self):
8888
self.section = BSS
8989

9090
def d_skip(self, amount, fill=None):
91-
# TODO fill should be 8bit, but we are currently filling with 32bit
9291
s = self.section
9392
amount = int(amount)
9493
if fill is not None and s is BSS:
9594
raise ValueError('fill not allowed in section %s' % s)
96-
fill = int(fill or 0)
97-
if amount % 4:
98-
amount += 4 - amount % 4
99-
amount = amount // 4
10095
if s is BSS:
10196
self.append_section(amount)
10297
else:
103-
for i in range(amount):
104-
self.append_section(fill)
98+
fill = int(fill or 0).to_bytes(1, 'little') * amount
99+
self.append_section(fill)
105100

106101
d_space = d_skip
107102

@@ -110,7 +105,7 @@ def assemble(self, lines):
110105
if label is not None:
111106
if label in self.symbols:
112107
raise Exception('label %s is already defined.' % label)
113-
self.symbols[label] = (self.section, self.offsets[self.section])
108+
self.symbols[label] = (self.section, self.offsets[self.section] // 4)
114109
if opcode is not None:
115110
if opcode[0] == '.':
116111
# assembler directive
@@ -125,7 +120,7 @@ def assemble(self, lines):
125120
func = getattr(opcodes, 'i_' + opcode, None)
126121
if func is not None:
127122
instruction = func(*args)
128-
self.append_section(instruction, TEXT)
123+
self.append_section(instruction.to_bytes(4, 'little'), TEXT)
129124
continue
130125
raise Exception('Unknown opcode or directive: %s' % opcode)
131126

tests/assemble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_assemble():
4242
assert {'start', 'end'} <= set(a.symbols)
4343
assert a.symbols['start'] == (TEXT, 0)
4444
assert a.symbols['end'] == (TEXT, 4)
45-
assert len(a.sections[TEXT]) == 4
45+
assert len(b''.join(a.sections[TEXT])) == 16 # 4 instructions * 4B
4646
assert len(a.sections[DATA]) == 0
4747
assert a.offsets[BSS] == 0
4848

0 commit comments

Comments
 (0)