Search

mardi 21 mars 2017

16-bit values: jumps (3)

Context

Note: read first and second part .
As a reminder, we have 2 different structures, named "4 cells" and "3 cells" to represent 16-bit integers.
Let's now redefine instructions + and - on those 2 structures.

Structure 1 - "4 cells"

A, B, C, D to represent integer N; with
  • A = 0
  • B = 0
  • C and D so that N = D * 256 + C
Cursor on C

Structure 2 - "3 cells"

A, B, C to represent integer N; with
  • A and B so that N = B * 256 + A
  • C = 0
Cursor on A

We can see that these structures mean that we divided N by 256 and stored both quotient and remainder.

While [

What we need to do is to start execution of a piece of code if and only if at least one of {quotient, remainder} is not null.

Using 4 cells structure (reminder: like the 3 cells structure, we can leverage the next cell X, after D...)
  • If D is not null, move it to X and increase B
  • If C is not null, move it to D and increase B
  • Move D back to C and X back to D
  • Go to B. If not null, enter while (and reset B, and back to C to keep system consistent)
  • Note: if B is null, or after while, we'll have to move back to C as well
Using 3 cells structure (reminder: cell X before A is null as well, as it's the C from previous block, and same applies to Y cell in the next block):
  • If B is not null, move it to C and increase X
  • If A is not null, move it to B and increase X
  • Move B back to A and C back to B
  • Go to X. If not null, enter while (and reset X, and back to A to keep system consistent)
  • Note: if X is null, or after while, we'll have to move back to A as well
Note: actually, code is the same for both structures.

Loop ]

To loop (note: we are on the remainder, but loop started one cell before). We need to evaluate if both quotient and remainder are null or not as before, store the result at the exact same place than before, and loop if not null. Then

  • If not null, the current cell will be reset after looping, thanks to our previous reset at the beginning. And it can't be done before looping. Otherwise, there won't be any loop
  • If null, then there is nothing to reset, but let's just go back on remainder


Using 4 cells structure:
  • If D is not null, move it to X and increase B
  • If C is not null, move it to D and increase B
  • Move D back to C and X back to D
  • Go to B. If not null, loop (no reset needed, nor back to C, as it's done by while)
Using 3 cells structure:
  • If B is not null, move it to C and increase X
  • If A is not null, move it to B and increase X
  • Move B back to A and C back to B
  • Go to X. If not null, enter while (and reset X, and back to A to keep system consistent)
  • Note: if X is null, or after while, we'll have to move back to A as well
Note: again, code is the same for both structures.


Operation"4 cells""3 cells"
[(while) >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]> >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<[[-]>
](loop) >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]> >[[->+<]<<+>>]<[[->+<]<+>]>[-<+>]>[-<+>]<<<]>


Aucun commentaire:

Enregistrer un commentaire