如何使用MIPS加载字 [英] How to use MIPS load word

查看:145
本文介绍了如何使用MIPS加载字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这个问题听起来很愚蠢:),但我有点困惑,有人可以解释为什么代码lw使用lw从内存中加载单词到寄存器,而0x1b430010已经在寄存器$ t1中通过使用luiori?

Maybe this question sound stupid :) but I am abit confused, can someone explain why does the code use lw to load word from memory to register while 0x1b430010 is already in register $t1 by using lui and ori?

lui $t1,0x1b43 
ori $t1,$t1,0x0010 
lui $t2,0xabbb 
ori $t2,$t2,0x8050

lw $t0,0($t1) 
srl $t0,$t0,6 
andi $t0,$t0,0x3

lw $t3,0($t2) 
andi $t3,$t3,0xff9f 
sll $t0,$t0,5 
or $t3,$t3,$t0 
sw $t3,0($t2)

推荐答案

0x1b430010t1中作为数字(32位无符号整数)表示内存地址. lui + ori根据直接在lui/ori指令操作码中编码的部分立即值在t1中建立此常量(每个MIPS指令均编码为32位字,因此这些位的一部分形成了CPU称为lui指令的模式或ori指令,其余的16位IIRC构成该指令要使用的立即值.

0x1b430010 is in t1 as number (32 bit unsigned integer), representing memory address. The lui + ori build this constant in t1 from partial immediate values encoded directly in lui/ori instruction opcodes (each MIPS instruction is encoded as 32 bit word, so part of those bits form a pattern known to CPU as lui instruction, or ori instruction, and the remaining, 16 bits IIRC, form the immediate value to be used by the instruction).

lw $t0,0($t1)完全不同,它将首先使用t1中的值(由常量位移" +0修改-括号前为"0")作为内存地址,即CPU将设置地址总线导线的值设置为0x1b430010,然后它将向存储芯片发出信号,通知它应该使用那些地址线,并从该地址进行存储器加载,从而设置总线数据线上的读取值.一旦内存芯片向CPU发送信号,表明已读取数据并且数据线处于正确状态,CPU会将状态存储为新值t0.

The lw $t0,0($t1) is quite different, it will first use the value in t1 (modified by constant "displacement" +0 - that's that "0" ahead of parentheses) as memory address, i.e. the CPU will set address bus wires to value 0x1b430010, and then it will signal the memory chip it should use those address wires and do the memory load from that address, setting up the read value on bus data-wires. Once the memory chip will signal the CPU, that data were read and data-wires are in correct state, CPU will store that state as new value of t0.

简而言之,它将从地址0x1b430010 的计算机存储器中加载的字加载到寄存器t0中-但是存储在该存储器中的实际值是多少,无法通过简短的代码段看出来.

In short, it will load word from computer memory at address 0x1b430010 into register t0 - but what is the actual value stored in memory there, that's not possible to tell from your short snippet.

JFYI:MIPS上的存储器可按字节寻址(地址总线是28或30位宽?还是全32?可能取决于特定的目标硬件)=地址总线的宽度定义了您可以寻址的最大区域,即,它限制了CPU地址空间中可用的最大可用内存).因此,通过设置特定的地址,您可以读取/修改内存的任何字节(只要您有足够的权限来执行此操作,并且它不是只读内存,也不是任何内存芯片未映射的空白空间).

JFYI: the memory on MIPS is addressable by bytes (address bus is 28 or 30 bits wide? or full 32? Depends probably on particular target HW = the width of address bus defines the maximum area you can address, i.e. it limits maximum possible memory available in the CPU address space). So by setting up particular address, you can read/modify any byte of memory (as long as you have enough privileges to do it, and it's not read-only memory, or void space unmapped by any memory chip).

有一个小问题,加载/存储字指令要求(以简化内存管理单元的硬件设计),内存地址必须按字对齐",即可以被4整除(等于下两位为零") .与读取半字类似,存储器地址必须是半字对齐的(可被2整除=底地址位为零).如果要使用未对齐的数据缓冲区,则必须按单个字节加载(并在四个字节的读取中组成字值),以避免在错误的地址上使用lw导致未对齐的内存访问崩溃.

There's a minor catch, that load/store word instructions require (to keep HW design of memory management unit simpler) the memory address to be "word aligned", i.e. divisible by 4 (equals "bottom two bits are zero"). Similarly to read half-word, the memory address must be half-word aligned (divisible by 2 = bottom address bit zero). If you are working with unaligned data buffers, you have to load it per single bytes (and compose word value out of four byte reads), to avoid crashing on unaligned memory access by using lw on wrong address.

值0x1b430010可以被4整除(底部四个位为零,而两个就足够了,实际上可以被16整除),并且该内存地址可能指向.data区域,因此从lw那里应该没有问题.

The value 0x1b430010 is divisible by 4 (bottom four bits are zero, while two would be enough, this is actually divisible by 16), and that memory address is probably pointing into .data area, so doing lw from there should work without problem.

这篇关于如何使用MIPS加载字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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