Max Fest
Max Fest

Reputation: 45

How do i save data in known memory location once for multiple reuse in x86 assembly?

I am writing an intel x86 assembly program to compute logarithms using a log table. I call the assembly function in a c program. I don't want to move all the values in the log table to memory every time i call. I'm new to assembly on a non-simulated processor, so I'm not even sure where I'm allowed to store it. 20,000 32-bit integers.

How can I store a "large" amount of data once at the beginning of a c program, so that I can access it in an assembly routine? If i put it in the .data section, is it moved to memory every time i call the actual function?

Edit: this is how i call the function

#include <stdio.h>
extern int doIt(float) asm("doIt");
int main(){
    printf("%d\n", doIt(7.0));
    printf("%d\n", doIt(4.0));
    ... //more calls of the sort
}

Not sure if the c code is completely correct. In doIt i need to access the mentioned table repeatedly.

Upvotes: 0

Views: 259

Answers (1)

Dario Rodriguez
Dario Rodriguez

Reputation: 812

To give it an answer:

#include <stdint.h>

const int32_t table[10]; /* .rodata */

int32_t table[10]; /* .bss */

/* 
 * However, if you initialize with any (nonzero) values
 * it goes to:
 */

 int32_t table[10]={
    0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
    0x00000000,0x00000000,0x00000000,0x00000000,0xaaaaaaaa
 }; /* .data */

 const int32_t table[10]={
    0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
    0x00000000,0x00000000,0x00000000,0x00000000,0xaaaaaaaa
 }; /* .rodata */

About the sections: .data and .rodata are stored in the object file and may not be loaded to RAM unless you need to use them, or anything contained into the same page -maybe you can change this behaviour with a linker script, I don't know-, and .bss section doesn't actually store any data, that is why once you initialize the variable, it moves to .data and gets it's image stored into the object file. Most compilers will ignore initialization to zero because the .bss variables do not have their image stored into the object file, so the loader fills their space to zero anyways when it loads the program.

Then, when you compile the object, you can import the symbol name from your ASM routine.

Upvotes: 1

Related Questions