Assembly 如何调用这个子程序?

Assembly 如何调用这个子程序?,assembly,mips,subroutine,nios,Assembly,Mips,Subroutine,Nios,我正在学习NIOS II IDE中的MIPS 32位汇编,我有一个完整的工作子例程,它将存储在r4和r5中的两个数字相乘,并在r2中返回结果: .global muladd # makes label "main" globally known .text # Instructions follow .align 2 # Align instructions to

我正在学习NIOS II IDE中的MIPS 32位汇编,我有一个完整的工作子例程,它将存储在r4和r5中的两个数字相乘,并在r2中返回结果:

      .global muladd            # makes label "main" globally known

        .text                   # Instructions follow
        .align  2               # Align instructions to 4-byte words

muladd:
   movi r2, 0 # total = 0
   movi r8, 0 # i = 0
L1:   # if( i >= a ) goto L2
   bge r8, r4, L2 # a i r4
    # total = total + b
   add r2, r2, r5 # öka b med r5
   addi r8, r8, 1 # i = i + 1
   br L1 # goto L1
L2: # return( total )
ret

如何调用子例程并从中打印一些内容以确保它按预期工作?这是我的第一个子例程,我以前从未调用过子例程,因此如果我不能立即理解所有内容,请原谅。

您从main调用子例程的方式如下:

main:
  ...
  li r4, 123    // load some test data into r4 and r5
  li r5, 1
  jal muladd    // call muladd. Return address is stored in r31
  nop           // branch delay slot
  // muladd returns to this address. 
  // If muladd worked correctly r2 should contain decimal 123+1, or 124
  // print subroutine call goes here
  ...

Muladd使用jr r31返回(跳转到寄存器31中包含的地址)。您的非标准环境可能将其拼写为
ret

@非常感谢您的评论。我已经用更多关于环境和汇编程序的信息更新了这个问题。它是32位MIPS和Altera DE2开发板,我使用的IDE称为Nios II,它也是所用处理器的名称。如果您愿意,请询问更多问题。从未遇到过此类环境,但如果它正在生成elf,请使用。此外,要调用例程,必须有
call
指令或
jump
指令。@anonymous
int 0x80
仅适用于x86。OP正在使用MIPS。感谢您的评论。在我的环境中,调用只是通过代码中的语句
call
,例如,要在MIPS中调用子例程
foo
,我只需执行
call foo
,然后参数和返回值在后台自动传输,就像所有寄存器都是“全局变量”在汇编的堆上。谢谢你的回答。在我的情况下,我可以只使用语句
call