Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Printing array of char to string in MIPS

  1. Nov 10, 2008 #1
    I am trying to mimic the function below in MIPS:

    extern void print_int(int x);
    extern void print_string(char x[]);

    char y[1];

    void main(void) {
    char y[1];
    y[0] = 'a';
    y[1] = '\0';

    and my MIPS code for the above C code is below

    can someone tell me why when I try to run this in spim it fails. It generates this error:

    Memory address out of bounds

    As far as I know I am already passing the address of y, which is an array of char terminated by a null string at the end. But why this error? How do I fix this?

    Code (Text):

        .globl main
                # Function Entry
        sw $ra,-20($sp)
        sw $fp,-24($sp)
        add  $fp, $zero, $sp
        subu $sp, $sp, 24
                # MOVE
        add $t0,$zero,0
        sw  $t0,-8($fp)
                # MOVE
        add $t0,$zero,97
        lw  $t1,-8($fp)
        add  $t2,$zero,-2
        add $t1,$t2,$t1
        add $t1,$fp,$t1
        sb  $t0,($t1)
                # MOVE
        add $t0,$zero,1
        sw  $t0,-12($fp)
                # MOVE
        add $t0,$zero,0
        lw  $t1,-12($fp)
        add  $t2,$zero,-2
        add $t1,$t2,$t1
        add $t1,$fp,$t1
        sb  $t0,($t1)
                # MOVE
        lw  $t0,-4($fp)
        add  $t1,$zero,-2
        add $t0,$t0,$t1
        add $t0,$fp,$t0
        lb  $t1,($t0)
        sw $t1,-16($fp)
                # PARAM
        lw  $t0,-16($fp)
        sub $sp,$sp,4
        sw $t0, ($sp)
                # PARAM in REG0
        lw  $a0,-16($fp)
                # PARAM in REG1
        lw  $a1,-16($fp)
                # Function call
        jal print_string
        add $sp,$sp,4
                # Function Ends
                # Exit Sequence
        lw $ra,-20($fp)
        add  $sp, $zero, $fp
        lw $fp,-24($fp)
        jal $ra
        li $v0,1
        lw $a0, 0($sp)
        jr $ra
        li $v0,4
        lw $a0, 0($sp)
        jr $ra
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted