
Task # 2 “Positive Automata”
For those who do not want to read:
Find the value of the expression: | x - | y ||
X, Y - any integer (and negative too)
')
Restriction: you cannot use sub, dec ... and any other subtraction, you cannot use flag registers and binary operations. (in particular shifts)
All that you have: je, cmp (you can not watch the flags), jmp, inc, mov. (I said a little)
In order to better understand the problem:
There is such a wonderful thing called:
Machine with unlimited registers (MNR)
So why this? For me personally - to stir the brains.
Now let's try it!
How can she use it? Example: Add two numbers, first number in R0, second number in R1. The answer is written in R0, with r0, r1> = 0
Note: I write the names of the registers rn, not Rn, and the names of the commands are also small, IMHO more convenient.
Decision:c1) s (r0); x = x + 1
c2) s (r2); r2 = r2 + 1
c3) j (r2, r1, c1); if r2 = r1, then jump to c1.
c4)
As you can see, if there is no jump on c3, then the program will stop and the answer in the register r0 will be.
Liberties:- give labels any names (what is before the bracket)
- You can write empty tags (to refer to them and exit the program.)
- do jumps like J (1,1,), this is the equivalent of an unconditional jump
- make entries of type T (,), for example: T (r1, 5) (in general this cannot be done, but ....)
- comments after ";", but as you wish
Limitations:- you can jump only on tags (you can not make the address variable)
- put in registers negative numbers
If you write large programs on a MNR (for example, an OS), it is convenient to make equivalent commands for assembler (testing).
The note:r0 ..... rn are words reserved in memory. Example: r0 dw 0
While all the "registers" are first at zero.
MNP teams / ASM. equivalent
T (r1, r2) <=> mov ax, r2
mov r1, ax
S (r1) <=> inc r1
Z (r1) <=> mov r1,0
J (r1, r2, c1) <=> mov ax, r2
cmp r1, ax
je c1
J (1,1, c1) <=> jmp c1
In general, the idea is to tie your right hand on your leg, glue 3 fingers on your left hand and go for a run on the asm. Well, nothing, we have 4 teams.
Enough theory, it's time to solve the problem.
Task 1. Find the error in my example.
Task 2. Subtract one number from another, with the first one being greater. r0 is the first, r1 is the second. Result in r0
Task 3. Find the value of the expression: | x - | y ||
X, Y - any integer (and negative too)
r0 - x; r1-y. Write the result to r0. In the comments cite analogue in assembler or MNR.
PS I wrote all this, hoping that you will also have interesting tasks, with the same restrictions.
PPS The comments are
Ayay's decision on the MNR, then mine, on ASM.
PPPS I deliberately expanded the capabilities of the MNR. By definition, it (MNR) generally works only with natural numbers; I entered the transfer in T (rn,), i.e. This is forwarding the number to the register immediately. Before that, it was possible only from the register to the register, but it is impossible to send negative numbers.