MIPS assembly programming - converting integer to decimal/binary

In summary, the conversation is about creating a MIPS assembly program that can display a given integer in either decimal or binary using only one function with two parameters. The function takes in an integer and a base (either '2' or '10') and the desired outcome is to have the digits in reverse order. Additional code may be needed to handle negative numbers. The person is seeking help with the logic needed to create this function.
  • #1
colerelm
2
0
I'm writing a MIPS assembly program and I'm trying to figure out a way to display a given integer in either decimal or binary using only one function with two parameters. The function takes in two parameters: an integer and a base (either '2' or '10'). I'd like to not edit anything other than just this one function.

Can anyone help me create such a function? I can't wrap my head around the logic needed to do so using assembly language.

Thanks in advance.
 
Technology news on Phys.org
  • #2
You can get the digits in reverse order by repeatedly dividing the integer or the quotient from the previous division by the base (2 or 10), and saving the remainders (into an array or push them onto the stack), which will contain the digits (in reverse order). You'll need some extra code to handle negative numbers as signed numbers.
 
Last edited:

1. How do I convert an integer to decimal in MIPS assembly programming?

To convert an integer to decimal in MIPS assembly programming, you can use the DIV instruction which divides two registers and stores the result in a third register. Simply divide the integer by 10 and store the result in a register, then use the remainder to get the corresponding decimal digit. Repeat this process until the integer becomes 0.

2. Can I convert a decimal number to binary in MIPS assembly programming?

Yes, you can use the DIV instruction to convert a decimal number to binary in MIPS assembly programming as well. Instead of dividing by 10, you would divide by 2 and use the remainder to get the corresponding binary digit. Repeat the process until the decimal number becomes 0.

3. How do I handle negative numbers when converting to decimal/binary in MIPS assembly programming?

In MIPS assembly programming, negative numbers are represented using two's complement notation. To convert a negative number to decimal, you can first check if the most significant bit is 1, and if so, subtract the two's complement from 2^n, where n is the number of bits. To convert to binary, you can use the same method but subtract from 2^(n-1) instead.

4. Is there a faster way to convert integers to decimal/binary in MIPS assembly programming?

Yes, there are other techniques such as using lookup tables or using bitwise operations that can be faster than using the DIV instruction. However, these methods may require more coding and may not be as straightforward as using the DIV instruction.

5. Can I convert a decimal/binary number to ASCII characters in MIPS assembly programming?

Yes, you can use the ADDI instruction to add the ASCII value of '0' to each digit and then store the result in memory. This will convert the decimal/binary number into a string of ASCII characters that can be printed or manipulated in other ways.

Similar threads

  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
10K
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
20
Views
5K
Back
Top