1 year ago

#314211

test-img

TorusWithSprinkles

MIPS (assembly) Print function not working as expected

I'm learning MIPS and trying to understand function calls and stuff. I wrote this basic 'printValue' function to print the value of a variable defined in Global memory, but it's printing 0 instead (it should print '2', as a2 is initialized) and I can't understand why. Here's my code:

.data

    # Allocate variables in global memory
    a2:
        .word 2
    a1:
        .word 3
    a0: 
        .word 5 
    x:
        .word 0
    result:
        .word 0

    output_msg:
        .asciiz "The result is "
    newl:
        .asciiz "\n"
    
.align 2

.text
        
.globl main

main:

    #prologue (push stack space)
    addiu $sp, $sp, -4  

    la $a0, a2
    jal printValue

    #epilogue
    addiu $sp, $sp, 4
    
    # Signal end of program
    li $v0, 10
    syscall
        
printValue: 
    addiu $sp, $sp, -4  # Allocate space on the stack
    
    # Print value
    li $v0, 1
    move $a0, $s0
    syscall
    
    jr $ra

Where is it going wrong? Thank you for any help, I really appreciate it!

assembly

mips

low-level

mips32

mips64

0 Answers

Your Answer

Accepted video resources