Under the hood of a simple C++ program

Lets take a simple C++ program and see what happens under the hood at the assembly level.

int add_nums(const int a, const int b) 
    return a+b; 

int main()
    int x = add_nums(2,3); 
    return 0; 

Now, lets create the assembly code. -S is the flag to give to either clang or gcc to generate the assembly code. We don’t use any sort of optimization to keep things simple.

clang++ -S add_nums.cpp

This generates the output file add_nums.s which is the assembler file.

The complete listing is here at github: add_nums.s so we will look into the more interesting parts here. This is compiled on the x86_64 architecture.

In main, the first instruction is to save the previous frame pointer. This I believe would be the frame pointer of the caller to main since main itself is the callee.

pushq   %rbp

Next notice how the base pointer is overwritten by the stack pointer since the previous frame pointer was already saved.

movq %rsp, %rbp

Since the stack pointer moves from top to bottom, this next instruction allocates 16 bytes for the stack.

subq    $16, %rsp

The next instructions move 2 and 3 to registers and makes the call to  __Z8add_numsii

callq __Z8add_numsii

Notice the name mangling here by default in C++. If instead this was a C program, the call would have been callq __add_nums. The name mangling is to support function overridding in C++.

Once main is ready to exit, the stack pointer is moved back to original position. Next, ebp is restored as well by popping the value from the stack and the function returns.

  addq $16, %rsp 
  popq %rbp 

This is how things work on Intel. This stackoverflow thread is another great reading to understand things as well. stackoverflow thread.

On the next post we will explore how the assembly is different on ARM architecture for the exact same program.

podcast (the eyes have it)

My very first podcast! 🙂 As excited as I am, I realize that there are a lot of kinks. Hopefully, there will be improvement as time goes by.

Happy Listening.

If you like the story, you can buy the book at amazon: http://amzn.to/1PUGIGp

Day 1

Aria is born. There she was out of her mother’s womb after spending 37 weeks and 3 days in mom’s warmth protected from the outside world. It had already been 3 days of labor or trying to get there and the climax could not have been any more dramatic. The nurse had checked her cervix 45 minutes ago and announced that the baby’s head was right there at the gate now. Dr. Teng arrived and took a look and called me to show me her head sneaking through. 15 minutes later with the last efforts required from mommy to push through, her head appeared and in the next second her entire little body was in the doctors hand. And then the crying. It had to be the sweetest sound I had heard in a while. Relief, relief, relief. Thank God. Rest is a bit of a blur now. Images in my head imprinted. Mommy crying, holding the baby. Baby being cleaned up. Two shots – one in each leg – Vitamin K and Hepatitis B. Weight being measured, height being measured. The time 10:54pm. Dr Teng saying that its a beautiful placenta. Does the dad want to cut the chord? Holding the baby. Calm, calm baby. Swaddle the baby. Pack up, moving to another room. Congratulations. Don’t let go of your daddy. The feeling of relief. The feeling of being happy. The feeling that everything is moving too fast. The feeling that you have to be focused and alert. The feeling of cluelessness. The feeling of exhaustion, and we are just getting started. 

A kind word

I was just reminiscing about some old times in Houston and I remembered the time when I used to take the bus to the university because we just shared one car in the family. I generally had to change two buses which was okay if I made it in time by 5:00pm, but if I got late and the bus service stopped, my Dad had to pick me up half way through. 

There was this one time though when our car was in service and I was late from the university. I missed my last connection to the bus 70 after already having changed 2 buses and was about 4 miles away from home. I had no choice but to walk which I interspersed with some running. It was a lonely walk – not many people walk in Houston (unless they are in the gym or on a dedicated trail). To top it off, It had been drizzling the entire day, but at that time the rain picked up and I was soaking in no time. 

I was exhausted and hungry. I came across a gas station, where I bought some peanuts to refuel myself. The lady at the counter was busy talking on the phone with someone. She  saw me, kept the phone aside for a second and said ‘take care there now‘ and had empathy in her eyes.

It was not much, but even after all these years, I still remember those kind words. That is perhaps the strength of kindness – it lingers on. It may not count much to some folks, but for what its worth, a kind word never goes waste. If nothing, it certainly warms the heart. 

Next time you get a chance, be nice. Someone may remember what you said for a long time. 

Time tested beauty tips

Time-Tested Beauty Tips

For attractive lips, speak words of kindness.
For lovely eyes, seek out the good in people.
For a slim figure, share your food with the hungry. For beautiful hair, let a child run his fingers through it once a day.
For poise, walk with the knowledge you’ll never walk alone …

People, even more than things, have to be restored, renewed, revived, reclaimed and redeemed and redeemed and redeemed. Never throw out anybody. Remember, if you ever need a helping hand, you’ll find one at the end of your arm. As you grow older you will discover that you have two hands. One for helping yourself, the other for helping others.

Sam Levenson

Note: this is the complete poem Audrey read to hers sons, Sean and Luca on Christmas Eve 1992. The author, Sam Levenson wrote it for his grandchild.

Wo kuch aise purane din ..

Wo kuch aise purane din ..

Wo kuch aise purane din yaad aate hain,

Jab yun asaan si hoti thi zindagi…

Na kal ka sochna, na aaj ki parwaah,

aur jab kabhi na hota tha dil tanha…

Kabhi khel khel mein hasna, khel khel mein rona,

Har baat par khush ho jaana,

Har anjaan aahat par darr jaana,

aasmaan mein udte jahaaz ko dekh kar heraan ho jaana,

Rang birangi tittali ko dekh kar muskarana,

Diwali ke patake, Holi ke rang,

Jab hota the mere sab dost mere sang,

bas wohi kuch purane din yaad aate hain,

jab yun asaan si hoti thi zindagi …


San Francisco Diaries

After being nudged for a while by a friend to do a photo essay, decided to finally give it a shot. I have always found San Francisco to be charming, eclectic, liberal, and beautiful. When I do visit San Francisco, its to show a visiting friend around to see the most popular spots. But its rare that I get a chance to see the everyday life in SF and its weird ways. And I guess there is a small part in me which aspires to be a photo journalist. Photographs do their own talking, so  enough words for now :). Hope you enjoy seeing these snapshots as much as I enjoyed taking them.

Skate Boarding at the Ferry Building

Art students drawing the Bay Bridge

Art students drawing the Bay Bridge near the Ferry Building

Jogger at the Telegraph Hill

Happy Jogger at the Telegraph Hill (She was so happy that she was being clicked 🙂

He was as enthusiastic as anyone gets to get his snaps clicked.

I could not believe my eyes, when I saw a guy dressed in a Suit running down the slope. But he was kind enough to let me shoot him. But what a sight 🙂

Bus driver fixing the power line on the bus.

Pretty girl at street crossing.

Students at train stop