Thread: Register tranfer language interpreter in C

  1. #16
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    why do you need a to "psedo" a CPU instruction set (ASM)? seems kinda pointless to me :\ . why don't you just make a real assembler (lol)?

  2. #17
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    is this what you want (pardon my D )?

    Code:
    /*
    CASM - a "psedo"-asmbler
    by Samuel Fredrickson
    */
    
    import std.c.stdio;
    
    /* global registers - just the basics*/
    char[2] ax;
    char[2] bx;
    char[2] cx;
    char[2] dx;
    
    void mov(char[32] line)
    {
    	if(line[3] != ' ')
    	{
    		printf("ERROR: mov syntax is \'mov [dest],[value]\'\n");
    	}
    	if(line[4] == 'a' && line[5] == 'x')
    	{
    		ax[0] = line[7];
    		ax[1] = line[8];
    		printf("ax = %c%c\n",ax[0],ax[1]);
    		return;
    	}
    	else if(line[4] == 'b' && line[5] == 'x')
    	{
    		bx[0] = line[7];
    		bx[1] = line[8];
    		printf("bx = %c%c\n",ax[0],ax[1]);
    		return;
    	}
    	else if(line[4] == 'c' && line[5] == 'x')
    	{
    		cx[0] = line[7];
    		cx[1] = line[8];
    		printf("bx = %c%c\n",ax[0],ax[1]);
    		return;
    	}
    	else if(line[4] == 'd' && line[5] == 'x')
    	{
    		dx[0] = line[7];
    		dx[1] = line[8];
    		printf("dx = %c%c\n",ax[0],ax[1]);
    		return;
    	}
    }
    
    int main(char[][] args)
    {
    	char *filename;
    	FILE *f;
    	static char buff[32];
    	int i;
    	
    	for(i = 0;i <= args.length;i++)
    	{
    		if(args[i] == "-f")
    		{
    			filename = args[i+1];
    		}
    	}
    	if( (f = fopen(filename,"r")) == null)
    	{
    		printf("ERROR: Could not Open File");
    		return 0;
    	}
    	
    	// get line information
    	while((fgets(buff,32,f)) != null)
    	{
    		if(buff[0] == 'm' && buff[1] ==	'o' && buff[2] == 'v')
    		{
    			mov(buff);
    		}
    	}
    	return 0;
    }
    there are some bugs -- mainly if I try to 'mov' anything else besides ax, it gets the value wrong.

    pardon the D code again -- it shouldn't be that hard to translate to C/C++

  3. #18
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    Oh, okay. If that's all you want, then all you need to do is write a text parser. Input a text file with assembly mnemonics, and opcodes, and do what they say.

    very simple. The parser is the hardest part and that isn't very hard. There are fine examples of parsers elsewhere on this board. I would say you should be able to do that in a week or two without much difficulty.

    No timing issues, no binary, nothing hard.
    It is not the spoon that bends, it is you who bends around the spoon.

  4. #19
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    No timing issues, no binary, nothing hard.
    yeah, im not a genius at programming and I could do something like that. this is a question that if answered might help him to(i also need it answered), what is the best way to parse a file without using alot of switch/if/else statements?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Replies: 0
    Last Post: 04-06-2007, 04:55 PM
  4. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  5. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM