Always Be Coding

Good Read (with some good references included):

View story at

Posted in Coding | Leave a comment

The Mail Must Get Through

This is from excellent article about software programming and open source development here,

To Summarize:

  • Every good work of software starts by scratching a developer’s personal itch.
  • Good programmers know what to write. Great ones know what to rewrite (and reuse).
  • If you have the right attitude, interesting problems will find you.
  • When you lose interest in a program, your last duty to it is to hand it off to a competent successor.

While I don’t claim to be a great programmer, I try to imitate one. An important trait of the great ones is constructive laziness. They know that you get an A not for effort but for results, and that it’s almost always easier to start from a good partial solution than from nothing at all.

Posted in Misc | Tagged , , | Leave a comment

Case Study – Compiler Optimization

They (compiler optimizations) do result in weird things sometimes and here is one such  found with FreeRTOS for ARM Cortex-M3 port. This is as result of “Tail Call Optimization” as I had mentioned it in detail in my last post here.

Such bugs can take a lot of time to debug and still keep you quite clueless because they manifest as if they are HeisenBug.

Nevertheless here is detailed summary of that bug,

*** Update:
To be precise above issue is actually compiler optimization magic coupled with how PendSV is handled by ARM Cortex-M3 NVIC.

Posted in Coding | Tagged , , | Leave a comment

Tail Call Optimization


Tail Call Optimization is an optimization strategy used by compiler to generate code in which subroutine/funciton call is done without adding stack frame to call stack. More info is available here,

In Action

I will try to explain using below simple program,

$ cat x.c
#include <stdio.h>

int bar(int val)
        volatile int cnt;
        cnt = val;
        while (cnt--)

int foo(int val)

int main()
        return 0;

Let’s compile without using any optimization flag first and see output disassembly,

$ arm-none-eabi-gcc -c x.c

$ arm-none-eabi-size x.o
   text    data     bss     dec     hex filename
    156       0       0     156      9c x.o

$ arm-none-eabi-objdump -S x.o | less
Disassembly of section .text:

00000000 <bar>:
   0:   e52db004        push    {fp}            ; (str fp, [sp, #-4]!)
   4:   e28db000        add     fp, sp, #0
   8:   e24dd014        sub     sp, sp, #20
   c:   e50b0010        str     r0, [fp, #-16]
  10:   e51b3010        ldr     r3, [fp, #-16]
  14:   e50b3008        str     r3, [fp, #-8]
  18:   e1a00000        nop                     ; (mov r0, r0)
  1c:   e51b3008        ldr     r3, [fp, #-8]
  20:   e3530000        cmp     r3, #0
  24:   03a02000        moveq   r2, #0
  28:   13a02001        movne   r2, #1
  2c:   e20220ff        and     r2, r2, #255    ; 0xff
  30:   e2433001        sub     r3, r3, #1
  34:   e50b3008        str     r3, [fp, #-8]
  38:   e3520000        cmp     r2, #0
  3c:   1afffff6        bne     1c <bar+0x1c>
  40:   e1a00003        mov     r0, r3
  44:   e28bd000        add     sp, fp, #0
  48:   e8bd0800        pop     {fp}
  4c:   e12fff1e        bx      lr

00000050 <foo>:
  50:   e92d4800        push    {fp, lr}
  54:   e28db004        add     fp, sp, #4
  58:   e24dd008        sub     sp, sp, #8
  5c:   e50b0008        str     r0, [fp, #-8]
  60:   e51b0008        ldr     r0, [fp, #-8]
  64:   ebfffffe        bl      0 <bar>
  68:   e1a00003        mov     r0, r3
  6c:   e24bd004        sub     sp, fp, #4
  70:   e8bd4800        pop     {fp, lr}
  74:   e12fff1e        bx      lr

00000078 <main>:
  78:   e92d4800        push    {fp, lr}
  7c:   e28db004        add     fp, sp, #4
  80:   e3a0000a        mov     r0, #10
  84:   ebfffffe        bl      50 <foo>
  88:   e3a03000        mov     r3, #0
  8c:   e1a00003        mov     r0, r3
  90:   e24bd004        sub     sp, fp, #4
  94:   e8bd4800        pop     {fp, lr}
  98:   e12fff1e        bx      lr

Points to note here are:

  1. Stack frame for foo is prepared by main in transition from main->foo
  2. Stack frame for bar is preapred by foo in transition from foor->bar
  3. Take a note of size of output object file, 156 bytes

Note: For ARM architecure lr stores return address of caller, bl instruction refers to branch and link operation whereas b instrction refers to branch only.

Let’s compile with optimization enabled now,

$ arm-none-eabi-gcc -Os -c x.c

$ arm-none-eabi-size x.o
   text    data     bss     dec     hex filename
     64       0       0      64      40 x.o

$ arm-none-eabi-objdump -S x.o | less
Disassembly of section .text:

00000000 <bar>:
   0:   e24dd008        sub     sp, sp, #8
   4:   e58d0004        str     r0, [sp, #4]
   8:   e59d3004        ldr     r3, [sp, #4]
   c:   e2432001        sub     r2, r3, #1
  10:   e3530000        cmp     r3, #0
  14:   e58d2004        str     r2, [sp, #4]
  18:   1afffffa        bne     8 <bar+0x8>
  1c:   e28dd008        add     sp, sp, #8
  20:   e12fff1e        bx      lr

00000024 <foo>:
  24:   eafffffe        b       0 <bar>

Disassembly of section .text.startup:

00000000 <main>:
   0:   e92d4008        push    {r3, lr}
   4:   e3a0000a        mov     r0, #10
   8:   ebfffffe        bl      24 <foo>
   c:   e3a00000        mov     r0, #0
  10:   e8bd4008        pop     {r3, lr}
  14:   e12fff1e        bx      lr

Points to note here are:

  1. Stack frame for foo is prepared by main in transition from main->foo
  2. No stack frame for bar, just a direct branch
  3. With size enabled optimization (-Os), size reduced to 64 bytes

From (2) above it is quite clear that, bar() will return directly to main(), at address c. This is what is “Tail Call Optimization”.


  1. Possibly can impact debugging process, because of lack of stack frame in call stack traversal.
  2. If in call, local storage is used then (as is will not be stored/passed in stack) may cause issues. But I guess compiler should handle this situation.

More Analysis

Is available here,

Posted in Coding | Tagged , , , | 1 Comment

The Impossible Dream

Also known as “Poem of La Mancha”, takes one to different horizon, towards infinity! Something that penetrates deep into heart, helps us in finding true purpose of life and to dream and achieve impossible…

An Inspirational Master-piece…

To dream the impossible dream,
to fight the unbeatable foe,
to bear with unbearable sorrow,
to run where the brave dare not go.

To right the unrightable wrong,
to love pure and chaste from afar,
to try when your arms are too weary,
to reach the unreachable star.

This is my quest,
to follow that star —
no matter how hopeless,
no matter how far.

To fight for the right
without question or pause,
to be willing to march into hell for a
heavenly cause.

And I know if I’ll only be true to this
glorious quest
that my heart will be peaceful and calm
when I’m laid to my rest.

And the world will be better for this,
that one man scorned and covered with scars
still strove with his last ounce of courage.
To reach the unreachable stars.

– Joe Darion

Posted in Inspiration | Tagged , | Leave a comment

ArchLinux – Bleeding Edge Distro

Fed up with crappy and bulky desktop environments that recent Linux distributions (like Fedora, Ubuntu) provides then ArchLinux is the way to go! If you appreciate freedom of selection of what you want in your system, if you want to stay on bleeding edge, if you want to “keep it simple, stupid” (KISS), then ArchLinux is the way to go!

What Is It

Arch Linux is an independently developed, i686/x86-64 general purpose GNU/Linux distribution versatile enough to suit any role. Development focuses on simplicity, minimalism, and code elegance.

What Is Different

  • Simple, stupid philosophy — minimal installer
  • Rolling release model — always up to date, instead of periodic release
  • Fast package manager — Pacman
  • Best documented wiki site — ArchWiki
  • You are master — You do everything starting from lowest level configuration


The Rise of “Worse is Better”,


Getting to basic command-line will take at-least couple of hours but you get to enjoy ride with ultimate beauty of sophistication that simplicity offers! You partition the system, mount the drives, install base system, install boot loader, configure hostname, configure pacman, and add user. Then all the way to a full fledged X and Desktop Environment of your choice! Giving a initial try in virtual environment is always safer!

Bleeding Edge

You get latest and greatest of all Linux packages.


  • For fairly experienced Linux users
  • Asks for that little extra — time and effort
  • For i686/x86_64 only — some effort for ARM is ongoing
  • Can be a problematic if you have to do it all again — e.g. crashed machine

But it something amazing, an opportunity to work with what you love, with amazingly fast package manager and to stay on bleeding edge to rule the world 🙂

Posted in Linux | Tagged , , | Leave a comment

VIM Essential Plug-ins


Being one of the favourite editors, I did some analysis to make it effective while dealing with large code base. Here are some outcomes in the form of external plug-ins:

  • FuzzyFinder –
    Helps in quick file search, file buffering, pattern search and lots more. This can be considered as superset of BufExplorer, NerdTree, and Taglist plug-ins. It is quite ingenious in finding patterns. Install plugin including depedancy of l9 as mentioned on above link. FuzzyFinder explores database created by ctags to search, run following command to create one:

     $ ctags -R --extra=+f --exclude="*.html" -exclude="*.tex"

    This includes files and directory information to ctags database, excluding un-wanted files.
    Some useful commands for FuzzyFinder are:
    |:FufBufferTag| – Buffer-Tag mode (|fuf-buffertag-mode|)
    |:FufTaggedFile| – Tagged-File mode (|fuf-taggedfile-mode|)
    |:FufBuffer| – Buffer mode (|fuf-buffer-mode|)
    |:FufFile| – File mode (|fuf-file-mode|)

  • BufExplorer –
    Bufexplorer provides a basic interface to manage multiple buffers in vim.
  • NERDTree –
    The NERD tree allows you to explore your filesystem and to open files and directories.
  • CCTree –
    To view static call graph using database created by CScope.
  • DrawIT –
    A nice plugin for ASCII drawing, useful especially to create block diagrams quickly.

Of course there are few more to above list but I will let you figure out them. These little contributions make development a lot effective! Thanks to contributors!

Handy commands

  • :browse oldfiles — Browse and open recently used file
  • :%s/foo/bar/g — To replace “foo” by “bar” throughout file
  • :!ls — Run bash command (here it lists files in cwd) without leaving vim
  • :tag <name> — Find a tag with specified name
  • :n <file> — open up file
  • :split <file> — open up file splitting current window horizontally
  • :vs <file> — open up file splitting current window vertically
  • :Explore — opens up directory listing of files at level of current open file, split window if current file is modified
  • :SExplore — split and explore directory listing
Posted in Linux | Tagged , , | Leave a comment