Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program in MIPS that uses masking to extract a specific nibble (4 consecutive bits, or 1 hex digit). A 32-bit word contains 8
Write a program in MIPS that uses masking to extract a specific nibble (4 consecutive bits, or 1 hex digit). A 32-bit word contains 8 nibbles, numbered 0 (low order) thru 7 (high order). Pseudo-code: int Num, Nibble_num, Nibble. int extract, mask, bit_pos. maskbits = 0x000F; cout << "Enter Number: "; cin >> Num; cout << "Enter Desired Nibble [0-7]: "; cin >> Nibble_num; cout << "Number = " << Num << endl; cout << "Desired Nibble = " << Nibble_num << endl; //-| Prepare mask and move to nibble position. nibblemask = 0x00000000 ori 0x000F; bit_pos = 4 * Nibble_num; mask = shift mask left by bit_pos bits //-| Apply mask to expose nibble, zero all other bits. extract = num AND mask //-| move nibble to low order position: Nibble = shift extract right by bit_pos bits cout << "NIBBLE " << Nibble_num << " = " << Nibble << endl; ----------------------------------------------- MIPS Instructions Needed: ori, and sll $result, $operand, shiftamt // Only for CONSTANT nibble position. sllv $result, $operand, $shiftamtreg similary, srl/srlv Example: Num = 4D87; Nibble_num = 2; maskbits = F mask = 000F; shift left 8 bits ==> mask = 0F00 extract = 4D87 and 0F00 ==> 0D00 Nibble = shift extract right 8 bits ==> 000D output: NIBBLE 2 = 12 ====================================================== TEST CASES: {12843287, 0} {12843287, 2} {3312843287, 5} {12843287, 3} {2147483647, 7} ======================================================
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started