Search code examples
stringmipsspimmars-simulatorbare

MIPS (Bare Mode) String Won't Print


Recently while starting to learn MIPS in university, I've come across a problem while trying to print 1 string, accept a user input, and then print another string and accept a user input. Both user inputs should be stored to registers a0 and a1 respectively.

The names of each string are promptD for the Dividend input, and enterD for the Divisor input (you might guess this is an unsigned division calculator program).

In my debugging attempts, I have narrowed the problem to a small snippet of the code, posted below.

I think I am incorrectly offsetting my first .data register to reach my 2nd .data register. The problem I am noticing as I've tried QTspim, xspim, PCspim, and MARS is that all 4 of these give the first string in .data a different initial register address.

For example: The string "Enter Dividend" will be in reg address 0x10010000 in MARS but will start in 0x10000000 in PCspim. The following register address for "Enter Divisor" will be in either 0x10010011 in MARS or 0x10000010 in PCspim.

In its current state thru MARS, the program snippet below asks the user to input dividend, and it will store the value. Immediately after storing to a0, the code will fail due to a line 37 (which is just the 3rd syscall) runtime exception at 0x00400024: address out of range 0x00000004. It is not prompting "Enter Divisor" at all.

To really see the problem in action, I think running this in MARS would help make it more clear. Is it a offsetting issue? Am I clobbering a register without seeing it? I haven't found much MIPS help on here that deals with problems without pseudo-instructions. I realize with them, I could load an address directly (la)...but I can't use them here.

Thanks

    .globl main

    .data        #for the data

    promptD: .asciiz "Enter Dividend \n"
    enterD:  .asciiz "Enter Divisor \n"
  #  result: .asciiz "Result = "

    .text           #for the instructions

    main:

    #for Dividend
    addi $v0, $0, 4     #store string instr to v0

    lui $a0, 0x1001     #address of promptD

    syscall              #display promptD

    addi $v0, $0, 5     #store input instr to v0

    syscall             # Get dividend

    add $a0, $0, $v0        # Dividend to $a0


    #for Divisor
    addi $v0, $0, 4         #store string instr to v0

    lui $a1, 0x1001         #Where I think the problem is...
                            #Address of first string followed by add offset?
    addi $a1, $a1, 33       #Maybe incorrect offset?

    syscall                 #display enterD

    addi $v0, $0, 5         #store input instr to v0

    syscall                # Get divisor

    add $a1, $0, $v0        # Divisor to $a1

    #end snippet

Solution

  • Here's the problematic code:

    lui $a1, 0x1001         #Where I think the problem is...
                            #Address of first string followed by add offset?
    addi $a1, $a1, 33       #Maybe incorrect offset?
    
    1. You're using the wrong register. The argument for syscall 4 should be placed in $a0, not $a1.
    2. The offset 33 is incorrect. If you look at the Data Segment viewer in Mars you can see that the NUL-terminator byte for promptD is located at 0x10010010, and that the enterD string begins at 0x10010011 (if you have a hard time reading hexadecimal ASCII codes you can tick the "ASCII" checkbox in the Data Segment viewer to view the data as characters). So the offset you should be using is 0x11 (17 decimal).