ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] Simpletron
    프로그래밍/JAVA 2023. 11. 8. 16:27

    2023-11-08 완벽 이해 완료

    package SimpletronProject;
    
    import java.util.Scanner;
    
    public class Simpletron {
    
        public static void main(String[] args) {
            System.out.println("*** Welcome to Simpletron! ***");
            System.out.println("*** Please enter your program one instruction ***");
            System.out.println("*** (or data word) at a time. I will type the location number ***");
            System.out.println("*** and a question mark (?). You then type the word for that location. ***");
            System.out.println("*** Type -99999 to stop entering ***");
            System.out.println("*** your program ***");
    
            Scanner sc = new Scanner(System.in);
            int accumulator = 0; // 누산기(계산결과 저장)
            int[] memory = new int[100]; // 메모리 공간 100
            int instructionCounter = 0; // 명령 카운터 현재 실행중인 명령 위치
            int instructionRegister = 0; // 그 위치의 내용을 꺼냄
            int operationcode = 0;
            int operand = 0;
    
            while (true) { // 메모리 관련 반복문
                System.out.printf("%02d ? ", instructionCounter); // 표현할 숫자 instruction의 숫자가 2보다 작다면 0을 붙임
                int input = sc.nextInt();
                if (input == -99999) { // -99999면 종료
                    System.out.println("*** Program loading completed ***\n*** Program execution begins ***");
                    break;
                } else if (input > 9999 || input < -9999) { // 9999보다 크거나, -9999보다 작으면 반복문 종료
                    System.out.println("10000 보다 작거나 -10000 보다 큰 값을 입력하여 주세요");
                    break;
                } else {
                    memory[instructionCounter] = input; // 아니면 값을 집어 넣음
                    ++instructionCounter;
                }
            } // end of memory while
    
            instructionCounter = 0;
            String operator = "";
            // 여기서부터 누산기 파트
            boolean running = true;
            while (running) {
                instructionRegister = memory[instructionCounter]; // 현재 명령의 위치의 값을 register에 넣음
                operationcode = instructionRegister / 100; // 앞에 두글자
                operand = instructionRegister % 100; // 뒤에 두글자
                switch (operationcode) {
                    case 10: // read, 키보드에서 콘솔 단어를 읽어 operand의 그것을 넣는다.
                        System.out.print("memory[" + operand + "]의 넣을 값 : ");
                        int inputRead = sc.nextInt();
                        memory[operand] = inputRead;
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 11: // write, 메모리의 operand 값의 위치한 값 출력
                        System.out.println(memory[operand]);
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 20: // load, 메모리의 operand 위치의 값을 누산기로 가져옴
                        accumulator = memory[operand];
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 21: // store, 누산기의 값을 메모리의 operand 위치의 저장
                        memory[operand] = accumulator;
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break; // break 추가
                    case 30: // add, 메모리의 operand 위치의 값을 누산기의 더함
                        if (accumulator > 9999 || accumulator < -9999 || memory[operand] > 9999 || memory[operand] < -9999) {
                            System.out.println(
                                    "*** Attempt to divide by zero ***\n*** Simpletron execution abnormally terminated");
                        } else {
                            accumulator += memory[operand];
                        }
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 31: // subtract, 메모리의 operand 위치의 값을 누산기에서 뺀다.
                        if (accumulator > 9999 || accumulator < -9999 || memory[operand] > 9999 || memory[operand] < -9999) {
                            System.out.println(
                                    "*** Attempt to divide by zero ***\n*** Simpletron execution abnormally terminated");
                        } else {
                            accumulator -= memory[operand];
                        }
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 32: // divide, 메모리의 operand 위치의 값을 누산기에서 나누어라.
                        accumulator /= memory[operand];
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 33: // multiply, 메모리의 operand 위치의 값을 누산기에서 곱함
                        accumulator *= memory[operand];
                        instructionCounter++; // 다음 메모리를 보기위해 하나씩 증가해야함
                        break;
                    case 40: // branch, operand에 지정된 위치로 점프
                        instructionCounter = operand;
                        break;
                    case 41: // branchneg, 누산기가 음수일 때 operand에 지정된 위치로 점프
                        if (accumulator < 0) {
                            instructionCounter = operand;
                        } else {
                            instructionCounter++;
                        }
                        break;
                    case 42: // branchzero, 누산기가 0일 때 operand에 지정된 위치로 점프
                        if (accumulator == 0) {
                            instructionCounter = operand;
                        } else {
                            instructionCounter++;
                        }
                        break;
                    case 43: // halt, 정지
                        System.out.println("*** Simpletron execution terminated ***");
                        running = false;
                        break;
                    default:
                        running = false;
                        break;
                }
            } // end of accumulator while
    
            System.out.print("accumulator ");
            if (accumulator >= 0) {
                operator = "+";
                System.out.printf("%s%04d\n", operator, accumulator);
            } // end of if
            else {
                System.out.printf("%s%04d\n", operator, accumulator);
            }
            System.out.printf("InstructionCounter %02d\n", instructionCounter);
            System.out.printf("InstructionRegister +%04d\n", instructionRegister);
            System.out.printf("operationcode %02d\n", operationcode);
            System.out.printf("operand %02d\n", operand);
    
            System.out.println("MEMORY : ");
    
            System.out.printf("\t");
            for (int i = 0; i < 10; i++) {
                System.out.printf("%5d\t", i);
            }
            System.out.println();
    
            for (int i = 0; i < 10; i++) {
                System.out.print(i * 10 + "\t"); // 탭을 추가하여 정렬
                for (int j = 0; j < 10; j++) {
                    System.out.printf("+%04d\t", memory[i * 10 + j]); // 4칸의 메모리 값을 출력하고 각 항목을 오른쪽으로 정렬
                }
                System.out.println();
            }
        } // end of main
    
    } // end of class

     

    • instructionCounter, instructionRegister의 개념을 완전히 잘못 짚고 있었음
    • branch도 잘못 알고 있었다. 그냥 점프의 개념과 비슷하다.
Designed by Tistory.