Question

I'm learning MIPS 32 bit assembly in the NIOS II IDE and I've got a complete working subroutine that multiplies two numbers stored in r4 and r5 and return the result in 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

How can I call the subroutine and print something from it to make sure that it is working as expected? This is my first subroutine and I have never called a subroutine before, so please pardon me if I don't understand everything right away.

Was it helpful?

Solution

You call the subroutine from main like this:

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 returns by using jr r31 (jump to the address contained in register 31). Your non-standard environment may be spelling this as ret.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top