File tree Expand file tree Collapse file tree 2 files changed +5
-29
lines changed
src/main/java/io/github/coderodde/simple/stack/machine Expand file tree Collapse file tree 2 files changed +5
-29
lines changed Original file line number Diff line number Diff line change @@ -560,6 +560,7 @@ public static final class ReadNumberInstructionImplementation
560560 @ Override
561561 public void execute (SimpleStackMachine machine ) {
562562 machine .push (machine .readInt ());
563+ machine .advanceInstructionPointer ();
563564 }
564565 }
565566
Original file line number Diff line number Diff line change @@ -173,37 +173,12 @@ public static void main(String[] args) {
173173 final SimpleStackMachine m = new SimpleStackMachine ();
174174 CodeBuilder cb = new CodeBuilder (64 );
175175
176- final int ENTRY_POINT = 0 ;
177- final int SUM_FUNC_ADDR = 20 ;
178-
179- // ENTRY POINT:
180- // read first int
181- cb .setInstructionPointer (ENTRY_POINT );
182- cb .emit (Operation .READ_INT .getOpcodeByte ()); // read first integer
183-
184- // read second int
185- cb .emit (Operation .READ_INT .getOpcodeByte ()); // read second integer
186-
187- // call sum function at SUM_FUNC_ADDR
188- cb .emit (Operation .CALL .getOpcodeByte ());
189- cb .emit (SUM_FUNC_ADDR );
190-
191- // print result (sum)
176+ cb .emit (Operation .READ_INT .getOpcodeByte ());
177+ cb .emit (Operation .READ_INT .getOpcodeByte ());
178+ cb .emit (Operation .ADD .getOpcodeByte ());
192179 cb .emit (Operation .PRINT_INT .getOpcodeByte ());
193-
194- // halt
195180 cb .emit (Operation .HALT .getOpcodeByte ());
196-
197- // SUM FUNCTION at SUM_FUNC_ADDR:
198- // Stack layout: top -> second integer, below -> first integer
199- cb .setInstructionPointer (SUM_FUNC_ADDR );
200-
201- // add the two integers on the stack
202- cb .emit (Operation .ADD .getOpcodeByte ());
203-
204- // return the result
205- cb .emit (Operation .RET .getOpcodeByte ());
206-
181+
207182 m .execute (cb .toByteArray ());
208183 }
209184
You can’t perform that action at this time.
0 commit comments