Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Little Man Computer - Simulator Applet  (Read 2872 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« on: 2011-09-22 17:54:01 »

If you're nerdy like me, you might be into low level programming, which means fiddling with instructions. Unfortunately, the complexity easily spirals out of control, hence developing in a simple instruction-set is a great way to learn how a CPU works, and being able to write tiny applications that get things done.

An excellent example of such a language is Little-Man-Computer. It features a CPU with 1 register, 1 input, 1 output, 1 flag and 100 memory slots. It is minimalistic to the point that it lacks instructions for multiplication and division.


Specification of the instruction set:
1xxADD xxADDAdd the value stored in mailbox xx to whatever value is currently on the accumulator.
2xxSUB xxSUBTRACTSubtract the value stored in mailbox xx from whatever value is currently on the accumulator.
3xxSTA xxSTOREStore the contents of the accumulator in mailbox xx.
5xxLDA xxLOADLoad the value from mailbox xx and enter it in the accumulator.
6xxBRA xxBRANCH (unconditional)Set the program counter to the given address (value xx).
7xxBRZ xxBRANCH IF ZERO (conditional)If the accumulator contains the value 0, set the program counter to the value xx.
8xxBRP xxBRANCH IF POSITIVE (conditional)If the accumulator is 0 or positive, set the program counter to the value xx.
901INPINPUTGo to the INBOX, fetch the value from the user, and put it in the accumulator
902OUTOUTPUTCopy the value from the accumulator to the OUTBOX.
000HLTHALTStop working.
xxxn/aDATAInstruction which simply loads the value into the next available mailbox.


Simple program: (adds 13 and 14)
addressinstrcodedescription
#0104ADD 4add value of address #4 to register
#1105ADD 5add value of address #5 to register
#2902OUTwrite register to output
#30HLThalt execution of program
#413DATA: 13store the value 13
#514DATA: 14store the value 14

Obviously you can make (conditional) jumps, which allows you to make loops.

I created an applet which simulates this CPU, for you guys to play around. It really gets fun, once you get the hang of it.

Warning! Untrusted content: Riven submitted an applet to JGO.


 
If the applet asks for permissions, it will have full access to your system. (read more)
JGO cannot be held responsible for the contents and/or behavior of the hosted applets.


First thing you can do is trying to read two numbers from INPUT, subtract them, and write them to the OUTPUT. Next you can write code that is able to multiply two numbers. Maybe you feel naughty and try to raise N to the power of M. You'll need to use self-modifying code, seriously.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #1 on: 2011-09-22 20:16:21 »

Weird...

There is no god.
Offline princec
« League of Dukes »

JGO Kernel
*****

Posts: 8089
Medals: 95


Eh? Who? What? ... Me?


« Reply #2 on: 2011-09-22 20:43:33 »

It's 2am nearly here so I'm not going to try it yet but I love the new applet stuff in JGO Smiley

Cas Smiley

Games published by our own members! Go get 'em!
Offline pitbuller

Sr. Member
**

Posts: 340
Medals: 9



« Reply #3 on: 2011-09-23 01:08:26 »

I see potential of great competitions.

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #4 on: 2011-09-23 02:56:40 »

When I fix the copy/paste functionality of the JTextArea... we can actually share code persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #5 on: 2011-09-23 14:43:47 »

When I fix the copy/paste functionality of the JTextArea... we can actually share code persecutioncomplex
Seems like this is caused by the sandbox: you cannot copy & paste to/from the app. Shocked Is this new behaviour?

Edit: Right... copy/paste is deemed a security threat since 6u24 Lips Sealed

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Cero

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #6 on: 2011-09-23 15:19:30 »

When I fix the copy/paste functionality of the JTextArea... we can actually share code persecutioncomplex
Seems like this is caused by the sandbox: you cannot copy & paste to/from the app. Shocked Is this new behaviour?

Edit: Right... copy/paste is deemed a security threat since 6u24 Lips Sealed

Yeah...
http://www.java-gaming.org/topics/is-it-possible-to-get-text-out-of-an-unsigned-applet/24506/msg/206538/view.html#msg206538
http://blogs.oracle.com/kyle/entry/copy_and_paste_in_java

It's weird.
And stupid I might add.

Offline lhkbob

JGO Neuromancer
****

Posts: 1174
Medals: 35



« Reply #7 on: 2011-09-23 15:24:23 »

So how long have we been able to embed applets in the forum? Is it a ninja feature?

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #8 on: 2011-09-23 17:07:53 »

So how long have we been able to embed applets in the forum? Is it a ninja feature?
Developing it now... the only thing that's missing is a safe way to upload the JARs.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #9 on: 2011-09-23 17:36:24 »

Back ontopic:

I added syntax highlighting and highlighted the line of code currently executed, which is very convenient when developing stuff.



This is the code that calculates N to the power of 1,2,3,4,5,... etc (until it reaches 999, which is the maximum value of a memory cell).

You can see how you can define labels (multiply:) and use labels (:multiply)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
INP // fetch 'base' value from user
STA :base
init: LDA :base
STA :countdown

multiply: LDA 91 // :base = @90, 91 is next
ADD 90
BRP :nxt // did we overflow ?
LDA :zero
STA 91 // fill last addess with zero
HLT
nxt: STA 91
LDA :countdown // subtract one from countdown
SUB :one
STA :countdown
BRZ :display // reached zero in 'countdown'
BRA :multiply // back

display: LDA 91
OUT
BRA :selfmod

selfmod: LDA 4 // advance LDA & STA instructions
ADD :one
STA 4
LDA 5
ADD :one
STA 5
LDA 8
ADD :one
STA 8
LDA 10
ADD :one
STA 10
LDA 16 // for display
ADD :one
STA 16
BRA :init

stop: HLT

@90 base: 0
one: 1
end: 0
countdown: 0
zero: 0

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Games published by our own members! Go get 'em!
Online ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #10 on: 2011-09-25 21:48:55 »

Ok I am in need of some explanations:
1. What is a register?
2. What do you mean by input, output and flag?

Offline namrog84

Full Member
**

Posts: 245
Medals: 12


Keep programming!


« Reply #11 on: 2011-09-25 23:23:45 »

Ok I am in need of some explanations:
1. What is a register?
2. What do you mean by input, output and flag?

registers are a type of memory location inside the processor.
You typically only interact or learn about them when doing assembly programming and whatnot.

common assembly code would be like
add $s0, $t0, $t1    # this would be equivalent to s0=t0+t1
the 3 variables shown are all registers
But in this case there are 3 registers instead of 1

It essentially will go from hard drive to memory(ram) to cpu's registers(cpu's memory) and then often times back and forth between cpu's memory and ram's memory quite a bit in a typical application.

I also, if I am not mistaken, the input/outputs are simply  input from user, and output to user.

I am really fuzzy on all of it though, so I am sorry Riven if I explained incorrectly.


His program is in my opinion/mind is a very basic processor emulator in a low level (high level being C/C++/Java/etc..).   One of the classes in computer science in college, sometimes called computer organization you learn about assembly or MIPs and get to design a basic CPU from the ground up. lots of binary/hex/registery/ and pain in the ass assembly code.



"Experience is what you get when you did not get what you wanted"
Online ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #12 on: 2011-09-25 23:30:12 »

This all sounds really really really really fun! I'm gonna be playing around with this for hours on end.

@Riven
Could you please post the source code of your Little Man Computer applet?

Offline ReBirth

JGO Wizard
****

Posts: 1275
Medals: 19



« Reply #13 on: 2011-09-25 23:56:32 »

This is what I (should) learn in college, thanks Cheesy

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #14 on: 2011-09-26 04:03:19 »

I also, if I am not mistaken, the input/outputs are simply  input from user, and output to user.

In the LMC spec, the input/output is poorly specified (I assume it is asynchronous), so if something is outputed twice, and you don't read the OUTPUT values (externally) fast enough, you'll lose data, and if you change the data in INPUT before the CPU has read them, they are lost too.

To keep it maintainable, I made both INPUT and OUTPUT blocking, with a modal dialog.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #15 on: 2011-09-26 04:48:38 »

@Riven
Could you please post the source code of your Little Man Computer applet?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
public class Lmc
{
   public static final int HLT                  = 0;
   public static final int ADD                  = 1;
   public static final int SUB                  = 2;
   public static final int STA                  = 3;
   public static final int LDA                  = 5;
   public static final int BRA                  = 6;
   public static final int BRZ                  = 7;
   public static final int BRP                  = 8;
   public static final int UIO                  = 9;

   private int             program_counter      = 0;
   private int             execute_counter      = 0;
   private int             last_reference       = 0;
   private int             last_program_counter = 0;
   private int[]           memory               = new int[100];
   private int             boxInput             = 0;
   private int             boxOutput            = 0;
   private int             register;
   private boolean         overflowFlag         = false;

   public Lmc()
   {
      this.init();
   }

   public void load(int[] memory)
   {
      if (memory.length != this.memory.length)
      {
         throw new IllegalStateException("expected " + this.memory.length + " memory values");
      }

      for (int i = 0; i < memory.length; i++)
      {
         if (memory[i] < 0 || memory[i] > 999)
            throw new IllegalStateException("illegal memory value @" + i + ": " + memory[i]);
         this.memory[i] = memory[i];
      }

      this.init();
   }

   public void init()
   {
      this.reset();
      this.execute_counter = 0;
   }

   public void reset()
   {
      this.boxInput = 0;
      this.boxOutput = 0;
      this.register = 0;
      this.overflowFlag = false;
      this.program_counter = 0;
      this.last_program_counter = -1;
      this.last_reference = -1;
   }

   public int getInstructionPointer()
   {
      return this.program_counter;
   }

   public int getLastInstructionPointer()
   {
      return this.last_program_counter;
   }

   public int getLastReference()
   {
      return this.last_reference;
   }

   public boolean getOverflowFlag()
   {
      return this.overflowFlag;
   }

   public int readInbox()
   {
      return this.boxInput;
   }

   public int readOutbox()
   {
      return this.boxOutput;
   }

   public int readRegister()
   {
      return this.register;
   }

   public int readMemory(int address)
   {
      return this.memory[address];
   }

   public int execute(EmulatorFeedback feedback) throws EmulatorCrash
   {
      // fetch instruction
     int data = this.memory[this.program_counter];
     
      this.last_program_counter = program_counter;

      // advance program counter
     this.program_counter += 1;
      this.execute_counter += 1;

      // decode instruction
     int instruction = data / 100;
      int arg = data % 100;
      this.last_reference = arg;

      switch (instruction)
      {
         case HLT:
            this.last_reference = -1;
            if (arg != 0)
            {
               feedback.invalidInstruction();
               throw new EmulatorCrash();
            }
            this.reset();
            feedback.halt();
            break;

         case ADD:
            int doAdd = this.memory[checkAddress(arg, feedback)];
            this.register = normalizeRegister(register + doAdd);
            break;

         case SUB:
            int doSub = this.memory[checkAddress(arg, feedback)];
            this.register = normalizeRegister(register - doSub);
            break;

         case LDA:
            this.register = this.memory[checkAddress(arg, feedback)];
            break;

         case STA:
            this.memory[checkAddress(arg, feedback)] = this.register;
            break;

         case BRA:
            this.program_counter = checkAddress(arg, feedback);
            break;

         case BRZ:
            if (this.register == 0)
               this.program_counter = checkAddress(arg, feedback);
            break;

         case BRP:
            if (!this.overflowFlag)
               this.program_counter = checkAddress(arg, feedback);
            break;

         case UIO:
            this.last_reference = -1;
            switch (arg)
            {
               case 1:
                  this.boxInput = feedback.input();
                  this.register = this.normalizeRegister(this.boxInput);
                  break;

               case 2:
                  this.boxOutput = this.register;
                  feedback.output(this.boxOutput);
                  break;

               default:
                  feedback.invalidInstruction();
                  throw new EmulatorCrash();
            }
            break;

         default:
            break;
      }

      return data;
   }

   private int normalizeRegister(int value)
   {
      final int max = 1000;

      this.overflowFlag = (value < 0 || value >= max);

      return ((value % max) + max) % max; // ensure value between 0 and 999
  }

   private static int checkAddress(int addr, EmulatorFeedback feedback) throws EmulatorCrash
   {
      if (addr < 0 || addr >= 100)
      {
         feedback.invalidAddress(addr);
         throw new EmulatorCrash();
      }
      return addr;
   }
}

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Online ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #16 on: 2011-09-27 00:45:57 »

Oh it's in pure Java? Then why did you need to sign it?

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #17 on: 2011-09-27 00:50:44 »

To enable copy & paste Angry

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Online ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #18 on: 2011-09-27 00:59:16 »

Stupid Oracle Angry

Note: You can't copy/paste from Flash apps either so this is not just Java. I don't get it, what's "not secure" about simple text?

Offline ReBirth

JGO Wizard
****

Posts: 1275
Medals: 19



« Reply #19 on: 2011-09-27 08:06:02 »

Very interesting. I have a little naughty idea in my mind right now~ Cool
Do you want to release it as full open-source?

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #20 on: 2011-09-27 09:28:15 »

Very interesting. I have a little naughty idea in my mind right now~ Cool
Do you want to release it as full open-source?
Nope, not like that. If you have specific plans, PM me and I'll make up my mind.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #21 on: 2011-09-27 11:14:42 »

I don't get it, what's "not secure" about simple text?

Paste: If you have anything from passwords to bank accounts in your clipboard the program will be able to pick it up.
Copy: Overwrite something important you have in your clipboard without asking you. Smiley

Mike

Offline theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #22 on: 2011-09-27 19:16:48 »

I don't get it, what's "not secure" about simple text?

Paste: If you have anything from passwords to bank accounts in your clipboard the program will be able to pick it up.
Copy: Overwrite something important you have in your clipboard without asking you. Smiley

Mike

I can't help thinking: Worrying about our ctrl-c-ed porn links, are we?


There is no god.
Offline ReBirth

JGO Wizard
****

Posts: 1275
Medals: 19



« Reply #23 on: 2011-09-28 08:07:49 »

Very interesting. I have a little naughty idea in my mind right now~ Cool
Do you want to release it as full open-source?
Nope, not like that. If you have specific plans, PM me and I'll make up my mind.
I will but not soon. My idea is kinda same as this, but different goal.

Offline Cero

JGO Neuromancer
****

Posts: 1050
Medals: 18



« Reply #24 on: 2011-09-28 11:01:36 »

I don't get it, what's "not secure" about simple text?

Paste: If you have anything from passwords to bank accounts in your clipboard the program will be able to pick it up.
Copy: Overwrite something important you have in your clipboard without asking you. Smiley

Mike

First: with those arguments, you could ban copy/paste entirely from an OS
second: whatever is in the clipboard isn't very safe. one of the browsers, if you copy a link, then close it, clipboard is empty
Quote
If you have anything from passwords to bank accounts in your clipboard
Obviously - so don't be that stupid.

Offline sproingie

JGO Strike Force
***

Posts: 897
Medals: 55



« Reply #25 on: 2011-09-28 11:46:37 »

Passwords are on my clipboard all the time, for either a limited time or for the duration of one paste, because I use a password manager that generates passwords like this: jGHt%6j#]KJ,Xsh%2

I hardly think using a proper password manager is stupid.  The real problem is that there isn't any consumer OS with the concept of a secure clipboard.
Offline Mickelukas

JGO Ninja
***

Posts: 731
Medals: 25


Java guru wanabee


« Reply #26 on: 2011-09-28 12:02:03 »

Obviously - so don't be that stupid.

You'd be surprised with how little most computer users think about security Smiley

Mike

Online ra4king

JGO Kernel
*****

Posts: 3156
Medals: 196


I'm the King!


« Reply #27 on: 2011-09-28 16:39:32 »

Saykureti? What's that?

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.156 seconds with 20 queries.