0
votes

So I've been working on an assignment, and the very last bit of it is really getting to me.

After all the computations I had to do, the final integer is stored in any "s" register and kept there for the rest of the script. I now need to print out this integer from the register, however I need to print a character per line so I believe I can only use syscall 11. anyone has any ideas how i could do this?

I tried moving the integer to an array and then printing it by nothing gets printed...

1
Can you post the code you've tried so far?craigcaulfield

1 Answers

0
votes

Take a look at my function intprint. It does the job recursively:

# Print integer character by character on separate lines
# Tested with MARS

.globl main
main:
    li $s0, 12345                   # integer to be printed

    li $v0,  1                      # print int
    move $a0, $s0
    syscall
    li $v0, 11                      # print char
    li $a0, '\n'
    syscall

    # print integer char by char recursively
    move $a0, $s0
    jal intprint

    li $v0, 10                      # exit
    syscall

intprint:

    addi $sp, $sp, -8               # preserve registers in stack 
    sw   $ra, 0($sp)
    sw   $s0, 4($sp)

    move $s0, $a0

    ble $s0, 9, output

    li $t0, 10                      # divide by 10
    div $s0, $t0
    mfhi $s0                        # remainder
    mflo $a0                        # quotient

    jal intprint                    # recursion 

    output:

            add $a0, $s0, '0'
            li $v0, 11
            syscall
            li $a0, '\n'
            syscall

    lw   $ra, 0($sp)                # restore registers from stack
    lw   $s0, 4($sp)

    addi $sp, $sp, 8                # restore stack pointer

    jr $ra

Output:

12345
1
2
3
4
5