L
luser- -droog
Is there any way to place labels in an array initializer
and have subsequent mentions of that label name
be automatically replaced by the offset within the array
of the first label?
I'm trying to write a little virtual machine interpreter
based on the minimal instruction set described in:
http://www.jucs.org/jucs_2_11/conditional_branching_is_not/Rojas_R.html
And it's going swimingly until I need to branch
in the "assembly" code.
1736(2)12:55 AM:~ 0> cat minvm.c
#include <stdio.h>
#include <stdlib.h>
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
typedef unsigned addr;
typedef unsigned word;
extern addr ip;
extern word acc;
extern word mem[];
void clear (void) { acc = 0; }
void inc (void) { ++acc; }
void load (addr A) { acc = mem[A]; }
void store (addr A) { mem[A] = acc; }
void brz (addr X) { if (!acc) ip = X; }
void (*optab[])() = { clear, inc, load, store, brz };
enum opcode { CLR, INC, LOAD, STORE, BRZ, HALT };
#define CLR(A) CLR, STORE, A
#define INC(A) LOAD, A, INC, STORE, A
#define MOV(A,B) LOAD, A, STORE, B
#define BRZ(A,X) LOAD, A, BRZ, X
#define GOTO(X) CLR, BRZ, X
enum tempvar { T0 = 900, T1, T2, T3, T4, T5 };
#define CMPL(A) CLR(T1), loop: INC(A), BRZ(A,end), INC(T1),
GOTO(loop), end: MOV(T1,A)
addr ip = 0;
word acc;
word mem[1000] = {
CLR(0), /* clear location zero */
MOV(0,1), /* copy into one */
HALT };
int exec (void) {
word inst;
while ((inst = mem[ip++]) != HALT) {
switch (inst) {
case CLR: case INC: optab[inst](); break;
case LOAD: case STORE: case BRZ: optab[inst](mem[ip++]);
break;
default: error("Illegal Instruction");
}
}
return 0;
}
int main (void) {
return exec();
}
and have subsequent mentions of that label name
be automatically replaced by the offset within the array
of the first label?
I'm trying to write a little virtual machine interpreter
based on the minimal instruction set described in:
http://www.jucs.org/jucs_2_11/conditional_branching_is_not/Rojas_R.html
And it's going swimingly until I need to branch
in the "assembly" code.
1736(2)12:55 AM:~ 0> cat minvm.c
#include <stdio.h>
#include <stdlib.h>
void error(char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
typedef unsigned addr;
typedef unsigned word;
extern addr ip;
extern word acc;
extern word mem[];
void clear (void) { acc = 0; }
void inc (void) { ++acc; }
void load (addr A) { acc = mem[A]; }
void store (addr A) { mem[A] = acc; }
void brz (addr X) { if (!acc) ip = X; }
void (*optab[])() = { clear, inc, load, store, brz };
enum opcode { CLR, INC, LOAD, STORE, BRZ, HALT };
#define CLR(A) CLR, STORE, A
#define INC(A) LOAD, A, INC, STORE, A
#define MOV(A,B) LOAD, A, STORE, B
#define BRZ(A,X) LOAD, A, BRZ, X
#define GOTO(X) CLR, BRZ, X
enum tempvar { T0 = 900, T1, T2, T3, T4, T5 };
#define CMPL(A) CLR(T1), loop: INC(A), BRZ(A,end), INC(T1),
GOTO(loop), end: MOV(T1,A)
addr ip = 0;
word acc;
word mem[1000] = {
CLR(0), /* clear location zero */
MOV(0,1), /* copy into one */
HALT };
int exec (void) {
word inst;
while ((inst = mem[ip++]) != HALT) {
switch (inst) {
case CLR: case INC: optab[inst](); break;
case LOAD: case STORE: case BRZ: optab[inst](mem[ip++]);
break;
default: error("Illegal Instruction");
}
}
return 0;
}
int main (void) {
return exec();
}