Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

assembly - Why is my MIPS base converter printing out the values from a previous loop after the current values?

I'm very new to MIPS and this has me completely baffled. I made a program to convert bases and it works fine the first time through, but when it loops, its displaying the values from the other registers from the previous iterations of the loop. The output is below. I've tried everything that I can think of and I'm out of ideas...

Enter a decimal number: 10
The number in base 2 is 00000000000000000000000000001010
The number in base 4 is 0000000000000022
The number in base 16 is 0000000A
The number in base 8 is 0000000012

Would you like to input another number? 1

Enter a decimal number: 11
The number in base 2 is 0000000000000000000000000000101100000000000000220000000A0000000012

The number in base 4 is 00000000000000230000000A0000000012

The number in base 16 is 0000000B0000000012

The number in base 8 is 0000000013

Would you like to input another number?

    .text
    .globl __start

__start:
    la $a0, prompt              # Prompt for a base10 integer
    li $v0, 4
    syscall

    li $v0, 5                   
    syscall

    move $a0, $v0               

    jal bin                     
    jal base4
    jal hex
    jal base8

    la $a0, endl                
    li $v0, 4
    syscall     

    la $a0, repeat              
    li $v0, 4
    syscall

    li $v0, 5                
    syscall

    beqz $v0, eop               

    la $a0, endl                
    li $v0, 4
    syscall

    j __start                

eop:    
    li $v0,10                       # End Of Program
        syscall     



##########################################################
#
#   BASE 16
#
##########################################################  

hex:    
    sub $sp, $sp, 24            # Push register onto stack
    sw $a0, 0($sp)
    sw $s0, 4($sp)
    sw $s1, 8($sp)
    sw $s2, 12($sp)
    sw $s3, 16($sp)
    sw $s4, 20($sp)

    move $s2, $a0               # Move a0 to s2

    la $a0, ans3                # Display string before hex answer
    li $v0, 4
    syscall

    li $s0, 8                   # 8 digits for hex word
    la $s3, hexresult           # Hex string set up here

hexloop:
    rol $s2, $s2, 4             # Start with leftmost digit
    and $s1, $s2, 0xf           # Mask 15 digits in s2 and place results in s1
    ble $s1, 9, hexprint        # If s1 <= 9, go to print
    add $s1, $s1, 7             # Else s1 = s1 + 7 (to get A-F)

hexprint:
    add $s1, $s1, 48            # Add 48 (30 hex) to get ascii code
    sb $s1,($s3)                # Store byte in result. s3 -> result
    add $s3, $s3, 1             # s3 = s3 + 1
    add $s0, $s0, -1            # s0 = s0 - 1
    bnez $s0, hexloop           # If s0 != 0, go to hexloop
    la $a0, hexresult           # display result

    li $v0, 4
    syscall

    jr $ra                      # Return
##########################################################
#
#   BASE 2
#
#########################################################

bin:    
    sub $sp, $sp, 24            # Push register onto stack
    sw $a0, 0($sp)
    sw $s0, 4($sp)
    sw $s1, 8($sp)
    sw $s2, 12($sp)
    sw $s3, 16($sp)
    sw $s4, 20($sp)

    move $s2, $a0               # Move a0 to s2

    la $a0, ans1                # Display string before bin answer
    li $v0, 4
    syscall

    li $s0, 32                  # 32 digits for base4 word
    la $s3, binresult           # Bin string set up here

binloop:
    rol $s2, $s2, 1             # Start with leftmost digit
    and $s1, $s2, 1             # Mask one digit in s2 and place results in s1


binprint:
    add $s1, $s1, 48            # Add 48 (30 hex) to get ascii code
    sb $s1,($s3)                # Store byte in result. s3 -> result
    add $s3, $s3, 1             # s3 = s3 + 1
    add $s0, $s0, -1            # s0 = s0 - 1
    bnez $s0, binloop           # If s0 != 0, go to binloop
    la $a0, binresult           # display result

    li $v0, 4
    syscall

    la $a0, endl
    li $v0, 4
    syscall

    jr $ra                      # Return

##########################################################
#
#   BASE 4
#
#########################################################       

base4:  
    sub $sp, $sp, 24            # Push register onto stack
    sw $a0, 0($sp)
    sw $s0, 4($sp)
    sw $s1, 8($sp)
    sw $s2, 12($sp)
    sw $s3, 16($sp)
    sw $ra, 20($sp)

    move $s2, $a0               # Move a0 to s2

    la $a0, ans2                # Display string before BASE 4 answer
    li $v0, 4
    syscall

    li $s0, 16                  # 16 digits for base4 word
    la $s3, base4result         # Bin string set up here

base4loop:
    rol $s2, $s2, 2             # Start with leftmost digit
    and $s1, $s2, 3             # Mask one digit in s2 and place results in s1

fourprint:
    add $s1, $s1, 48            # Add 48 (30 hex) to get ascii code
    sb $s1,($s3)                # Store byte in result. s3 -> result
    add $s3, $s3, 1             # s3 = s3 + 1
    add $s0, $s0, -1            # s0 = s0 - 1
    bnez $s0, base4loop         # If s0 != 0, go to binloop

    la $a0, base4result         # display result
    li $v0, 4
    syscall

    la $a0, endl
    li $v0, 4
    syscall

    jr $ra                      # Return

##########################################################
#
#   BASE 8
#
#########################################################           

base8:  
    sub $sp, $sp, 24            # Push register onto stack
    sw $a0, 0($sp)
    sw $s0, 4($sp)
    sw $s1, 8($sp)
    sw $s2, 12($sp)
    sw $s3, 16($sp)
    sw $ra, 20($sp)

    move $s2, $a0               # Move a3 to s2

    la $a0, endl
    li $v0, 4
    syscall     

    la $a0, ans4                # Display string before bin answer
    li $v0, 4
    syscall

    li $s0, 10              #  digits for octal word
    la $s3, octresult           # Bin string set up here

    rol $s2, $s2, 2             # Start with leftmost digit
    and $s1, $s2, 0x7           # Mask 7 digits in s2 and place results in s1

base8loop:
    rol $s2, $s2, 3             # Start with leftmost digit
    and $s1, $s2, 0x7           # Mask 7 digits in s2 and place results in s1


base8print:
    add $s1, $s1, 48            # Add 48 (30 hex) to get ascii code
    sb $s1,($s3)                # Store byte in result. s3 -> result
    add $s3, $s3, 1             # s3 = s3 + 1
    add $s0, $s0, -1            # s0 = s0 - 1
    bnez $s0, base8loop         # If s0 != 0, go to binloop
    la $a0, octresult           # display result

    li $v0, 4
    syscall     


    jr $ra                      # Return


.data

binresult:  .space 32
base4result:.space 16
hexresult:  .space  8
octresult:  .space 10
endl:       .asciiz "
"
prompt:     .asciiz "Enter a decimal number: "
ans1:       .asciiz "The number in base 2 is "
ans2:       .asciiz "The number in base 4 is "
ans3:       .asciiz "The number in base 16 is "
ans4:       .asciiz "The number in base 8 is "
repeat:     .asciiz "Would you like to input another number? "
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The strings you print with syscall 4 need to be ASCIIZ, i.e. ASCII with a zero terminator. So you need to store a byte with the value zero after the last character in each string. And to be able to store that extra byte, you need to reserve one additional byte for each string (i.e. .space 33 for binResult, etc).

Actually, just increasing the number of bytes reserved with .space should be enough, as .space should zero-initialize the memory. But adding an extra sb per string just to be sure wouldn't hurt much.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...