0

I have a base code in MIPS that is half working it accepts a decimal number and has to convert it to bases from 2 to 36 but it has the following problems:

1- Make the output number appear in the right order 2- Make it accept negative numbers 3- Make it accept 0 4- Make bases after 10 display ASCII characters

Im stuck at number 4 no idea how to make it, so if someone can help me with that piece of code i would be happy here is my part of the code i did:

        .data
buffer: .space 256
str000: .asciiz "Introduzca un número (n) en base 10: "
str001: .asciiz ": "
str002: .asciiz "n en base "
str003: .asciiz "\n"

        .text
# mueve la línea siguiente justo antes de la versión que desees probar
integer_to_string:
integer_to_string_v4:
    move    $t0, $a2        # char *p = buff
    # for (int i = n; i > 0; i = i / base) {
        blt $a0, $0, negativo5
        move    $t1, $a0        # int i = n

volver3:
    beqz    $t1, B4_9   
B4_3:   blez    $t1, B4_7       # si i <= 0 salta el bucle
    div $t1, $a1        # i / base
    mflo    $t1         # i = i / base
    mfhi    $t2         # d = i % base
    bge $t2, 10, B4_10
    addiu   $t2, $t2, 48        # d + '0'
    sb  $t2, 0($t0)     # *p = $t2 
    addiu   $t0, $t0, 1     # ++p
    j   B4_3            # sigue el bucle
    # }
B4_10:  
    addiu   $t2, $0, 55
    sb  $t2, 0($t0)
    addiu   $t0, $t0, 1
    j   B4_3 



B4_9:
    li  $v0, 1
    syscall
B4_7:   blt $a0, $0, negativo6
B4_8:   sb  $zero, 0($t0)       # *p = '\0'
    move    $t3, $a2
    subi    $t0,$t0,1
vuelta4:
    bge $t3,$t0,fin_bucle4
    lb  $t7, 0($t3)
    lb  $t8, 0($t0)
    sb  $t7, 0($t0)
    sb  $t8, 0($t3)
    addi    $t3,$t3,1
    subi    $t0,$t0,1
    j   vuelta4
fin_bucle4: jr  $ra

negativo5: 
    abs $t1, $a0
    j   volver3

negativo6:
    addi    $t4, $0, '-'
    sb  $t4, 0($t0)
    addiu   $t0, $t0, 1
    j   B4_8


# Imprime el número recibido en base 10 seguido de un salto de linea
test1:                  # $a0 = n
    addiu   $sp, $sp, -4
    sw  $ra, 0($sp)
    li  $a1, 10
    la  $a2, buffer
    jal integer_to_string   # integer_to_string(n, 10, buffer); 
    la  $a0, buffer
    jal print_string        # print_string(buffer); 
    la  $a0, str003
    jal print_string        # print_string("\n"); 
    lw  $ra, 0($sp)
    addiu   $sp, $sp, 4
    jr  $ra

# Imprime el número recibido en todas las bases entre 2 y 36
test2:                  # $a0 = n
    addiu   $sp, $sp, -12
    sw  $ra, 8($sp)
    sw  $s1, 4($sp)
    sw  $s0, 0($sp)
    move    $s0, $a0        # n
        # for (int b = 2; b <= 36; ++b) { 
    li  $s1, 2          # b = 2
B6_1:   la  $a0, str002
    jal print_string        # print_string("n en base ")
    move    $a0, $s1
    li  $a1, 10
    la  $a2, buffer
    jal integer_to_string   # integer_to_string(b, 10, buffer)
    la  $a0, buffer
    jal print_string        # print_string(buffer)
    la  $a0, str001
    jal print_string        # print_string(": "); 
    move    $a0, $s0
    move    $a1, $s1
    la  $a2, buffer
    jal integer_to_string   # integer_to_string(n, b, buffer); 
    la  $a0, buffer
    jal print_string        # print_string(buffer)
    la  $a0, str003
    jal print_string        # print_string("\n")
    addiu   $s1, $s1, 1     # ++b
        li  $t0, 36
    ble $s1, $t0, B6_1      # sigue el bucle si b <= 36
    # } 
    lw  $s0, 0($sp)
    lw  $s1, 4($sp)
    lw  $ra, 8($sp)
    addiu   $sp, $sp, 12
    jr  $ra

    .globl  main
main:
    addiu   $sp, $sp, -8
    sw  $ra, 4($sp)
    sw  $s0, 0($sp)
    la  $a0, str000
    jal print_string        # print_string("Introduzca un número (n) en base 10: ")
    jal read_integer
    move    $s0, $v0        # int n = read_integer()
    move    $a0, $s0
    jal test1           # test1(n)
    move    $a0, $s0
    jal test2           # test2(n)
    li  $a0, 0
    jal mips_exit       # mips_exit(0)
    li  $v0, 0
    lw  $s0, 0($sp)
    lw  $ra, 4($sp)
    addiu   $sp, $sp, 8
    jr  $ra

read_integer:
    li  $v0, 5
    syscall 
    jr  $ra

print_string:
    li  $v0, 4
    syscall 
    jr  $ra

mips_exit:
    li  $v0, 17
    syscall 
    jr  $ra
  • Do you understand the logic? Just check if the digit is greater than 9, and if so, adjust it to print the appropriate letter. Note that the letters are consecutive in the ascii table, so you only need to add an offset. – Jester Apr 06 '16 at 22:12
  • Yes im doing that after the division i check if the rest is equal or bigger than 10 then i go to branch where i do the following B4_10: addiu $t2, $0, 65 sb $t2, 0($t0) addiu $t0, $t0, 1 j B4_3 – Stoyan Parvanov Apr 07 '16 at 09:07
  • 1
    Can't see `B4_10` in the code posted. Anyway, you shouldn't add `65` because that would turn 10 into `K` not `A`. You should add `55`. – Jester Apr 07 '16 at 11:09
  • I had the same idea this morning when i was doing this but when i use 55 i doesn't work i read somewhere on the net that its adding 65 then resting 10 but did get why and how exactly – Stoyan Parvanov Apr 07 '16 at 17:20
  • Besides branching on 0-9 vs. A-Z, another option is to use a lookup table, [like the answers in this x86 question](http://stackoverflow.com/questions/36336045/print-register-value-to-console/36369143). i.e. get a digit in your chosen base by indexing into an array of `"0123456789abcdef"`. – Peter Cordes Apr 07 '16 at 18:10
  • By my professor instruction we are not allowed to make it that way :D i have a whole exercise with array that convert from decimal to hex – Stoyan Parvanov Apr 07 '16 at 21:08

0 Answers0