/*
|
/*
|
* mkremtest.c -- generate remainder test program
|
* mkremtest.c -- generate remainder test program
|
*/
|
*/
|
|
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
|
|
|
|
unsigned myrem(unsigned x, unsigned y) {
|
unsigned myrem(unsigned x, unsigned y) {
|
return (unsigned) ((signed) x % (signed) y);
|
return (unsigned) ((signed) x % (signed) y);
|
}
|
}
|
|
|
|
|
unsigned myremu(unsigned x, unsigned y) {
|
unsigned myremu(unsigned x, unsigned y) {
|
return x % y;
|
return x % y;
|
}
|
}
|
|
|
|
|
unsigned nums[] = {
|
unsigned nums[] = {
|
0x00000000,
|
0x00000000,
|
0x00000001,
|
0x00000001,
|
0x00000002,
|
0x00000002,
|
0x00000003,
|
0x00000003,
|
0x0000FFFC,
|
0x0000FFFC,
|
0x0000FFFD,
|
0x0000FFFD,
|
0x0000FFFE,
|
0x0000FFFE,
|
0x0000FFFF,
|
0x0000FFFF,
|
0x00010000,
|
0x00010000,
|
0x00010001,
|
0x00010001,
|
0x00010002,
|
0x00010002,
|
0x00010003,
|
0x00010003,
|
0x7FFFFFFC,
|
0x7FFFFFFC,
|
0x7FFFFFFD,
|
0x7FFFFFFD,
|
0x7FFFFFFE,
|
0x7FFFFFFE,
|
0x7FFFFFFF,
|
0x7FFFFFFF,
|
0x80000000,
|
0x80000000,
|
0x80000001,
|
0x80000001,
|
0x80000002,
|
0x80000002,
|
0x80000003,
|
0x80000003,
|
0x8000FFFC,
|
0x8000FFFC,
|
0x8000FFFD,
|
0x8000FFFD,
|
0x8000FFFE,
|
0x8000FFFE,
|
0x8000FFFF,
|
0x8000FFFF,
|
0x80010000,
|
0x80010000,
|
0x80010001,
|
0x80010001,
|
0x80010002,
|
0x80010002,
|
0x80010003,
|
0x80010003,
|
0xFFFFFFFC,
|
0xFFFFFFFC,
|
0xFFFFFFFD,
|
0xFFFFFFFD,
|
0xFFFFFFFE,
|
0xFFFFFFFE,
|
0xFFFFFFFF,
|
0xFFFFFFFF,
|
};
|
};
|
|
|
int snums = sizeof(nums) / sizeof(nums[0]);
|
int snums = sizeof(nums) / sizeof(nums[0]);
|
|
|
|
|
struct {
|
struct {
|
char *name;
|
char *name;
|
unsigned (*func)(unsigned x, unsigned y);
|
unsigned (*func)(unsigned x, unsigned y);
|
} ops[] = {
|
} ops[] = {
|
{ "rem", myrem },
|
{ "rem", myrem },
|
{ "remu", myremu },
|
{ "remu", myremu },
|
};
|
};
|
|
|
int sops = sizeof(ops) / sizeof(ops[0]);
|
int sops = sizeof(ops) / sizeof(ops[0]);
|
|
|
|
|
int chooseReg(void) {
|
int chooseReg(void) {
|
int r;
|
int r;
|
|
|
r = (rand() >> 8) % 16;
|
r = (rand() >> 8) % 16;
|
return r + 8;
|
return r + 8;
|
}
|
}
|
|
|
|
|
void chooseRegs(int *r1, int *r2, int *r3, int *r4) {
|
void chooseRegs(int *r1, int *r2, int *r3, int *r4) {
|
*r1 = chooseReg();
|
*r1 = chooseReg();
|
do {
|
do {
|
*r2 = chooseReg();
|
*r2 = chooseReg();
|
} while (*r2 == *r1);
|
} while (*r2 == *r1);
|
do {
|
do {
|
*r3 = chooseReg();
|
*r3 = chooseReg();
|
} while (*r3 == *r2 || *r3 == *r1);
|
} while (*r3 == *r2 || *r3 == *r1);
|
do {
|
do {
|
*r4 = chooseReg();
|
*r4 = chooseReg();
|
} while (*r4 == *r3 || *r4 == *r2 || *r4 == *r1);
|
} while (*r4 == *r3 || *r4 == *r2 || *r4 == *r1);
|
}
|
}
|
|
|
|
|
int newLabel(void) {
|
int newLabel(void) {
|
static int lbl = 1000;
|
static int lbl = 1000;
|
return lbl++;
|
return lbl++;
|
}
|
}
|
|
|
|
|
int main(void) {
|
int main(void) {
|
int i, j, k;
|
int i, j, k;
|
unsigned res;
|
unsigned res;
|
int r1, r2, r3, r4;
|
int r1, r2, r3, r4;
|
int lbl;
|
int lbl;
|
|
|
printf("\tadd\t$7,$0,'.'\n");
|
printf("\tadd\t$7,$0,'.'\n");
|
for (i = 0; i < snums; i++) {
|
for (i = 0; i < snums; i++) {
|
for (j = 0; j < snums; j++) {
|
for (j = 0; j < snums; j++) {
|
if (nums[j] == 0) {
|
if (nums[j] == 0) {
|
continue;
|
continue;
|
}
|
}
|
for (k = 0; k < sops; k++) {
|
for (k = 0; k < sops; k++) {
|
if (nums[i] == 0x80000000 && nums[j] == 0xFFFFFFFF && k == 0) {
|
if (nums[i] == 0x80000000 && nums[j] == 0xFFFFFFFF && k == 0) {
|
continue;
|
continue;
|
}
|
}
|
res = ops[k].func(nums[i], nums[j]);
|
res = ops[k].func(nums[i], nums[j]);
|
chooseRegs(&r1, &r2, &r3, &r4);
|
chooseRegs(&r1, &r2, &r3, &r4);
|
lbl = newLabel();
|
lbl = newLabel();
|
printf("\tadd\t$%d,$0,0x%08X\n", r1, nums[i]);
|
printf("\tadd\t$%d,$0,0x%08X\n", r1, nums[i]);
|
printf("\tadd\t$%d,$0,0x%08X\n", r2, nums[j]);
|
printf("\tadd\t$%d,$0,0x%08X\n", r2, nums[j]);
|
printf("\t%s\t$%d,$%d,$%d\n", ops[k].name, r3, r1, r2);
|
printf("\t%s\t$%d,$%d,$%d\n", ops[k].name, r3, r1, r2);
|
printf("\tadd\t$%d,$0,0x%08X\n", r4, res);
|
printf("\tadd\t$%d,$0,0x%08X\n", r4, res);
|
printf("\tbeq\t$%d,$%d,L%d\n", r3, r4, lbl);
|
printf("\tbeq\t$%d,$%d,L%d\n", r3, r4, lbl);
|
printf("\tadd\t$7,$0,'?'\n");
|
printf("\tadd\t$7,$0,'?'\n");
|
printf("L%d:\n", lbl);
|
printf("L%d:\n", lbl);
|
}
|
}
|
}
|
}
|
}
|
}
|
printf("out:\n");
|
printf("out:\n");
|
printf("\tadd\t$6,$0,0xF0300000\n");
|
printf("\tadd\t$6,$0,0xF0300000\n");
|
printf("out1:\n");
|
printf("out1:\n");
|
printf("\tldw\t$5,$6,8\n");
|
printf("\tldw\t$5,$6,8\n");
|
printf("\tand\t$5,$5,1\n");
|
printf("\tand\t$5,$5,1\n");
|
printf("\tbeq\t$5,$0,out1\n");
|
printf("\tbeq\t$5,$0,out1\n");
|
printf("\tstw\t$7,$6,12\n");
|
printf("\tstw\t$7,$6,12\n");
|
printf("halt:\n");
|
printf("halt:\n");
|
printf("\tj\thalt\n");
|
printf("\tj\thalt\n");
|
return 0;
|
return 0;
|
}
|
}
|
|
|