使用yyparse()创建一个两遍汇编程序?

时间:2021-10-24 09:43:14

I'm writing an assembler for a custom micro controller I'm working on. I've got the assembler to a point where it will assemble instructions down to binary.

我正在为我正在研究的自定义微控制器编写汇编程序。我有汇编程序,它将指令汇编为二进制。

However, I'm now having problems with getting labels to work. Currently, when my assembler encounters a new label, it stores the name of the label and the memory location its referring to. When an instruction references a label, the assembler looks up the label and replaces the label with the appropriate value.

但是,我现在遇到了使标签生效的问题。目前,当我的汇编程序遇到新标签时,它会存储标签的名称及其引用的内存位置。当指令引用标签时,汇编器会查找标签并用适当的值替换标签。

This is fine and dandy, but what if the label is defined after the instruction referencing it? Because of this, I need to have my parser run over the code twice.

这很好,花花公子,但如果标签是在引用它的指令后定义的呢?因此,我需要让我的解析器在代码上运行两次。

Here's what I currently have for my main function:

这是我目前的主要功能:

303 int main(int argc, char* argv[])
304 {
305 
306     if(argc < 1 || strcmp(argv[1],"-h")==0 || 0==strcmp(argv[1],"--help"))
307     {
308         //printf("%s\n", usage);
309         return 1;
310     }
311     // redirect stdin to the file pointer
312     int stdin = dup(0);
313     close(0);
314 
315     // pass 1 on the file
316     int fp = open(argv[1], O_RDONLY, "r");
317     dup2(fp, 0);
318 
319     yyparse();
320 
321     lseek(fp, SEEK_SET, 0);
322 
323     // pass 2 on the file
324     if(secondPassNeeded)
325     {
326         fp = open(argv[1], O_RDONLY, "r");
327         dup2(fp, 0);
328         yyparse();
329     }
330     close(fp);
331 
332     // restore stdin
333     dup2(0, stdin);
334 
335     for(int i = 0; i < labels.size(); i++)
336     {
337         printf("Label: %s, Loc: %d\n", labels[i].name.c_str(), labels[i].memoryLoc);
338     }
339     return 0;
340 }

I'm using this inside a flex/bison configuration.

我在flex / bison配置中使用它。

1 个解决方案

#1


If that is all you need, you don't need a full two-pass assembler. If the label is not defined when you reference it, you simply output a stand-in address (say 0x0000) and have a data structure that lists all of the places with forward references and what symbol they refered to. At the end of the file (or block if you have local symbols), you simply go through that list and patch the addresses.

如果这就是您所需要的,则不需要完整的双通道汇编程序。如果在引用标签时未定义标签,则只需输出一个替代地址(例如0x0000),并具有一个数据结构,列出所有具有前向引用的位置以及它们所引用的符号。在文件的末尾(或者如果您有本地符号则阻止),您只需浏览该列表并修补地址即可。

#1


If that is all you need, you don't need a full two-pass assembler. If the label is not defined when you reference it, you simply output a stand-in address (say 0x0000) and have a data structure that lists all of the places with forward references and what symbol they refered to. At the end of the file (or block if you have local symbols), you simply go through that list and patch the addresses.

如果这就是您所需要的,则不需要完整的双通道汇编程序。如果在引用标签时未定义标签,则只需输出一个替代地址(例如0x0000),并具有一个数据结构,列出所有具有前向引用的位置以及它们所引用的符号。在文件的末尾(或者如果您有本地符号则阻止),您只需浏览该列表并修补地址即可。