user3093146
user3093146

Reputation: 41

Convert an int to a string of characters

In MIPS assembly how would I parse an integer such as 255 into a string of characters '2' '5' '5'.

255 could be in $t0

'2' '5' '5' could then be stored in $t1 and then printed.

How would I do this?

Upvotes: 1

Views: 7977

Answers (3)

phuclv
phuclv

Reputation: 41794

The idea of converting a number into a string in some base is basically done by repeatedly dividing it by the base (10 in this case) and write the remainder backwards until the value is zero. That's the easiest solution. For example for 255

255/10 = 25 remain 5 ↑
 25/10 =  2 remain 5 ↑
  2/10 =  0 remain 2 ↑

The remainder is 2, 5, 5 as expected

For decimal you have another option, that is double dabble, which can convert binary into packed BCD without division. Then you can unpack the nibbles into chars easily

Upvotes: 0

gon1332
gon1332

Reputation: 2090

Asterisk did a very good job! I'm just writing to make his answer more complete.

The message

     -- program is finished running (dropped off bottom) --

is shown because he didn't end his program with

                  li $v0, 10
                  syscall

You should always finish your program with the above lines to terminate the execution normally.

From http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm:

e.g. To indicate end of program, use exit system call; thus last lines of program should be:

    li $v0, 10     # system call code for exit = 10
    syscall        # call operating sys

Upvotes: 0

Asterisk
Asterisk

Reputation: 3574

Here is the version in Python. Just translate it into mips assembly.

def to_str(n):
    output = ''
    while True:
        m = n % 10
        output = str(m) + output
        n = n / 10
        if n == 0:            
            break
    print(output)

The idea is to repeadly get remainders of division by base(in this case 10). For example:

n = 255
n % 10 -> 5, n = 255 / 10 = 25
n % 10 -> 5, n = 25 / 10 = 2
2 % 10 -> 2, n = 2 / 10 = 0

Now, just get remainders of division and print them in reverse order.

Here is one solution in mips assembly:

.data
    N: .word 2554
    digits: .space 16 # number of digits * 4 bytes
    num_digits: .word 3 # number of digits - 1

.text
    lw $t0, N # $t0 = N
    lw $t1, num_digits
    sll $t1, $t1, 2
    la $s0, digits
    add $s0, $s0, $t1
loop:
    div $t2, $t0, 10
    mfhi $t2 # remainder is in $t2
    sw $t2, 0($s0)
    subi $s0, $s0, 4
    div $t0, $t0, 10
    beqz $t0, print
    b loop
print:
    # print contents of digits
    li $t0, 0 # loop counter
    lw $t1, num_digits
    la $s0, digits
print_loop:
    bgt $t0, $t1, end
    # print the digit
    lw $a0, 0($s0)
    li $v0, 1
    syscall

    # put a space between digits
    la $a0, 32
    li $v0, 11
    syscall

    # move the next digit and increment counter
    addi $s0, $s0, 4
    addi $t0, $t0, 1    
    b print_loop
end:

This results in the following output:

2 5 5 4 
-- program is finished running (dropped off bottom) --

Upvotes: 3

Related Questions