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
|
@micropython.asm_rv32
def asm_sum_words(a0, a1):
# a0 = len
# a1 = ptr
# a2 = sum
# a3 = dummy
li(a2, 0)
jal(zero, loop_entry)
label(loop1)
lw(a3, 0(a1))
add(a2, a2, a3)
addi(a1, a1, 4)
addi(a0, a0, -1)
label(loop_entry)
bne(a0, zero, loop1)
mv(a0, a2)
@micropython.asm_rv32
def asm_sum_bytes(a0, a1):
# a0 = len
# a1 = ptr
# a2 = sum
# a3 = dummy
li(a2, 0)
jal(zero, loop_entry)
label(loop1)
lbu(a3, 0(a1))
add(a2, a2, a3)
addi(a1, a1, 1)
addi(a0, a0, -1)
label(loop_entry)
bne(a0, zero, loop1)
mv(a0, a2)
import array
b = array.array("l", (100, 200, 300, 400))
n = asm_sum_words(len(b), b)
print(b, n)
b = array.array("b", (10, 20, 30, 40, 50, 60, 70, 80))
n = asm_sum_bytes(len(b), b)
print(b, n)
b = b"\x01\x02\x03\x04"
n = asm_sum_bytes(len(b), b)
print(b, n)
|