By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

help in writing cpu simulator

P: 7
hi all
i need a help in writing cpu simulator

i will thank everybody who will guide me how to start doing that and explain this:

Program conditionals (loops, if-then-else) are translated into a different family of assembly instructions called branch instructions. Branch instructions check some simple condition, and if the condition is true the next instruction executed by the CPU will not be the following instruction in the code, but rather the CPU will jump to another place in the program. For example, let's assume that instruction number 15 in the program is a branch instruction that tells the CPU to check whether a certain value is positive, and if so jump back 4 instructions. The CPU will then check the condition: if the condition fails (the value is not positive) the next instruction it will execute will be instruction number 16 --- the following instruction. If the condition holds however --- the value in question is indeed positive --- the CPU will jump back 4 instructions, and the next instruction executed will be instruction number 11 (= 15 - 4).
also this :


Syntax Equivalent Java Code Details
nop /**/ No-OPeration: Do nothing
add rA rB rC rA = rB + rC Sum registers rB and rC and store the result in register rA
sub rA rB rC rA = rB - rC Subtract register rC from register rB and store the result in register rA
mul rA rB rC rA = rB * rC Multiply registers rB and rC and store the result in register rA
div rA rB rC rA = rB / rC Divide register rB by register rC and store the result in register rA
b Offset PC = PC + Offset Branch: add the offset to the PC register (equivalent to goto)
bz rA Offset if(rA==0) PC = PC + Offset Jump "Offset" instructions if rA contains 0 (conditional goto)
bpos rA Offset if(rA > 0) PC = PC + Offset Jump "Offset" instructions if rA contains a positive integer (conditional goto)
bneg rA Offset if(rA < 0) PC = PC + Offset Jump "Offset" instructions if rA contains a negative integer (conditional goto)
load rA rB rA = memory[rB] Load the data from memory address rB into register rA
store rA rB memory[rB] = rA Store the value in register rA in memory address rB
read rA rA = SimpleInput.in.readInt() Read an integer from the user and store it in register rA
write rA System.out.println(rA) Print the value stored in rA
exit rA System.exit(rA) Exit the emulator with the value from register rA as exit status
set rA Number rA = Number Store the integer "Number" in rA


i also thanks everybody to provide me the main algorithm
Jan 21 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
hi all
i need a help in writing cpu simulator

i will thank everybody who will guide me how to start doing that and explain this:

Program conditionals (loops, if-then-else) are translated into a different family of assembly instructions called branch instructions. Branch instructions check some simple condition, and if the condition is true the next instruction executed by the CPU will not be the following instruction in the code, but rather the CPU will jump to another place in the program. For example, let's assume that instruction number 15 in the program is a branch instruction that tells the CPU to check whether a certain value is positive, and if so jump back 4 instructions. The CPU will then check the condition: if the condition fails (the value is not positive) the next instruction it will execute will be instruction number 16 --- the following instruction. If the condition holds however --- the value in question is indeed positive --- the CPU will jump back 4 instructions, and the next instruction executed will be instruction number 11 (= 15 - 4).
also this :


Syntax Equivalent Java Code Details
nop /**/ No-OPeration: Do nothing
add rA rB rC rA = rB + rC Sum registers rB and rC and store the result in register rA
sub rA rB rC rA = rB - rC Subtract register rC from register rB and store the result in register rA
mul rA rB rC rA = rB * rC Multiply registers rB and rC and store the result in register rA
div rA rB rC rA = rB / rC Divide register rB by register rC and store the result in register rA
b Offset PC = PC + Offset Branch: add the offset to the PC register (equivalent to goto)
bz rA Offset if(rA==0) PC = PC + Offset Jump "Offset" instructions if rA contains 0 (conditional goto)
bpos rA Offset if(rA > 0) PC = PC + Offset Jump "Offset" instructions if rA contains a positive integer (conditional goto)
bneg rA Offset if(rA < 0) PC = PC + Offset Jump "Offset" instructions if rA contains a negative integer (conditional goto)
load rA rB rA = memory[rB] Load the data from memory address rB into register rA
store rA rB memory[rB] = rA Store the value in register rA in memory address rB
read rA rA = SimpleInput.in.readInt() Read an integer from the user and store it in register rA
write rA System.out.println(rA) Print the value stored in rA
exit rA System.exit(rA) Exit the emulator with the value from register rA as exit status
set rA Number rA = Number Store the integer "Number" in rA


i also thanks everybody to provide me the main algorithm
Well you should have done something at least. You just posted the question and no ideas of your own. Go through the instructions you want to simulate and put labels on them. Then read about how to use labels in Java. After that you should be able to do this.
Jan 22 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.