MIPS Help (converting numbers to diff bases)

In summary: The idea was to accept any base from 2-36. To do this, you will need to make some changes to your code. First, you will need to use syscall with $v0 set to 8 to read in a string instead of an integer. Then, you will need to go through each character in the string and convert it to an integer based on its ASCII value. For example, if the character is 'A', you will need to subtract 55 to get the integer 10. After converting each character, you can use the converted values to perform your base conversion. This will allow your program to accept any base from 2-36. Additionally, you may need to make some changes to your code to handle bases
  • #1
CsRookie
3
0
Alright, I have a program that converts numbers from base 2-10 and it works just fine but I can not get the program to convert past base 10 whenever i input any char such as 1A it says invalid input. I have tried changing some of the lines in the code but it ends with errors. Right now it is error free for up to base 10. What direction do i need to be looking into in order to get this to convert up to base 36. Thanks in advance for the help! :smile:

Code:
	.data
msg1: .asciiz "Please insert value (A > 0) : "
msg2: .asciiz "Please insert the number system B you want to 
convert to (2<=B<=10): "
#Above sting must be in one line
msg3: .asciiz "\nResult : "
.text
.globl main
main:
addi $s0,$zero,2
addi $s1,$zero,10
getA:

li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
blt $v0,$zero,getA

move $t0,$v0
getB:

li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB

add $t1,$zero,$v0

li $v0,4
la $a0,msg3
syscall

add $a0,$zero,$t0
add $a1,$zero,$t1

jal convert

li $v0,10
syscall

convert:
#a0=A
#a1=B

addi $sp,$sp,-16

sw $s3,12($sp) #counter,used to know
#how many times we will pop from stack
sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp)

add $s0,$zero,$a0
add $s1,$zero,$a1

beqz $s0,end

div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3

add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert        #call convert

end:

lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done
lw $a0,16($sp)
li $v0,1
syscall
done: 
addi $sp,$sp,20
jr $ra   #return
 
Technology news on Phys.org
  • #2
CsRookie said:
Alright, I have a program that converts numbers from base 2-10 and it works just fine but I can not get the program to convert past base 10 whenever i input any char such as 1A it says invalid input. I have tried changing some of the lines in the code but it ends with errors. Right now it is error free for up to base 10. What direction do i need to be looking into in order to get this to convert up to base 36. Thanks in advance for the help! :smile:

Code:
	.data
msg1: .asciiz "Please insert value (A > 0) : "
msg2: .asciiz "Please insert the number system B you want to 
convert to (2<=B<=10): "
#Above sting must be in one line
msg3: .asciiz "\nResult : "
.text
.globl main
main:
addi $s0,$zero,2
addi $s1,$zero,10
getA:

li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
blt $v0,$zero,getA

move $t0,$v0
getB:

li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB

add $t1,$zero,$v0

li $v0,4
la $a0,msg3
syscall

add $a0,$zero,$t0
add $a1,$zero,$t1

jal convert

li $v0,10
syscall

convert:
#a0=A
#a1=B

addi $sp,$sp,-16

sw $s3,12($sp) #counter,used to know
#how many times we will pop from stack
sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp)

add $s0,$zero,$a0
add $s1,$zero,$a1

beqz $s0,end

div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3

add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert        #call convert

end:

lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done
lw $a0,16($sp)
li $v0,1
syscall
done: 
addi $sp,$sp,20
jr $ra   #return

If your number base is larger than 10, it will use letters instead of numerals. So for example, if the base is 16, a possible input number might be AF2. You are using syscall with $v0 set to 5 to read an integer, but AF2 isn't considered an integer. (It has nonnumeric characters.) To input a number in a base larger than 10 you're going to have to read the number in as a string (syscall with $v0 set to 8), and then process the characters in the string.
 
  • #3
Mark44 said:
If your number base is larger than 10, it will use letters instead of numerals. So for example, if the base is 16, a possible input number might be AF2. You are using syscall with $v0 set to 5 to read an integer, but AF2 isn't considered an integer. (It has nonnumeric characters.) To input a number in a base larger than 10 you're going to have to read the number in as a string (syscall with $v0 set to 8), and then process the characters in the string.

I am still having trouble getting this run properly.

I have tried to change syscall and make it so that if the ints are greater than 9 then add 55 to process the ascii code for it but I can not get this to compile correctly.

Does anyone have any examples or links with helpful info?
 
  • #4
CsRookie said:
I am still having trouble getting this run properly.

I have tried to change syscall and make it so that if the ints are greater than 9 then add 55 to process the ascii code for it but I can not get this to compile correctly.
Adding 55 won't work. Suppose you have entered 16 for the base (hexadecimal). If the number string contains the character 'A', you need to convert this to 10 by subtracting 55 from the ASCII value of 'A' (which is 65). If the character is A through F, subtract 55 to get 10 through 15.

If you need to work with bases other than 16 that are larger than 10, you need to adjust things accordingly.

How many different bases are you going to accept in your program?
CsRookie said:
Does anyone have any examples or links with helpful info?
 
  • #5
Mark44 said:
Adding 55 won't work. Suppose you have entered 16 for the base (hexadecimal). If the number string contains the character 'A', you need to convert this to 10 by subtracting 55 from the ASCII value of 'A' (which is 65). If the character is A through F, subtract 55 to get 10 through 15.

If you need to work with bases other than 16 that are larger than 10, you need to adjust things accordingly.

How many different bases are you going to accept in your program?

The idea was to accept any base from 2-36

but I would be perfectly fine getting it to accept bases
2,10,16,36
 
  • #6
So what you need to do in your program is enter the base B (syscall with an integer value).

If 2 <= B <= 10, the input string will consist of decimal digits.
If B >= 11, the input string will consist of decimal digits and alpha characters. If B == 36, the digits will be 0, 1, 2, ..., 8, 9, A, B, C, ..., X, Y, Z, with A == 10, B = 11, ..., Y = 34, Z = 35. What I said before about subtracting 55 to get the value of a digit that is an alpha character still applies.

In base-36, to convert 2AY to decimal, this is 2*(36)^2 + 10 * (36) + 34. The best way to do the conversion is to loop through the characters in the string.
 

What is MIPS?

MIPS (Microprocessor without Interlocked Pipelined Stages) is a reduced instruction set computer (RISC) instruction set architecture (ISA) developed by MIPS Technologies. It is commonly used in embedded systems and is the basis for many other RISC architectures.

How do I convert a number to a different base in MIPS?

To convert a number to a different base in MIPS, you will need to use the MIPS instruction set for arithmetic and logical operations. You can use the DIV or DIVU instruction to divide the number by the desired base, and then use the MFHI or MFLO instruction to retrieve the remainder. Repeat this process until the remainder is 0, and then combine the remainders in reverse order to get the converted number.

What is the difference between a signed and unsigned number in MIPS?

A signed number in MIPS is a number with a sign bit, which indicates whether the number is positive or negative. An unsigned number does not have a sign bit and can only represent positive numbers. In MIPS, signed numbers are represented in two's complement form, while unsigned numbers are represented in binary form.

How do I represent negative numbers in MIPS?

Negative numbers in MIPS are represented in two's complement form. This means that the sign bit is used to indicate whether the number is positive or negative, and the remaining bits represent the magnitude of the number. To convert a negative number to two's complement form, you can take the one's complement of the number (invert all the bits) and then add 1 to the result.

Can I convert a number to a different base without using division in MIPS?

While division is the most common method for converting numbers to different bases in MIPS, it is also possible to do so without using division. This can be achieved by using logical operations such as AND and OR to manipulate the bits of the number. However, this method may be more complex and may not be as efficient as using division.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
Back
Top