I have the following basic python function:
def squared(num):
if num < 2:
print ('OK!')
return num * num
Which produces the following bytecode:
>>> dis.dis(squared)
2 0 LOAD_FAST 0 (num)
3 LOAD_CONST 1 (2)
6 COMPARE_OP 0 (<)
9 POP_JUMP_IF_FALSE 20
3 12 LOAD_CONST 2 ('OK!')
15 PRINT_ITEM
16 PRINT_NEWLINE
17 JUMP_FORWARD 0 (to 20)
4 >> 20 LOAD_FAST 0 (num)
23 LOAD_FAST 0 (num)
26 BINARY_MULTIPLY
27 RETURN_VALUE
Most of the above look like mov
and jmp
-type operators. However, what do the following most closely mean in assembly?
LOAD_FAST
, LOAD_CONST
?
What might be the closest assembly instructions for this?
LOAD_FAST
is accessing a local variable - this would be a move from a memory location (relative to the stack frame pointer), or possibly a move from a register (depending on the platform's standards, and the number of locals vs. the number of available registers).LOAD_CONST
would generally be a move of a constant value. But note that trying to find a one-to-one equivalence between a high-level bytecode and an assembly language isn't going to get you very far:PRINT_ITEM
, for example, is not something you're going to find as a single assembly opcode. – Katlin