程序集PC相对寻址模式 [英] Assembly PC Relative Addressing Mode

查看:230
本文介绍了程序集PC相对寻址模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究数据路径,并且一直在尝试理解分支指令.

I am working on datapaths and have been trying to understand branch instructions.

这就是我的理解.在MIPS中,每条指令均为32位.这是4个字节.因此,下一条指令将相隔四个字节.

So this is what I understand. In MIPS, every instruction is 32 bits. This is 4 bytes. So the next instruction would be four bytes away.

在示例中,我说PC地址是128.我的第一个问题是了解128的含义.我目前的看法是它是内存中的索引,因此128指的是内存中128个字节.因此,在数据路径中总是说要在PC上加4.将4位加到128位中,得出132,但这实际上是现在的132个字节(下一条指令).这就是我理解的方式.

In terms of example, I say PC address is 128. My first issue is understanding what this 128 means. My current belief is that it is an index in the memory, so 128 refers to 128 bytes across in the memory. Therefore, in the datapath it always says to add 4 to the PC. Add 4 bits to the 128 bits makes 132, but this is actually 132 bytes across now (next instruction). This is the way I make sense of this.

在等号分支中,假设偏移量是001的二进制数.我知道必须对扩展名进行签名,因此我将添加零(为便于阅读,我将省略它).然后您将左移两个,得到100.移位的目的是什么?偏移量实际上代表字节吗,向左移动代表位?如果是这样,将其添加到PC对我来说毫无意义.因为如果PC引用字节索引,那么将偏移量左移两位的方法就是将偏移量(以字节数为单位)添加到PC(即字节数).如果PC 128实际上是指128位,即32个字节,那为什么我们只加4以得到下一条指令呢?当说PC + 4时,这实际上意味着增加4个字节吗?

In branch equals, say the offset is a binary number of 001. I know I must sign extend, so I would add the zeroes (I will omit for ease of reading). Then you shift left two, resulting in 100. What is the purpose of the shift? Does the offset actually represent bytes, and shifting left will represent bits? If so, adding this to the PC makes no sense to me. Because if PC refers to byte indeces, then adding the offset shifted left two would be adding the offset in number of bytes to PC that is in number of bytes. If PC 128 actually refers to 128 bits, so 32 bytes, then why do we only add 4 to it to get to the next instruction? When it says PC+4, does this actually mean adding 4 bytes?

我的基本问题是PC相对寻址的工作方式,PC + 4的含义以及偏移量为何偏移2的原因.

My basic issue is with how PC relative addressing works, what the PC+4 means, and why offset is shifted by 2.

推荐答案

左移n位与将数字乘以2 n 是相同的事情.左移2位乘以4.

Shifting left by n bits is the same thing as multiplying the number by 2n. Shifting left 2 bits multplies by 4.

如果您的分支偏移量向左移动2,则意味着您的分支偏移量操作数以整个指令单位而不是字节为单位.因此,具有8个操作数的分支指令意味着跳转8条指令,即32个字节.

If your branch offset is shifted left by 2, that means your branch offset operand is in whole instruction units, not bytes. So a branch instruction with an 8 operand means, jump 8 instructions, which is 32 bytes.

MIPS乘以4,因为指令始终为32位. 32位是4个字节.

MIPS multiplies by 4 because instructions are always 32 bits. 32 bits is 4 bytes.

保证MIPS指令从4均分的地址开始,这意味着PC的低两位保证始终为零,因此所有分支偏移都保证在低两位具有00. .因此,将低两位存储在分支指令中毫无意义. MIPS设计人员正在尝试最大程度地提高分支指令可以达到的范围.

MIPS Instructions are guaranteed to begin at an address which is evenly divisible by 4. This means that the low two bits of the PC is guaranteed to be always zeros, so all branch offsets are guaranteed to have 00 in the low two bits. For this reason, there's no point storing the low two bits in a branch instruction. The MIPS designers were trying to maximize the range that branch instructions can reach.

PC表示程序计数器".程序计数器是当前指令的地址. PC + 4指向当前指令后4个字节的地址.

PC means "Program Counter". The program counter is the address of the current instruction. PC+4 refers to the address 4 bytes past the current instruction.

MIPS分支偏移量相对于分支之后指令的地址.立即操作数为零的分支是无操作的,它分支到分支之后的指令.符号扩展立即操作数为-1的分支会分支回到该分支.

MIPS branch offsets, like most processors, are relative to the address of the instruction after the branch. A branch with an immediate operand of zero is a no-op, it branches to the instruction after the branch. A branch with a sign extended immediate operand of -1 branches back to the branch.

分支目标位于((branch instruction address) + 4 + ((sign extended branch immediate operand) << 2)).

这篇关于程序集PC相对寻址模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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