MIPS(汇编)打印功能未按预期工作

发布于 2025-01-15 16:45:39 字数 826 浏览 1 评论 0原文

我正在学习 MIPS 并试图理解函数调用之类的东西。我编写了这个基本的“printValue”函数来打印全局内存中定义的变量的值,但它打印的是 0 (它应该打印“2”,因为 a2 已初始化),我不明白为什么。这是我的代码:

.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

哪里出了问题?感谢您的帮助,我真的很感激!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文