我最近也在抽空闲时间写这个汇编器[s:223] ,
根据你公布的指令集。
比较简陋,但是命令行的,直接处理写好的汇编文件,生成一个可执行的二进制文件。
初步计划只支持一层宏定义,支持注释,暂时不支持变量,先怎么简单怎么写。。。
才把框架搭起来,业余时间比较少,我自己慢慢来,有成果会更新上来的[s:274] 。
下面是目前的代码,
以前没写过,闭门造车,高手们不要贱笑[s:116] 。
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE;
typedef unsigned char byte;
typedef unsigned char UINT8;
typedef unsigned char u8;
typedef unsigned short UINT16;
typedef unsigned short u16;
typedef unsigned UINT32;
typedef unsigned u32;
/* MAX code length in byte */
#define MAX_CODE_LEN 2
typedef struct
{
char * asm_string;
u8 code_len;
u8 code[MAX_CODE_LEN];
}machine_code_type;
#define MAX_CODE_SIZE 4096
u8 code_output_buff[MAX_CODE_SIZE];
machine_code_type mach_code_table[] =
{
{"NOP", 1, 0x18},
{"MOV A,#XX", 2, 0x00, 0x00},
{"MOV R1,#XX", 2, 0x01, 0x00},
{"MOV R2,#XX", 2, 0x02, 0x00},
{"MOV R3,#XX", 2, 0x03, 0x00},
{"MOV R1,A", 1, 0x19},
{"MOV R2,A", 1, 0x1A},
{"MOV R3,A", 1, 0x1B},
{"MOV [R2,R3],A", 1, 0x10},
{"MOV A,[R2,R3]", 1, 0x08},
{"MOV R1,[R2,R3]", 1, 0x09},
{"MOV R2,[R2,R3]", 1, 0x0A},
{"MOV R3,[R2,R3]", 1, 0x0B},
{"ADD A,R1", 1, 0x20},
{"INC A", 1, 0x28},
{"SUB A,R1", 1, 0x30},
{"DEC A", 1, 0x38},
{"AND A,R1", 1, 0x40},
{"OR A,R1", 1, 0x48},
{"NOT A", 1, 0x50},
{"XOR A,R1", 1, 0x58},
{"JMP [R2,R3]", 1, 0x63},
{"JNC [R2,R3]", 1, 0x60},
{"JNE [R2,R3]", 1, 0x61},
{"JA [R2,R3]", 1, 0x62},
{"SETX", 1, 0x68},
{"CLRX", 1, 0x70},
{"SETZ", 1, 0x69},
{"CLRZ", 1, 0x71},
{"SET B0", 1, 0x6A},
{"SET B1", 1, 0x6B},
{"SET B2", 1, 0x6C},
{"SET B3", 1, 0x6D},
{"SET B4", 1, 0x6E},
{"SET B5", 1, 0x6F},
{"CLR B0", 1, 0x72},
{"CLR B1", 1, 0x73},
{"CLR B2", 1, 0x74},
{"CLR B3", 1, 0x75},
{"CLR B4", 1, 0x76},
{"CLR B5", 1, 0x77},
{"", 0, 0x00, 0x00}
};
#define MAX_ASM_FILE_SIZE (128*1024) // 128KB
u8 asm_buff[MAX_ASM_FILE_SIZE];
/* read file to buff */
int read_asmfile(u8 *buff, char *filename)
{
return 0;
}
/* delete comments in buff */
int delete_comments(u8 *buff)
{
return 0;
}
/* replace definition in buff */
int preprocess(u8 *buff)
{
return 0;
}
/* format ASM line to standard */
int format_asm(u8 *buff)
{
return 0;
}
/* translate asm to machine code */
int translate_asm_to_code(u8 *asm_buff, u8 *code_buff, u32 code_buff_size)
{
return 0;
}
/* write machine code to file */
int binary_output(u8 *code_buff, char *filename)
{
return 0;
}
void usage(char *prog_name)
{
printf("Must input 2 arguments\\n");
printf("%s <asm_filename> <output_name>\\n", prog_name);
}
int main(int argc, char **argv)
{
memset(code_output_buff, 0x0, MAX_CODE_SIZE);
memset(asm_buff, 0x0, MAX_ASM_FILE_SIZE);
if (argc != 3)
{
usage(argv[0]);
return 0;
}
/* 1. read ASM file to buff */
read_asmfile(asm_buff, argv[1]);
/* 2. delete comments */
delete_comments(asm_buff);
/* 3. replace definition (pre-process) */
preprocess(asm_buff);
/* 4. format ASM line */
format_asm(asm_buff);
/* 5. translate ASM to machine code */
translate_asm_to_code(asm_buff, code_output_buff, MAX_CODE_SIZE);
/* 6. write machine code to file */
binary_output(code_output_buff,argv[2]);
return 0;
}
win7下使用MinGW环境编译通过。
200字以内,仅用于支线交流,主线讨论请采用回复功能。