Riven
|
 |
«
Posted
2011-09-22 21: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:1xx | ADD xx | ADD | Add the value stored in mailbox xx to whatever value is currently on the accumulator. | 2xx | SUB xx | SUBTRACT | Subtract the value stored in mailbox xx from whatever value is currently on the accumulator. | 3xx | STA xx | STORE | Store the contents of the accumulator in mailbox xx. | 5xx | LDA xx | LOAD | Load the value from mailbox xx and enter it in the accumulator. | 6xx | BRA xx | BRANCH (unconditional) | Set the program counter to the given address (value xx). | 7xx | BRZ xx | BRANCH IF ZERO (conditional) | If the accumulator contains the value 0, set the program counter to the value xx. | 8xx | BRP xx | BRANCH IF POSITIVE (conditional) | If the accumulator is 0 or positive, set the program counter to the value xx. | 901 | INP | INPUT | Go to the INBOX, fetch the value from the user, and put it in the accumulator | 902 | OUT | OUTPUT | Copy the value from the accumulator to the OUTBOX. | 000 | HLT | HALT | Stop working. | xxx | n/a | DATA | Instruction which simply loads the value into the next available mailbox. | Simple program: (adds 13 and 14)address | instr | code | description | #0 | 104 | ADD 4 | add value of address #4 to register | #1 | 105 | ADD 5 | add value of address #5 to register | #2 | 902 | OUT | write register to output | #3 | 0 | HLT | halt execution of program | #4 | 13 | DATA: 13 | store the value 13 | #5 | 14 | DATA: 14 | store 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. [applet archive=lmc_ui.jar class=eden.emulator.lmc.LmcApplet width=512 height=256] 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!
|
|
|
theagentd
|
 |
«
Reply #1 - Posted
2011-09-23 00:16:21 » |
|
Weird...
|
Myomyomyo.
|
|
|
princec
|
 |
«
Reply #2 - Posted
2011-09-23 00: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  Cas 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
pitbuller
|
 |
«
Reply #3 - Posted
2011-09-23 05:08:26 » |
|
I see potential of great competitions.
|
|
|
|
Riven
|
 |
«
Reply #4 - Posted
2011-09-23 06:56:40 » |
|
When I fix the copy/paste functionality of the JTextArea... we can actually share code 
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
Riven
|
 |
«
Reply #5 - Posted
2011-09-23 18:43:47 » |
|
When I fix the copy/paste functionality of the JTextArea... we can actually share code  Seems like this is caused by the sandbox: you cannot copy & paste to/from the app.  Is this new behaviour? Edit: Right... copy/paste is deemed a security threat since 6u24 
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
|
lhkbob
|
 |
«
Reply #7 - Posted
2011-09-23 19:24:23 » |
|
So how long have we been able to embed applets in the forum? Is it a ninja feature?
|
|
|
|
Riven
|
 |
«
Reply #8 - Posted
2011-09-23 21: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!
|
|
|
Riven
|
 |
«
Reply #9 - Posted
2011-09-23 21: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 STA :base init: LDA :base STA :countdown
multiply: LDA 91 ADD 90 BRP :nxt LDA :zero STA 91 HLT nxt: STA 91 LDA :countdown SUB :one STA :countdown BRZ :display BRA :multiply
display: LDA 91 OUT BRA :selfmod
selfmod: LDA 4 ADD :one STA 4 LDA 5 ADD :one STA 5 LDA 8 ADD :one STA 8 LDA 10 ADD :one STA 10 LDA 16 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! Check 'em out!
|
|
ra4king
|
 |
«
Reply #10 - Posted
2011-09-26 01: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?
|
|
|
|
namrog84
|
 |
«
Reply #11 - Posted
2011-09-26 03: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"
|
|
|
ra4king
|
 |
«
Reply #12 - Posted
2011-09-26 03: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?
|
|
|
|
ReBirth
|
 |
«
Reply #13 - Posted
2011-09-26 03:56:32 » |
|
This is what I (should) learn in college, thanks 
|
|
|
|
Riven
|
 |
«
Reply #14 - Posted
2011-09-26 08: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!
|
|
|
Riven
|
 |
«
Reply #15 - Posted
2011-09-26 08: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 { int data = this.memory[this.program_counter]; this.last_program_counter = program_counter;
this.program_counter += 1; this.execute_counter += 1;
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; }
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!
|
|
|
ra4king
|
 |
«
Reply #16 - Posted
2011-09-27 04:45:57 » |
|
Oh it's in pure Java? Then why did you need to sign it?
|
|
|
|
Riven
|
 |
«
Reply #17 - Posted
2011-09-27 04:50:44 » |
|
To enable copy & paste 
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings!
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2011-09-27 04:59:16 » |
|
Stupid Oracle  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?
|
|
|
|
ReBirth
|
 |
«
Reply #19 - Posted
2011-09-27 12:06:02 » |
|
Very interesting. I have a little naughty idea in my mind right now~ Do you want to release it as full open-source?
|
|
|
|
Riven
|
 |
«
Reply #20 - Posted
2011-09-27 13:28:15 » |
|
Very interesting. I have a little naughty idea in my mind right now~ 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!
|
|
|
Mike
|
 |
«
Reply #21 - Posted
2011-09-27 15: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.  Mike
|
|
|
|
theagentd
|
 |
«
Reply #22 - Posted
2011-09-27 23: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.  Mike I can't help thinking: Worrying about our ctrl-c-ed porn links, are we? 
|
Myomyomyo.
|
|
|
ReBirth
|
 |
«
Reply #23 - Posted
2011-09-28 12:07:49 » |
|
Very interesting. I have a little naughty idea in my mind right now~ 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.
|
|
|
|
Cero
|
 |
«
Reply #24 - Posted
2011-09-28 15: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.  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 If you have anything from passwords to bank accounts in your clipboard Obviously - so don't be that stupid.
|
|
|
|
sproingie
|
 |
«
Reply #25 - Posted
2011-09-28 15: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.
|
|
|
|
Mike
|
 |
«
Reply #26 - Posted
2011-09-28 16:02:03 » |
|
Obviously - so don't be that stupid.
You'd be surprised with how little most computer users think about security  Mike
|
|
|
|
ra4king
|
 |
«
Reply #27 - Posted
2011-09-28 20:39:32 » |
|
Saykureti? What's that?
|
|
|
|
johnkershaw
Senior Newbie 
|
 |
«
Reply #28 - Posted
2012-09-02 16:54:42 » |
|
Sorry to necropost, but I'm VERY interested in using your Little Man Computing app with my students this term, and I suspect others may too. I'm switching to teaching GCSE Computing this year, from ICT. One of the programming research components has students use LMC to investigate 'bottom line' computing, learning how computers shift bits around, seeing how memory is used at low-level, etc. Unfortunately, the LMC app they recommend: http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.htmldoesn't work very well in any of my (Mac) browsers, specifically the text boxes are too small and only display one character, not much use when the box contain '901' and all I can see is a centre '0'. Definitely not good for students putting screenshots into reports... So I went looking for an alternative LMC app... and there aren't any! The *idea* is well documented, but implementations are thin on the ground. Yours is excellent, especially the syntax colouring and single-stepping with line highlight. Unfortunately, I'm new to Java and can't figure out how to get this to be a standalone app, even though you kindly posted the source. I downloaded Eclipse, did the intro-tutorials, then pasted your code as a new project, but both 'checkAddress' and 'execute' are marked with red crosses, I *think* because 'EmulatorCrash cannot be resolved to a type'. Do I need to load a library/module/something to make this work? I did google 'EmulatorFeedback' and 'EmulatorCrash' but the only results point back to your page, which makes me think this is something specific to your code that I'm missing. Alternatively, can I download the finished .jar file somehow? Not as good as having the source to go through with the students, but better than having to have them load the LMC from the discussion page! John. PS No offence, but the anti-spam sign-up requirements for this BB are mighty steep for someone just wanting to ask a casual question!
|
|
|
|
jonjava
|
 |
«
Reply #29 - Posted
2012-09-02 21:24:44 » |
|
We use MIPS in our uni, it's made in pure java too. http://courses.missouristate.edu/kenvollmar/mars/[EDIT]: Oh you want to run it directly in the browser? Nvm then. But if you run it as an applet only isn't it hard to save and load previous code snippets?
|
|
|
|
|