Closest Assembly Related To Python Bytecode
Solution 1:
Python's bytecode is for a stack-based VM to simplify interpreters and waste less space on addresses (e.g. register numbers) by making them implicit. Loads are pushing onto that stack.
If you transliterated this in a very literal and braindead manner to an asm equivalent for fixed-width integers (unlike Python arbitrary precision integers), every LOAD_FAST might be a load from a local variable (in memory on the stack) into a register. But you'd still have to choose which registers, and real ISAs have a limited number of registers. But yes, LOAD_FAST is like a load.
Of course if you weren't intentionally being literal just for the sake of it, you'd know you already had num
in a register and not load it again. So you'd use an instruction that read the same register twice, like imul eax, eax
And you'd have local variables living in registers when possible, not spilling them to the stack in the first place until you run out of registers.
If you want to learn about asm for CPUs, you can write an equivalent function in C and compile it (with optimization enabled, at least -Og
if not -O2
or -O3
) on the Godbolt compiler explorer: https://godbolt.org/. Or on your desktop, but Matt Godbolt wrote some nice filtering to remove noise and leave only the interesting parts. See also How to remove "noise" from GCC/clang assembly output?
Post a Comment for "Closest Assembly Related To Python Bytecode"