Help with MIPS Decimal to any base

  • Thread starter nhammen
  • Start date
  • Tags
    Base Mips
In summary, the conversation is about a student being stuck with the last part of an exercise given by their teacher. The exercise involves modifying a piece of code that accepts decimal numbers and prints them in different bases. The student is struggling with showing the correct ASCII characters and is unsure where to subtract 10. They have read on forums that they need to add 55 to the value, but it doesn't matter where exactly. This is because they need to map the numbers 10-35 to the letters A-Z, which have ASCII values of 65-90.
  • #1
nhammen
1
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
 
Physics news on Phys.org
  • #2
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.
 

1. How do I convert a decimal number to any base using MIPS?

In order to convert a decimal number to any base using MIPS, you will need to use the built-in MIPS instruction "DIV" which stands for divide. This instruction will divide the decimal number by the desired base. Then, you will need to use the "MFHI" instruction to store the remainder of the division. This remainder will be the first digit of your converted number. Repeat this process with the quotient until it becomes 0, and then you will have your converted number.

2. How do I handle decimal numbers that have a fraction when converting to a different base?

When converting a decimal number with a fraction to a different base, you will need to use the same process as converting a whole number, but you will need to take into account the decimal point. The digits to the left of the decimal point will be converted as usual, while the digits to the right of the decimal point will be converted using multiplication instead of division. For example, if converting from base 10 to base 2, you would multiply the decimal fraction by 2 until it becomes a whole number, and then use the above process to convert it.

3. Is there a limit to the base that I can convert to using MIPS?

No, there is no limit to the base that you can convert to using MIPS. As long as you have enough digits to represent the converted number, you can convert to any base. However, keep in mind that the larger the base, the longer the converted number will be.

4. Can I convert from any base to decimal using MIPS?

Yes, you can also convert from any base to decimal using MIPS. The process is similar to converting from decimal to any base, but instead of dividing by the desired base, you will need to multiply by the base. Then, add all the resulting products to get the decimal equivalent.

5. Are there any specific registers or memory locations I need to use when converting numbers using MIPS?

Yes, when converting numbers using MIPS, you will need to use the "LO" and "HI" registers to store the quotient and remainder respectively. You will also need to use the "MFLO" and "MFHI" instructions to retrieve the values stored in these registers for further calculations. Additionally, you can use any available memory locations to store your converted number.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
7K
Back
Top