Chocode
Chocode

Reputation: 157

MIPS converting numbers

I'm currently building a program to convert decimal number to binary number (32-bit, with left-padded zeros and a space per 4 numbers) since I'm not up to the level to implement it immediately, I'm just planning the whole procedure in pseudocode.

Here's what I have so far (two ways):

first one:

list.   space 128 #create an array for 32 integers
beq i=32-1, -1   #if the last index of array is <0
div n, 2      #n is the input number
list[i]= mfHi #store remainder in the last index
n = mfhi #quotient would be the new n
i--

for this case, after this loop, other then the converted binary part, other indexes in the array will be filled with zeros because the number is being divided 32 times in total.

The second way is to store it in a string, make a loop that iterates until n becomes 0, and keep dividing n by 2. And after all, we reverse the string and pad zeroes.

Are these two solutions implementable? or should I change the way. Any comments are welcomed with thanks.

Upvotes: 0

Views: 1158

Answers (3)

nissim abehcera
nissim abehcera

Reputation: 831

Your code is working now .

.data 
list: .space 128
enter: .asciiz "Enter an integer: " 
ipnum: .asciiz "Input number is " 
Bin: .asciiz "Binary: " 
Qtr: .asciiz "Quaternary: " 
Oct: .asciiz "Octal:" 
new: .asciiz "\n" 



.text
.globl main

main:
  la $a0, enter  #load address into $a0
   li $v0, 4      #call to print string
   syscall

   li $v0, 5       #read integer
   syscall
   move $s0, $v0   #inter stored in $s0 $t0 isn't saved registry 

  la $a0, new      #print two new lines
  li $v0, 4
  syscall

  la $a0, new
  li $v0, 4
  syscall



  la $a0, ipnum    #print the string
 li $v0, 4
 syscall

 li $v0, 1       #print the input number
  move $a0, $s0
  syscall

  move $s1,$a0    # remove input to $s1 registry, $s0 is occupied
la $a0, new      #print two new lines
li $v0, 4
syscall

la $a0, new
li $v0, 4
syscall

la $a0, Bin    #print "Binary:"
li $v0, 4
syscall

#binary:          # not here because index i restart from 0 all the time
 li $s0, 0      #index i

 li $t1, 2      #assign 2 into t1
binary:
  div $s1, $t1   #
   mfhi $t2       #set remainder as t2

   sw $t2, list($s0) #save remiander to the ith index of array  list + $s0 value            as offset

  mflo $s1       #input number now become quotient

addi $s0, $s0, 4 #i = i+4, since dealing with integer

beq $s1, $zero, fill_array

j binary


fill_array:    #before printing the array, it's necessary to fill 0 the remainder   places (array)
  beq $s0,504,next
  sw $zero,list($s0)
  addiu $s0,$s0,4
  j fill_array

next:
  li $a1, 504     #set $a1 as 504 to print last index of myArray.No 128 because    126*4=504 is a last index
#print backwards from the last indext of the array

print:
    beq $a1, -4, exit #if finished printing until list[0]



  lw $t3, list($a1)

   li $v0 1        #print out the number
   move $a0, $t3
   syscall

  addi $a1, $a1, -4 #i = i-4


  j print

 exit:
   li $v0, 10
   syscall

Upvotes: 0

Chocode
Chocode

Reputation: 157

Thanks for answering, it seems like you have used stack to implement this. However, I was having a hard time to comprehend the code since I just started learning MIPS today..Here's what I have tried to implement using array. Too many errors... .data enter: .asciiz "Enter an integer: " ipnum: .asciiz "Input number is " Bin: .asciiz "Binary: " Qtr: .asciiz "Quaternary: " Oct: .asciiz "Octal:" new: .asciiz "\n" list: .space 128

    .text
    .globl main

main:
    la $a0, enter  #load address into $a0
    li $v0, 4      #call to print string
    syscall

    li $v0, 5       #read integer
    syscall
    move $t0, $v0   #inter stored in $t0

    la $a0, new      #print two new lines
    li $v0, 4
    syscall

    la $a0, new
    li $v0, 4
    syscall



    la $a0, ipnum    #print the string
    li $v0, 4
    syscall

    li $v0, 1       #print the input number
    move $a0, $t0
    syscall

    la $a0, new      #print two new lines
    li $v0, 4
    syscall

    la $a0, new
    li $v0, 4
    syscall

    la $a0, Bin    #print "Binary:"
    li $v0, 4
    syscall

binary:
    li $s0, 0      #index i

    li $t1, 2      #assign 2 into t1
    div $t0, $t1   #
    mfhi $t2       #set remainder as t2

    sw $t2, 0($s0) #save remiander to the ith index of array

    mflo $t0       #input number now become quotient

    addi $s0, $s0, 4 #i = i+4, since dealing with integer

    beq $t0, $zero, print

    j binary

#print backwards from the last indext of the array

print:
    beq $a1, -4, exit #if finished printing until list[0]

    li $a1, 128     #set $a1 as 128 to print last index of myArray

    lw $t3, list($a1)

    li $v0 1        #print out the number
    move $a0, $t3
    syscall

    addi $a1, $a1, -4 #i = i-4


    j print

exit:
    li $v0, 10
    syscall

Upvotes: 0

nissim abehcera
nissim abehcera

Reputation: 831

You have to store remainder before dividing, otherwise the remainder of input will not be stored.Take a look this code and try to understand it .

.data
list:   .space 128 #create an array for 32 integers
string:  .asciiz "Enter a number : "

.text               
.globl  main

main:
  jal converter_dec_bin


  # End Program
     li $v0, 10
     syscall


converter_dec_bin:
 addiu $sp,$sp,-8
 sw $ra,0($sp)

xor $t1,$t1,$t1
xor $t2,$t2,$t2
addiu $t2,$t2,504 # 126*4
addiu $t1,$t1,1522  # input number

loop:
    rem $t3,$t1,2
    sw $t3,list($t2)
    subiu $t2,$t2,4
    divu $t1,$t1,2
    bne $t1,0,loop

xor $t4,$t4,$t4
fill_list:
    sw $t4,list($t2)
    subiu $t2,$t2,4
    bne $t2,0,fill_list
xor $t2,$t2,$t2
print_out:

    li $v0, 1
        lw $t0,list($t2)
    move $a0, $t0
        syscall
    addiu $t2,$t2,4
    bne $t2,508,print_out

lw $ra,0($sp)
addiu $sp,$sp,8
jr $ra

Upvotes: 1

Related Questions