Why `addi $reg, $0, N` instead of `li $reg N`?

China☆狼群 提交于 2020-12-15 04:30:31

问题


I'm learning MIPS by myself with sources found online* and see a lot of people doing addi $reg, $0, N to store a numerical value N inside a register $reg, and am wondering why is this so ubiquitous when li $reg N would do.

What am I missing ?

*(so don't have exhaustive while accessible explanations of what I learn)


回答1:


Assembler has:

  • directives — .text, .data, .align 2, others
  • labels — main:
  • regular instructions — addi $a0, $0, 1 and beq $a0, $a1, label
  • pseudo instructions — li $a0, 1 and bge $a0, $a1, label
  • storage declarations — .word 100 (some call these directives as well)

.text & .data switch between code and data sections that subsequent lines of assembly intended for.

The regular instructions offer direct correspondence with the hardware machine code language.

Pseudo instructions are instructions that the hardware doesn't really have directly, but can be accomplished with some substitution, which is usually expansion in the number of arguments to another instruction (e.g. adding $0 to the list of arguments), and sometimes even having more than one machine code instructions.  li is easier to read than addi ... $0 ..., so it is preferred — unless your educator has instructed you to avoid pseudo instructions.

If you're using the MARS simulator, you'll find that its help menu details the members of these different categories.

When you write a pseudo instructions, you can see what real machine code instruction(s) the assembler translated them into.


Just like any language, assembly language provides for constant data, like string literals, floating point constants, etc..  It also supports global/static variables.  These are all done using the storage declarations.



来源:https://stackoverflow.com/questions/64471863/why-addi-reg-0-n-instead-of-li-reg-n

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