Pages

Friday 14 December 2012

MIPS (BITWISE OPERATION)


MIPS (BITWISE OPERATION)
Komputer yang pertama adalah beroperasi dengan menggunakan full word.
Walaupun begitu, ia lebih mudah bagi komputer untuk beroperasi menggunakan dalam satu word or individual bits berbanding full word.

MIPS bitwise instruction amat berguna untuk masukkan dan mengeluarkan kumpulan yang terdiri daripada bits dalam sesuatu word.


Bawah merupakan table bagi logical instruction:

Logical Operations

C operators
MIPS instructions
Shift left

<< 
sll
Shift right

>> 
srl
Bit-by-bit AND

&
and ; andi
Bit-by-bit OR

|
or ; ori
Bit-by-bit NOT

~
nor
Table 1: Logical Instruction. MIPS implements NOT menggunakan NOR with one operand being 0.

Kelas pertama yang menggunakan operations tersebut dipanggil sebagai shifts. “shift”  boleh menggerakkan semua bits di dalam word ke kiri ataupun ke kanan dan mengisikan 0s untuk menggantikan bits yang digerakkan semasa shift operations.
Terdapat 3 basic MIPS shift instructions: sll, srl, sra.

Shift operation
1. sll (shift left logical operation: bergerak ke kiri dan mengisikan bits yang kosong dengan 0s.)
Sebagai contoh: Register $t1  mempunyai 1410  dan instructions order untuk shift to the left dengan 5.
Original value: 0000 0000 0000 0000 0000 0000 0000 1110 2 = 1410
New value: 0000 0000 0000 0000 0001 1100 0000 2 = 44810
MIPS instruction untuk beroperasi seperti atas  adalah sll $t1,$0,5. (Assumed $0 adalah original value.)
*Shift left logical juga dapat dikira dengan multiply the original number dengan 2dimana i merupakan shifts amount value, that is 5.
Jawapan: 14 x 25 = 44810
(Note: Mesti tambah 0s di belakang selepas shift left operation.)

2. srl (shift right logical operation: bergerak ke kanan dan mengisikan bits yang kosong dengan 0s.)
Operation of Shift right logical ibarat operation of shift left logical dimana cara mereka shift adalah terbalik sahaja.
Sebagai contoh: Register $t1  mempunyai 1671210  dan instructions order untuk shift to the left dengan 5.
Original value: 0000 0000 0000 0000 0100 0001 0100 1000 2 = 1671210
New value: 0000 0000  0000  0000  0000 0010 0000 10102 = 52210
MIPS instruction untuk beroperasi seperti atas  adalah srl $t1,$0,5.(Assumed $0 adalah original value.)
 (Note: Mesti tambah 0s di depan selepas shift right operation.)

3. sra (shift right arithmetic: bergerak ke kanan dengan mengisikan bits yang kosong dengan sign extending.)
Sebagai contoh:
a. Shift right arithmetic by 5 bits(Sign +)
Original value: 0000 0000 0000 0000 0000 1101 0000 1110 2
New value: 0000 0000 0000 0000 0000 0000 0110 1000 2
b. Shift right arithmetic by 5 bits(Sign ˗)
Original value: 0000 0000 0000 0000 0000 1101 0000 1110 2
New value: 1111 1000 0000 0000 0000 0000 0110 1000 2
Secara keseluruhannya, shift instruction adalah seperti berikut:
op

rt
rd
shamt
funct
6 bits
5bits
5 bits
5 bits
5 bits
6 bits
Notice: op = operation code              shamt = shift amount              rt = target register specifier
            rd= destination register specifier                    funct = function field

*shamt field adalah diggunakan untuk shift amount. Oleh itu, value bagi shamt adalah hanya di antara 0 dan 31 (unsigned) dimana 111115 bits adalah bersamaan dengan 3110.

Logical operation
Dalam logical operation, terdapat 2 basic MIPS shift instructions iaitu and dan juga or.
- and: outputs adalah 1 apabila 2 inputs adalah 1.
- or: outputs adalah 1 apabila sekurang-kurangnya satu daripada inputs adalah 1.
a. Sebagai contoh,
Register $t1 mengandungi 0000 0000 0000 0000 0001 1010 1011 0100 dan
register $t2 mengandungi 0000 0000 0000 0000 0100 1011 1000 0101
selepas executing the MIPS instruction and $t0,$t1,$t2,
Jawapan:
Value bagi register $t0 adalah  0000 0000 0000 0000 0000 1010 1000 0100.

b. Dengan menggunakan contoh yang sama,
Register $t1 mengandungi 0000 0000 0000 0000 0001 1010 1011 0100 dan
register $t2 mengandungi 0000 0000 0000 0000 0100 1011 1000 0101
selepas executing the MIPS instruction or $t0,$t1,$t2,
Jawapan:
Value bagi register $t0 adalah  0000 0000 0000 0000 0101 1011 1011 0101.

*Terdapat satu lagi logical operation yang beroperasi secara berlawanan dengan logical operation yang lain iaitu NOT.
NOT mengambil satu operand dan meletakkan 1 in the result apabila satu operand bit is 0 and mengambil satu operand dan meletakkan 0 in the result apabila satu operand bit is 1.
Kalau satu operand adalah 0, kita boleh tafsirkan NOT seperti:-
NOT: A NOR 0 = NOT (A OR 0) = NOT (A)

As reference:

AND: A logical bit-by-bit operation with 2 operands that calculate a 1 only if there is a 1 in both operands.
OR:  A logical bit-by-bit operation with 2 operands that calculate a 1 if there is a 1 in either operands.
NOT: A logical bit-by-bit operation with 1 operands that inverts the bits; that is, it replaces every 1 with 0 and every 0 with 1.
NOR: A logical bit-by-bit operation with 2 operands that calculates the NOT of the OR of the 2 operands, that is it calculates a 1 only if there is a 0 in both operands.


 Written by: SIM FU CHENG(B031210069)

0 comments:

Post a Comment