Python Forum

Full Version: How to create an 'interpreter of assembler'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I’m doing an exercise which stores data in strings in a list. Each string contains a command. The list is meant to be interpreted in order of the strings. There just so happens to be a command which tells you to repeat a number of steps based on its location in the list.

For example:

simple_assembler(['mov a 5','inc a','dec a','dec a','jnz a -1','inc a'])
in the string with ‘jnz’, the -1 tells us to move one step backwards until a = 0. So I’m trying to write a program that can repeat iteration. I tried calling the function within the function under a while statement, but I think it resets each character’s value.

Let me explain the reasoning:

in the first step ‘mov a 5’, a becomes 5
so a=5
Second step ‘inc a’, which means increase a by 1
so a = 6
third and fourth step: decrease a by one
so ‘a = 4’
fifth step ‘jnz a -1’ which means go back one step and repeat it until a = 0
last step:
a = 1

So how do we tell the program to repeat a step?
What did you try? Could you provide any code?
You should have a pointer (an index) that tells you where you are in the program. Normally, this would be incremented by one after each command, to move you to the next command. However, branching commands like 'jnz' would do something else with the pointer, in this case move it back one instead of moving it forward one.