- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey!
I have to write a RAM program to accept all inputs of the form $1^n 2^{n^2} 0$.
The instructions of the RAM machine are the following:
I have done the following:
Then the RAM program is the following:
Is this correct?? (Wondering)
I have to write a RAM program to accept all inputs of the form $1^n 2^{n^2} 0$.
The instructions of the RAM machine are the following:
LOAD a ; c(0) ← v(a), c(i) is the integer stored in register i (the contents of register i), v(a) is the value of operand a
(v(=i)=i, v(i)=c(i), v(*i)=c(c(i)) )
STORE i ; c(i) ← c(0)
STORE *i ; c(c(i)) ←c(0)
ADD a ; c(0) ← c(0)+v(a)
SUB a ; c(0) ← c(0)-v(a)
MULT a ; c(0) ← c(0) × v(a)
DIV a ; c(0) ← ⌊c(0)/v(a)⌋
READ i ; c(i) ← current input symbol
READ *i ; c(c(i)) ← current input symbol. The input tape head moves one square right in either case.
WRITE a ; v(a) is printed on the square of the output tape currently under the output tape head. Then the tape head is moved one square right.
JUMP b ; The location counter is set to the instruction labeled b.
JGTZ b ; The location counter is set to the instruction labeled b if c(0)>0; otherwise, the location counter is set to the next instruction.
JZERO b ; The location counter is set to the instruction labeled b if c(0)=0; otherwise, the location counter is set to the next instruction.
HALT ; Execution ceases.
(v(=i)=i, v(i)=c(i), v(*i)=c(c(i)) )
STORE i ; c(i) ← c(0)
STORE *i ; c(c(i)) ←c(0)
ADD a ; c(0) ← c(0)+v(a)
SUB a ; c(0) ← c(0)-v(a)
MULT a ; c(0) ← c(0) × v(a)
DIV a ; c(0) ← ⌊c(0)/v(a)⌋
READ i ; c(i) ← current input symbol
READ *i ; c(c(i)) ← current input symbol. The input tape head moves one square right in either case.
WRITE a ; v(a) is printed on the square of the output tape currently under the output tape head. Then the tape head is moved one square right.
JUMP b ; The location counter is set to the instruction labeled b.
JGTZ b ; The location counter is set to the instruction labeled b if c(0)>0; otherwise, the location counter is set to the next instruction.
JZERO b ; The location counter is set to the instruction labeled b if c(0)=0; otherwise, the location counter is set to the next instruction.
HALT ; Execution ceases.
I have done the following:
Code:
Read x from input
d=0, s=0
while x!=0
if x-1=0
d=d+1
else
s=s+1
Read x from input
p=d*d
r=s-p
if r=0
Write 1
Then the RAM program is the following:
Code:
READ 1
LOAD =0
STORE 2
LOAD =0
STORE 3
while: LOAD 1
JZERO endwhile
LOAD 1
SUB =1
JGTZ two
LOAD 2
ADD =1
STORE 2
two: LOAD 3
ADD =1
STORE 3
READ 1
endwhile: LOAD 2
MULT 2
STORE 4
LOAD 3
SUB 4
STORE 5
LOAD 5
JZERO output
HALT
output: WRITE =1
HALT
Is this correct?? (Wondering)