Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of ori in this part of MIPS code?

Tags:

assembly

mips

Can someone explain the use of "ori" here? I know it's bitwise OR, but I don't know how it works or why it's needed here.

 #objective of the program is to add 5 and 7
.data #variable declaration follow this line
.text #instructions follow this line
main:
ori $s0, $zero, 0x5
ori $s1, $zero, 0x7
add $t0, $s0, $s1
li $v0,10 # required for only QtSPIM
syscall # required for only QtSPIM
#end of program
like image 443
athena Avatar asked Aug 14 '14 06:08

athena


1 Answers

  ori $s0, $zero, 0x5
  ori $s1, $zero, 0x7

The two instructions load a constant of 0x05 into register $s0 and 0x07 into register $s1.

MIPS doesn't has an instruction that directly loads a constant into a register. Therefore logical OR with a operand of zero and the immediate value is is used as a replacement. It has the same effect as move. Translated to c-style code these two lines are:

  $s0 = 0 | 0x05;
  $s1 = 0 | 0x07;

You could also use:

  addi $s0, $zero, 0x5
  addi $s1, $zero, 0x7

This does the same thing, but uses add instead of logical or. Translated to code this would be.

  $s0 = 0 + 0x05;
  $s1 = 0 + 0x07;
like image 91
Nils Pipenbrinck Avatar answered Oct 22 '22 22:10

Nils Pipenbrinck