Atralb
Atralb

Reputation: 811

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

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)

Upvotes: 0

Views: 243

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26646

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.

Upvotes: 2

Related Questions