title: MIPS converting 32-bit signed 2s-com number, decimal number, or an 8-digit signed hexadecimal
body: <p>interactively read a String value representing a 32-bit signed 2s-complement binary (S2CB) number, a decimal number that can be represented as a 32-bit S2CB number, or an 8-digit signed hexadecimal number. Your program must display the specified value in all three number bases. </p>
  
  <p>So far the program asks the user for numbers, but I am having problems creating the actual conversion algorithms. So far the program asks for input and determines if it's correct style.  Can anyone please help me? Thanks in advance.</p>
  
  <pre><code>.data
  
      inputString:    .asciiz "                                  "
      testString: .asciiz "Enter a number as decimal (12345), binary (%10101), or hex (0xABCDEF): "
  
      decDigits:  .asciiz "0123456789"
      hexDigits:  .asciiz "0123456789ABCDEF"
      binDigits:  .asciiz "000000010010001101000110011110001001101010111100110111101111"
  
      detectedDec:    .asciiz "Decimal input detected"
      detectedHex:    .asciiz "Hexadecimal input detected"
      detectedBin:    .asciiz "Binary input detected"
      detectedBad:    .asciiz "Invalid input detected"
  
      .text
  begin:  li $v0, 4
      la $a0, testString
      syscall
  
  read:   li $v0, 8
      la $a0, inputString
      li $a1, 33
      syscall
  
      jal isDec
  
      beq $v0, 0, n1
      li $v0, 4
      la $a0, detectedDec
      syscall
      j endPgm
  
  n1: jal isHex
  
      beq $v0, 0, n2
      li $v0, 4
      la $a0, detectedHex
      syscall
      j endPgm
  
  n2: jal isBin
  
      beq $v0, 0, bad
      li $v0, 4
      la $a0, detectedBin
      syscall
      j endPgm
  
  bad:    li $v0, 4
      la $a0, detectedBad
      syscall
      j endPgm
  
  ###### Is Decimal? Returns 1 in $v0 if yes
  
  isDec:  li $t0, 0
      li $t2, 0
  
  decLoop:lb $t1, inputString($t0)
      beq $t1, 0, decDone
      beq $t1, 10, decDone
      bgt $t1, 57, notDec
      blt $t1, 48, notDec
      li $t2, 1
      addi $t0, $t0, 1
      j decLoop
  
  notDec: li $v0, 0
      jr $ra
  
  decDone:beq $t2, 0, notDec
      li $v0, 1
      jr $ra
  
  ######
  
  ###### Is Binary? Returns 1 in $v0 if yes
  
  isBin:  li $t0, 1
      li $t2, 0
  
  hasPer: lb $t1, inputString
      bne $t1, 37, notBin
  
  binLoop:lb $t1, inputString($t0)
      beq $t1, 0, binDone
      beq $t1, 10, binDone
      bgt $t1, 49, notBin
      blt $t1, 48, notBin
      li $t2, 1
      addi $t0, $t0, 1
      j binLoop
  
  notBin: li $v0, 0
      jr $ra
  
  binDone:beq $t2, 0, notDec
      li $v0, 1
      jr $ra
  
  ######
  
  ###### Is Hex? Returns 1 in $v0 if yes
  
  isHex:  li $t0, 2
      li $t2, 0
  
  has0x:  lb $t1, inputString
      bne $t1, 48, notHex
      lb $t1, inputString + 1
      bne $t1, 120, notHex
  
  hexLoop:lb $t1, inputString($t0)
      beq $t1, 0, hexDone
      beq $t1, 10, hexDone
      bgt $t1, 57, testAF
      blt $t1, 48, testAF
      li $t2, 1
      addi $t0, $t0, 1
      j hexLoop
  
  testAF: bgt $t1, 70, notHex
      blt $t1, 65, notHex
      li $t2, 1
      addi $t0, $t0, 1
      j hexLoop
  
  notHex: li $v0, 0
      jr $ra
  
  hexDone:beq $t2, 0, notDec
      li $v0, 1
      jr $ra
  
  ######
  
  endPgm: li $v0, 10
      syscall
  enter code here
  </code></pre>