[c] Difference between "move" and "li" in MIPS assembly language

I was practicing converting C code into MIPS assembly language, and am having trouble understanding the usage of move and li in variable assignment.

For example, to implement the following C line in MIPS:

int x = 0;

If I understand it correctly (I highly doubt this, though), it looks like both of these work in MIPS assembler:

move $s0, $zero
li $s0, $zero

Am I wrong? What is the difference between these two lines?

This question is related to c assembly mips

The answer is


The move instruction copies a value from one register to another. The li instruction loads a specific numeric value into that register.

For the specific case of zero, you can use either the constant zero or the zero register to get that:

move $s0, $zero
li   $s0, 0

There's no register that generates a value other than zero, though, so you'd have to use li if you wanted some other number, like:

li $s0, 12345678

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to assembly

Why does C++ code for testing the Collatz conjecture run faster than hand-written assembly? While, Do While, For loops in Assembly Language (emu8086) Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPUs How to run a program without an operating system? Difference between "move" and "li" in MIPS assembly language Carry Flag, Auxiliary Flag and Overflow Flag in Assembly How do AX, AH, AL map onto EAX? JNZ & CMP Assembly Instructions Difference between JE/JNE and JZ/JNZ The point of test %eax %eax

Examples related to mips

Difference between "move" and "li" in MIPS assembly language MIPS: Integer Multiplication and Division Mips how to store user input string Error "gnu/stubs-32.h: No such file or directory" while compiling Nachos source code How to Calculate Jump Target Address and Branch Target Address?