简体   繁体   English

在MIPS中使用SW和LW访问代码段存储器

[英]Accessing code segment memory using SW and LW in MIPS

Is it possible to access the code segment memory using the SW and LW instructions in MIPS, given the address of the instructions? 给定指令的地址,是否可以使用MIPS中的SWLW指令访问代码段存储器?

For example: 例如:

0x1000: ADDI $s1, $zero, 0x1000
0x1004: LW $s2, 4($s1)

What would the code load into $s2 ? 代码会将什么加载到$s2 0x0000 (given the data segment is empty) or binary representation of the instruction at 0x1004 ? 0x0000 (假设数据段为空)或0x1004指令的二进制表示?

EDIT: 编辑:

AFAIK, pipelining in MIPS processor is possible due to separation of the instructions memory and the data memory - correct me if I'm wrong. AFAIK,由于指令存储器和数据存储器的分离,因此可以在MIPS处理器中进行流水处理-如果我错了,请纠正我。

EDIT 2: 编辑2:

I've found a question , the answer to which implies that the instructions can be accessed and modified using LW and SW . 我发现了一个问题 ,答案表明可以使用LWSW来访问和修改指令。 Thus the answer is $s2 will contain the binary representation of the instruction at 0x1004. 因此,答案是$s2将包含0x1004处指令的二进制表示。

You will load the machine encoding of the instruction at address 0x1004 . 您将在地址0x1004加载指令的机器编码。

MIPS has a flat memory model; MIPS具有平面内存模型; different segments of an executable are mapped / loaded into different parts of a single flat memory address space; 可执行文件的不同被映射/加载到单个平面存储器地址空间的不同部分; it's a Von Neumann architecture where code bytes and instruction bytes are the same thing, and share the same address space. 这是冯·诺依曼(Von Neumann)架构 ,其中代码字节和指令字节是相同的,并且共享相同的地址空间。

Code addresses use the same address-space as data addresses . 代码地址与数据地址使用相同的地址空间 Martin's answer suggests it may be possible to create a MIPS where at least the permissions are different, and of course an embedded MIPS with its code in ROM couldn't modify its instructions with stores. 马丁的回答表明,至少在权限不同的情况下,可以创建一个MIPS,并且其ROM中的代码的嵌入式MIPS当然不能通过存储修改其指令。 But even then code and data would have to be mapped into different parts of the same physical address space, even if stores to code addresses faulted. 但是即使这样,即使将代码和地址存储到错误的地方,也必须将代码和数据映射到同一物理地址空间的不同部分。 Possibly you could build a MIPS where even loads of code addresses faulted, but that's unlikely. 可能您可以构建一个MIPS,其中甚至许多代码地址也会出现故障,但这不太可能。 Jumps to data addresses might also fault if you disabled execute permission on that region / page. 如果在该区域/页面上禁用了执行权限,则跳转到数据地址也可能会出错。

On a normal MIPS with its instruction in RAM, self-modifying code is possible if you have write+exec permissions configured. 在具有指令在RAM中的普通MIPS上,如果配置了write + exec权限, 则可以进行自我修改的代码 (But note that for correctness you would usually need to flush i-cache, which the code in that Q&A isn't doing.) (但是请注意,为了正确起见,通常需要刷新i-cache,而该Q&A中的代码并没有这样做。)


And BTW, .data in the asm source really means the .data section , which the linker eventually links into the data segment of the executable. 和BTW, .data在ASM源的真正含义.data 部分 ,其中Linker最终链接到可执行的数据 See What's the difference of section and segment in ELF file format . 请参阅ELF文件格式的section和segment有什么区别

The most important point here is that segments of an executable aren't the same thing as x86-style segmented memory. 这里最重要的一点是,可执行文件的段与x86样式的段内存不同。 (The terminology has a similar origin, though). (不过,术语有相似的来源)。

Depends on what you mean with "MIPS": 取决于您对“ MIPS”的含义:

  • A real MIPS CPU like you find them in some WLAN routers? 像您这样的真正MIPS CPU在某些WLAN路由器中可以找到它们吗?

  • Some MIPS emulator like SPIM or MARS? 一些MIPS模拟器,例如SPIM或MARS?

In the case of a real MIPS CPU it depends how the memory management unit is configured: 如果是真正的MIPS CPU,则取决于内存管理单元的配置方式:

If the memory management unit allows read access to the code segment you will indeed get the binary representation of the instruction at address 0x1004. 如果内存管理单元允许对代码段的读取访问,则实际上您将在地址0x1004处获得指令的二进制表示。

(By the way: You would need to use addi $s1, $0, 0x1004 to ensure $s1 really contains 0x1004 because $s1 could contain another value than 0 .) (顺便说一句:您将需要使用addi $s1, $0, 0x1004来确保$s1确实包含0x1004因为$s1可能包含另一个值,而不是0

If the memory management unit does not allow access to the code segment the program will crash. 如果内存管理单元不允许访问代码段,则程序将崩溃。 (Most MIPS CPUs seem not to allow this setting.) (大多数MIPS CPU似乎不允许此设置。)

If you use some emulator like SPIM, MARS (or any other one) it depends on how the emulator is working... 如果您使用诸如SPIM,MARS(或其他任何一种)之类的仿真器,则取决于仿真器的工作方式...

Theoretically there could be three types of emulators: 从理论上讲,可以有三种类型的仿真器:

  • Some which crash 一些崩溃
  • Some which read the binary representation 一些读取二进制表示形式
  • Some which read some "stupid" value 有些读一些“愚蠢”的值

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

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