1 year ago

#370621

test-img

TorusWithSprinkles

MIPS function syscall printing random number/memory address instead of parameter

I'm new to MIPS and I'm trying to write a very basic function that takes 4 numbers (so 4 parameters) and adds them together. I'm doing it so 3 of the numbers are hard-coded in memory and the 4th number (x) is received by user input.

Right now I'm just trying to print all 4 numbers once I'm inside the function (just to verify that they were all passed in correctly), but instead it's printing a random long number (the address?) and I can't tell what's going on.

.data

# initialize variables
num1:
    .word 10
num2:
    .word 15
num3: 
    .word -7
    
    
x:
    .word 0


input_msg: 
    .asciiz "Enter a number for x :"


.text
        
.globl main

main:

    addiu $sp, $sp, -4  

    #get value of x from user input
    la $a0, input_msg
    jal getInteger
    move $s0, $v0  #returned value goes into $s0 (this all works correctly)

    #parameters for function call
    move $a0, $s0
    la $a1, num1
    la $a2, num2
    la $a3, num3
    jal add_nums

    addiu $sp, $sp, 4

    # end of program
    li $v0, 10
    syscall

add_nums:
    #allocate stack space
    addiu $sp, $sp, -16

    li $v0, 1
    syscall

    li $v0, 1
    syscall

    li $v0, 1
    syscall

    li $v0, 1
    syscall

It prints "111111268500992" which I assume is arbitrary, but what's really going on here? I thought I understood function calls but this has me perplexed.

assembly

mips

low-level

mips32

mips64

0 Answers

Your Answer

Accepted video resources