以下是不起作用的函数:
char * insert_symtab(char *buf)
{
char *ret = (char *)malloc(strlen(buf) + 512);
char *tmp = NULL;
char *repl = NULL;
char obuf[32]; //for converting int to hex
Symtab_struct *cursym;
lineQueue *lq = readlines(buf);
int i;
strcpy(ret, "");
while (!lq->empty())
{
tmp = getlinefromqueue(lq);
for (i = 0; (cursym = symtab->findNodeByNumber(i)) != NULL; i++)
{
sprintf(obuf, "0x%04x", cursym->sym_location);
//printf("-->looking for %s\n", cursym->sym_name);
if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
{
printf("---->Found %s\n", cursym->sym_name);
strncpy (repl, obuf, strlen(cursym->sym_name));
}
}
strcat(ret, tmp);
strcat(ret, "\n");
}
return ret;
}
这样运行时,我得到以下输出:
[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
$start
如果我进去换衣服
if ((repl = strstr(tmp, cursym->sym_name)) != NULL)
to
if ((repl = strstr(tmp, "$start")) != NULL)
我得到以下输出:
[WARN process_includes] Cannot find file included 1test2.asm
[DEBUG build_symtab] Inserting symbol to location 0x0000
[DEBUG build_symtab] Inserting symbol $start to location 0x0008
[DEBUG build_symtab] Inserting symbol $eight to location 0x0008
[WARN in Symtab::insertNode] Redefinition of symbol $start is ignored
---->Found $start
;include '1test2.asm'
:start:
mov a b
add a b
:eight:
:start:
sub a b
0x0000
应该是这样的。附件是整个项目的pastebin链接:
main.cpp:
http://pastebin.com/AiDCbsCt
符号.h:
http://pastebin.com/Kmcn6NzV
符号.cpp:
http://pastebin.com/mxtPL1d2
有什么想法吗?