Translation of Common C Constructs to MSP430 Assembly

Here is a list of simple C code implementation in MSP430. We can learns its implementation by analysing the assembly code. We can generate the assembly code by compiling the C code using -S option.

  • Local variable

Consider a simple code which contains a single statement, assignment of a local variable to hundred. The first step is common in every assembly code, it is the stack pointer is decremented by double the number of local variable. This will not be mentioned below. Initially stack pointer points to the address 0x0280. In this case, as there is only one local variable hence stack pointer is decremented by 2 and stored to register r1. And then to a general purpose register r4, even though r4 is a general purpose register it is used to store the stack frame pointer.

The only step in this code is assignment, hence move the value to address present in register r4(stack), which is in the stack.

A common step at the end of every c code is to return the stack pointer to memory 0x0280.

  • Static Integer

Consider a C code which contain a static integer assigned to 100. And in the next step we assign it to 200. Let’s examine the assembly code.

The static integer is given  a permanent address in the address space. Hence any further assignment will be done to that address directly.

  • Function with Parameters

Consider a function with two parameters and return the sum of two parameter. The first step is, main calls the function.

The parameters are stored to register r14 and r15. call will make the control to jump to the label fun which is the base address of our function.

The first statement in function fun is to push the value already in r4’s address to stack(return address and local variable if any). Next, decrement the stack pointer so that it point to the stack frame of the function.

The body of the function, which will add the values stored in r14 and r15 and stores the result in r15.

During the return procedure the first step is to add the number that was decremented from stack pointer in the first step. Now popping the stack give the return address. Back in main, the result in r15 is moved to the stack.

  • Pointer to Integer

Consider a C code which contains an integer having value 10 and a pointer pointing to this integer. Assign a new value, 20, to this integer using the pointer.

As main contain two local variables, the stack pointer is decremented by 4. The value of the stack pointer SP is now 0x027c. The number is moved to address stored in the register r4+2. Move the value in register r4 to r15. Adding 2 to r15 gives the location where the address of the integer is stored. Now we can move the new value to the address stored in r15.

  • Pointer to Function

Consider a pointer to a function returning void and taking zero parameters. We are calling a function using this pointer. And we are not doing anything in the function.

In the first step we are moving the address of the function to the address stored in the register r4. The value in the address present in r4(i.e. the address of fun) is moved to register r15. In the call instruction we are not using the label instead the register r15, which contain the base address of function fun.

The function fun simply push the return address and pop the return address from the stack and returns to main.


About Odol Shinu

I've completed my B Tech in Information Technology in 2010 from Government Engineering College Sreekrishnapuram Palakkad under Calicut University.

Posted on November 4, 2010, in MSP430 Family. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: