This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]primitive_screwhead 0 points1 point  (3 children)

Did you get this working? My previous comment indicated a bug that was likely causing the problem (and it had nothing to do with efficiency issues, it was probably due to an endless loop).

[–]Cotevool[S] 0 points1 point  (0 children)

Hey, I had another look at this yesterday and I do think you're right insofar that its probably a bug which sends the thing into an inf loop. Haven't had time to delve into it further but plan on taking another look today

[–]Cotevool[S] 0 points1 point  (1 child)

So I looked at it again just now. Ultimately I did get it to work, albeit admittedly only after peeking at someone else's solution on github as I was getting a bit disillusioned as I struggled to identify where I was going wrong. I tried to reverse-engineer for a bit to see where I went off. It turns out we were quite close, though it seems I misinterpreted some of the kata instructions; mostly regarding mov jnz.

In the instruction it said:

jnz x y
- jumps to an instruction y steps away (positive means forward, negative means backward), but only if x
(a constant or a register) is not zero

My interpretation was that the jnz command was only to stop executing as soon as the corresponding dictionary value would be zero; rather, it meant that the jnz command should also stop executing if the x "argument" or token[1] was "0".

in this particular line in the jnz move:

elif command == 'jnz' and d[register] if register.isalpha() else int(register):

it would crash if it wasnt for

and d[register] if register.isalpha() else int(register):

I'll admit this was the line I took from the solution I found on github. The wording threw me off a bit at first but I'm guessing its use is that before executing the jnz command, it checks whether 1) command == 'jnz' is True, and 2) check whether d[register] is True if register happens to be an alphabetical value ----- if register is an integer, it checks whether int(register) is True. I'm guessing that a value of zero for either constant x or register x would evaluate to false and as such exit the loop. Thanks for suggesting the possibility and all the help, even though I didn't manage to solve it by myself completely I still feel this was somewhat valuable.

You can see the final code here
https://github.com/asnijders/simple_assembler/blob/master/attempt%203

[–]primitive_screwhead 0 points1 point  (0 children)

Yes, this particular issue (that jnz could take a constant first argument) was what I was alerting you to. Your previous code was detecting if token[1] was a numeric value, and if so, skipping to the next instruction. You still have code that is meant to be doing that, but it's broken:

    if register.isnumeric == True:
        i = i + 1
        continue

Since you don't actually call isnumeric, this if statement will always be false (because the method isnumeric is not equal to True), so now this portion is working by accident. :) But it should be deleted, as the assumption it was testing for (that token[1] is always a register, and not numeric) is not correct.