构建汇编 [英] Building an assembler

查看:126
本文介绍了构建汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立,我已经建立了一个CPU架构的汇编。该架构是类似的MIPS,但这并不重要。

I need to build an assembler for a CPU architecture that I've built. The architecture is similar to MIPS, but this is of no importance.

我开始使用C#,虽然C ++会比较合适。 (C#意味着更快的开发时间,对我来说)。

I started using C#, although C++ would be more appropriate. (C# means faster development time for me).

我唯一的问题是,我不能跟一个好的设计这种应用。我建立一个2通汇编程序。我知道我需要在每遍做。\\

My only problem is that I can't come with a good design for this application. I am building a 2 pass assembler. I know what I need to do in each pass.\

我已经实现了第一遍,我意识到,如果我要组装线code在同一行......没有错误thrown.This只意味着一件事解析较差的技术。

I've implemented the first pass and I realised that if I have to lines assembly code on the same line ...no error is thrown.This means only one thing poor parsing techniques.

所以全能程序员,汇编的父亲开导我,我应该如何进行。
我只需要支持符号和数据申报。说明有固定的大小。

So almighty programmers, fathers of assembler enlighten me how should I proceed. I just need to support symbols and data declaration. Instructions have fixed size.

请让我知道如果你需要更多的信息。

Please let me know if you need more information.

推荐答案

我已经写了三四个简单的装配。如果不使用解析器生成,我所做的就是建模 S-C汇编,我知道最好的6502。

I've written three or four simple assemblers. Without using a parser generator, what I did was model the S-C assembler that I knew best for 6502.

要做到这一点,我用一个简单的语法 - 一条线是下列之一:

To do this, I used a simple syntax - a line was one of the following:

nothing
[label] [instruction] [comment]
[label] [directive] [comment]

一个标签是一个字母后跟任意数量的字母或数字。

A label was one letter followed by any number of letters or numbers.

这是指令是<&空白GT;<&助记符GT; [操作数]

An instruction was <whitespace><mnemonic> [operands]

一个指令是&LT;&空白GT; .XX [操作数]

A directive was <whitespace>.XX [operands]

注释是*将结束行。

操作数取决于指令和指令。

Operands depended on the instruction and the directive.

指令
.EQ等同于定义常量

Directives included .EQ equate for defining constants

.OR code的设置起始地址

.OR set origin address of code

杂项文件的字节十六进制字符串

.HS hex string of bytes

.AS字节的ASCII字符串 - 除了空格分隔任何 - 无论它开始结束了它

.AS ascii string of bytes - any delimiter except white space - whatever started it ended it

.TF输出目标文件

.BS为n的n个字节储备块存储

.BS n reserve block storage of n bytes

当我写,我写了简单的解析器为每个组件。每当我遇到了一个标签,我把它放在一个表,它的目标地址。每当我遇到了一个标签,我不知道,我标志着指令不完整,把未知的标签一起,需要固定的指令引用。

When I wrote it, I wrote simple parsers for each component. Whenever I encountered a label, I put it in a table with its target address. Whenever I encountered a label I didn't know, I marked the instruction as incomplete and put the unknown label with a reference to the instruction that needed fixing.

在所有的源代码行已经过去了,我通过修复表中查找,并试图找到符号表项时,如果我这样做,我修补了说明。如果没有,那么这是一个错误。

After all source lines had passed, I looked through the "to fix" table and tried to find an entry in the symbol table, if I did, I patched the instructions. If not, then it was an error.

我一直指令的名字,所有操作数有效的寻址模式表。当我的指令,我试图解析每个寻址方式反过来,直到东西的工作。

I kept a table of instruction names and all the valid addressing modes for operands. When I got an instruction, I tried to parse each addressing mode in turn until something worked.

,就应该休息一天也许两个做这件事。

Given this structure, it should take a day maybe two to do the whole thing.

这篇关于构建汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆