Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a java code that simulates a CPU memory stack. In this project we will extend the hypothetical machine discussed in the text on

Please write a java code that simulates a CPU memory stack. image text in transcribedimage text in transcribed

In this project we will extend the hypothetical machine discussed in the text on page 6. Our simulated computer has three components: CPU, memory, and stack. The CPU has four registers: PC, IR, AC, and REG, where REG is a special register for holding temporary data in the CPU. All registers are 16 bits wide. This computer has 16 bit instructions and 16 bit operands. The opcodes used by our computer are given below: Opcode Instruction 0001 Load AC from memory 0010 Store AC to memory 0011 Load AC from REG 0100 Store AC to REG 0101 Add to AC from memory 0110 Load REG with operand 0111 Add REG to AC 1000 Multiply REG to AC 1001 Subtract REG from AC 1010 Divide AC by REG value (integer division) 1011 Jump to subroutine starting at the address 1100 Return from subroutine 1111 Halt (end of program) You need to write Java code that will simulate the working of this computer. You will have three classes, one for each component: CPU, Memory, and Stack. The computer should be able to run programs that have subroutines. Every time a subroutine is called, contents of CPU registers are saved in the Stack and whenever the subroutine returns, contents of registers are restored by popping the Stack. ===Main Program===== 1. 2. 3. 4. 5. 6. 7. 8. 9. 400 1940 ;load AC from memory 940 - AC has value 3 401 5941 ; add to AC from memory 941 - AC has value 5 402 2941 ;store AC in memory 941 - 941 has value 5 403 6000 ;load register with 0 404 3000 ;load AC from register - AC is now 0 405 2942 ;store AC to memory 942 - 942 is now 0 406 BA00 ; branch to subroutine starting at address 100 407 BACA ; branch to subroutine starting at AOA 408 1942 ;load AC from memory 942 - AC is decimal 60 (line 8 subroutine 2) 409 6014 ;load register with 0x14 = decimal 20 40A 7000 ;add register to AC - AC is now decimal 80 40B 2942 ;store AC in memory 942 - 942 is now decimal 80, hex 50 40C F000 ;halt program 10. 11. 12. 13. ;code separator Subroutine 1 ===== 1. 2. 3. 4. 5. 6. 7. 8. 9. A00 6001 ;load register with 1 A01 3000 ;load AC from register AC is now 1 A02 6009 ;load register with 9 A03 7000 ;add register to AC - AC is now decimal 10, hex A A04 4000 ;store AC in register - register is now decimal 10 A05 1942 ;load AC from memory 942 AC is now 0 (942 is already 0) A06 7000 ;add register to AC - AC is now decimal 10 A07 2942 ;store AC in memory 942 - 942 is now decimal 10 A08 C000 ;return from subroutine ========= Subroutine 2 1. 2. 3. 4. 5. 6. 7. 8. 9. AOA 6005 ;load register with 5 AOB 3000 ;load AC from register - AC is now 5 AOC 600A ;load register with A - decimal 10 AOD 8000 ;multiply AC with register - AC is now decimal 50 A0E 4000 ;store AC to register - register is now decimal 50 AOF 1942 ;load AC from memory 942 AC is now decimal 10 A10 7000 ;add register to AC - AC is now decimal 60 A11 2942 ;store AC to memory 942 - 942 is now decimal 60 A12 C000 ;return from subroutine In this project we will extend the hypothetical machine discussed in the text on page 6. Our simulated computer has three components: CPU, memory, and stack. The CPU has four registers: PC, IR, AC, and REG, where REG is a special register for holding temporary data in the CPU. All registers are 16 bits wide. This computer has 16 bit instructions and 16 bit operands. The opcodes used by our computer are given below: Opcode Instruction 0001 Load AC from memory 0010 Store AC to memory 0011 Load AC from REG 0100 Store AC to REG 0101 Add to AC from memory 0110 Load REG with operand 0111 Add REG to AC 1000 Multiply REG to AC 1001 Subtract REG from AC 1010 Divide AC by REG value (integer division) 1011 Jump to subroutine starting at the address 1100 Return from subroutine 1111 Halt (end of program) You need to write Java code that will simulate the working of this computer. You will have three classes, one for each component: CPU, Memory, and Stack. The computer should be able to run programs that have subroutines. Every time a subroutine is called, contents of CPU registers are saved in the Stack and whenever the subroutine returns, contents of registers are restored by popping the Stack. ===Main Program===== 1. 2. 3. 4. 5. 6. 7. 8. 9. 400 1940 ;load AC from memory 940 - AC has value 3 401 5941 ; add to AC from memory 941 - AC has value 5 402 2941 ;store AC in memory 941 - 941 has value 5 403 6000 ;load register with 0 404 3000 ;load AC from register - AC is now 0 405 2942 ;store AC to memory 942 - 942 is now 0 406 BA00 ; branch to subroutine starting at address 100 407 BACA ; branch to subroutine starting at AOA 408 1942 ;load AC from memory 942 - AC is decimal 60 (line 8 subroutine 2) 409 6014 ;load register with 0x14 = decimal 20 40A 7000 ;add register to AC - AC is now decimal 80 40B 2942 ;store AC in memory 942 - 942 is now decimal 80, hex 50 40C F000 ;halt program 10. 11. 12. 13. ;code separator Subroutine 1 ===== 1. 2. 3. 4. 5. 6. 7. 8. 9. A00 6001 ;load register with 1 A01 3000 ;load AC from register AC is now 1 A02 6009 ;load register with 9 A03 7000 ;add register to AC - AC is now decimal 10, hex A A04 4000 ;store AC in register - register is now decimal 10 A05 1942 ;load AC from memory 942 AC is now 0 (942 is already 0) A06 7000 ;add register to AC - AC is now decimal 10 A07 2942 ;store AC in memory 942 - 942 is now decimal 10 A08 C000 ;return from subroutine ========= Subroutine 2 1. 2. 3. 4. 5. 6. 7. 8. 9. AOA 6005 ;load register with 5 AOB 3000 ;load AC from register - AC is now 5 AOC 600A ;load register with A - decimal 10 AOD 8000 ;multiply AC with register - AC is now decimal 50 A0E 4000 ;store AC to register - register is now decimal 50 AOF 1942 ;load AC from memory 942 AC is now decimal 10 A10 7000 ;add register to AC - AC is now decimal 60 A11 2942 ;store AC to memory 942 - 942 is now decimal 60 A12 C000 ;return from subroutine

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions