Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CsRookie

macrumors newbie
Original poster
Apr 13, 2011
17
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! :):apple:


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
 
Last edited:
Not being familiar with MIPS assembler I'm going throw out that you try changing 'addi $s1,$zero,10' to 'addi $s1,$zero, 36'. That's assuming the add immediate supports immediate values less than 256. As well as changing the range denoted in 'msg2'.
 
Not being familiar with MIPS assembler I'm going throw out that you try changing 'addi $s1,$zero,10' to 'addi $s1,$zero, 36'. That's assuming the add immediate supports immediate values less than 256. As well as changing the range denoted in 'msg2'.

I tried your suggestion, it still will not output a value with a char such as 1A with 16 base etc. Thanks for the suggestion even though you were unfamiliar with mips.

Anyone else have any ideas?:confused:

:apple:
 
Your problem occurs with the syscall to 'print_int' with this section of code:

Code:
	lw		$a0,16($sp)
	li		$v0,1			#print_int
	syscall

which limits the values of an 'int' to 0-9.

You'll need to write your own output routine that builds a string buffer containing the converted number, ascii '0'-'Z' and then do a syscall to 'print_string' to output the results.
 
Your problem occurs with the syscall to 'print_int' with this section of code:

Code:
	lw		$a0,16($sp)
	li		$v0,1			#print_int
	syscall

which limits the values of an 'int' to 0-9.

You'll need to write your own output routine that builds a string buffer containing the converted number, ascii '0'-'Z' and then do a syscall to 'print_string' to output the results.

Thank you for the help!!:apple:
 
I still have not been able to get this to compile correctly, I have tried making it so that when numbers are greater than 9 make it add 55 to get ascii code etc but I can not get it to compile and run. Does anyone have an example of what this should be looking like?

Thank you in advance :apple:
 
I still have not been able to get this to compile correctly, I have tried making it so that when numbers are greater than 9 make it add 55 to get ascii code etc but I can not get it to compile and run. Does anyone have an example of what this should be looking like?

A. Post your code.


B. Have you designed what the algorithm should look like at a high level?

For example, what does it look like in C or some other higher level language?
If the algorithm is wrong, then coding it in assembly language will still be wrong. If the algorithm is right, then transcoding it from higher-level into assembly language shouldn't be that hard. Also, if you code it first in C, you can compile it and test it to make sure it works.
 
Again I have no working experience with the MIPS instruction set nor its assembler language. I have looked over an on-line reference manual on both and offer the following slightly modified code which I believe should work as desired.

Code:
    .data

msg1:
    .asciiz "Please insert value (A > 0) : "

msg2:
    .asciiz "Please insert the number system B you want to convert to (2<=B<=36): "

msg3:
    .asciiz "\nResult: "

digits:
    .asciiz "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

conversion_buffer:
    .space  2

    .text
    .globl main

#
# Program entry point
#
main:
    addi    $s0,$zero,2     # lower boundary condition
    addi    $s1,$zero,36    # upper boundary condition

# output message to console
getA:
    li      $v0,4           #print_string
    la      $a0,msg1        #"Please insert value (A > 0) : "
    syscall

# input user response
    li      $v0,5           #read_int
    syscall
    blt     $v0,$zero,getA
    
    move    $t0,$v0

# output message to console
getB:
    li      $v0,4           #print_string
    la      $a0,msg2        #"Please insert the number system B you want to convert to (2<=B<=10): "
    syscall

# input user response
    li      $v0,5           #read_int
    syscall
    blt     $v0,$s0,getB
    bgt     $v0,$s1,getB
    
    add     $t1,$zero,$v0
    
# output message to console
    li      $v0,4           #print_string
    la      $a0,msg3        #"\nResult: "
    syscall
    
    add     $a0,$zero,$t0
    add     $a1,$zero,$t1
    
    jal     convert
    
    li      $v0,10          #exit
    syscall
    

#
# Subroutine - convert, and output, value A to number base B
#
#a0=A
#a1=B

convert:
    addi    $sp,$sp,-16     #reserve space on stack for 'locals'
    
    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)      #???return address???
    
    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 (self) convert
    
end:
    lw      $ra,0($sp)
    lw      $s1,4($sp)
    lw      $s0,8($sp)
    lw      $s3,12($sp)
    beqz    $s3,done        #check if anymore 'numbers' to print, if not skip don't print

# output integer to console
    la      $a0,digit
    lw      $t4,16($sp)
    add     $a0,$a0,$t4
    lb      $t4,($a0)
    la      $a0,conversion_buffer
    sb      $t4,($a0)
    li      $v0,4           #print_string
    syscall
done: 
    addi    $sp,$sp,20
    jr      $ra             #returnz

EDIT:

After thinking about your post some more I found your request more and more confusing.

The code you presented converts a base 10 number to another number base system that is specified with a base 10 number. I modified it to provide conversions given in base 10 numbers to bases less than, or equal to, 36.

Your question could be interpreted several ways so I ask what EXACTLY is it you wish to do?
 
Last edited:
No responce from the original poster but I really had to know if my attempt at a solution worked so I searched around and found a MIPS simulator, MARS (Mips Assembler Runtime Simulator) to try it out against. MARS is a JAVA application and worked just fine under the current version of Mac OS.

I was able to paste, assemble and run the above posted code (based upon this info) which worked as I expected but may not be the solution the original poster was really looking for. Then again perhaps it was homework and he had no intention of acknowledging the provided solution regardless.

Oh well!
 
Again I have no working experience with the MIPS instruction set nor its assembler language. I have looked over an on-line reference manual on both and offer the following slightly modified code which I believe should work as desired.

Code:
    .data

msg1:
    .asciiz "Please insert value (A > 0) : "

msg2:
    .asciiz "Please insert the number system B you want to convert to (2<=B<=36): "

msg3:
    .asciiz "\nResult: "

digits:
    .asciiz "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

conversion_buffer:
    .space  2

    .text
    .globl main

#
# Program entry point
#
main:
    addi    $s0,$zero,2     # lower boundary condition
    addi    $s1,$zero,36    # upper boundary condition

# output message to console
getA:
    li      $v0,4           #print_string
    la      $a0,msg1        #"Please insert value (A > 0) : "
    syscall

# input user response
    li      $v0,5           #read_int
    syscall
    blt     $v0,$zero,getA
   
    move    $t0,$v0

# output message to console
getB:
    li      $v0,4           #print_string
    la      $a0,msg2        #"Please insert the number system B you want to convert to (2<=B<=10): "
    syscall

# input user response
    li      $v0,5           #read_int
    syscall
    blt     $v0,$s0,getB
    bgt     $v0,$s1,getB
   
    add     $t1,$zero,$v0
   
# output message to console
    li      $v0,4           #print_string
    la      $a0,msg3        #"\nResult: "
    syscall
   
    add     $a0,$zero,$t0
    add     $a1,$zero,$t1
   
    jal     convert
   
    li      $v0,10          #exit
    syscall
   

#
# Subroutine - convert, and output, value A to number base B
#
#a0=A
#a1=B

convert:
    addi    $sp,$sp,-16     #reserve space on stack for 'locals'
   
    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)      #???return address???
   
    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 (self) convert
   
end:
    lw      $ra,0($sp)
    lw      $s1,4($sp)
    lw      $s0,8($sp)
    lw      $s3,12($sp)
    beqz    $s3,done        #check if anymore 'numbers' to print, if not skip don't print

# output integer to console
    la      $a0,digit
    lw      $t4,16($sp)
    add     $a0,$a0,$t4
    lb      $t4,($a0)
    la      $a0,conversion_buffer
    sb      $t4,($a0)
    li      $v0,4           #print_string
    syscall
done:
    addi    $sp,$sp,20
    jr      $ra             #returnz

EDIT:

After thinking about your post some more I found your request more and more confusing.

The code you presented converts a base 10 number to another number base system that is specified with a base 10 number. I modified it to provide conversions given in base 10 numbers to bases less than, or equal to, 36.

Your question could be interpreted several ways so I ask what EXACTLY is it you wish to do?

Hello, could you explain why you are using div and rem? I'm working on something similar to this and I would like to understand it better. Also, this code basically prints each decimal integer, as individual characters in a string?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.