如何将汇编语言转换为二进制语言?

发布于 2024-09-25 07:45:32 字数 486 浏览 8 评论 0原文

例如:

        .text
        .align 2
        .global main
        .equ val,0x4712         # 16-bit binary code for 0x4712: 0100 0111 0001 0010
                                # Program code starts now
main:                           # This label must be main:, not MAIN: 
        movi r16,val            # WHAT WOULD THIS LINE BE IN BINARY?
        movi r17,0
loop:   addi r17,r17,1
        subi r16,r16,1 
        bne  r16,r0,loop
stop:   br   stop
.end 

For example:

        .text
        .align 2
        .global main
        .equ val,0x4712         # 16-bit binary code for 0x4712: 0100 0111 0001 0010
                                # Program code starts now
main:                           # This label must be main:, not MAIN: 
        movi r16,val            # WHAT WOULD THIS LINE BE IN BINARY?
        movi r17,0
loop:   addi r17,r17,1
        subi r16,r16,1 
        bne  r16,r0,loop
stop:   br   stop
.end 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还在原地等你 2024-10-02 07:45:32

你是说用手?

您可以手动获取微处理器的指令集表,了解寻址模式和其他数据表示问题,然后将其转换为方便的形式,例如十六进制表示法。

然后,您需要使用一些特定于设备的进程(来自磁盘的文件、来自串行线的文件、从带有一堆开关的前面板键入)将该信息获取到设备内存中。

显然,您需要解决各种工具链问题才能将二进制文件放入机器中。如果您只是为了搞笑而这样做,十六进制、铅笔和便笺簿多年来一直适合许多人。

编辑——

你必须知道几件事。

首先是操作码,除了操作码之外,您还需要了解寻址模式。

考虑这个 6502:

LDA #$00
LDA $00
LDA $1234

这是 6502 上的三个不同的指令。

第一个指令将 $00、十六进制的 0 加载到累加器 (A)。 # 符号告诉汇编器您正在使用“立即”寻址模式(6502 共有 13 种寻址模式)。

第二个将位于地址 $0000 的内存位置的值加载到累加器中。在6502上,它具有“零页”模式,因此可以更轻松地从内存的第一页(地址$0000-$00FF)访问内存。

第三个将位于地址 $1234 的内存位置的值加载到累加器中。这是绝对寻址,只需指定您感兴趣的内存的实际地址。

我强调这个示例是因为,乍一看,这三个看起来都一样。但事实上,它们都编译成 3 条不同的指令或操作码。因此,了解程序集告诉您的内容非常重要,以便您可以为处理器选择正确的操作码。

现在,如果您查看 6502 的操作码指南,然后查看查看 LDA 指令,您将看到每条指令的不同二进制值。

因此,在这种情况下,您会得到:

$A9 $00
$A5 $00
$AD $12 $34

这是这 3 条指令的二进制(十六进制)表示形式。

第一个 $A9 用于“立即”寻址模式,第二个 $A5 用于零页寻址,最后 $AD 用于绝对寻址。

另请注意,操作数后面是参数。对于 6502,它们只是跟随在字节流中。不同的处理器做不同的事情。请注意,对于 Absolute,我们有 2 个字节,$12 和 $34,每个字节代表 16 位地址总数的一半。我相信这是正确的,地址的最高有效字节首先出现,但它可能会颠倒($A9 $34 $12)。

这就是手工组装的基础。

其他需要注意的问题包括程序集将加载到什么位置等问题。这将影响标签等物品的价值。

在 6502 中:

label:  LDA #$00
        JMP label

如果您的程序集从地址 $1000 开始,则这将汇编为:

$A9 $00
$4C $10 $00

如果您的程序集从地址 $5555 开始,则:

$A9 $00
$4C $55 $55

看,JMP(跳转)指令 ($4C) 需要跳转到的地址,并且程序集中的标签与其在程序中的位置相关。方便的是,在这种情况下,标签位于最开始。但您可以看到地址是如何编码到最终机器代码中的。

6502 的组装非常简单。现代处理器则不然。现代汇编程序为您做了很多工作,并且您拥有更复杂的 CPU 和更大的指令集,以及诸如对齐问题之类的问题 - 这些都是 6502 所缺少的。但是作为手动汇编程序,您要对所有这些负责那些细微差别。

您的微处理器手册应该会告诉您这些细微差别。但对于现代复杂的 CPU,操作和学习可能并不简单。

不一定要阻止您这样做,但请注意,这可能需要大量工作。

但这是您需要做的事情的本质。

You mean, by hand?

By hand, you grab the instruction set sheet for your microprocessor, understand your addressing modes, and other data representation issues, and then convert that to something convenient, like a hexadecimal notation.

Then you would need to get that information in to the devices memory, using some device specific process (file from disk, file from serial line, keyed in from a front panel with a bunch a switches).

Obviously, there can be all sorts of tool chain issues that you'll need to figure out to get your binary into a machine. If you're just doing it for laughs, hexadecimal, a pencil, and a legal pad have suited many for years.

Edit --

You have to know several things.

First, the op codes, and along with the op codes, you need to know the addressing modes.

Consider this 6502:

LDA #$00
LDA $00
LDA $1234

Those are three different instructions on the 6502.

The first loads the Accumulator (A) with $00, 0 in hex. The # symbol tells the assembler that you're using the "immediate" addressing mode (the 6502 has 13 total addressing modes).

The second load the Accumulator with the value of the memory location located at address $0000. On the 6502, it has a "zero page" mode, so it can more easily access memory from the first page of memory (addresses $0000-$00FF).

The third loads the Accumulator with the value of the memory location located at address $1234. This is absolute addressing, simply specifying the actual address of the memory you are interested in.

I highlight this example because, at a glance, all three of these look the same. But in truth, they all compile to 3 distinct instructions, or opcodes. So, it's important to understand what your assembly is telling you so that you can select the correct opcode for your processor.

Now, if you look at a opcode guide for the 6502, and look up the LDA instruction, you'll see the different, binary values for each instruction.

So, in this case you would get:

$A9 $00
$A5 $00
$AD $12 $34

That's the binary (in hex) representation of those 3 instructions.

The first, $A9, is for the "immediate" addressing mode, the second, $A5, for Zero Page addressing, and finally $AD for Absolute.

Also note that following the operands, are the arguments. For the 6502, they just follow in the byte stream. Different processors do different things. Note, that for the Absolute, we have 2 bytes, $12 and $34, each representing half of the total, 16 bit address. I believe this is correct, that the Most Significant Byte of the address comes first, but it might be reversed ($A9 $34 $12).

So, that's the fundamental of assembling by hand.

Other things to note are issues like what location the assembly will be loaded at. That will affect values for things like your labels.

In 6502:

label:  LDA #$00
        JMP label

If your assembly is starting at the address $1000, this this will assemble to:

$A9 $00
$4C $10 $00

If your assembly is starting at address $5555 then:

$A9 $00
$4C $55 $55

See, the JMP (Jump) instruction ($4C) needs an address to jump to, and the label in your assembly is relative to its location in the program. Conveniently, in this case, the label is at the very beginning. But you can see how the address is coded in to the final machine code.

6502 is EASY (really easy) assembly. Modern processors, well, aren't. The modern assemblers do a lot of work for you, and you have more complicated CPUs with larger instruction sets, and things like alignment issues -- these are all missing from the 6502. But as a hand assembler, you're responsible for all of those nuances.

Your microprocessor manual is supposed to tell you these nuances. But with the modern complicated CPUs, its likely non-trivial to do and learn.

Don't necessarily want to deter you from this, but be aware, it may well be a lot of work.

But this is the essence of what you need to do.

独自←快乐 2024-10-02 07:45:32

我使用过的所有汇编器都有一个功能,可以在执行汇编时将二进制文件包含在代码旁边的输出列表中。

All of the assemblers I've worked with had a feature to include the binary in the output listing next to the code when performing an assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文