MIPS Assembly Labels

末鹿安然 提交于 2020-12-25 04:32:47

问题


Does assembly for MIPS read every label? ignore the task and syntax, I just put something together quick.

add reg3, reg1, $zero
add reg1, reg1, reg2
beq reg1, reg3, BRANCH1      #reg2 contents are zero
bne reg1, $zero, BRANCH2     #reg1 doesn't equal zero
BRANCH1: add returnReg, reg1, $zero
BRANCH2: add returnReg, reg2, $zero
jr returnAddress

would this read line-by-line, including the labels, unless they were jumped over? For instance, would BRANCH1 be executed every single time, unless the contents of reg1 are equal to zero?

From wiki: A label is something to make your life simple. When you reference a piece of your program, instead of having to count lines, you can just give it a name You use this in loops, jumps, and variable names. Labels don't appear in your final code, they're only there for convenience, one of the few perks you'll get from the typical MIPS assembler. It also makes life easy for the assembler, because it can now easily go around relocating and linking code. Don't worry if you don't know what those are, that'll come later.

From this, I take that labels are nothing more than a line reference. Which means that jumping around the code (using bne, beq, jr, j, etc.) is the only way to prevent an instruction on a certain line to be read. Is this correct thinking?


回答1:


Labels are only so you can reference the line with a jump. The CPU itself will only see the machine code. The same is true of any comments in your code. They are only there in the assembler - this is then converted into machine code.

You will need to jump over a line if you don't want it executing.




回答2:


Labels are created for humans to write and read more convenient, the after assembling, they're translated into machine code. You can, of course, don't use any labels, but this will introduce at last two problems:

  1. you need to count A LOT;
  2. once you change somewhere in your assembly program, almost you need to change most of your jr, j, jal, etc, because you're using absolute address of your program.

If you use labels(now they're references to sections in your program), these program will be solved with no extra effort!



来源:https://stackoverflow.com/questions/21652995/mips-assembly-labels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!