문제

I have a simple c program for printing n Fibonacci numbers and I would like to compile it to ELF object file. Instead of setting the number of fibonacci numbers (n) directly in my c code, I would like to set them in the registers since I am simulating it for an ARM processor.How can I do that?

Here is the code snippet

#include <stdio.h>
#include <stdlib.h>

#define ITERATIONS 3

static float fib(float i) {
    return (i>1) ? fib(i-1) + fib(i-2) : i;
}

int main(int argc, char **argv) {

    float i;
    printf("starting...\n");

    for(i=0; i<ITERATIONS; i++) {
    printf("fib(%f) = %f\n", i, fib(i));
    }

    printf("finishing...\n");

    return 0;
}

I would like to set the ITERATIONS counter in my Registers rather than in the code.

Thanks in advance

도움이 되었습니까?

해결책

The register keyword can be used to suggest to the compiler that it uses a registers for the iterator and the number of iterations:

register float i;
register int numIterations = ITERATIONS;

but that will not help much. First of all, the compiler may or may not use your suggestion. Next, values will still need to be placed on the stack for the call to fib(), and, finally, depending on what functions you call within your loop, code in the procedure are calling could save your register contents in the stack frame at procedure entry, and restore them as part of the code implementing the procedure return.

If you really need to make every instruction count, then you will need to write machine code (using an assembly language). That way, you have direct control over your register usage. Assembly language programming is not for the faint of heart. Assembly language development is several times slower than using higher level languages, your risk of inserting bugs is greater, and they are much more difficult to track down. High level languages were developed for a reason, and the C language was developed to help write Unix. The minicomputers that ran the first Unix systems were extremely slow, but the reason C was used instead of assembly was that even then, it was more important to have code that took less time to code, had fewer bugs, and was easier to debug than assembler.

If you want to try this, here are the answers to a previous question on stackoverflow about resources for ARM programming that might be helpful.

One tactic you might take is to isolate your performance-critical code into a procedure, write the procedure in C, the capture the generated assembly language representation. Then rewrite the assembler to be more efficient. Test thoroughly, and get at least one other set of eyeballs to look the resulting code over.

Good Luck!

다른 팁

Make ITERATIONS a variable rather than a literal constant, then you can set its value directly in your debugger/simulator's watch or locals window just before the loop executes.

Alternatively as it appears you have stdio support, why not just accept the value via console input?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top