Skip to content

Instantly share code, notes, and snippets.

@bfu4
Created May 15, 2021 07:24
Show Gist options
  • Save bfu4/5cbed549bd8d709e0ae4018941538ac9 to your computer and use it in GitHub Desktop.
Save bfu4/5cbed549bd8d709e0ae4018941538ac9 to your computer and use it in GitHub Desktop.
micropython example
# ==== PSEUDO translation for micropython
# ==== GIVEN:
print("hi")
# `dis` interprets this as
"""
2 0 LOAD_NAME 0 (print)
2 LOAD_CONST 0 ('hi')
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
"""
# ==== MPY
"""
0x00000000 4d05 021f 204c 080c 0007 1070 7269 6e74 M... L.....print
0x00000010 2e70 7920 0011 007b 1004 6869 3401 5951 .py ...{..hi4.YQ
0x00000020 6300 0000 0000 0000 0000 0000 0000 0000 c...............
"""
"""
offset 8
@ BASE_RESERVED
@ LOAD_QSTR 7072696e742e70 (print.py) #1
@ LOAD_NAME #1
@ LOAD_QSTR 0x04 + hi #2
@ CALL_FUNCTION #1
@ POP_TOP
@ LOAD_CONST_NONE
@ RETURN_VALUE
"""
# With this view, it can be assumed that in context, .py and { are disregarded,
# the exception to that being that micropython actually is an extra layer on top of python (unlikely).
# micropython knows when returning nothing, to load_const_none.
# similar to dis' example, we can see python loading in a string to an extent, and invoking a function
# respective to that.
#
# Both exhibit the behavior of popping the top after the invocation, loading none, then returning none (void).
# Overall, the bytecode is rather similar, besides for all of the opcodes being micropython's opcodes (shortened to be human-readable), parseable from bc0.h header.
@bfu4
Copy link
Author

bfu4 commented May 15, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment