diff --git a/src/Makefile b/src/Makefile index 3614a3c..d49bec6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,13 @@ LDFLAGS="-L/usr/local/opt/flex/lib" +automaton: ram + ./ram < stack.asm + echo "### machine.py ###" + cat machine.py + echo "### execution ###" + python machine.py + bubble_sort: ram ./ram < bubble_sort.asm echo "### machine.py ###" diff --git a/src/stack.asm b/src/stack.asm index e0f1ec9..9c06274 100644 --- a/src/stack.asm +++ b/src/stack.asm @@ -1,17 +1,15 @@ -(5,2,5,5,0) +(37, 6, 0, 0, 0, 1, 1, 1, 0, 0, 3, 2, 1, 3, 0, 0, 0, 1, 2, 1, 1, 0, 0, 1, 0, 0, 2, 2, 1, 0, 0, 2, 2, 2, 3, 1, 3, 1) ADD(0, 0, r0) // r0 current state in pushdown automaton ADD(2, 0, r1) // r1 cursor position in input word, (i2 first symbol of input word, i1 length of input word) -ADD(10, 0, r9) // r9 address of stack top -ADD(2, 0, r10) // r10 bottom symbol -SUB(i1, i2, r7) -SUB(r7, 2, r7) // r7 number of instructions +ADD(11, 0, r11) // r11 address of stack top +ADD(3, 0, r12) // r12 bottom symbol +ADD(i0, 1, r7) // r7 address registry after last instruction in input +ADD(i1, 2, r10) // r10 last symbol of input word address + 1 -ADD(1, 0, r8) // reset loop counter -ADD(i1, 2, r2) // r2, address first automaton instruction begin state 'q', instruction: (q, a, A, w, q′) +ADD(i1, 2, r2) // r2, address first automaton instruction begin state 'q', instruction: (q, a, A, w, q′), (reset loop) JUMP(3) // skip loop increment on 1st iteration -ADD(1, r8, r8) // increment loop counter ADD(r6, 1, r2) // r2, address of next instruction to check -JL(r8, r7, ) // loop condition +JE(r2, r7, 42) // loop condition ADD(r2, 1, r3) // r3, address instruction read symbol 'a' ADD(r3, 1, r4) // r4, address instruction read stack symbol 'A' ADD(r4, 1, r5) // r5, address, instruction word pushed on stack length @@ -19,20 +17,52 @@ ADD(r5, i@r5, r6) ADD(1, r6, r6) // r6, address, next state q' JE(r0, i@r2, 2) // current state is the expected state in the instruction -JUMP(-9) // if not, next iteration +JUMP(-8) // if not, next iteration -JE(i@r1, 2, 3) // read symbol is epsilon -JE(i@r3, i@r1, ) // symbol in input == read symbol -JUMP(-12) +JE(i@r1, 2, 4) // read symbol is epsilon +JE(r1, r10, -11) // check if input word has been read, if yes next iteration +JE(i@r3, i@r1, 5) // symbol in input == read symbol +JUMP(-12) // next iteration -JE(i@r4, 2, ) // read stack symbol is epsilon (read symbol was epsilon) -JE(r@r6, i@r4, ) // stack top is the expected symbol +JE(i@r4, 2, 6) // read stack symbol is epsilon (read symbol was epsilon) +JE(r@r11, i@r4, 8) // stack top is the expected symbol JUMP(-15) -JE(i@r4, 2, ) // read stack symbol is epsilon (read symbol was symbol in put) -JE(r@r6, i@r4, ) // stack top is the expected symbol +JE(i@r4, 2, 10) // read stack symbol is epsilon (read symbol was symbol in input) +JE(r@r11, i@r4, 14) // stack top is the expected symbol JUMP(-18) +ADD(i@r6, 0, r0) // a=epsilon, A=epsilon, replace current state +JE(i@r5, 0, -22) // restart loop if nothing to write on stack +JUMP(16) // go to copy word on stack + + +ADD(i@r6, 0, r0) // a=epsilon, A=symbol +JE(i@r5, 0, 2) // check if word to copy is empty +JUMP(13) // go to copy word on stack +JUMP(-27) // restart loop + +ADD(i@r6, 0, r0) // a=symbol, A=epsilon +ADD(r1, 1, r1) // move cursor in input word +JE(i@r5, 0, -30) // restart loop +SUB(r11, 1, r11) +JUMP(7) // go to copy word on stack + +ADD(i@r6, 0, r0) // a=symbol, A=symbol +ADD(r1, 1, r1) // move cursor in input word +JE(i@r5, 0, 2) // check if word to copy is empty +JUMP(3) // go to copy word on stack +SUB(r11, 1, r11) +JUMP(-38) // restart loop + +SUB(r6, 1, r9) // address symbol to copy on stack +JE(r9, r5, -40) // inner loop condition, restart outer loop +ADD(r11, 1, r11) +ADD(i@r11, i@r9) +SUB(r9, 1, r9) +JUMP(-4) + + JE(r0, 1, 3) // check if final state ADD(0, 1, o1) // reject = 1 JUMP(2)