Followers

MIPS logic operation

Arithmetic and Logic Operations

Intro

    All arithmetic operation in MIPS uses R-Type instruction format where all 3 operands are registers which consists of the destination (rd), source (rs) and target (rt). In arithmetic and logic operations, small constants are used frequently and made up 50% of operands. For example, ' a = a +5 ' or ' a > 5'.

    There are 3 ways of specifying constant in MIPS:

  •  Store in memory and load them.
  •  Using register $0 that load constant zero.
  •  Using immediate values. 

Logic Operation

1. Addition

     As stated it uses the the R-type instruction format which consists of the opcode and the operands. It has several opcodes such as :

(a) add = addition (R-Type)
(b) addi = add immediate (I-Type)
(c) addiu = add immediate unsigned number (I-Type)
(d) addu = add unsigned number (R-Type)

    For addi and addiu, it doesn't use the R-Type format but instead uses the I-Type format where we use a constant replacing the target(rt) register in R-Type.

Extra Note: 
  • Signed arithmetic are real numbers where  (+)ve and (-)ve counts represented in a 32-bit 2's complement binary digit. They may generate an overflow.
  • Unsigned arithmetic are numbers that are always (+)ve and will never generate an overflow.

2. Subtraction

     Just like addition we also uses the R-Type and the I-type format in subtraction. Examples of instructions are :

(a) sub = subtraction (R-Type)
(b) subi = sub immediate (I-Type)
(c) subu = sub unsigned (R-Type)


3. Multiplication & Division

   For Multiplication and division logic operation, the results obtained may be larger than 32-bits. Thus, we use 2 special 32-bits registers designed specifically for multiplication and division operations. These are called (hi = holds the upper 32-bits number) and (lo = holds the lower 32-bits number).

  Example of MIPS instruction are: 

  (a) mult, $s1, $s2 (multiply register s1 and s2)
  (b) div, $t1, $t2 (divide register t1 by t2)

  To use the hi and lo we uses the instruction code " mflo " or " mfhi ". Examples are:

  (a)  mult, $s1, $s2   # This is the operation 
        mfhi $s3            # Moves the upper 32-bits number of the product into register s3
        mflo $s4            # Moves the lower 32-bits number of the product into register s4

   This is the same for division except for " lo " in division it gets the quotient and for " hi " in divison it gets the remainder. 

  (b)  div, $t1, $t2       # This is the operation
         mflo $t3             # Puts the quotient of the product into register t3
         mfhi $t4             # Puts the remainder of the product into register t4    


4. Shift operation

   There are 3 basic shift operations which are:

   (a) sll ( Shift Left Logical = shifts the 32-bits number to the left and fill up the empty spaces with 0's )
   
     Example:
     Shift left by 4-bits

      1001 0001 1011 1100 0000 1010 0000 1111
                                becomes
      0001 1011 1100 0000 1010 0000 1111 0000

      * The 4-bits number (1001) vanish as we shifted to the left by 4-bits and we add four 0's the fill up the      
         empty space left in the 32-bits number. 

  (b) srl ( Shift Right Logical = shifts the 32-bits number to the right and fill up the empty spaces with 0's)
      
       Example:
       Shift right by 4-bits

         1111 0000 1010 1001 1001 1010 0000 1111
                                      becomes
         0000 1111 0000 1010 1001 1001 1010 0000

       *  This time the 4-bits to the right(1111) vanished as we shifted the number to the right. The empty   
           space is fill with 0's as usual.

  (c) sra (Shift Right Arithmetic = shifts right nad fills empty space with sign extending)

       Example: 
       Shift right arithmetic by 4-bits (sign +)

          1010 0101 1001 1111 1010 0101 0110 1010
                                       becomes
          0000 1010 0101 1001 1111 1010 0101 0110

       * The sign (+) extend means the empty spaces is filled with 0's.

        Example:
        Shift right arithmetic by 4-bits (sign -)
 
           1010 0101 1001 1111 1000 0101 0110 0000
                                         becomes 
           1111 1010 0101 1001 1111 1000 0101 0110 

        * The sign(-) extend means the empty spaces is filled with 1's.


5. logical operation 

    There are 2 types of basic MIPS shift instruction:

    (a) AND = outputs is 1 if and only if both inputs are 1.
    (b) OR = outputs is 1 if at least one of the inputs is 1.
 


 (a) AND 
      
      For AND logical operators we can assume that the 2 numbers in each register multiply each other to get    
      the product. Example:

      and, $s0, $s1, $s2     # Multiply register s1 and s2 and put the results in in s0.

      Let $s1 = 0000 0000 0000 0000 1111 1111 1111 1111
      Let $s2 = 1111 1111 1111 1111 0000 0000 0000 1000

     Thus, the result in $s0 will be = 0000 0000 0000 0000 0000 0000 0000 1000


Extra Note:
  • AND logical operators also uses the R-Type and the I-Type instruction format such as:
          (1) and (R-Type)
          (2) andi (I-Type)



(b) OR

      For OR logical operators we can assume that the 2 numbers in each register add each other to get the   
      product. Example:

      or, $t0, $t1, $t2   # Add register t1 and t2 and put the result in t0

      Let t1 = 0000 0000 0000 0000 0000 0000 0000 1010
      Let t2 = 1111 1010 0110 1111 0001 0000 1100 1010

      Thus, the result in $t0 will be = 1111 1010 0110 1111 0001 0000 1101 0100


Extra Note:
  •  OR logical operators uses the R-Type and I-type instruction formats just like AND.




 


 
      

Thanks for reading, please leave your comment here.

4 comments:

Anonymous said...

Hey I know this is off topic but I was wondering if you knew of any widgets I
could add to my blog that automatically tweet my newest twitter updates.
I've been looking for a plug-in like this for quite some time and was hoping maybe
you would have some experience with something like this. Plwase let me know if you run into anything.

I truly enjoy reading your blog and I look forward to
your new updates.

My web page :: visit here to see h10 hotels offer codes - -

Anonymous said...

Thank you for some other excellent post. Where else could anybody get that type
of info in such an ideal way of writing?

I have a presentation next week, and I am at the look for such
information.

Feel free to visit my blog - socialcrazies.com buy facebook likes

Anonymous said...

Hi there! I could have sworn I've visited this web site before but after browsing through some of the articles
I realized it's new to me. Anyways, I'm certainly happy I came
across it and I'll be book-marking it and checking back often!

Here is my webpage: belinda broido

Unknown said...

obat penghilang bekas luka
pembesar penis
obat pembesar penis

Copyright of Comorarch group.