0

I have this code which I compiled for MSP430 platform using msp430-gcc.

#include<stdio.h>
#include<stdint.h>

// Preprocessor definitions
#define ROR(x,r) ((x>>r) | (x << (64-r)))
#define ROL(x,r) ((x << r) | (x >> (64-r)))
#define R(x,y,k) (x = ROR(x,8), x+=y, x^=k, y = ROL(y,3), y^=x)

void encrypt(uint64_t* , uint64_t*, uint64_t*);

void main(){
    uint64_t key[2] ={0x0706050403020100, 0x0f0e0d0c0b0a0908}  ;    // key
    uint64_t plain_text[2] = {0x7469206564616d20, 0x6c61766975716520};
    uint64_t cipher_text[2] = {0,0};      //initializing cipher text to 0s
    uint64_t* pt = plain_text;
    uint64_t* ct = cipher_text;   // Cipher Text
    uint64_t* k = key;    // 64 b
    encrypt(pt, ct, k);
}

 /*
 * Ecnryption Method
 */
void encrypt(uint64_t* pt, uint64_t* ct, uint64_t* k){

    uint64_t i;
    uint64_t B = k[1];
    uint64_t A = k[0];
    // Encryption rounds
    for(i=0; i<32; i++){
        R(ct[1], ct[0], A);
        R(B,A, i);        
        }
    }

I wanted the memory segment usage statistics for this program and I did that by generating the object file and using the $ size command. The result I got out of that is as below:

text       data     bss     dec     hex filename
    278       0       0     278     116 encrypt.o

I dont understand why the data segment, which tells me about my RAM usage is zero. I am assuming that my bss segment is zero as I don't have any uninitialized variables.

I would be really grateful if somebody can explain me what is happening here. Thank you.

NanoNi
  • 315
  • 4
  • 16
  • 2
    You can look at the assembly listing and see where your literals are going. They may be included as an immediate instruction which would place them in your `text` section. – Alden Apr 28 '15 at 15:12
  • Also the whole object file will be loaded into RAM, so I'm not sure why you are concerned about the `data` section being zero. There really isn't anything to put into the section, but your code will take up space (278 bytes). – Alden Apr 28 '15 at 15:19
  • Alden, I would like to know how much ROM and RAM memory is consumed by this program. My goal is to reduce them as much as I can. Since `data` + `bss` section tells me about the RAM usage, I want to know these values. – NanoNi Apr 28 '15 at 15:51

2 Answers2

0

The data section contains initialized data, i.e., global or static variables with a non-zero value:

int x = 5;
void f() {
    static int y = 42;
}

The bss section contains uninitialized data, i.e., global or static variables with the default (zero) value:

int x;
void f() {
    static int y;
}

Your program contains no such variables, so these sections are empty.

All your variables are automatic, i.e., on the stack. The stack is allocated dynamically, and there is no easy way to detect the maximum stack size by just looking at the object file.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

What you are declaring are local variables. They will be stored in the stack. In the general case, the RAM size occupied by the stack cannot be determined at the compile time. However, for simple applications such a yours it's possible to estimate the upper bound of stack usage, either by using objdump and counting the bytes manually, or using some of existing tools for static analysis.

To store the variables in .data and .bss segments, use the static type qualifier:

void main() {
     static uint64_t key[2] ={0x0706050403020100, 0x0f0e0d0c0b0a0908}; 

This is a better practice; dynamic allocation such as allocation in the stack is not something you want to do in microcontroller applications.

kfx
  • 8,136
  • 3
  • 28
  • 52