Tuesday, August 24, 2010

Programming Languages / Layers of Abstraction, Part 2

Well, at least someone commented, and it was provocative (to me) enough to merit another post. Though even if it weren't I would probably write another post anyway. Here is the comment:
mpk3 said...
Thank you Matthew. I think that helped me, but I am still wondering what those different conversations in machine, assembly, symbolic, and programming languages mean. Could you translate for us? What would those rows of numbers and letters accomplish on a computer?

First, OH NOOOO!, my name has been revealed, and the initials of the commenter have been revealed as well, I think the world is going to explode.

I guess it didn't. Well, it appears the question is aimed at computing on an even smaller level. This lower level, lower than code, is the actual circuit, which again, not surprisingly is also very similar to human thought. It relies on small silicon circuit gates. At this point there is a huge design compromise, either you can make a chip which can differenciate between 2 voltages, thus the 0 and 1, or the more efficient but less expensive circuits which can differenciate between as many voltages as you would like. But if you take the latter route the problems snowball. You now need a power supply that is even more precise with the current it outputs, and you need to develop a standard amount of voltages that can be detected. If not, person A will make a computer that is totally incompatible with the computer that person B made. So for all parties involved it is easier and cheaper to go with a circuit of a set resistance. If the current reaching the circuit is above the threshold, it moves on (1), if not (0).

To get anything useful done with such a basic component, you're going to need to chain them together. Here's where symbolic logic becomes useful again.

NOT gate:
the most fundamental unit in computing, it is simply a single transistor. It always receives a 1 from the power pulsing through the computer and then takes an input value, if it receives another 1, it outputs 0, if it receives a 0, it outputs a 1. The following circuits are made from multiple NOT gates

OR gate:
takes two inputs (which, consequently are outputs from other circuits), and if at least one is a 1, outputs a 1 (which, consequently is an input for the next circuit), if both are 0, than it outputs a 0

AND gate:
takes two inputs, and if both are 1, then it outputs a 1, otherwise it outputs a 0

XOR gate:
takes two inputs, it 1 is a 1 then it outputs a 1, if both are 0 or both are 1, it outputs a 0

To illustrate what happens at the most basic level, i will use an example out of the book, 'How Computers Work' by Ron White.

The computer adds (which is really the only thing it can do) using combinations of gates called adders. First, a half-adders, which can only add 2 single binary digits. For instance 1 and 1. A half adders combines the output of an AND and an XOR gate. So 1 and 1 are sent to an AND gate, producing a 1. Then 1 and 1 are sent to an XOR gate, producing a 0. Put 1 and 0 together you get 10 binary, which is 2 in decimal. A full adder is simply a more elaborate collection of gates that i cant explain without a drawing. Also, a full adder must be combined with a half adder to add 2 digit binary numbers, thus getting more complex.

save image
This is all meant to illustrate the lowest level of computing, how 10+11, when run through a combination of circuits ends up as 101.

So now up to the higher levels, which are actually quite dissapointing, but it's what mpk3 asked for. Well, a low level is this 0110101010111, and if you translate a higher level command such as 'if (x == 3) System.out.println("Yes");" you would get something like this 10110101011101010001001010101010000001110101011010100101010000000111111111110100010111011010101000110111111011010101011001011010101010101010101101etc. And if you go up even higher you just get more of the same. So really computing from that standpoint comes down to righting a combination of 1s and 0s, called a compiler, which will turn commands such as 'disp' or 'out' into more 1s and 0s, or commands such as clicking a link and scrolling down a webpage into even more 1s and 0s.

But, as i close, how is this similar to human thought. A neuron is simply a collection of cells which takes input from other neurons, if the input from other neurons is strong enough, it gives an output. A neuron is both organic and far more advanced in that it can take many many inputs and produce one outputs, where a circuit is limited to only two inputs.

As always, leave comments and suggestions for future posts.

Further Reading:
How Computers Work, by Ron White

Saturday, August 7, 2010

iPhone 4 + bad antenna = a revolutionary rubber band Steve Jobs can't make enough of.

Ok, ok, that was some unashamed apple bashing in the title, but here is the follow up to my previous iPhone 4 antenna article. The situation is still suprisingly active, which is why i waited a couple weeks to let the dust (sorta) settle.
This will degrade into lots of jokes at the end but for now, the serious:
Apple held a press conference to deal with the issue and confirmed that it cannot be fixed with software (after saying a week earlier that software would fix the problem) and instead offered people free bumpers. A bumper is NOT a case, it is an oversized, heavy-duty rubberband which covers the stainless steel rim of the phone. It cost a whopping $30 at launch, though apple is refunding purchasers automatically. Steve Jobs said they can't make enough, maybe so, there is no way to tell, but I do find it interesting that the iPhone 4 was the first iPhone to have a bumper style case instead of a full case. The iphone 4 is made out of very durable glass (in a durability test, it took 3 falls from standing height onto concrete to break) and shouldn't need a case at all, isn't it convenient that apple only makes a "case" to cover the part of the phone that must be covered. Then Steve Jobs went on to say that the iPhone 4 is not alone in this problem (ironically, the iPhone 3G had this problem in a more minor fashion) but he points out other phones such as the droid X, and somehow has video footage of it succumbing to the "death grip." Yet other users of the droid X don't have this issue (and gary definitely doesn't have this issue). So ya, apple said they messed up, and is paying (pocket-change after another record quarter) for it's mistake.

And now for a gag reel of sorts:
1. Microsoft COO Kevin Turner said about the iPhone 4: "It looks like the iPhone 4 might be their Vista, and I'm okay with that." http://www.engadget.com/2010/07/14/ce-oh-no-he-didnt-microsofts-kevin-turner-says-the-iphone-4/
2. If a bumper is too bulky, why not a band-aid, you can get 6 assorted colors for only five dollars, remember, "Ben Franklin would approve."http://www.engadget.com/2010/07/20/antenna-aid-bandages-your-iphone-4-reception-issue-hopes-for-ro/

3. If a bandaid was too goofy, here is an end call sticker to cover up that pesky seam. http://www.engadget.com/2010/07/22/iphone-end-call-sticker-is-an-ironic-solution-to-your-receptio/


See, even when apple messes up they create a new category of products. Hope you enjoyed it!

Further reading:
http://www.engadget.com/2010/07/16/apple-to-give-away-free-bumpers-to-iphone-4-users/
http://www.engadget.com/2010/07/23/apple-keeps-antennagate-alive-with-droid-xs-kill-switch-vide/
http://www.knowyourcell.com/apple/apple-iphone-4/iphone-4-reviews/544700/apple_iphone_4_bumper_review.html

Programming Languages / Layers of Abstraction

Winston requested I do a post on programming languages. And I have been considering how to go about doing this for over a week now, and I think I've got a decent way to explain it. To fully understand the subject would take a whole book, as this subject overlaps heavily with how human thought works. To avoid have to read a whole book just take my word for it that programming and layers of abstraction very closely mirror how the brain works. I know, it's hard to just trust me on this one, and if you don't believe me, then just go read Godel, Escher, Bach by Douglas Hofstadter. But that is probably 750 pages more than your interested in reading. So here we go.....
Layers of Abstraction
A computer is merely a sophisticated combination of circuits which can determine either the presence or lack of electric current. So the basic unit of information in a computer is the bit, which is a 1 or a 0, on or off. But one bit isn't enough to accomplish anything, so 8 bits are grouped into the fundamental data unit called a byte. A byte provides 2^8, or 256 combinations. So here is the lowest layer of abstraction,: feeding a computer a series of 1s and 0s. Well, this makes programming pretty difficult, expecting humans to give a computer thousands of 1s and 0s so that it will do what you want. Because this is too difficult, computer chips are designed to natively support machine language. This machine language is still incredibly tedious, and we lazy humans expect an easier way to program our computers because machine language requires many hours of effort to program even the most minute tasks. Here is machine language:

   6      5     5     5     5      6 bits
[  op  |  rs |  rt |  rd |shamt| funct]  R-type
[  op  |  rs |  rt | address/immediate]  I-type
[  op  |        target address        ]  J-type

 So we invented assembly languages. These languages are more effective and quicker but must first be converted back into machine language before they can be run.
For some time people were willing to deal with assembly language and the conversion process it required because it was still alot faster than writing 00101110101000101111101010001011, even though it looked like jibberish, here is an example:

.list
.org $9D95 - 2
  .db t2ByteTok, tAsmComp
Main:
  b_call(_ClrLCDFull);

So then people decided to make symbolic languages, which required more conversion but were much simpler to write in. The first of these had to be made independentlyfor all models of computers but looked something like this:

10 INPUT "What is your name: ", U$
20 PRINT "Hello "; U$
30 INPUT "How many stars do you want: ", N
40 S$ = ""
50 FOR I = 1 TO N
60 S$ = S$ + "*"
70 NEXT I
80 PRINT S$
90 INPUT "Do you want more stars? ", A$
100 IF LEN(A$) = 0 THEN GOTO 90
110 A$ = LEFT$(A$, 1)
120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30
130 PRINT "Goodbye "; U$
140 END
A very big improvement from assembly language. Overtime these languages have become even more sophisticated. I am currently taking a course in the Java programming language so here is a simple java program:
public class NameMaker
{
 private String fullName;
 public NameMaker(String firstName, String lastName)
 {
  fullName = firstName + lastName;
 }
 public String getFullName
 {
  return fullName;
 }
}
public class NameTester
{
 public static void main(String[] args)
 { 
  String name1 = "John";
  String name2 = "Doe";
  NameMaker name3 = new NameMaker(name1, name2);
  System.out.println(name3);
 }
}

Im sorry for the crazy font issues, but this is all for now, in the comments let me know what follow ups i need to write, because i realize this is only the beginning but im not sure what the best path to take would be, i will judge that by your responses.