2
votes

I'm very new to MIPS and this site (this is my first post) so please bear with me here...I have to take a user-entered number, reverse it integer by integer using division (HI/LO) and store the remainder into a new register, in order to compare the resulting reversed number against the original to see if it is a palindrome. That's fine, got that division part (I think?), but once I divide the second time and try to add the second remainder onto the remainder of the first division, it will simply overwrite the contents of the register, not add a number at the end of it, right? I can't find the answer to this on the internet. How can I accomplish this? Because simply doing 'move' will overwrite the contents, correct? Here's my code so far

li  $v0, 4  # System call code for print string
la  $a0, Prompt # Load address for Prompt into a0
syscall

li  $v0, 5  # System call code for read integer
syscall     # Read the integer into v0
move  $t0, $v0  # Move the value into t0

move  $t9, $t0

li  $s0, 10 # Load 10 into s0 for division
li  $s1, 0  # Load 0 into s1 for division

div     $t0, $s0  # Divides t0 by 10

mfhi    $t1     # Move remainder into t1
mflo    $t0     # Move quotient into t0

Essentially, I want to concatenate the remainders together, not add them together or overwrite the register. Let's say the first remainder is 3, second is 6, third is 9. At the end of it, I don't want it to be 18, or 9. I want it to be 369.

1

1 Answers

1
votes

Your div/mfhi/mflo were fine. What you needed was a loop that has a second variable.

I've created the equivalent C code and added it as a comment block and created a working program [please pardon the gratuitous style cleanup]:

#   int
#   rev(int inp)
#   {
#       int acc;
#       int dig;
#
#       acc = 0;
#
#       while (inp != 0) {
#           dig = inp % 10;
#           inp /= 10;
#
#           acc *= 10;
#           acc += dig;
#       }
#
#       return acc;
#   }

    .data
Prompt:     .asciiz     "Enter number to reverse:\n"
nl:         .asciiz     "\n"

    .text
    .globl  main

main:
    li      $v0,4                   # System call code for print string
    la      $a0,Prompt              # Load address for Prompt into a0
    syscall

    li      $v0,5                   # System call code for read integer
    syscall                         # Read the integer into v0
    bltz    $v0,exit                # continue until stop requested
    move    $t0,$v0                 # Move the value into t0

    li      $t3,0                   # initialize accumulator

    li      $s0,10                  # Load 10 into s0 for division
    li      $s1,0                   # Load 0 into s1 for division

next_digit:
    beqz    $t0,print               # more to do? if no, fly
    div     $t0,$s0                 # Divides t0 by 10

    mfhi    $t1                     # Move remainder into t1 (i.e. dig)
    mflo    $t0                     # Move quotient into t0 (i.e. inp)

    mul     $t3,$t3,$s0             # acc *= 10
    add     $t3,$t3,$t1             # acc += dig
    j       next_digit              # try for more

print:
    li      $v0,1                   # print integer syscall
    move    $a0,$t3                 # get value to print
    syscall

    li      $v0,4
    la      $a0,nl
    syscall

    j       main

exit:
    li      $v0,10
    syscall