简体   繁体   English

通过汇编指令了解寄存器的地址分配

[英]Understanding address assignment to registers via assembly instructions

If I have a CPU/system with the following characteristics...如果我有一个具有以下特征的 CPU/系统...

  • 16 bit architecture (16 bit registers and bus) 16 位架构(16 位寄存器和总线)
  • 8 total registers共 8 个寄存器
  • A set of 64 assembly instructions一套64条汇编指令

And assuming my assembly instructions follow the format...并假设我的组装说明遵循格式......

OPCode (6 bits) + Register (3 bits) + Register (3 bits) + Unused (4 bits)


** Example Instructions (below) **

Assembly: LOAD R1,  R7 (Loads value of address stored in R1 into destination register R7)
Machine: 110000 001 111 0000

Assembly: STORE R1,  R7 (Stores value in R1 into destination address stored in register R7)
Machine: 110001 001 111 0000

These types of instructions make sense to me because all of the required bits fit nicely into a 16 bit format and thus into the instruction register (which hold 16 bits), but I am confused on how one gets the desired address into a register to begin with due to this instruction length constraint?这些类型的指令对我来说很有意义,因为所有必需的位都非常适合 16 位格式,因此可以放入指令寄存器(保存 16 位),但我对如何将所需地址放入寄存器开始感到困惑由于这个指令长度限制?

If an address is 16 bits on this system, it seems to me like I would need more than 16 bits to represent an instruction that would assign an address value to any given register before I could even use something like a LOAD or STORE instruction...如果这个系统上的地址是 16 位,在我看来,我需要超过 16 位来表示一条指令,该指令将一个地址值分配给任何给定的寄存器,然后我什至可以使用诸如 LOAD 或 STORE 指令之类的东西。 .

OPCode (6bits) + destinationRegister (3 bits) + addressLiteral (16 bits) ???

However, something like this would not fit in my 16 bit instruction register.然而,像这样的东西不适合我的 16 位指令寄存器。 What am I not understanding here?我在这里不明白什么? Any help is greatly appreciated, thanks!非常感谢任何帮助,谢谢!

  • Fixed-length instruction sets:定长指令集:

    • LC-3, an 8 register machine, has fixed sized 16-bit instructions: it allows a 9-bit offset in certain 16-bit instructions. LC-3 是一种 8 寄存器机器,具有固定大小的 16 位指令:它允许在某些 16 位指令中存在 9 位偏移。 The 9-bit offset is used as an immediate to form a pc-relative address, from which a full 16-bit value is loaded as data. 9 位偏移量用作立即数以形成相对于 pc 的地址,从该地址加载完整的 16 位值作为数据。 So, the trick there is to locate the full 16-bit value as data, somewhere nearby the code that is using it (eg within +/-256 words).因此,诀窍是将完整的 16 位值作为数据定位在使用它的代码附近的某个位置(例如,在 +/-256 个字内)。

    • MIPS is a 32-bit instruction set, in a 32-bit address space. MIPS 是一个 32 位指令集,位于 32 位地址空间中。 Using two instructions each having 16-bits of immediate value, a full 32-bit address can be composed.使用两条指令,每条指令都有 16 位立即数,可以组成一个完整的 32 位地址。

    • Hack / nand2tetris has 16-bit instructions and has a special form for loading constants/address, the instruction form has one bit that says whether it is an A-type, which then allows 15 bits of constant or address. Hack / nand2tetris 有 16 位指令,并且有一个特殊的形式用于加载常量/地址,指令形式有一个位表示它是否是 A 类型,然后允许 15 位的常量或地址。

    • MARIE, an accumulator machine, has 16-bit fixed length instructions, but only 4k of memory, so allows a 12-bit absolute address embedded in the 16-bit instructions. MARIE 是一个累加器机器,有 16 位定长指令,但只有 4k 的内存,所以允许在 16 位指令中嵌入 12 位绝对地址。

    • PDP-8, an accumulator machine, has 12-bit instructions in a 12-bit address space. PDP-8 是一种累加器机器,在 12 位地址空间中有 12 位指令。 Instructions can directly reference nearby memory (within the same 128 word page as the code), or any thing on the zero page (lowmem, also 128 words).指令可以直接引用附近的内存(与代码相同的 128 字页内),或零页上的任何东西(lowmem,也是 128 字)。

  • Variable length instruction sets often allow a full length immediate following the instruction, such are x86, 68000, others.可变长度指令集通常允许在指令之后立即出现全长,例如 x86、68000 等。 The processors will automatically include the size of such an immediate in the full length of the instruction.处理器将自动将这种立即数的大小包含在指令的全长中。

To go more meta, instruction sets have formats, and the formats within an instruction set will vary to accommodate different kinds of operations, say, 3 reg, vs. 2 reg plus large-ish immediate.为了获得更多元数据,指令集具有格式,并且指令集中的格式会有所不同以适应不同类型的操作,例如 3 reg 与 2 reg 加上 large-ish 立即数。 It all goes to instruction encoding, and part of the idea here is to offer software the features it needs, while also keeping the hardware implementation manageable.这一切都与指令编码有关,这里的部分想法是为软件提供所需的功能,同时保持硬件实现的可管理性。


When designing an ISA, there's a fair amount to think about.在设计 ISA 时,需要考虑很多事情。 One thing is position independent code, which allows code to be loaded anywhere in the address space, and even shared at different locations in different address spaces;一是位置无关代码,它允许代码在地址空间的任何地方加载,甚至在不同地址空间的不同位置共享; ideally code will run without any runtime relocations so the code can be fully immutable.理想情况下,代码将在没有任何运行时重定位的情况下运行,因此代码可以是完全不可变的。 There are also some considerations for dynamically loadable shared libraries (DLLs).动态可加载共享库 (DLL) 也有一些注意事项。

Thus, pc-relative addressing modes are very useful, and, absolute addresses should be confined to data and not allowed in code, unlike LC-3, MARIE, PDP-8.因此,pc-relative 寻址模式非常有用,并且绝对地址应该限制在数据中并且不允许在代码中使用,这与 LC-3、MARIE、PDP-8 不同。

That's correct, in a RISC-like machine with fixed-width instructions the same word size as addresses and registers, it will take multiple instructions to generate an arbitrary constant in a register.没错,在具有与地址和寄存器字长相同的固定宽度指令的类似 RISC 的机器中,需要多条指令才能在寄存器中生成任意常数。

You can do你可以做

  • a PC-relative load on an ISA that has it (like ARM where assemblers can automatically generate a nearby "literal pool" when you write things like ldr r0, =0x1234567 )具有它的 ISA 上的 PC 相关负载(例如 ARM,当您编写诸如ldr r0, =0x1234567之类的东西时,汇编程序可以自动生成附近的“文字池”)
  • add-with-PC like RISC-V auipc 像 RISC-V auipc这样的 PC 插件
  • PC-relative scaled offset like AArch64 adrp (go by pages) / add (fix it up with the offset within page, or use the low 12 bits as an offset within an ldr instruction)与 PC 相关的缩放偏移量,如 AArch64 adrp (逐页)/ add (使用页内的偏移量修复它,或使用低 12 位作为ldr指令中的偏移量)
  • or the classic lui / addi for arbitrary non-address constants, with two immediates of widths that add up to the word size (like MIPS 16+16, or RISC-V high 20 from lui + low 12 from a normal I-type instruction)或用于任意非地址常量的经典lui / addi ,两个立即数的宽度加起来等于字长(如 MIPS 16+16,或来自lui的 RISC-V 高 20 + 来自普通 I 型指令的低 12 )

You typically want one opcode for an instruction format that takes 1 register, and uses the rest as immediate bits, giving you the max space.您通常需要一个指令格式的操作码,该指令格式需要 1 个寄存器,并将其余部分用作立即位,从而为您提供最大空间。

In your case, 6 opcode bits + 1 register would take 9 bits, leaving only 7 immediate bits.在您的情况下,6 个操作码位 + 1 个寄存器将占用 9 个位,只剩下 7 个立即位。

So that's not very much, not enough to even generate a 16-bit constant in 2 instructions even with a 2nd opcode that ADDs or ORs into the bottom of one register.所以这不是很多,甚至不足以在 2 条指令中生成一个 16 位常量,即使使用添加或 OR 到一个寄存器底部的第二个操作码也是如此。 Unless you want to sacrifice multiple opcodes (eg use the low couple bits of an opcode as extra immediate bits), that's not very good.除非您想牺牲多个操作码(例如,使用操作码的低位作为额外的立即位),否则这不是很好。

So you might want to use PC-relative loads as the primary way to generate large constants.因此,您可能希望使用 PC 相对负载作为生成大常量的主要方式。 (So one opcode, 1 register, leaving 7 bits of offset, maybe scaled by 2 so it's word-aligned). (所以一个操作码,1 个寄存器,留下 7 位偏移量,可能按 2 缩放,所以它是字对齐的)。

Or a special instruction that reads the whole next instruction word as an immediate.或者一条特殊指令,将整个下一个指令字作为立即数读取。 Decoding could consider this instruction like a jump over the data as well as a load of that data.解码可以将此指令视为跳过数据以及加载该数据。 (In a simple scalar pipelined design, maybe pulling it out of the fetch stage and sending a NOP down the rest of the pipe. It would need a bunch of special cases, and maybe have performance potholes if your pipeline's hazard detection still looks at it before replacing with NOP. And puts an extra muxer or AND gate in the path instruction bits take in the decode stage.) I don't know if any real ISAs actually do this; (在一个简单的标量流水线设计中,可能将其从获取阶段拉出并在管道的其余部分发送一个 NOP。它需要一堆特殊情况,并且如果您的管道的危险检测仍在查看它,可能会有性能坑洼在用 NOP 替换之前。并在路径指令位中放置一个额外的多路复用器或 AND 门进入解码阶段。)我不知道是否有任何真正的 ISA 真的这样做; some 32-bit ISAs with 16-bit compressed instructions (like ARM Thumb mode or RV32c) have variable-width instructions that are either 2 or 4 bytes, signalled by some easy-to-decode bits in the first 2-byte chunk.一些具有 16 位压缩指令的 32 位 ISA(如 ARM Thumb 模式或 RV32c)具有 2 或 4 字节的可变宽度指令,由前 2 字节块中的一些易于解码的位发出信号。

I am assuming this is a fictional instruction set architecture since based of the question it doesn't seem real.我假设这是一个虚构的指令集架构,因为基于这个问题它似乎并不真实。

Addresses are commonly loaded from the program using "immediate" style instructions and therefore would require an instruction outside of the form of "op + reg1 + reg2 + padding"地址通常使用“立即”样式指令从程序加载,因此需要“op + reg1 + reg2 + padding”形式之外的指令

For example, in MIPS you can use Load Upper Immediate and Load Immediate to load a 32 bit value into a register with two 32 bit instructions.例如,在 MIPS 中,您可以使用 Load Upper Immediate 和 Load Immediate 将 32 位值加载到具有两条 32 位指令的寄存器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM