{{blog.title}}
{{blog.date}} | {{blog.stars}} stars
Author: Kiran
• Sunday, June 26, 2016
Years ago, I published a design pattern (a solution that could be applied to a common, recurring problem. Hence, the name 'pattern'). I named it 'Multi Key Lock'. Here's a brief...

The multiple key locks are unlocked only when all the keys are used. Similarly, there are situations where multiple conditions needs to be satisfied for an event to come to pass -- possibly (and, most likely), across different components / modules. In such cases, this pattern comes in handy.

This pattern propounds a lock object that represents a specific condition or state that comes to pass when a chain of events or conditions occurs. The 'clients' of this lock object are the players (i.e., modules or components) that affect the condition in some way.

The lock object exposes an API for the clients to try unlock. Whenever a particular client's state requires to unlock this, it invokes the unlock API. This triggers an evaluation process, which is invoking a callback function in every client. The callback functions are supposed to return a boolean value -- as to whether, that particular client 'thinks' it is in a state suitable to unlock. In case, all the parties return true, the lock object gets unlocked, and it notifies all the parties of its state.

The parties or clients of this lock object does not have to know the other parties involved -- thereby, averting the need for any dependencies amongst one another.

Note the striking similarity with a AND gate, which evaluates to true, only when all its inputs are true. Hence, an alias name 'AND' condition).

Find the link to the actual write-up published here.

Cheers!
Author: Kiran
• Monday, June 11, 2012
The Stack, I believe is perhaps the most important data-structure. Ever! The Atlas amongst all its siblings (and cousins), as it carries the gruelling burden of entire world of computing on its back. Without it, computing could not have advanced this far – let alone a tenth of this distance, not even an inch ahead. Such is its glory. Believe me; No exaggeration. I swear!

It is so very entwined into the DNA of computational theory that its prominence begins to surface, if you look closely at how programming languages and the instruction set of processors have evolved. Ever since the diaper days of structured programming, in 1950s, functions (or procedures or sub-routines) have been an integral part of writing programs.

Calling a function, under the hood, employs a stack. Could you imagine developing a real-world application without the use of functions? Of all the zillion lines of code in today's monstrous applications, there would probably be a few million functions, to say the least. With multiple such programs running simultaneously, hundreds of stack are at play, expanding and shrinking, churning the wheels of your program, dragging it along the path of execution. The stack is always around – working under cover – supporting execution of your program.

Digging Deeper: Functions and Stack Frames
Whenever a function is invoked, the stack (associated with the execution thread) expands – a 'frame' is 'pushed' onto the stack; similarly, when the function returns, the frame is 'popped'out of the stack. Frame is that region of the stack used by the run-time to realize a function invocation. It typically includes the return address, arguments passed, the local variables etc. The lifetime of a frame is same as the lifetime of the function itself (i.e. period from the point the function is called, until the point it returns). The layout of a frame is architecture specific (often referred to as the calling convention). The compiler emits the code to handle creation and cleanup of stack frames.

There are two parties involved in a function call scenario: the caller (the code that calls the function in question, which is usually another function; in the above code fragment 'main' is the caller), and the callee (the function being called; in the above code, 'Func' is the callee). Accordingly, there are two variant implementations of this calling convention, based on who creates and / or cleans up the frame.

Let us dissect further, and understand the anatomy of the stack layout for the standard calling convention on the ubiquitous Intel X86 processor. Prepare to get your hands bloody :-).

Stack Layout in StdCall Calling Convention
For reference, lets take the following code snippet:

int Func(BOOL bTest, int *pNum);
int main()
{
int n = 0;
Func(TRUE, &n);
}
where, 'Func'is a function that modified 'n' in someway (which is immaterial to the topic). Lets say, it modified the value of 'n' to 12, and returns 1 (return value). So, the expected output is: 1, 12. However, the output was 1, 0!
To make sense of whats going on, we have to examine the assembly code for the function call – both at the point of invocation, and within the function itself.

The assembly equivalent of the call to 'Func' in the main function is (as generated by the compiler):
1: mov ecx, dword ptr [ebp-0Ch]
2: push ecx
3: push $1
4: call Func
5: add esp,8
In line 1, the address of the variable 'n' is computed, and moved to the register ecx. In line 2, the address is pushed onto the stack (the argument). In line 3, TRUE for the boolean argument is pushed. Notice that the last argument is pushed first onto the stack. i.e., the rightmost argument gets pushed first, followed by the previous argument until the first argument to the function. In line 4, function is actually invoked. It internally pushes the return address onto the stack, and branches execution to the function entry point. In line 5, the stack pointer is incremented by 8bytes – this is equivalent to popping off both the arguments, and hence cleaning up the stack frame.

Before we look at the assembly code of the function 'Func' itself, a diagrammatic representation of how the stack frame looks, when 'Func' is executing.


The assembly code for the function 'Func' itself:
1: push ebp
2: mov ebp,esp
3: sub esp,0C0h
<function logic>

4: mov esp,ebp
5: pop ebp
6: ret

The lines 1 – 3, sets up the stack for the function, while the lines 4-6 towards the end of the function cleans up the stack to its original state (when it entered this function).

The line 1 above is the first instruction of the function (aka entry point). The first thing a function does is to save the frame pointer (which is ebp on x86) on the stack itself. In line 2, the current value of the stack pointer is moved into ebp – making the ebp point to the beginning of the new stack frame. This is necessary because the frame pointer is used to relatively reference any local variable. In line 3, the stack pointer is subtracted by 12 bytes (0xC). This is to create space for local variables used in this function. This is equivalent of pushing 12 bytes onto the stack.

In the line 4, is setting the stack pointer to the beginning of this frame. This is equivalent to collapsing the stack (reclaiming all the space created for local variables). Note that it is an exact reverse of what line 2 did. In line 5, the top-most value on the stack is popped into ebp, which is equivalent to restoring the previous frame pointer. Again, this is exact reverse of what line 1 did. Line 6 happily returns the control back to the caller, and in the process pops the return address off the stack.
Author: Kiran
• Saturday, October 02, 2010
Recently, I had the misfortune of sitting through the movie 'Dabangg'. We were gullible. We fell into the PR and marketing web weaved by the film makers. It just does not live up to all that hype and hoopla - Not even remotely close.

Firstly, the script turns out to be a damp squib - somewhat hollow and lacking in creativity. Much like a dish hastily put together that is not particularly delectable. A fusion of unscrupulous local politics and a glorified family fued (there seems to be no worthy reason as to why the fued should amplify, but to drag the movie on). An item number is also squeezed in that could have been done without. Further, it leaves you with a touch of antuquity. In a stark contrast, Peepli [Live] that is shot entirely in a country setup, and has a rural dialect does not leave you with such a taste. Perhaps, this is because of the lack of stuff in the script.

Secondly, Sallu's style does not carry the movie too far - contrary to what the makers would have expected. As for the stunts, the bar has already been set very high by none other than Rajani Sir; anybody else, it can only be an attempt to imitate at best. Also, you wont fail to notice The Matrix slow motion stunt being plagiarized by yet another flick.

However, few things stand out in an otherwise hollow production - some RoI to be content with. Sonu Sood (who plays Chhedi Singh) deserves a pat on the back - an almost impeccable performance. Could we forget the fresh face - one that is as fresh as the morning dew! An oasis in a desert (not a mirage) ;-) She manages to spell her sensuous magic and compensate for the lesser dialog space allocated to her.

From the box office perspective, it seems to have raked in big moolah. In fact, posting record earnings in the opening week itself. Quite contrary to what I had to say. In the end -- one might say -- the box office success is all that matters, who cares if it bores the audiences to death!

Author: Kiran
• Saturday, October 25, 2008

Happy Diwali!

The Diwali season this year has got to be special. In fact, a more special one to all the country men; for, we had more to cheer about and celebrate than just the festivities, colors and the brilliant display of rockets that illuminate the night skies – a much more sophisticated, technologically advanced rocket that carried a billion hopes to the moon. Yes, am talking about the Chandrayaan-I mission.

May be, it is not unprecedented endeavor, and some have done it already. Nevertheless, it provides a launch-pad that would propel India onto the frontlines of space exploration. It is just a piece in the jigsaw puzzle. With the recently commissioned Deep Space Network, follow-up missions like Chandrayaan-II and plans to put man in space by 2015, it would be more than a journey. And, it is your prerogative to be proud.

But then, isn’t India more about exploring the Inner Space? The rich and diverse spiritual heritage that has passed on from generations has been the guiding light to the rest of the world. A spiritual superpower status that it commanded, it was (and still is) the torch-bearer to a serious seeker. The magnetic spell still remains, as even today seekers from the west are drawn to the shores of this country to acquire the wisdom and experience the healing touch that it has to offer.

Amidst the clamor of modernization, we are probably sidelining this gift that has been conferred upon us. May be, it is time to recognize that we should not overlook our strengths in order to acquire newer capabilities. With an unusual surge of optimism, I would say, we can do better in both these worlds. I hope I would live long enough to see that day!
Author: Kiran
• Sunday, October 19, 2008

No. This is not a review of the movie that goes by the same title. Instead, it is rumination around an interesting facet of human psychology – Prejudice, as I have experienced it. I hope this write-up will shore up fellow victims while sending out a strong message to the perpetrators (It would be rather ambitious of me to expect that they comprehend it; for, they are usually thick-skinned).

“Assumption is the mother of all screw-ups”, wrote an ex-colleague in his e-mail signature. How true! People are habitual of assuming things, er… a lot of things. Just like that! Would anyone like to believe that their reasoning could go wrong? Eventually, they develop a prejudice towards something / someone. For some reason, people seemed to think that I am doing good, by myself. Any help from their end is probably an excess and they should refrain from it. “Phew! I wish that was true”, I chuckle. Imagine doing everything, all by yourself. Perhaps, I exude a sense of well-being, which I should be proud of.

There is another fall-out of prejudice - Envy. Some people just can’t digest the fact that you can do some things better (Of course, that also means they could do some things better than you). They never let go an opportunity to snub you. “Mr. X knows everything about rolling out a summer internship”, they would say on topics that you know about. “It is a mistake that we did - You, me and whoever. We should have done it the other way”, they quip just to highlight that you too made a mistake, although you are not even directly related to it.

For god’s sake, I never claimed I am perfect. Nevertheless, it is an honor; for, they are only serving to underline your wins by deliberately (and, in some cases, desperately) trying to condone them and highlight your (miniscule) slips. Again, I take pride in this! May be, it is worth shouting from the roof-top, “Is that all? I can take more. My ears are wide open”. Usually, I laugh it away, saying “Thank you” in my mind.