MIPS中括号的作用是什么? [英] What is the function of parentheses in MIPS?

查看:489
本文介绍了MIPS中括号的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一本项目书,作为对MIPS的介绍,但遇到了一个问题.本书中的代码行之一是lb $t3, ($t2).我不知道括号是做什么的,因为在此之前,我还没有看到它们的用处,而书中只是没有提到它们.为什么代码不只是lb $t3, $t2?

I've been working through a project book as an introduction to MIPS and I've come across a problem. One of the lines of code in the book is lb $t3, ($t2). I have no clue what the parentheses do because prior to this, I haven't seen them used, and the book just doesn't mention them to begin with. Why wouldn't the code just be lb $t3, $t2?

推荐答案

MIPS寻址模式语法为constant($reg) .

MIPS addressing-mode syntax is constant($reg).

($t2)作为0($t2)的特殊情况简写.相同的指令可以做
lb $t3, 13($t2)将地址13 + $t2中的内存中的一个字节加载(并符号扩展)到寄存器$t3中.

($t2) is allowed as a special case short-hand for 0($t2). The same instruction could do
lb $t3, 13($t2) to load (and sign-extend) a byte from memory at address 13 + $t2 into register $t3.

MIPS的唯一寻址模式是reg + sign_extended_imm16;加载/存储指令(包括lblbu)是I型的.省略0 in 0($ t2)只是源代码级语法.机器代码仍然有16个零位来编码该寻址模式.

MIPS's only addressing mode is reg + sign_extended_imm16; load / store instructions (including lb and lbu) are I-type. Omitting the 0 in 0($t2) is just a source-level syntax nicety; the machine code still has 16 zero bits to encode that addressing mode.

(顺便说一句,我忽略了使用2个整数寄存器的MIPS索引FP加载/存储指令(如lwxc1)的存在.它们仅用于FP

(BTW, I'm ignoring the existence of MIPS indexed FP load/store instructions like lwxc1 that use 2 integer registers. They're FP-only perhaps because an integer store version would need 3 GP registers as inputs, but that register file otherwise only needs 2 read ports (for a scalar pipeline). There's also lwpc for PC-relative addressing in some newer MIPS revisions. But lw / lb / lbu and so on have no bits to encode what type of addressing mode; it's always reg + imm16. Anything else requires a different instruction, and classic MIPS didn't have anything else.)

语义含义(对于asm文本语法的人类读者而言)是一种取消引用操作,例如在C int t3 = *t2;t2[0]中加载指向的值(lb)而不是int tmp = (int)p将指针复制为整数(move).

The semantic meaning (for human readers of asm text syntax) is a dereference operation, like in C int t3 = *t2; or t2[0] loads the pointed-to value (lb) instead of int tmp = (int)p copying the pointer as an integer (move).

()始终是一个内存操作数,只能与装入或存储指令一起使用.裸寄存器不是内存寻址模式,不能用作第二个操作数来加载或存储指令.

() is always a memory operand and can only be used with load or store instructions. Bare registers are not memory addressing modes, and can't be used as the 2nd operand to load or store instructions.

这是一件好事,加载/存储指令在寻址模式下需要parens,因此您不会混淆哪个操作数是地址,哪个值是值.例如sw $t3, 12($t2)$t3中的字存储到地址12+$t2中的存储字中.如果sw $t3, $t2是有效的语法,您可能会忘记,即使其他所有指令都将目标作为第一个操作数,MIPS上的存储器指令的地址也总是正确的(就像大多数RISC一样).

It's a good thing that load / store instructions require parens on the addressing mode, so you don't get mixed up on which operand is the address and which is the value. e.g. sw $t3, 12($t2) stores the word in $t3 into a word of memory at the address 12+$t2. If sw $t3, $t2 was valid syntax, you might forget that the address is always on right for memory instructions on MIPS (like most RISCs), even though every other instruction has the destination as the first operand.

这使它在视觉上与move $t3, $t2有所区别.能够在视觉上做到这一点,很好地发现了负载和存储在代码块中.

This makes it more visually distinct from move $t3, $t2. Spotting the loads and stores in a block of code is nice to be able to do visually.

如果您正在为MIPS汇编语言设计自己的语法或编写汇编器,则可以合法地编写lb $t3, $t2作为lb $t3, 0($t2)的简写.但是asm语法是由汇编程序的(作者)定义的,MIPS语法的设计者决定不这样做.

If you were designing your own syntax for MIPS assembly language, or writing an assembler, you could make it legal to write lb $t3, $t2 as a short-hand for lb $t3, 0($t2). But asm syntax is defined by (the authors of) the assembler, and the designers of MIPS syntax decided not to do that.

在此之前,我还没有看到它们被使用过,而书中只是没有提到它们.

prior to this, I haven't seen them used, and the book just doesn't mention them to begin with.

继续阅读;希望本书在首次介绍新语法后能继续解释新语法.教程或书籍向您展示一些代码作为示例而又不停地解释所有内容是完全正常的,尤其是当解释是基于尚未完成的概念时.

Keep reading; hopefully the book goes on to explain new syntax some time after first introducing it. It's totally normal for a tutorial or book to show you some code as an example without stopping to explain everything, especially when the explanation is based on concepts it hasn't got to yet.

这篇关于MIPS中括号的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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