Saturday, December 31, 2011

Book Review: "Secrets" by Daniel Ellsberg

Do you always do the "right" thing, or do you take the path of least resistance? "Secrets", by Daniel Ellsberg reveals the life of a person who did the right thing.

Daniel Ellsberg is the person who leaked The Pentagon Papers, a secret study about the lessons learned during the Vietnam war. Leaking the study required a lot of personal courage. He was tried for acts of treason, though he was later acquitted. I find the lives of people like Daniel fascinating. They knew the path of least resistance, and yet they actively worked against it because of their conviction about the "right" thing to do. I wonder if I will have the strength that people like Daniel did.

Secrets starts of with the Gulf of Tonkin incident, and Daniel talks about his observations when he was posted in Vietnam. This section of the book was very riveting: Daniel has incisive observations about the war that he saw. Later, Daniel was a scholar for the RAND corporation, studying the Vietnam war. The Pentagon Papers are the result of this study. Daniel talks about his experience with the secrecy in RAND and the Department of Defense.

I found Daniel's observation about the effect of access to secret clearance beautifully insightful. He warned Kissinger what the access to secret information will do. (Page 237 in the paperback version). I'll paraphrase Daniel's commentary:
At first, you'll feel exhilarated by the volume and extent of information. Then, you'll feel like a fool for having analyzed these topics without even knowing that these secret documents existed. Then, you'll be aware of others who don't have this information, and you'll think that these people are fools. Finally, you'll become incapable of learning from people, because most people don't have access to these secret documents. No matter how great their experience compared to yours, you'll be incapable of learning from them. 
This passage should be read in the original. It is a beautifully crisp understanding of the impact of secret clearances, and power in general.

Daniel talks about his thought-process, and why he decided to release the confidential study. He talks about the impact of the study, and the impact to his freedom. It makes a very interesting read.

It reads like a war thriller, but it carries deep lessons about how difficult it is to do the right thing.

Friday, December 30, 2011

Book Review: Autobiography of a Yogi

Why not read some third-grade fiction this weekend? Pick up a copy of "Autobiography of a Yoga" by Paramhansa Yogananda.

The book is about a "yogi", a person who has attained enlightenment of some sort. However, the book reads like a supernatural fairy tale. The very first chapter started off with this hoot of a passage.
The helpless humiliations of infancy are not banished from my mind. I was resentfully conscious of not being able to walk or express myself freely....  Psychological ferment and my unresponsive body brought me to many obstinate crying-spells.
The author claims that he was fully conscious of his infancy, and that he cried because he couldn't express himself. This is pretty damn hard to believe. Of course, the author is gone now, and no supporting evidence can be obtained. From what we know about brain development, it is difficult to believe that any child could have such capability. A few sentences before that, he claims that in his previous birth he was a Himalayan yogi. You would think such a highly perceptive creature would wait for the infancy days to get over before trying to talk.

A few pages later,  we get another passage tailor-made for the gullible audience:
An immense flash of light at once manifested to my inward gaze. Diving shapes of saints, sitting in meditating posture in mountain caves, formed like miniature cinema pictures on the large screen of radiance within my forehead.
"Who are you", I spoke aloud
"We are the Himalayan yogis." The celestial response is difficult to describe; my heart was thrilled.
"Ah, I long to go to the Himalayas and become like you!" The vision vanished, but silvery beams expanded in ever-widening circles to infinity.
"What is this wondrous glow"
"I am Iswara, I am Light." The voice was as murmuring clouds.
"I want to be one with Thee!"
It is as poorly written as it is imagined. In the first chapter, he had recognized himself as a Himalayan yogi from a previous birth, and now that he sees a few people more, he needs them to positively identify themselves. The book is filled with extravagant recounting of supernatural events. Looks like the author lived in a charmed world, only dimly aware of laws of Physics. The miracles themselves are difficult to take seriously. Extraordinary claims require extraordinary proof, none of which is provided by the book. And they aren't even written in a funny or engaging manner. They're stated with a nonchalance that suggests that this is commonplace. Even if this were a work of fiction I'd pass it by.

The lesson from the book is that you are a yogi if unnatural stuff happens to you. If it doesn't, tough luck. Maybe next birth pal. Wait this life out. Even as a spiritual text the book is abysmal. It has no lesson about how to live a good life, or how to achieve peace. A much more concise and beautiful lesson is the one-liner from Mae West, "You only live once, but if you do it right, once is enough."

I am amused by "Autobiography of a Yogi". It comes highly recommended from seemingly sane people. People swear by it, and say that it changed their life. You have to be very desperate for meaning if this book gives you any. It is like finding deep meaning in "Baa Baa Black Sheep".

Due to the poorly imagined plot, and the haphazard story-line, I didn't read too far. If someone could point out passages that they thought were spiritually enlightening, I'd be happy to revise my opinion.

(In case you are offended by this: Calm down. If this guy is half as divine as he claims, he won't mind it.)

Thursday, December 29, 2011

Book Review: The Golden Gate by Vikram Seth

Want to read a moving novel in sonnets? Grab a copy of "The Golden Gate", by Vikram Seth.

I am prejudiced against "popular" Indian authors. Past experiences have made me critical towards them, especially if their fiction is popular in the West. I don't know what I was thinking when I picked up "The Golden Gate" by Vikram Seth. I had heard of Vikram Seth, but I had not read anything by him.

The Golden Gate is a story about a few friends, and a few years of their life. The book is written entirely in verse, which took me by surprise. It has been a few years since I read verse. I need not have worried, the Golden Gate is a great way to start reading verse again. It is easy to read, and many passages in it are moving, and thought provoking. The book starts out with a person who should be happy at his success, but he feels lonely. It follows his journey, and tells you about his friends, and their lives. Written during the turbulent period of the Vietnam era, it raises interesting questions about life, ambition, following one's dreams, and what it takes to be happy and at peace.

The locations are all from the San Francisco Bay Area: San Francisco, San Jose, Marin county, and introduce the reader to the joy of living in this wonderful part of the world. It makes the reader appreciate the world we live in, and our friends.

I started out skeptical, and ended up enjoying the book immensely. I was sad when it ended, Vikram had done such a fine job of introducing the characters that they felt like friends.

Get the book, and enter the charming world of Vikram Seth's verse.

Image courtesy Wikipedia

Wednesday, December 28, 2011

Book Review: Spice Route

Want to know why bay leaves were once more expensive than saffron? Read "The Spice Route", by John Keay.

The book is a history of the Spice Route, and the general quest of Europeans to find exotic aromas and flavors. It goes through time, showing how the trade route was a long chain of sea and overland routes through the Middle Ages. Various spices were harvested in South East Asia: India, Indonesia, and other countries. These were traded by Arabian traders, who brought them to the Middle East. Here, they were traded to others who took them to the Mediterranean.

The history of this route is fascinating: spices were a big motivation for the European powers to expand East and West. Many campaigns were waged: trying all the routes, trying all ways to gain more access. The merchants from Holland successfully bypassed the Mediterranean, and the Spanish and Portuguese managed to go in different directions: the Spanish went towards the New World, while the Portuguese went East towards Goa and Japan. It is difficult to transport a person to that time, where travel was wretched and dangerous, but John does a fine job in giving the reader a flavor of that world.

The spice route led to a colonization of the world, as the European powers carved up the commercial world among themselves. It is instructive to read this history, and learn about how a new improvement fundamentally changes the field.

This book will shed a light into the colonial era, and will take the reader back through time. It made me look at my spice cabinet very differently, and treat my bay leaves with a lot more respect.

Tuesday, December 27, 2011

Book Review: H20 A Biography

Looking for some good science writing about a common topic? Pick up Philip Ball's, "H20, A Biography".

Philip writes about water from a few different angles. It talks about how water originated in the Universe. The book talks about the origin of the Universe (Big Bang, not 6000 years), and how planets were formed. Then it goes into how Earth might have got its water. We take water for granted, but many planets have little or no water at all. If we believe that intelligent life requires water, we need to understand how water could have come around in the first place. It is a very strange set of circumstances that caused Earth to get water and retain it. Many planets might have had water, but they lost it over time. Astronomers and Physicists have looked at Venus and Mars and tried to reason about how Earth might have had water. The Science behind this is quite beautiful and involved, and it helps that Philip explains it so well.

Next, Philip explains how humans discovered water. This is the most interesting part of the book, the history of Science. Ancients thought that water was an element, and they did not know that ice, water and steam are the same compound. This led to a long set of mistakes and it shaped the thinking of early chemists, leading them astray. I found this section remarkable. How does one realize what water contains? Philip lists all the major Physics and Chemistry milestones that allowed humans to recognize that water is a compound of hydrogen and oxygen. The book also talks of how humans realized that ice, water and steam are the same compound. The mis-steps are very instructive. They lead to a better understanding of how science proceeds and how mistakes are corrected. Early chemists thought that when something is burned, it gives up phlogiston (rather than taking up oxygen). This was an easy mistake, given the instruments of the time. Early chemistry made quick advances when people showed that burning was a process of oxidation, combining chemically with oxygen. (Similar mistakes were made in early experiments with electricity. Electricity was through to flow from the positive lead to the negative lead, when the actual flow is in the opposite direction. Thus, electrons now have a negative charge by convention.)

In the third section, the book talks about the behavior of water in cells, and the anomalous behavior of water under various circumstances: low temperature, high pressure, low pressure, presence of solid bodies. I didn't know much of it, though I confess that I wasn't able to retain much of this information. It was just too dense for me in parts, and I had to skim through. The book ends with drinking water, and explains why the lack of drinking water is one of the biggest problems facing humanity.

The book is a beautiful history of early chemistry and various aspects of water. It does get technical at times explaining in quite some detail how certain things work. It is best to skim such sections and return to them later. A majority of the book is an easy and insightful read.

Monday, December 26, 2011

Book Review: Seeing Voices

Oliver Sacks is a neurologist of some renown. He has written some fascinating books on the working of the mind: the most famous are "The Man Who Mistook His Wife for a Hat", and "Musicophilia". "Seeing Voices" is a short book about the deaf.

When I first picked up the book, I wondered if brain development for deaf people is very different from that of hearing people. Oliver's book lists the various ways in which language development suffers if a deaf person isn't allowed to sign naturally, or if he doesn't have access to other people who sign. I didn't know much about sign language before reading the book. Sign language isn't a simple translation of English or Hindi into gestures. Rather, it has its own idioms, its own grammar. So sign has a notion of poetry and of puns.

Oliver lists various cases of deaf people whose parents did not sign. When these children grew up and finally encountered signing, a world of language opened up to them. This process is beautifully described, and provides valuable insights into the working of the language part of the brain. For example, without language, there was no way to describe time: today versus tomorrow blend in if there is no way to describe them.

There are also examples of a few hearing individuals whose language development has been artificially suppressed. These examples showed how both hearing and non-hearing individuals pick up language in similar manner.

"Seeing Voices" is a great book to read if you have a small child in the house. Children learn sign language faster than spoken language. Spoken language requires a complicated co-ordination of the vocal chords, and these develop rather late in children. Sign language requires arm motion which children acquire rather early. Based on this, many hearing parents teach their children sign language. From an early age, the child learns how to express himself: pain, hunger, boredom, and a need for mommy, daddy, or sibling. This allows for a limited communication between parent and child, removing frustrations on both ends.

After reading this book, I could think of many friends who would love to read this. You don't need to be interested in neuroscience, language development, child development, or the world of the deaf to love this book. This short book makes you aware of language, and gives you much to consider about.

Sunday, December 25, 2011

Book Review: The Man Who Knew Infinity

Srinivas Ramanujan was a brilliant Indian mathematician who made seminal contributions to Number Theory and the theory of continued fractions. Robert Kanigel's book, "The Man Who Knew Infinity" is a biography of Ramanujan.

The book talks about Ramanujan's life in quite some detail. Robert has provided a lot of color by traveling to the places involved, and deeply researching Ramanujan's life. The book has photographs of the places and maps of areas, making it easy to identify with the story. The biography is well balanced and impartial. If this was all the book contained, it would already be a worthy read.

What I found most interesting was the associated commentary on Indian society, values and the education system. Ramanujan was ignored by the Indian education system, largely because he refused to conform to its requirements. Ramanujan showed an early brilliance in Mathematics. But the system didn't care for exceptional performance in one field. Due to the inflexibility of the system, the talent of a brilliant mathematician was wasted. Having suffered through the Indian education system, I found the passages revealing. Even back in Ramanujan's day, the system was inflexible and idiotic. Many people recognized the inflexibility of the system, its arbitrary outcome, and the ill effect on genuine talent.

How much part did Indian society and customs play in Ramanujan's downfall? Ramanujan refused to alter his diet in cold, cloudy England. As a result, he got very little vitamin D, and suffered poor health. This is a problem that persists to this day: Indians who firmly adhere to a restricted diet suffer from problems in countries where an Indian diet is unsuitable. Ramanujan's wife was poorly treated by his mother, and this poor treatment led to misunderstanding and stress in Ramanujan's life. Would the outcome have been different if the Indian arranged marriage was not as stifling?

Ramanujan's life is full of questions. It makes me sad at the outcome. But more importantly, it makes me wonder.

Would a Ramanujan be possible today? Could a lower middle class boy with no talent for anything other than Mathematics be recognized as a genius? Could he even reach a point where he could seek collaboration or patronage from Western mathematicians?

It is a very sad tale, though one I think every Indian student should know. Indian students yearn for exposure to foreign education and worldwide recognition. This is one example of a person who got both, and yet he had a sad and lonely life. The outcome could have been so much different. Ramanujan was as good as Euler or a Gauss, according to people who worked with him. And yet his talent was squandered away. An early death, a lonely life full of struggle. And Ramanujan was lucky. Today, he probably would have no hope of success.

Image courtesy Wikipedia.

Monday, December 19, 2011

Book Review: The Male Brain & The Female Brain

Rather than waste your time with pop-culture books about men and women, how about read books with real science and insight this holiday season?

This is a review of two delightful books called "The Female Brain" and "The Male Brain" written by Louann Brizendine. Dr. Brizendine is a professor at UC San Francisco. Both books talk about the peculiarities of each brain from a scientific viewpoint. The books are scientifically accurate, and are accessible to a layperson with no background in neurology.

The books contain a lot of insight about the behavior of both men and women. For example, The Female Brain talks about how women's brain is highly geared towards social connections. It provides many examples to demonstrate this, and talks about the development of a female brain from a newborn to adult and to a mature brain. Along the way, you see the various changes in the brain. A lot of behavior changes accompany the development of the brain. This book made me understand the motivation behind the baffling behavior of friends and relatives.

The Female Brain was the earlier book. Recently, Dr. Brizendine wrote The Male Brain. The second book is as interesting and as revealing as the first. It talks of the development of the male brain through the years. There are many ways in which the adult male brain is different from the teenage male brain. Reading about the male brain allowed me to better understand how I will change as I grow older. It also allowed me to understand male toddlers behavior.

Both books are a quick read, they make cutting-edge scientific research accessible to everyone. This is scientific writing at its best.

Wednesday, October 12, 2011

Book Review: Kashmir, The Wounded Valley

Many years ago, Neha and I were walking around Santa Rosa in Northern California. We came across a used bookstore where the owner was listening to some good blues, and improvising with a harmonica. It was a perfect setting. We entered the store and found some very interesting books. Some of these books have been impossible to find elsewhere.

Today's review is about one book from that store. The book is "Kashmir, The Wounded Valley", by Ajit Bhattacharjea.

The book is a condensed history of the Kashmir valley. It starts around 700AD, when authoritative written record begins, and continues on till 1992. It is an insightful read. Written in an easy style, Ajit takes the reader through time in Kashmir's history. Entirely factual, and entirely gripping, the book has the pull of a crime thriller rather than the yawn of history books. It is even more chilling when you realize that all the events occurred. And when you turn to the evening news, you realize that we are still living with the repercussions.

Two things struck me when reading the book.
  1. The valley has had mismanagement and cruel monarchs. Few rulers have cared for the well-being of their people. Dynasties follow a predictable pattern. The first monarch in a dynasty is an aggressive conqueror, or a cunning statesman. The second is a benevolent and able leader. Successive leaders are selfish, greedy, and insensitive to the needs of the people. Few monarchs have focused on improving education or living conditions. As a result, the people have suffered greatly and their lot has worsened over the years. Even before the British Raj, the condition of the people was miserable.
  2. The people are surprisingly resilient, and a little gullible. Kashmiris have a liberal outlook towards life. Most Kashmiris converted from Hinduism and Buddhism to Islam willingly. The benevolent influence of liberal sufis struck a chord among the liberal Kashmiris. Culture played a bigger role than religion in their life. All through the turmoil of Mughal India, Kashmir had peaceful co-existence among Hindus and Muslims. The gullibility of the people leads to their willingness to be ruled and oppressed. And it shows in the lack of popular uprising to demand better rights and better living conditions. They supported outsiders to overthrow the existing monarch, only to find the new boss was same as the old boss. Even when democracy was at hand, they did not demand their rights aggressively. As a result, leaders turned into tyrants, ignoring the needs of the people and holding Kashmir back from fully awakening.
The book also explains how Kashmir became a contentious issue between India and Pakistan, and the mistakes that led us there. The book is impartial: it points out mistakes on all sides. The ruler of Kashmir, Hari Singh, refused to make a concrete decision on joining either side. He had hopes of staying independent despite the impossibility of such an arrangement for Kashmir. Rulers of India and Pakistan refused to give the Kashmir issue the importance it deserved. And as we have seen countless times before, administrative incompetence and widespread corruption made a bad situation worse. Heavy-handed actions by the central government, corruption by well-known people, and a lack of respect for the people's needs alienated an entire state. The people started out amicable to India, eager to strengthen ties with the land of Nehru and Gandhi. Over years, all good will was squandered, culminating in the excesses of the armed forces. Warm friends turned into bitter enemies.

A small bruise festered into an open sore, leading to much pain and hardship for both countries for over fifty years. Over time, that pain has spread through both countries. It has led to war and terrorism.

This book should be a required reading for every Indian and Pakistani student. It has valuable lessons for future statesmen. Due to mortal follies, a place with heavenly beauty has been turned into a dark hell. Having deteriorated so far, it is debatable if the situation will ever improve. If a workable remedy exists, knowing how we got into this mess is the first step towards it.

Monday, October 10, 2011

The State of Free Software Today: What We Need to Do

Richard Stallman (popularly called RMS) wrote an eulogy to Steve Jobs recently. It was widely reported due to its angry tone. In summary, RMS was happy that Steve Jobs is dead because Jobs personifies the "walled garden" approach to computing. In the Free Software camp, nothing is more abhorrent than the walled garden approach. Free software is all about giving people a lot of freedom: including the full source code of the software, and the freedom to modify it and copy it. The walled garden approach is all about tight control: not just of the source code but also the development and the overall experience.

It is easy for me to pick sides: I have been a free software enthusiast since 1995, when my friend and I chanced upon something called Linux. I spent many hours playing with Linux, and have used it as my primary system for well over a decade. In addition to Linux, I spend all my time in free application software: Firefox, Chrome, Blender, GIMP, R, Eclipse, Emacs, Arduino, ... The list goes on.

I abhor the walled garden approach. It is damaging to my livelihood as a software engineer if I cannot learn from a system or modify it. I enjoy programming and have tinkered with most systems I own. My tinkering was lousy and inconsequential, but I enjoyed it. And I learned a lot by poking inside them.

It is easy to identify with RMS' criticism of Steve Jobs. It is easy to rally people when you demonize someone. But free software doesn't need this kind of rallying.

I think RMS is wrong.

The Lure of Free Software
Free software is compelling. Many programs like Blender, Eclipse, Emacs are the best in their class. They are exceptional tools. I write better in Emacs than any other editor. Emacs is an extension of my mind: it is a supremely capable editor. My other editor, vim, is equally good. It is beautiful to watch an expert use emacs or vi: the software makes them insanely productive. Free software has done exceptionally well in technical areas like Statistics (R), 3D modelling (Blender), web serving (Apache), microcontroller hackery (Arduino), development tools (Eclipse, GCC, emacs/vi, ..) In these areas, non-free tools are at a significant disadvantage. Who would bother using some souped-up microcontroller tool when Arduino works so beautifully? Who would bother with a strange new Statistical language, now that you can download, install and use R right away? And if you have a newbie question with R, the wizard Peter Dalgaard himself replies to questions! That is how awesome the community is. The software is great, the community is knowledgeable and helpful, and you can read the source code to learn more. It doesn't get better than that.

With software like that, you don't need to yell about the virtues of free software and the evils of the walled garden. The software stands on its own merit. And it continues to attract people like me who care about the freedom.

The free software world has produced some exceptional non-technical software: Firefox and Chrome as browsers, VLC as a media player. But in the non-technical area, free software has lagged behind. Ubuntu is making a gorgeous operating system that my mother can use. But companies like Canonical are the exception rather than the norm.

The Lure of the Walled Garden
In recent past, Apple has gained a large following. Their products like the iPhone and the iPad are phenomenally popular. People are buying these products because of the superior user interface, and the ease of use. The user interface really is the killer feature. Free software advocates might dislike the walled garden approach, but we cannot deny that Apple devotes extraordinary resources to getting an excellent user interface. The first bite of the Apple is bewitchingly delicious. The interface sells.

For a long time, the free software world has been blind to the needs of the "common" user. We have aimed at the technical audience, and have ignored the needs of the vast majority of computer users: our parents, our grandparents, our friends. We justified this stance because there were limited developers, and we needed to get our own house in order. We needed development tools, drivers for technical products, technical software. Developers were more interested in scratching their own itches, writing programs that satisfied their needs. Nobody can be blamed for the situation we are in. Every free software developer has been working hard, quite often without pay. Asking a Statistics programmer to write a cute game would be a disaster.

In the meanwhile, a walled garden has appeared with exceptional usability, even if it has a limited set of features.

It is a testimony to good usability that people are willing to enter the walled garden. Many people don't care about the freedom. People who do care continue to enter the walled garden despite their loss of freedom. It is a question of convenience, and people choose their battles carefully.

The way forward
We have many glimmers of hope. Look at Firefox or Chrome. Both are free software, and both wrested browser market share from Internet Explorer (IE). IE was never an explicit walled garden, but it did curtail freedoms: it ran on specific platforms, it fragmented the web with its own extensions. For a long time, the lack of a good browser was the chief hurdle for free software users. We couldn't use IE, and nothing else was good enough. Navigator was barely usable.

Then came Firefox: it was a compelling browser. Firefox was fast, it ran on every system, it was more secure. Most importantly, it was beautiful. Compared to Netscape Navigator and IE: man, was it gorgeous! Put the navigation buttons and URL bar along the menu bar, and you had one pretty interface. Chrome went one step ahead: it is an exceptional browser. It is fast, it is pretty, it is secure. Oh, and the source is available. People who don't care about freedom of software use it because it is good. And people who care about freedom can download its source code.

Something similar is happening with Android now. Android's source code is available. But that's not why millions flock to buy the Nexus-S or the Droid. They want the system because it is excellent. With Gingerbread, the Android system looks beautiful. With the next release, I'm sure it will look even better. Now that it is comparable on features, it is polishing its usability.

That is the way forward: to make exceptional software. Make the best damn system in the world. Make it gorgeous, fast, cheap, reliable, rugged, stable. And then release the source code. Find opportunities like the Mozilla Foundation did with Firefox. Like Canonical did with Ubuntu. Or like Google did with Chrome, Chrome OS and Android.

Users are being charmed by the the usability of the software that Steve Jobs provided. Provide the same usability with the freedom. The walled garden will disappear by itself.

Sunday, October 09, 2011

Indian Universities are no longer world class

A recent survey carried out by the Times Higher Education magazine ranked all the universities in the world. Sadly, Indian universities don't figure too highly. The best Indian university in that list is IIT Bombay. Its ranking is 301-350: no specific rank is given. One thing is for sure, IIT Bombay is worse than many small universities few people have heard about, like places in Turkey and Brazil.

Many years ago, I had written about how the IITs are not world class. When you venture out of India, you realize how bad they are. That article got a lot of pageviews, and lots of comments. Many IIT students wrote to say how deluded I was, in their own special way. Eat humble pie, my friends.

The Times Higher Education survey is quite comprehensive. You can look at the methodology details here. In short, they asked industry and education leaders for their views of universities. Universities were graded along a variety of factors: the number of citations for their papers, the amount of research funding, the impact of its research on industry, the teaching environment. The study looks solid: all income has been adjusted for purchasing power parity, so poor countries are not penalized for their lower cost of living. Also, there is a normalization across disciplines, so sciences where overall publication frequency is lower are not penalized for having lesser research output. The weight for research output is 30%, which is perhaps too low. They have an iPhone application which allows you to change the weights and see the impact on the ranking.

I don't have access to their raw data, but from reading about the study, their method looks sound. Ranking an institution is a difficult task, and this study is probably the most precise answer we have right now. A better study could look at the impact of graduates from the universities, or the difference between the capability of students with and without the education: thereby measuring the impact of the institution itself. Alas, such controlled studies are difficult to come by.

So where does this leave us? This leaves us in the sorry state of admitting that Indian educational institutions are really not that good. The Chinese are far ahead of us. The prestigious Tsinghua University in China is at position 71, ranking near well known universities Rice and Vanderbilt, both in the US. Among Asian countries, Japan, Hong Kong, and Singapore all fare very well. In fact, the top Asian universities are dominated by Japan, Hong Kong, China, Singapore, and Korea, and now the reasons are somewhat clearer. . These countries have made investments in education, and the results speak for themselves. Even between Brazil, Russia, and China, India spends the least amount of money on education, per student.

Admitting that our educational institutions have fallen behind is the first step. Only after we admit the problem can we find ways to rectify the situation.

Saturday, October 08, 2011

Learning a new software: Blender

Blender is a 3D design software. It is a complete tool, allowing you to create entire worlds in 3D, modelling, meshing, applying texture, and animating the world.

Here is an example of what Blender can do. This movie was created completely in Blender.

Isn't that impressive? What is more impressive is that Blender is available for free, you can download it and install it right now (Windows/Mac/Linux). And if you want to make your own movie based on the characters behind Sintel, you can download all the art and characters at the Sintel website. This level of sharing is refreshing: it allows new graphics designers to learn a complex tool, and be able to look behind the scenes of a very complex project.

Blender has a beautiful UI, though it looks very complicated initially. When you load it up the first time, the number of buttons and controls is overwhelming. This is what the default Blender 2.5 UI looks like. There is a small cube placed at the center of the viewport, and many properties and control widgets are open. Once you learn a few Blender principles, you find that the UI is clearly arranged, and every control is in the perfect place. As with any good tool, the tool itself vanishes pretty quickly. Within hours of learning Blender, you focus on the 3D modeling activity rather than the Blender tool.

Learning Blender
I tried learning how to use Blender using a book. It is hard to explain the various UI elements using written words. It is even harder to describe the 3D world you are creating using just words. The best book I have found till now is called Blender for Dummies. It requires no previous knowledge, and is an excellent resource to begin modelling simple meshes in Blender. It covers Blender 2.5.

In case you want to start while your book is on its way, here are a list of online resources. I list Blender 2.5 resources only. The Blender interface changed a lot between 2.4 and 2.5. If you are starting out, it is best to start with 2.5 directly.
  1. Getting Started with Blender: This is a set of seven video tutorials. They are short and cover the very basics of using Blender. You can watch all seven within an hour and you will have a great idea of the Blender UI and basic features.
  2. Neal Hirsig's blender learning course: More than a collection of isolated tutorials, Neal has created a comprehensive learning course. You can download all videos to your machine and follow along with Neal as he patiently explains every feature of Blender. Neal's explanatory style is impeccable: it is a pleasure to follow along and learn. You can downloads all these videos from
  3. A collection of tutorials at Blenderlinks: Finally, when you have mastered the UI, you can focus on a specific topic by looking through the links at Blenderlinks. The videos are created by many contributors, so the videos differ in sound quality and expository style. But if you want to learn a single topic, the videos at cgcookie linked from Blenderlinks often provide the perfect recipe.
Blender is a powerful software, and it compares well against software which costs thousands of dollars. With a little bit of effort, you can learn how to create an entire 3D world inside your computer. It is scriptable in Python, which allows you to use Blender 3D models from an automated system. Give Blender a try, and see what you think! 

Friday, October 07, 2011

Book Review: A Contract With God

I came across a copy of Will Eisner's, "A Contract With God" Trilogy at my library. This includes three books, "A Contract With God", "A Life Force", and "Dropsie Avenue". I had heard vaguely of this book, so I picked it up. I was expecting a good graphic novel.

I wasn't expecting a book of this lyrical depth and insight into human behavior.

These graphic novels are about life in a poor New York neighborhood starting near the Depression and going to more recent times.  It describes the life of poor migrants to America; their dreams, their lives, and how they escape the squalor of their world.

I am struck by the depth and force of some of the stories. A few stories in the first book were very hard-hitting and thought provoking. I stopped reading after a few stories because there was so much to mull about. It is a rare book which has this level of insight coupled with such beautiful art.

These books are not for children, though they may be okay for teenagers. They have plentiful sex and nudity, putting the "graphic" back in "graphic novel". Use your own discretion if you have children or teenagers around or are squeamish about sex.

The book can be ordered from online stores. Amazon has a hardbound copy for $20 which is a steal.

When Boston Brahmins were hoping that American classical would improve upon European Continental music, Jazz took the world by surprise. In much the same way, Will Eisner's graphic novel is the epitome of American graphic art. Completely out of the blue, it defined the graphic novel genre and gave readers and future novelists something original and beautiful.

Thursday, October 06, 2011

Google Plus versus online news sites (like Reddit)

Google Plus is a new social networking site. If you haven't heard about it, it is similar to other social networking sites like Orkut or Facebook.

There has been a lot of discussion about how Google Plus resembles Facebook. Indeed, you can search the Internet for the two names and there are countless comparisons. I think this misses a broader point.

Once I started using Google Plus, I haven't checked Reddit.

In case you don't know, reddit is a popular news website. Reddit started out as a small startup with mostly programming topics. The stories were interesting and it had an impressive signal to noise ratio due to the self-selected audience. For a while, reddit was even better than Slashdot, another popular geek news website.

Over time, reddit descended into cheeky cuteness, pictures, and pointless articles. At some point, I stopped reading the reddit main page, and started reading the programming subreddit. Even that is becoming irrelevant. At around this time, Google Plus came in.

Social Networking connects me to virtual cows
I'm very skeptical about social networking. Very, very skeptical. Social networking puts me in touch with a wide number of acquaintances, where the level of interaction is shallow at best. I dislike shallow interactions, and so I avoid checking social networking sites. I check these sites roughly once every quarter. The quality of posts is between pointless and annoying. Things really went down the toilet when Facebook allowed games to post updates. Suddenly, you could see how some long-forgotten acquaintance of yours had just obtained a virtual cow in some online farm simulation. Do you want to make a farm next to his? No thanks. If I had hours to waste and wanted to simulate reality, I would duel in the sky.

So the previous social networking stuff was a total chore. I would check it infrequently, and post self-promotional content: links to my articles when I was happy with one.

It took a lot of prodding from my friends to try out Google Plus. You know who you are: thanks!

Google Plus connects me to Peter Norvig
When Google Plus came in, it allowed me to subscribe to people who weren't friends: Tim O'Reilly, or Peter Norvig. These people have insightful articles to post or reshare. They are interesting to me, and are relevant to my area of work. Suddenly, Google Plus had much higher value content than Reddit. It was the Reddit front page, all over again. Except this time, it wasn't fluffykitten13, but Peter Norvig himself. Woohoo! The articles were interesting, topical, timely.

I doubt Google Plus would descend into the cutesy pointlessness of Reddit anytime soon. I doubt Peter Norvig would switch completely to posting lolcats. Actually, knowing Peter, he probably would post lolcats to prove someone wrong.

And that's the beauty of Google Plus, the articles are linked to the poster's reputation. I wouldn't post lolcat pictures: partly because I have better things to do, and partly because my friends would think me crazy if I did. People are free to engage in their interest of choice: it is like giving everyone their own reddit-like site. Unlike blogs, the content is usually smaller. You can subscribe to many people, and find related content very easily. Like blogs, it is personally generated content. The content is frequently updated, topical, personal, and recent. The site layout is great and no page is missing.

Google Plus isn't perfect. I still read Slashdot due to reasonable editorial filter and good comment moderation. Google Plus comments on highly visible posts are quite useless and spammy. But overall, Google Plus has given me a high signal news source.

Wednesday, October 05, 2011

The complicated Indian negotiation ritual: why decisions take much longer

In traditional Indian conversation, you are never supposed to express willingness or displeasure in a straightforward manner. So when you are having lunch with extended family, you are not supposed to clearly express that the daal was excellent and you would like some more. A similar dance is played out when arriving at agreement of any sort. The standard Indian conversation hovers on generalities with no preference clearly outlined. Most conversations are elaborate dances: both sides express a little more of their preference without clearly saying what they would like. Here is an example of a couple called Harry and Sally at a restaurant:

Harry: What would you like to eat?
Sally: Anything is good.
(At this point, you would think that they're in agreement. Well, you are wrong. Listen to the remaining conversation.)

Harry: Would fish curry be good?
Sally: Yeah, that's ok. Does this restaurant make good lamb curry, I wonder?

Harry: I guess, I haven't tried it. But I hear everything here is good. Would you like lamb?
Sally: Lamb is good too, I haven't had it for a while now.

Harry: Would you like to try their lamb curry?
Sally: That sounds good. What else is on their menu?

Harry: They have a lot of things. Their vegetable selection is also great. The pav bhaji looks enticing.
Sally: Yeah, pav bhaji sounds interesting. What other vegetables do they have?

Harry: Their cauliflower curry gets good reviews. I tried it once. It is much like the fish curry, but the fish curry is clearly better. Weren't you craving fish yesterday?
Sally: Yeah, but I wonder if their preparation of fish is good. You can get that if it is their best dish.

Harry: No, fish isn't their best dish, they make everything well. The cauliflower is also cooked perfectly.
Sally: I see. Both sound great. You know, just the other day I was telling my mom that it is difficult to make good okra, unless it is fresh. Did you know that?

Harry: No, I didn't know that. But that makes sense. Okra goes limp when it is old.
Sally: I wonder how this restaurant makes okra.

Harry: I wonder too, I haven't tried it. Would you like to try it?
Sally: Oh no, I didn't mean to imply that. Fish curry sounds great.

Harry: But now I'm interested in the okra, let's order some this time.
Sally: Ok.

All along, Sally didn't make her preference known, and neither did Harry. This elaborate ritual is even worse as the number of people increases. Going for lunch with one person is hard, lunch in a group of ten means that deciding the order can take a long time. I hope you had a snack before starting.

There's no guarantee that this ritual produces the correct outcome. Among young people and close friends, this ritual has given way to specifying direct preference. And I feel a sense of relief when I can clearly list my preferences without having the song and dance. In cases when the song and dance is involved, the outcome is usually unfortunate. And when you cannot express your preference, you certainly cannot express your displeasure at the outcome.

As the number of people increases, the outcome becomes more and more incorrect. Perhaps this is good, because it gives plentiful fodder for the gossip mill later on. "Jason was telling me that dining with Sally was such a chore, and how she was completely insensitive to Mark's needs."

This ritual is carried out not just for meals, but for many other decisions: what people want to do, where they want to go, whom they want to marry...

You notice some things only when you stop doing them. The complicated Indian negotiation ritual is one of these. Among friends and close family, I love the ability to say clearly what I want. Decisions happen quicker, and we can discuss things of consequence in the time saved.

 (Fun fact: If Neo and Morpheus were Indian, the red pill, blue pill decision would have lasted the entire movie.)

Saturday, October 01, 2011

What to do when friends are expecting: A guide for friends of new parents

Your friends are having babies, and you haven't got any yourself. There are lots of guides for new parents, but few guides explaining what the friends of new parents can do. This guide will help you learn about the process, and help you deal with the change.

  1. Your friends still love you the same. You would take care of a throbbing headache rather than being with friends. Having a child is similar. They need to take care of the child before being with you.
  2. Your friends' priorities will change. It might have been you, now it will be the baby. Don't be hurt by this. You will feel the same when you have children.
  3. Your friends will have zero time initially. Bringing up a baby takes a lot of time and effort from both parents.
  4. Your friends still need social contact. They enjoy social visits, but the flavor changes considerably. Mothers might have less time to meet, but they continue enjoying telephone conversations and short visits.

During Pregnancy
  1. Mom will be tired constantly. Pregnant mothers go through many physical changes. They feel tired constantly from carrying the extra weight. Be mindful of this. Moms also have to use the restroom very frequently. Don't engage your friend for any long activity. Even long conversations are enough to tire mothers. If the mother looks or sounds uncomfortable, be prepared to give her some time off. If you are on a telephone, ask her if she needs to hang up.
  2. Moms go through psychological changes. Many women go through mood swings. All this means that women's behavior changes considerably. Their behavior towards you will change too. Don't take it personally.
  3. Less time. Your friends don't have a child yet. But preparing for a child is also time consuming. There are things to buy, doctors to visit, and a house to prepare. Limit your demands on their time.
  4. Less patience. During pregnancy, your friends are going through a lot of mental and physical strain. Their levels of patience are very low. Pranks and tomfoolery are a terrible idea.
  5. Positive conversations. Pregnancy is a scary time for parents already. Do your part by keeping all conversations positive. If you have heard of a scary pregnancy or delivery: keep the story to yourself! If there is a cautionary tale, give the moral precisely while keep gory details to yourself. Try to make all emotional stories positive. Negative emotional stories trouble the mother and make her very nervous. Dads can handle emotional stories better during this time. If you need to tell an un-nerving story, tell it to the dad, out of earshot of the mom.
  6. Steer clear of stomach size conversations. Moms are worried about their health, their appearance, their progress. Our friends suggest the phrase, "You are carrying very well" rather than the variants that raise questions about the size or the rate of development of the baby. Definitely stay away from comments like "Are you sure you're not carrying twins?", or "Are you sure you're in your seventh month, you look small".  There's nothing to be gained from comparing the size of one mother's belly with other mothers. If you are concerned about the baby's development, tell the father the specific concern and your suggestions. Let the dad discuss it with the OB or the mom.
  7. You might want to ask the parents before gifting them anything for the baby. Or give them something that can be returned or exchanged. New parents get too many gifts. Make your gift an asset, not a burden.
  8. How you can help: try to visit your friends at their place. Call before starting to confirm it is a good time and don't be upset by a last-minute change of plans. Carry some home-prepared meal that the mother enjoys. Be prepared to leave quickly.
During Labor
  1. The steps in labor are: contractions or water breaking (either can happen first), active labor (pushing) and delivery.
  2. Labor takes time. Mothers may spend twelve to fourteen hours in labor, but it could easily be twice as much. First-time mothers usually take more time during delivery.
  3. Active labor itself can take a few hours and is the most strenuous activity the mother will ever do in her life. Active labor is like doing twice your capacity of push-ups every five minutes.
  4. Labor takes effort. A lot of effort. Don't expect parents to do anything during this time, even take your phone calls. In addition, don't expect them to do anything for at least four days after (for the father) and two weeks after (for the mother) as the fatigue of delivery takes time to wear off.
  5. If the child was delivered through a C-section, double all recovery times. C-sections take a long time to heal, and the dad is fatigued from his supporting role during this time.
  6. The only priority is the child. Parents are completely focused on delivering the baby. They have no time, let alone patience, for anything else. This is no time for telephone conversations, or status updates. Be patient. You'll know when labor is done.
  7. How you can help: spread the word, and ask others to hold off their calls to the parents.
After Delivery
  1. Most hospitals keep mom and baby in a different room for a day or two. This is a critical time for mom to recover and learn about baby care from the nurses. Visiting mom and dad in the hospital room is usually a bad idea. There is no space in the hospital room, and mom and dad are constantly busy. In addition, the mom is weak and worn.
  2. Mom has never been this tired. Labor drains the mother completely. Let her rest! You can speak with her later.
  3. There's a lot to do. Post delivery, the child and mother have to be taken care of. There's food, bowel movements, health checks, doctor visits, ...
  4. Limit infections. Just after delivery, both mom and baby are susceptible to infections. They will limit the number of people they meet. Give them a few weeks before visiting. if you have had a contagious disease like the flu, wait for many weeks of good health before visiting them. Be honest if you have any illness that can be spread.
  5. Zero time. Mom and dad are focused on the child. They sleep when the child sleeps, and when the child is awake, they are completely busy with the child. If you do want to visit, be ready to help with the house during the visit. Your friend might ask you to help with the laundry or with the cooking or cleaning.
  6. Prepare to hang up the phone anytime. When baby needs parents, they need to go right away. While talking with a new parent, I was once in the middle of a sentence, and my friend blurted, "Baby crying, got to go" and hung up. At the moment I thought it strange. Later I realized that it was an excellent idea rather than squirming on the phone.
  7. Zero patience. New parents have absolutely no patience. Be as frank and honest about things. Luckily both mom and dad are happy with brutal honesty at this time. (Just not about mom's appearance.)
  8. How you can help: congratulate the parents on email, and plan social visits at their convenience, not yours. If you stay close, consider helping them with laundry, grocery shopping, dishes, cleaning and cooking.

Hopefully these steps have enlightened you on the new world of your friends. Your friends might seem self-centered, and eager for your help. But this is the time they need to focus on themselves, and they need your support.

Rest assured that they will allow you to prioritize correctly, and provide the same care for you when you have children.

Friday, September 16, 2011

Linux udev: two-factor USB token turned into a car ignition key

We use two-factor authentication at work. The one-time token generator looks a lot like an automobile key. It is easy to modify Linux to make a silly car noise when the token generator is plugged in. It turns your lame one-time token into a cool ignition key for your laptop.

Here is a video that shows what it looks like:

To do this for your device, create a shell script to make the sound of a car. I have created an audio file called car.mp3 containing the car sound. The shell script plays this file with mplayer. Record the sound of your car, or grab a file from the internet.
$ cat /root/
# Play a silly car starting sound
mplayer /root/car.mp3 &
Now you need obtain the vendor ID. Disconnect your token generator and run this command:
$ udevadm monitor
Now insert your device. You will see an output like this:
KERNEL[] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1 (usb)
KERNEL[] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0 (usb)
KERNEL[] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/0003:1050:0010.0015 (hid)
UDEV  [] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1 (usb)
UDEV  [] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0 (usb)
UDEV  [] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/0003:1050:0010.0015 (hid)
KERNEL[] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/input/input33 (input)
UDEV  [] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/0003:1050:0010.0015/hidraw/hidraw0 (hidraw)
KERNEL[] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/input/input33/event13 (input)
KERNEL[] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/0003:1050:0010.0015/hidraw/hidraw0 (hidraw)
UDEV  [] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/input/input33 (input)
UDEV  [] add  /devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/input/input33/event13 (input)
The part after the 0003: is your vendor ID. Of course, it will be different for you. Now create a udev rule to specify the action. This is what my rule looks like:
$ cat /etc/udev/rules.d/90-token.rules
# Make a silly sound like the starting of a car
SUBSYSTEM=="usb", ATTR{idVendor}=="1050", MODE="0664", GROUP="plugdev", RUN+="/root/"
That's it, you don't need to restart anything. If you have done it correctly, then the car sound will play every time you insert your key.

Tuesday, September 13, 2011

Android ARM Assembly: Calling Assembly from Android (Part 8)

This is part eight in a series on learning ARM assembly on Android. This part covers calling Assembly code from Android applications.

Part 1: Motivation and device set up
Part 2: A walk-through of a simple ARM assembly program
Part 3: Registers, memory, and addressing modes
Part 4: Gnu tools for assembly; GCC and GDB
Part 5: Stack and Functions
Part 6: Arithmetic and Logical Expressions
Part 7: Conditional Execution
=> Part 8: Assembly in Android code

The articles follow in series, each article builds on the previous.

Native Development Kit
You've written some assembly code, made it run real fast, and now you want to include it in an Android application. What's the advantage of running little assembly programs that can't use the full feature set available to Android? You need the Android Native Development Kit.

Download the Native Development Kit from the Android development page. Follow the instructions on that page to install it. Installation is little more than unzipping the file in the correct directory.

Sample Application
My sample application has three parts. The first is the most familiar part: this is the assembly source. This is in an assembly source file called jni/multiple.s. This code computes 10y for a given value y.
	@ This file is jni/multiple.s
	.align	2
	.global	armFunction
	.type	armFunction, %function
	@ Multiply by 10. Input value and return value in r0
	stmfd	sp!, {fp,ip,lr}
	mov	r3, r0, asl #3
	add	r0, r3, r0, asl #1
	ldmfd	sp!, {fp,ip,lr}
	bx	lr
	.size	armFunction, .-armFunction

The assembly code is called from a C stub. The C stub must have a very fixed name: Java_name_of_package_Class_function. This looks downright ugly but is required for Java to look up the correct function. I create a C stub to hold the strange name, and to accept the weird JNI arguments. You don't need to have a C stub, but it makes life easy.

The type jint is a java int that you can treat as a 32 bit int value. Other types are jboolean, jbyte, jchar, jshort, jlong, jfloat, jdouble, and jobject. Notice the signature of the JNI function: it accepts the environment, which is a JNIEnv pointer, and an arbitrary object. Finally, we have the input value, which is an integer. In its implementation, we call our ARM assembly function on the input. The return value is a jint, which indicates that we are returning an integer.
/* This file is jni/hello-jni.c */

#include <jni.h>

/* This stub calls the function. It helps to have a stub like this to
 * save yourself the hassle of defining the function call in
 * Assembly. */
jint Java_com_eggwall_android_assembly_AssemblyActivity_factorialJNI(
	JNIEnv* env, jobject object, jint input) {
	/* Try calling some local code */
	return armFunction(input);
Finally, there is a file that defines the sources in a Makefile. This is the jni/ file. It puts together the stub and the assembly code into a library called "hello-jni".
# This file is jni/

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

# I want ARM, not thumb.

# Name of the local module
LOCAL_MODULE    := hello-jni
# The files that make up the source code
LOCAL_SRC_FILES := hello-jni.c multiple.s


The stub and the assembly input are compiled by calling the following command from the root directory of your project. This is the directory that contains jni/, src/, res/, ..etc. I am assuming the ndk is installed in /usr/local/android-sdk-linux_x86/android-ndk-r6b.
$ ls
AndroidManifest.xml  assets/  bin/  build.xml
gen/  jni/  libs/  obj/  proguard.cfg  res/  src/
$ /usr/local/android-sdk-linux_x86/android-ndk-r6b/ndk-build
Compile arm    : hello-jni <= multiple.s
SharedLibrary  :
Install        : => libs/armeabi/

Finally, there is a Java source code to create the Activity in Android. This code creates an Android application. It extends Activity, and overrides the onCreate method. In this, it creates a TextView, which is a label, and then sets the contents of the label to the return value of the function. It defines a function called factorialJNI which accepts an integer input and returns an integer. It is marked as native, indicating that its implementation is not in Java.

Finally, a static initialisation loads the jni library that was defined in the XML file.
import android.widget.TextView; 
import android.os.Bundle; 
public class AssemblyActivity extends Activity { 
	public void onCreate(Bundle savedInstanceState) { 
		// Create a new Textview 
		TextView  tv = new TextView(this); 
		// Print the multiple of 13 through assembly. 
		tv.setText("The multiple was: " + factorialJNI(13)); 
	 * Multiply the number by 10.
	 * @param input, the number to be multiplied
	 * @return the multiple of the number
	public native int factorialJNI(int input); 
	/* This is used to load the 'hello-jni' library on application
	 * startup. The library has already been unpacked into
	 * /data/data/ at
	 * installation time by the package manager.
	static { 

That is a lot of code to run a single assembly function! But now that you've seen the overall structure, you can begin modifying it to run your own assembly code. This is not a good way to experiment with assembly programming, though. Assembly programs can be hard to debug and it helps to have good tools during development. I would recommend developing using emacs, gcc, gdb, and other GNU tools, just as before. When the code is working correctly, hook it into Android Java source. The Android NDK has some useful debugging facilities, but I would consider them options of last resort.
You can download the entire ARM Android assembly example as an Eclipse project here.

Speed versus Complexity
Just because you are calling assembly code does not automatically make your program faster. The Dalvik virtual machine runs most code fairly fast, and the effort to develop assembly code is not worth the minor improvement in code execution speed. Here are some reasons why you might want to use native code.
  1. Legacy code. You have a lot of existing code that you want to plug into Android.
  2. Optimised code. You have CPU-intensive code that has been carefully optimised.
Assembly code takes a longer to develop, more effort to maintain, and is difficult to port. There are few people who can read assembly code, so your code will be out of reach of many programmers.

A careful consideration of implementation speed and code complexity will lead you to the correct balance.
Things to try out
  1. Create a new function called factorial(int a) and its corresponding stub. Call it from Java.
  2. Create a new source file called factorial.s, and put the function in there. Modify jni/ to run it correctly.
  3. Try adding an input area, where a number is entered. Pass this number to the assembly source code, and print out its factorial.
My example builds upon the hello-jni example that ships with the NDK. You can read the other NDK samples for inspiration. You can learn more by reading the JNI reference. It covers the data types, and the various functions available from native code.

Monday, September 12, 2011

Android ARM Assembly: Conditional execution (Part 7)

This is part seven in a series on learning ARM assembly on Android. This part covers conditional execution.

Part 1: Motivation and device set up
Part 2: A walk-through of a simple ARM assembly program
Part 3: Registers, memory, and addressing modes
Part 4: Gnu tools for assembly; GCC and GDB
Part 5: Stack and Functions
Part 6: Arithmetic and Logical Expressions
=> Part 7: Conditional Execution
Part 8: Assembly in Android code

The articles follow in series, each article builds on the previous.

Compare and Jump
The previous lessons touched on a variety of microprocessor instructions. Let's move to conditional execution. These are the basis of all the if, while, for loops in programming languages.

Conditions are implemented in microprocessors using the Status Register CPSR. The various bits on the status register are set using instructions. Then, you can jump to a different position in the code if the specific bit is set or unset. While this scheme looks primitive, it forms the basis of all conditional execution in every language on computers. Let's see the status register again.

The conditions in the status register are:
  1. Negative: the result was negative (bit 31 of the result was set)
  2. Zero: the result was zero (all bits of the result were unset)
  3. Carry: integer addition, subtraction or shifts produce a carry or borrow (result bits could be anything)
  4. oVerflow: there was carry or borrow during signed addition or subtraction (result bits could be anything)
The status register has other bits relating to the different processor modes, but we can ignore them for now. The bits in the CPSR are set on four instructions: CMP, CMN, TEQ, TST. Let's see these instructions.

CMP Rn, shifter_operand

This performs Rn - shifter_operand, throws away the result and updates the status register. So doing a CMP R2, R2 would produce a zero output, and the Zero condition would be set. It is important to note that the result is never stored anywhere. From your perspective, you are asking the processor what the result would be like.

This is a full list of the comparison instructions.

InstructionRough IntentionThe condition flags are set based on this value
CMP Rn, shifter_operand(Addition)Rn - shifter_operand
CMN Rn, shifter_operand(Negation)Rn + shifter_operand
TST Rn, shifter_operand(Test)Rn & shifter_operand
TEQ Rn, shifter_operand(Test Equivalence)Rn ^ shifter_operand

Once the condition is set, you can use that condition to jump to a specific label. The easiest jump instruction doesn't care for flags. We saw it in part 5, where it was used to return execution back from our function.
        bx      lr
This instruction branched execution to the address contained in the Link Register. This could be done with any register, though you better be sure that the register contains a valid address. It also needs to be a 32-bit aligned address. All ARM instructions are 32-bit (word) aligned. This isn't Intel, every instruction is exactly 32 bits long. BX is a Branch, while BL is a Branch and Link.

Branch just jumps to the address specified in the register.

Branch and Link stores the address of the existing Program Counter in the Link Register, in case you want to jump back. This is something you do all the time, so there is one instruction to handle it.

ARM and Thumb Instructions
Most new ARM processors support two different instruction sets: ARM and Thumb. Thumb instructions are a subset of ARM instructions and are 16 bits each. Going back and forth between ARM and Thumb is possible, though it should be done correctly. The branch instructions ending in "X" do this. A "BX" instruction allows you to switch between ARM and Thumb while the "B" instruction doesn't. In general, you should use "BX" and "BLX" when you are unsure of the resulting code. If you are certain, you can use "B" for branch, and "BL" for branch and link.

Condition Codes
How about adding the actual condition? The condition goes as a suffix to the instruction. Say you only want to check if the Zero condition was true. Then, the condition is called EQual, with mnemonic EQ. So this operation only branches when the zero condition is set:

The full list of conditions is:
SuffixMeaningCondition tested
EQEqualZ == 1
NENot EqualZ == 0
CS or HSUnsigned Higher or Same (Carry Set)C == 1
CC or LOUnsigned Lower (Carry Clear)C == 0
MIMInusN == 1
PLPLus or ZeroN == 0
VSOverflow (V Set)V == 1
VCNo Overflow (V Clear)V == 0
HIUnsigned HigherC == 1 and Z == 0
LSUnsigned Lower or SameC == 0 or Z == 1
GESigned Greater Than or EqualN == V
LTSigned Lesser Than N != V
GTSigned Greater Than Z == 0 and N == V
LESigned Less Than or Equal ToZ==1 or N != V

So you could have an instruction BXNE r3, which will branch to r3, only if the previous comparison did not set the Zero condition code.

Conditional Execution
Let's look at a single if-then loop to see how to run a simple conditional in Assembly.
 1         .section        .data
 2         .align 2
 3 higher:
 4         .asciz "Yes, r2 is higher than or equal to r1\n"
 5 lower:
 6         .asciz "No, r2 is lower than r1\n"
 8         .text
 9         .align  2
10         .global main
11         .type   main, %function
12 main:
13         stmfd   sp!, {fp, lr}
15         @ Load some values
16         mov     r1, #32
17         mov     r2, #33
19         @ Check if r2 is lower than r1
20         cmp     r2, r1
22         @ If it is greater or equal, jump ahead
23         bge     greaterOrEqual
25         @ Otherwise it was lower
26         ldr     r0, =lower
27         @ Now skip past to the common point again
28         b       common
30 greaterOrEqual:
31         ldr     r0, =higher
33 common:
34         @ Print the message
35         bl      puts
37         @ Return 0
38         mov     r0, #0
39         ldmfd   sp!, {fp, lr}
40         bx      lr

This program loads values in r2 and r3 and then compares the two. If (r2-r1) is greater than or equal to 0, then we skip to the greaterOrEqual label. Since there are only one kind of instructions (ARM instructions), we don't bother with the "X" variant of the branch. At the greaterOrEqual label, we move the string corresponding to the higher message. Otherwise, we move the string corresponding to the lower message into r0. Either case, we want to get to the common code, which prints the message, and returns 0. If we didn't return to the common case, it would continue running instructions linearly, and load r0 with the higher message. Then, we return 0, like every well behaved main() function should.

The Program Counter holds the address that will be executed next. Since it is also r15, you can directly modify it. This is a terrible idea in most cases, so you should have a good reason to do this. In theory, you could implement logic by conditionally moving values into the Program Counter. For example, you could add 8 to the current value in the PC to skip an instruction. In practice, you will not do this because it is prone to errors. Such logic fails when the program is modified, and the address of instructions changes.

Every Instruction is Conditional
Now that you know what conditional execution looks like, you are ready for a real twist. In ARM architectures, every instruction is conditional. So the ADD, SUB, LDR, STR instructions can all have these condition codes appended to them. We could write instructions containing very few BX or BLX calls.

In practice, however, you should use conditional operations only for a single instruction or two. If you find that a lot of operations act on the same condition, you should branch and skip all these operations. This is much more efficient, since ARM processors fetch more than one instruction and act upon parts of it in parallel. If you can start to execute the next few instructions, your code is considerably faster. So as a rule, use conditional operations only for two instructions or fewer. If you have a longer list of conditional operations that depend on the same condition, pull them out in a separate routine.

In addition to every instruction being conditional, every instruction can set the CPSR flags. For this, you need to append the "S" flag (for Status) to every instruction. Here is what a MOV instruction really looks like:
MOV{condition_code}{S} Rd, shifter_operand
This allows you to set the status flag without performing a CMP, saving another instruction. The status flag can be set on most operations you have seen till now. So you can automatically set the condition codes when performing arithmetic operations.

Try your hand at these to see if you understand branches.
  1. What does the MOVGE r0, r2 instruction do?
  2. Rewrite the program to use just one branch rather than two.
  3. Rewrite the program using conditional move to avoid a branch. In this case, a conditional move is perhaps cleaner since the condition only changes one instruction.
  4. Write a function isZero(n) to accept an unsigned number n and to return 1 if n is equal to 0 and n otherwise.
  5. Improve on isZero() to write almostFactorial(n) which returns n*(n-1) if n is not zero, and 1 otherwise.
  6. Try your hand at turning almostFactorial(n) to factorial(n) which returns n! for small values of n. As n gets large, you run out of bits, so you will need to test your function with small inputs.
  7. Write a simple while { .. } loop in C and let GCC compile it to assembly. See if you can make sense of the code. GCC can compile code with optimisations turned on with the "-O2" flag. Try to read the assembly output with and without optimisations. See if you can figure out the optimisation logic.
This marks the end of all the functional pieces of ARM architecture. You should be able to read the disassembly of nearly any ARM program now. Isn't that impressive? Look up unknown instructions in the reference manual. As always, the ARM Reference Manual has more information on all condition codes, conditional execution and instructions that accept conditions and can set the flags.

The final piece will be to integrate this with Android. We will see how to call assembly code from Android to be able to run fast processing directly in assembly.