Mittwoch, 22. September 2010

The Little Man's Machine

As I am more the technologycal gifted person than the creative, I find it very interesting how machine code language works.
In another of our courses called Introduction to Computer Science we had to programm very simple things with an application called the LittleMan'sMachine, which is nearly like a machine code language. I will talk about the little difference later.
You can find this application on: http://www.atkinson.yorku.ca/~sychen/research/LMC/LittleMan.html .
As you need Java installed on your computer here is a link for Java too: http://www.java.com/de/download/windows_xpi.jsp?locale=de&host=www.java.com .

So we had some tasks to do with this application. After getting to know how the application works we had two main tasks which were the following:


fourth example:
  • make a program that compares two numbers (in cells 30 and 31),
  • if the first is smaller than the second, a "0" should be written to outbox,
  • if the second is smaller than the first one, a "1" should be written to outbox.
  • test your program with different numbers. 
fifth example
  • make a program that divides the number in cell 30 by the number in cell 31,
  • writes the whole number result into cell 32 and the rest in cell 33.
I will upload both of my conclusions but in order to spare time i will only explain the more difficult one (fifth example) .


fourth example conclusion:


fifth example conclusion:


Okay now I will explain the fifth example.
The task was to divide a number by another and gives out the whole number result as well as the rest.
So at first you need to think about how to divide, because in this application there is no operator that can divide.
But as you think about what dividing is you might find out that dividing is simply repeatly subtracting.
For example 5 divided by 2 is 2 and 1 rest, like 5-2=3 and you save 1 time substracted, 3-2=1 and you save 2 times substracted,1 < 2 so you save 2 times substracted and there is 1 rest.
So what out programm has to do is substract the second number from the first and save the number of substractions until the rest is smaller than the second number. 

Explanation of the code:
Cell 0: We load the number in cell 30 which is in the upper example the 5
Cell 1: We save the loaded number (here 5) in cell 20
Cell 2: We load the number in cell 30 again (makes no sence in first run but later we have to jump back to this point)
Cell 3: We subtract the number in cell 31 from the accumulator (here 5-2), the result is saved in the accumulator
Cell 4: We check whether the number in the accumulator is higher or 0. (here 3 >= 0 ==  true) As this is the case we jump to cell 10
Cell 10: We save the value of the accumulator in cell 20 (here 3)
Cell 11: We load the value of cell 32 which is the cell where in the end the whole number result is written in but right now the value is 0
Cell 12: We add the number in cell 8 which is a 1 to the accumulator (0+1=1)
Cell 13: We store the 1 in cell 32 that represents that we did 1 substraction until now
Cell 14: We jump back to cell 2
Cell 2: We load the rest of our number from cell 20 into the accumulator (here 3)
Cell 3: We substract the number in cell 31 from the accumulator value (here 3-2=1)
Cell 4: We check whether the number in the accumulator is higher or 0. (here 1 >= 0 ==  true) As this is the case we jump to cell 10
Cell 10: We save the value of the accumulator in cell 20 (here 1)
Cell 11: We load the value of cell 32 which is the cell where in the end the whole number result is written in (here 1)
Cell 12: We add the number in cell 8 which is a 1 to the accumulator (1+1=2)
Cell 13: We store the 2 in cell 32 that represents that we did 2 substractions until now
Cell 14: We jump back to cell 2
Cell 2: We load the rest of our number again (here1)
Cell 3: We substract the number in cell 31 from the accumulator value (here 1-2=-1)
Cell 4: We check whether the number in the accumulator is higher or 0. (here -1 >= 0 == false) As this isnot the case we do not jump to another cell than simply the next.
Cell 5: We add the last substracted number from the accumulator to get the rest (here -1+2=1)
Cell 6: We save the rest in cell 33 (here 1)
Cell 7: The programm ends


So in the end just the difference between this and real machine code. This is with numbers from 1 to 10 but the real machine code works with only ones and zeros so it would look even more complicated and nearly unreadable.

Keine Kommentare:

Kommentar veröffentlichen