Sunday, June 7, 2009

Trip to North East India (Day 1)

Hello all. Here's a new post after a looooong break. This is not a technical post, but a chronicle of our trip to beautiful North East India. Last week my wife and I got the opportunity of visiting Kolkata, Darjeeling, Gangtok and Shillong over a period of one week. Here's the timeline of events.

Day 1
7 AM
Leave home for Bangalore International Airport by taxi. I have now traveled a couple of times with CelCabs and have found their service to be very punctual and comfortable. As of this writing, they charge Rs.14 per km for either Indica or Indigo type of vehicle 24 hours of the day. No extra charge for late night or early morning travel. Moreover they have an attractive offer of Rs.11 per km for the return journey if booked 2 hours before the scheduled time of arrival.

12:15 PM
Arrive Netaji Subhas Chandra Bose International Airport, Kolkata. We had a hard time finding an empty luggage trolley, but thankfully were able to find one after a few minutes. There was a booth of West Bengal Tourism inside the airport and we tried to see if they could arrange for a half-day tour of Kolkata city. We did not have much time to spare in Kolkata since our onward train was scheduled to leave in the night. After spending about 30 minutes of fruitless time trying to get a tourist taxi at a reasonable fare, we gave that up and decided to go our own way. Interestingly a Jet Airways employee was seated inside the WB Tourism booth and seemed to have better knowledge of taxi availability than the people manning the booth!

1:00 PM
Hired a regular taxi from the prepaid taxi counter at the airport. The charge was Rs. 220 to Howrah Station. As soon as we exited the airport, the Kolkata heat hit us... hard. It's the kind of humid weather that one comes across in coastal cities like Mumbai or Chennai, the kind that keeps you sweating continuously. The temperature was at a scorching 35 degrees celsius.

The taxis in Kolkata are still old yellow Ambassador cars driven furiously by impatient drivers. The streets are chock-a-block with people reminding one that Kolkata was after all the most densely populated city in India. It was lunch hour and traffic was moving at a crawl near the city center. One sight that people from other cities might not have seen is of the trams. The tracks go right through the middle of the streets and the trams chug their way along slowly, often honking to make their way. Altogether it is an utterly chaotic scene, what with taxis, autos, buses and trams all struggling to get their way and hasty pedestrians interspersing their way in between.

2:30 PM
Arrive Howrah Station. The station is very big with more than 20 railway platforms. We left our luggage in the railway station cloak room. They charge a nominal amount of Rs. 10 per day for every piece of luggage. Note that this service is only available for train passengers; one needs to produce a valid train ticket to avail the facility. Thus freed from the burden, we had lunch at the railway station food court.

3:00 PM
We wanted to visit the Belur Math, made famous by the great spiritual teachers of India, Sri Ramakrishna Paramahamsa and Swami Vivekananda. The Math is around 10 km from Howrah Station and so we thought it would be a good idea to hire a taxi. The moment we stepped out of the station, we were swamped by taxi drivers offering to take us anywhere we wanted. The flip side was that they were quoting anywhere from Rs. 160 to Rs. 200. We finally convinced a taxi driver to take us by meter. There are also regular bus services from Howrah Station to Belur Math. We had to pass through Howrah en route to the Math. The squalor and filth in which the people live there is really appalling. It is no surprise that the great seers like Swami Vivekananda had their roots in Howrah and took up the work of assuaging the wretchedness of mankind with such zeal.

Upon reaching our destination, we were in for a nasty surprise when the taxi driver demanded double the meter fare. After a few minutes of heated argument, we got away by paying a little over the meter fare and swallowing unjustified abuses hurled at us. The government should really bring in stricter measures to regulate the errant taxis in the city.

4:00 PM
Belur Math opens for the public. First we visited the museum that traces the chronology of the Ramakrishna Mission. One can also find several rare personal belongings of Sri Ramakrishna and Swami Vivekananda there. We had to leave our shoes and slippers at a free counter and walk barefoot on the hot gravel. And it was really hot at that time and the ground was burning.


Next we visited the Ramakrishna temple where people worship an idol of Sri Ramakrishna. Talking and otherwise making noise inside the temple hall is not allowed and we sat for a few minutes there in the calm silence. Photography is strictly prohibited inside the Belur Math premises and so we had to be content with storing the images in our minds!

The Hoogly river flows just behind the temple and people enjoy boating on the river. Having relaxed on the banks of the Hoogly for several minutes, we made our way to some shops that were selling items manufactured in the Math. We found bags, purses and carpets made of jute as well as books, photos, CD's and other memorabilia of the Math.

6:00 PM
Catch a bus to from Belur Math to Esplanade. The bus service in Kolkata comprises frequently plying small buses that carry around 30-40 people and look quite like matchboxes. Most of them are old and ramshackle, many with window panes broken. The drivers tend to ply the buses rashly, but that is the situation elsewhere in India too. The best part is the ridiculously low fare; traveling from Belur Math to Esplanade which is easily more than 10 km cost us only Rs. 5.50 per head! Thus the buses offer an inexpensive and fast means of transport for the common man.

7:00 PM
Catch another bus from Esplanade to Kali Ghat.

8:00 PM
Arrive Kali Temple. This temple has a historical significance in that the name Kolkata has been derived from "Kali Ghat", the area in which the temple is located. We had a rather tough time locating the actual temple from the Kali Ghat bus stop. Although there was a huge rush inside the temple, the streets one block away were close to deserted and we had to ask several passersby before we could finally locate the shrine.

Even before one is able to enter the temple, the priests or pandas try to exploit the gullible tourists. They hang around outside the temple and promise tourists that they could avoid the crowds and have darshan of the deity very quickly by paying a certain sum of money to them. The day we went there was a Saturday, a festival day of special worship and there was a long queue of devotees waiting to have darshan. Since we did not have much time left, we paid Rs. 100 to one of the pandas who led us through bewildering mazes, what with scaling walls and all, and took us close to the deity. We were warned to take special care of our wallets, purses and mobile phones and also not to give money to anybody inside the temple even if they asked. Well, that last advice turned out to be a farce, since the people inside the temple are all hand in glove with the pandas. As much as the darshan of the Mother Goddess was elevating, it was equally distressing, nay appalling the way the priest in the sanctum sanctorum demanded money from us. Similarly we were next taken to the spot where the right toe of Sati devi was supposed to have fallen in the days of yore during Shiva's Rudra Tandava. The panda there was equally bad and demanded Rs. 500 from us; we flatly refused and somehow escaped from his clutches.

This kind of open exploitation of tourists by the pandas vitiates the sanctity of the temple. Altogether it was a very unpleasant experience for us.

9:00 PM
Catch a bus and arrive at Howrah Station. We had dinner at the IRCTC canteen in the station, retrieved our luggage from the cloak room and boarded our train to New Jalpaiguri.

I'll continue the narrative of our visit to Darjeeling, Gangtok and Shillong in my next post. So long then.

Tuesday, February 10, 2009

AMD64 Function Calling Sequence

Gone are the days when function parameters were passed on the stack. In earlier architectures, the stack frame looked like this:
[local variables]
old ebp
return address
parameter 1
parameter 2
parameter 3
The function parameters were pushed onto the stack starting from the rightmost parameter. This was followed by the return address pushed by the call instruction, and the old ebp register value pushed by the function prolog. Then came the stack space for the local variables.

With the advent of 64-bit architectures such as the AMD-64, this scheme has changed. There are 8 more general purpose registers in the 64-bit architecture (labeled r8 through r15) which can be used for parameter passing. These eight registers are labeled r8d through r15d if only 32 of their 64 bits need to be accessed.

The AMD64 Application Binary Interface (ABI for short) defines detailed rules about how these registers are to be used. We will look at only two important rules:
  1. Registers rbp, rbx, r12, r13, r14, r15 belong to the calling function. Mnemonic: the B registers (rBp, rBx) and r12-r15 belong to the caller.
  2. Integer arguments are passed in the following registers in order: rdi, rsi, rdx, rcx, r8, r9. Integer values are generally returned in the register rax.
Let us see what these rules imply.

Rule number 1. If function bar is called by function foo, then foo expects the values of the specified registers to be unchanged when bar returns. To adhere to this convention, the compiler generates code to preserve the values of these registers in the function prolog of bar. If you look at the disassembly of function bar, then you will see a few push instructions at the beginning; the number of push instructions depends on how many of the caller-owned registers are actually modified in the callee. Correspondingly the function epilog contains instructions to pop the saved values back into these registers before the function returns.

The other registers belong to the called function. Which means that the called function is not required to preserve the other registers and is free to modify their values.

Rule number 2. Notice that upto six integer arguments can be passed in registers. This covers the most common parameter types and eliminates parameter passing on the stack. This speeds up function calls quite a bit since there is no need to do memory reads and writes - just fill up the registers and call the function. Remember that memory access is two or more orders of magnitude slower than register access.

Neat, eh? Note that there are several other details involved that have been skipped in this description. You can refer to the AMD64 ABI for more information.

Monday, February 9, 2009

Magic Squares

A magic square is a square whose cells are filled with numbers such that the sum of each row, column and diagonal is the same. Here's how to create a magic square having three rows. Fill the 9 cells with the numbers 1-9 sequentially. Start with a 1 in the center of the first row (x denotes an as yet unfilled cell):
x1x
xxx
xxx
Now there are two simple rules to follow.
1) Try to move North-East. If there is no available cell there, then wrap around. If we go out of the top of the square, then we wrap downwards; if we go out of the right of the square, then we wrap leftwards. To understand "wrapping" better, imagine the square rolled into a cylinder behind the screen from top down or right to left.
2) If a cell is present to the north-east and if it is already occupied, then enter the next number in the cell directly below the current one. Similar action is taken after filling the cell at the top-right corner of the square.
Using these two simple rules, the cells get filled up like this:
(x1x)---(x1x)---(x1x)---(x1x)
(xxx)---(3xx)---(3xx)---(35x)
(xx2)---(xx2)---(4x2)---(4x2)

(x16)---(x16)---(816)---(816)
(35x)---(357)---(357)---(357)
(4x2)---(4x2)---(4x2)---(492)
The same procedure applies to any magic square having an odd number of rows.
Happy squaring!