Skip to content

Commit eef5685

Browse files
.skip / .space directive, improved dump(), fixes #20
1 parent 332a2fb commit eef5685

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

demo.s

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
.text
44

5-
start: ld r0, r1, 0 # a comment!
5+
textstart: ld r0, r1, 0 # a comment!
66
st r0, r1, 0
77
add r0, r1, r2
88
add r0, r1, 42
@@ -48,9 +48,15 @@ start: ld r0, r1, 0 # a comment!
4848
wake
4949
sleep 1
5050
halt
51-
end:
51+
textend:
5252

5353
.data
54+
data0: .skip 4, 0x23
55+
data1: .space 4, 0x42
56+
data2: .skip 4
57+
dataend:
5458

5559
.bss
56-
60+
bss0: .skip 4
61+
bss1: .skip 4
62+
bssend:

esp32_ulp/assemble.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Assembler:
1010

1111
def __init__(self):
1212
self.symbols = {}
13-
self.sections = dict(text=[], data=[], bss=0)
13+
self.sections = dict(text=[], data=[])
1414
self.offsets = dict(text=0, data=0, bss=0)
1515
self.section = TEXT
1616

@@ -56,24 +56,27 @@ def append_section(self, value, expected_section=None):
5656
s = self.section
5757
if expected_section is not None and s is not expected_section:
5858
raise TypeError('only allowed in %s section' % expected_section)
59-
if s is TEXT or s is DATA:
59+
if s is BSS:
60+
# just increase BSS size by value
61+
self.offsets[s] += value
62+
else:
6063
self.sections[s].append(value)
6164
self.offsets[s] += 1
62-
elif s is BSS:
63-
self.sections[s] += value # just increase BSS size by value
6465

6566
def dump(self):
6667
print("Symbols:")
6768
for label, section_offset in sorted(self.symbols.items()):
6869
print(label, section_offset)
6970
print("%s section:" % TEXT)
7071
for t in self.sections[TEXT]:
71-
print(hex(t))
72+
print("%08x" % t)
73+
print("size: %d" % self.offsets[TEXT])
7274
print("%s section:" % DATA)
7375
for d in self.sections[DATA]:
74-
print(d)
76+
print("%08x" % d)
77+
print("size: %d" % self.offsets[DATA])
7578
print("%s section:" % BSS)
76-
print("size: %d" % self.sections[BSS])
79+
print("size: %d" % self.offsets[BSS])
7780

7881
def d_text(self):
7982
self.section = TEXT
@@ -84,6 +87,24 @@ def d_data(self):
8487
def d_bss(self):
8588
self.section = BSS
8689

90+
def d_skip(self, amount, fill=None):
91+
# TODO fill should be 8bit, but we are currently filling with 32bit
92+
s = self.section
93+
amount = int(amount)
94+
if fill is not None and s is BSS:
95+
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
100+
if s is BSS:
101+
self.append_section(amount)
102+
else:
103+
for i in range(amount):
104+
self.append_section(fill)
105+
106+
d_space = d_skip
107+
87108
def assemble(self, lines):
88109
for label, opcode, args in self.parse(lines):
89110
if label is not None:

tests/assemble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_assemble():
4444
assert a.symbols['end'] == (TEXT, 4)
4545
assert len(a.sections[TEXT]) == 4
4646
assert len(a.sections[DATA]) == 0
47-
assert a.sections[BSS] == 0
47+
assert a.offsets[BSS] == 0
4848

4949

5050
test_parse_line()

0 commit comments

Comments
 (0)