Mips-如何在火星上运行多个文件

Har*_*ony 0 assembly mips mars-simulator

我正在尝试编写一个程序,让用户输入一个选项,它从另一个文件分支并运行一个程序。我试过寻找答案,但我没有任何运气。我将所有要使用的文件都放在同一个目录中,并将它们全部更改为 .global,然后进入设置以组合目录中的所有文件。

.data
options: .asciiz "Enter 1 for Roadway\nEnter 2 for Equipment\nEnter 3 for Labor\nEnter 4 for Total Projects Costs\n"

.text
.global main

main:
    li $v0, 4   
    la $a0, options
    syscall

    li $v0, 5
    move $t0, $a0
    syscall

    beq $t0, 1, option1
    #beq $t0, 2, option2
    #beq $t0, 3, option3
    #beq $t0, 4, option4

    li $v0, 10
    syscall

option1:
    #this is where I'd like to run code from another file

    jr, $ra
Run Code Online (Sandbox Code Playgroud)

Ped*_*d7g 5

MARS 模拟器具有“设置/组装目录中的所有文件”设置,允许将多个文件链接到单个程序中。

示例...我在磁盘上创建了新目录并将两个 .asm 文件保存到其中:

函数文件

.text

.globl printAsciiz

    # $t5 = address of ASCIIz string, modifies $v0
printAsciiz:
    # store original $a0 to stack (to preserve it)
    addi    $sp, $sp, -4
    sw      $a0, ($sp)
    # output the message from t5
    li      $v0, 4
    move    $a0, $t5
    syscall
    # restore $a0 from stack
    lw      $a0, ($sp)
    addi    $sp, $sp, 4
    # return from function
    jr      $ra
Run Code Online (Sandbox Code Playgroud)

主程序

.data
    msg:    .asciiz     "Hello world\n" 

.text

.globl main
main:
    # call output function from other file
    la      $t5, msg
    jal     printAsciiz
    # terminate program
    li      $v0, 10
    syscall
Run Code Online (Sandbox Code Playgroud)

现在,当切换到编辑器选项卡时main.asm(!重要,当编辑选项卡切换到时func.asm,构建过程将func.asm作为独立应用程序执行结果 - 有点笨拙和奇怪的行为),然后点击构建按钮(使用“所有文件" 设置为 ON),生成的二进制文件将main.asm通过加载$t5地址开始,然后printAsciizfunc.asm.