-->

Print MIPS Register Contents

2019-08-31 02:32发布

问题:

I am trying to print an unsigned integer value from a MIPS register as ASCII text to the console.

In other words, let's pretend $a0 has "0x4ab3c823" in it. I want to print out "4ab3c823" to console in xSPIM.

Here is my attempt. I keep getting the decimal values, not the ASCII. It's only a snipped of the entire program, so I cut out the rest.

.data
printspace:   .space 8
.text
printHex:
    move    $t0,$a0
    la      $a0,printspace  #Save address of 8 blank bytes to $a0
    sw      $t0,0($a0)      #Copys the integer I want to print to $a0's address in memory
    li      $v0 1
    syscall

    jr      $ra

回答1:

I assume you mean you want hex output, as ASCII output would be with other letters.

I don't think SPIM has hex output. That means you would have to print character by character. That would involve taking $a0 four bits at a time, and adding a constant (depending on if it's between 0-9 or A-F) to turn it into a printable ASCII character.

In the MARS simulator, li $v0 34 prints in hex.



回答2:

With some fiddling, I got the following to do what I needed. Posting for future parties sake.

.text

hexToConsole:
    #$t0 already contains what we want to print

    li      $t1,58          
    la      $t2,0xf0000000  


    maskAndShift:
            beq     $t0,$zero,exit    
            and     $t3,$t2,$t0           
            sll     $t0,$t0,4               
            srl     $t3,$t3,28              
            addi    $t3,$t3,48              
            blt     $t3,$t1,print     
            addi    $t3,$t3,39              
            b       print

    print:
            move    $a0,$t3                 
            li      $v0,11                  
            syscall
            b       maskAndShift

    exit:
            j       $ra