Help with MIPS Decimal to any base

  • Thread starter Thread starter nhammen
  • Start date Start date
  • Tags Tags
    Base Mips
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 2K views
nhammen
Messages
1
Reaction score
0
My teacher gave us an exercise for this week to modify a piece of code that already accepts decimal number and prints all the bases 2-36 we had to make that the output number for each base appear in right order, accept 0 and negative number, but I am stuck with the last part where it has to show the correct ASCII characters

What I've been doing is when the division is done i check the rest if its bigger or equal to 10 then i jump to new branch where i add 65 then i store the byte add 1 to counter and I am stuck here by my teacher i have to subtract 10 but no idea where exactly I've read here on forums its with 55

Im confused with this last part if someone can help

The code I am editing is B4_3: and B4_10:
Code:
        .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, 65
    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
 
on Phys.org
nhammen said:
What I've been doing is when the division is done i check the rest if its bigger or equal to 10 then i jump to new branch where i add 65 then i store the byte add 1 to counter and I am stuck here by my teacher i have to subtract 10 but no idea where exactly I've read here on forums its with 55
It does not matter where exactly. You have to map 10 to A (65), 11 to B (66) and so on, so you have to add 55.

English comments are so much easier to read for most others.