How Do You Convert Decimal to Binary Using MIPS Code?

  • Thread starter Thread starter hkboy123
  • Start date Start date
  • Tags Tags
    Binary Mips
Click For Summary

Discussion Overview

The discussion revolves around writing MIPS code to convert a decimal number to its binary representation. Participants explore the memory representation of integers and the methods to extract and display binary digits from a decimal value.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant requests assistance in writing MIPS code for decimal to binary conversion.
  • Another participant inquires about the memory representation of the number, suggesting that understanding this is crucial for the conversion process.
  • A third participant clarifies that if the number is a standard word-based integer, it is already in binary form in memory, and outlines a pseudo-code approach for extracting each bit and printing the value.
  • The pseudo-code provided includes details about using bitwise operations, specifically the right shift operator and bitwise AND, to obtain individual bits from the integer.
  • There is a suggestion to allocate space for a string to store the binary digits, with a method described for converting the bit values into character format for display.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific implementation details, as the discussion includes varying approaches and clarifications regarding the representation and extraction of binary digits from integers.

Contextual Notes

The discussion does not resolve the assumptions regarding the platform being used for MIPS coding or the specifics of the integer representation, which may affect the implementation of the proposed methods.

hkboy123
Messages
2
Reaction score
0
Hello. I was simply wondering if anyone has any idea on how to write a MIPS code to convert a decimal number to its binary number. Anyone can help me?
 
Technology news on Phys.org
hkboy123 said:
Hello. I was simply wondering if anyone has any idea on how to write a MIPS code to convert a decimal number to its binary number. Anyone can help me?

What is the memory representation of your number?
 
integer
 
hkboy123 said:
integer

If its a standard word based integer, it is already in binary form in memory, and all you have to do is to get each bit and print the value. I don't know the platform you are using so I'll do pseudo-code:

Assuming unsigned integer:

x = size of word in bits
For i = 0 to x-1
bitarray[x-i-1] = (Word Value >> i) AND 1
Next i

The >> is a right shift operator, and AND is a bitwise AND instruction, not the comparison operator.

Bitarray has an array containing each digit value from left to right as you would read it if it were printed. If you want it the other way just modify the index in the loop. What you would do instead of creating the array, you would basically allocate some space for a string and then the value of the jth character would be equal to:

string[j] = '0' + (((Word Value >> i) AND 1) AND 255).
 

Similar threads

Replies
1
Views
10K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 3 ·
Replies
3
Views
39K
  • · Replies 57 ·
2
Replies
57
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
13
Views
4K
  • · Replies 9 ·
Replies
9
Views
14K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K