|
#include <windows.h>
#include <stdio.h>
#include <arm-codegen.h>
#include <arm-dis.h>
unsigned long fib_c(unsigned long n) {
if (n < 2)
return(1);
else
return(fib_c(n-2) + fib_c(n-1));
}
void setup_fib_jit (unsigned int *pins) {
/* label1 */
ARM_CMP_REG_IMM8 (pins, ARMREG_R0, 2); /* is n < 2 ? */
ARM_MOV_REG_IMM8_COND (pins, ARMREG_R0, 1, ARMCOND_LO); /* if yes return value is 1 */
ARM_MOV_REG_REG_COND (pins, ARMREG_PC, ARMREG_LR, ARMCOND_LO);
/* if yes return address in PC; */
/* and exit to main or previous recursive call */
ARM_PUSH2 (pins, ARMREG_R0, ARMREG_LR); /* save n and return address to the stack*/
ARM_SUB_REG_IMM8(pins, ARMREG_R0, ARMREG_R0, 2); /* n = n-2 */
ARM_BL (pins, -7); /* recurse to label1 for fib(n-2) */
ARM_LDR_IMM (pins, ARMREG_R1, ARMREG_SP, 0); /* load n from the stack */
ARM_STR_IMM (pins, ARMREG_R0, ARMREG_SP, 0); /* store result fib(n-2) */
ARM_SUB_REG_IMM8(pins, ARMREG_R0, ARMREG_R1, 1); /* n = n-1 */
ARM_BL (pins, -11); /* recurse to label1 for fib(n-1) */
ARM_POP2 (pins, ARMREG_R1, ARMREG_LR); /* pop result fib(n-2) and return address */
ARM_ADD_REG_REG (pins, ARMREG_R0, ARMREG_R0, ARMREG_R1); /* add both results */
ARM_MOV_REG_REG (pins, ARMREG_PC, ARMREG_LR);
/* return address in PC; */
/* and exit to main or previous recursive call */
}
int main (int argc, char *argv[]) {
UINT32 n, ins[500], *pins = ins;
unsigned long (*fib_jit)(int n) = (unsigned long (*)(int n)) ins;
unsigned long r1, r2, t0, t1, t2;
setup_fib_jit (pins);
_armdis_dump (stdout, ins, 52);
if (argc <= 2) {
if (argc == 1)
n=1;
else
n=atoi (argv[1]);
t0 = GetTickCount();
r1 = fib_c (n);
t1 = GetTickCount();
r2 = fib_jit (n);
t2 = GetTickCount();
}
else {
fprintf (stderr, "%s: Wrong number of arguments\n", argv[0]);
exit (-1);
}
printf (" fib_c(%d) result: %d\n\texecution time: %lf\n", n, r1, (t1-t0) / 1000.0);
printf ("fib_jit(%d) result: %d\n\texecution time: %lf\n", n, r2, (t2-t1) / 1000.0);
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Sjoerd Bakker was a 6510 (C64) machine language editor for a Dutch computer magazine in the mid eighties of the previous century.