0

How do I make my code print actual values (.word) contained in the MY_INT_ARRAY label rather than simply print out what seems to be an address? The values contained in the array are -1, 4, 5, 0, etc. however these are not what appear on the console when I try to run my code. Instead, addresses appear.

Code:

    # All program code is placed after the
    # .text assembler directive

.text
    la  $s0, MY_INT_ARRAY_LEN
    lw  $s0, 0($s0)             # s0: MY_INT_ARRAY_LEN
    addi  $t0, $zero, 0         # t0: to hold the "index" of the next array entry

    la  $t1, MY_INT_ARRAY       # t1: to hold the "address" of the array entries  
                                    #     initialised to the address of the first
                                #     byte of the array
    
    NEXT_ARRAY_PRINT:
    beq $t0, $s0, DONE_PRINTING # jump to DONE_PRINTING when reached array's end
    addi $v0, $zero, 1          # set v0 to invoke "print integer" syscall
    addu $a0, $zero, $t1        # a0 <- the integer to be printed
    syscall                     # invoking the syscall to print the integer

    addi  $v0, $zero, 4         # set v0 to invoke "print string" syscall
    la $a0, MSG_SPACER          # a0 <- the address of the string's starting byte
    syscall                     # invoking the syscall to print the string

    addi $t1, $t1, 4           # incrementing t1 by 4 (why?)
    addi $t0, $t0, 1           # incrementing t0 (the array index) by 1

    j NEXT_ARRAY_PRINT          # jump to NEXT_ARRAY_PRINT (our for loop)

    DONE_PRINTING:
    addi $v0, $zero, 10         # set v0 to "10" to select exit syscall
    syscall                     # invoking the syscall to actually exit!s

    # All memory structures are placed after the
    # .data assembler directive
.data
    MSG_SPACER:  .asciiz ", "   # the comma (and space) string
    MY_INT_ARRAY:               # our integer array
        .word   -1
        .word    4
        .word    5
        .word    0
        .word   -2
        .word   -5
        .word    3
        .word    1
    MY_INT_ARRAY_LEN:   .word   8 # the length of the array
  • You're going to want an `lw` in the loop, to load from the address of each 4-byte word. Looks like just `lw $a0, ($t1)` instead of copying t1 to a0 would do it. That's why you're incrementing t1 by 4, of course. – Peter Cordes Nov 23 '20 at 11:11
  • @PeterCordes My code in console view lists the values followed by a comma because I used MSG_SPACER however after my last value is printed again a comma is printed. This is what I see in my console: -1, 4, 5, 0, -2, -5, 3, 1, As you can see a comma appears after the last value 1. Do you know a way of removing this last single comma? – DeadlyDragonnn Nov 23 '20 at 13:43
  • Peel the first or last iteration so you can print one fewer comma than number. MARS doesn't have cursor-movement system calls so you can't backspace and overwrite it with a space or anything. – Peter Cordes Nov 23 '20 at 13:54
  • @peterCordes I'm not quite sure what you mean when you say peel one of the iterations? Is there some way to use an if statement in MIPS so that my MSG_SPACER is used only x-1 times (x being array length possibly) or am I overthinking what you're suggesting – DeadlyDragonnn Nov 23 '20 at 19:06
  • "peel" in compiler / optimization terminology means pull one loop iteration out of the loop, to do separately (https://en.wikipedia.org/wiki/Loop_splitting#Loop_peeling). Do (pseudocode) `if(n>0) print *ptr` *before* falling into the loop, so the loop does `print ", "; print *++ptr;` so the first element printed by the loop body is the 2nd element, and what you print ends with the last element, not a comma. – Peter Cordes Nov 24 '20 at 01:40
  • @PeterCordes are you saying that I should do "if (MY_INT_ARRAY_LEN>0) print MSG_SPACER" but of course in MIPS assembly language. Or by n>0 do you mean iteration no is greater than 0? And ptr? – DeadlyDragonnn Nov 24 '20 at 09:54
  • Think about what either of those would do and it should be obvious. I meant `n=array length`, since I wanted it to work exactly like a `for(i=0 ; i – Peter Cordes Nov 24 '20 at 10:40
  • @PeterCordes bne $s0, 1, Exit <--- here's how I understand bne, $s0 and 1 would need to not be equal for the Exit label to execute. In your suggestion, what two values must not be equal before I run a label containing a comma and a space? – DeadlyDragonnn Nov 25 '20 at 17:37
  • Re-read my previous comments; you're missing the point and trying to bend my words into your existing inefficient loop structure. `bne $t1, $t2, top_of_loop` should go at the *bottom* of your loop. [Why are loops always compiled into "do...while" style (tail jump)?](https://stackoverflow.com/q/47783926) – Peter Cordes Nov 26 '20 at 03:48
  • @PeterCordes Your comments confuse me further. Currently $t1 holds an address and $t2 doesn't exist. As for top_of_loop are you referring to NEXT_ARRAY_PRINT. And you say inefficient loop structure and then suggest I still continue to add to the bottom of the loop? – DeadlyDragonnn Nov 26 '20 at 08:19
  • @PeterCordes do you know of any others who are good at explaining MIPS assembly language to beginners and who might know of a workaround? – DeadlyDragonnn Nov 26 '20 at 08:24

0 Answers0