Call label in different mips file

75 Views Asked by At

I have two mips assembly files. In file A I need to call file B so that it calculates something, but I don't understand how to do this.

I have already tried to use .globl, and with this, I'm able to call the correct label from A. My problem is that both files have a main label, which throws an error. But if I remove the main label in B then I'm able to call it. But file B has to be able to run on its own and be called from file A, and I have not yet found a way to run a file without a main label.

Is there a way to execute a file without a main label, for example, can I specify a different entry point? Or is there another way, where I do not have to alter file B and still be able to call it as well as run it on its own?

Thanks for any help!

Edit: I have File A:

  .data
  .globl main
  .text
main:
  ori   $a1, $0 , 0x9384  # N = 0x00009384
  sll   $a1, $a1, 16      # N = 0x93840000
  ori   $a1, $a1, 0x9ca7  # N = 0x93849ca7; N input

  ori   $t6, $0 , 0x0123  # M = 0x00000011
  sll   $t6, $t6, 16      # M = 0x00110000
  ori   $t6, $t6, 0x4567  # M = 0x00112233; M input

  ori   $t7, $0 , 0x8C8D  # K = 0x00008C8D
  sll   $t7, $t7, 16      # K = 0x8C8D0000
  ori   $t7, $t7, 0x9129  # K = 0x8C8D9129; K constant

  beq $0, $0, test # MMM(m, k, n)
  addu $t5, $t1, $0
 ... continue with rest of code

where I put some values in registers and then I wnat to call test label in File B (with some example code)

.data
.text
test:
  sll $t6, $t6, 1

After that I want to continue in File A after the beq. So basically work with the result from file B.

Emily

1

There are 1 best solutions below

2
Erik Eidt On

A program should have only one main across all files, sort of by definition of a program; it is a matter of the correct control flow — either your program starts in A or starts in B, but both is illogical.

What you can do to share code in files is create a third file, C (with shared functions but without any main), and use that file with two separate programs, one created from A (with its own main) & C, and, also with a program created from B (with its own main) and C.  So, you have two programs then, that each with their own main and each individually sharing a third file, C.

Of course, you can also use 4 files: A, A1, B, B1 where two programs are built as follows:

  • A, A1, B1 — main in A, other sharable functions in A1, B1
  • B, B1, A1 — main in B, other sharable functions in A1, B1

In my experience, QtSpim doesn't even require you to use .globl when loading one file after another; it simply merges the assembly sources together, resolving any unresolved labels from the first file with those found in the second file.


The exact same will apply to C language programs.  You can only have one main per program, but you can build two separate programs that share files (that don't have a main) between the two programs.


If you really must make both files into programs each using each other's functions, you can forgo having a main in both of them by turning off the default exception handler in using Simulator -> Settings, MIPS tab, turn off Load Exception Handler.

The default exception handler loads a small snippet of code into the user text segment — this snippet of code is startup code that literally calls main with argc and argv style parameters; forgoing the default exception handler means your program will become the startup code (which also means there's no startup code to return (jr $ra) to, must use syscall to end the program, whereas when using the default exception handler can use either jr $ra or syscall #10 to stop program).

Using that setting it will run first whatever code comes in the first file loaded, so you can run program A by loading file A, then loading file B, and run program B by loading B, then loading A.  Note that in both A and B, you'll forgo the label main, and put whatever code you want to run first as the first in the .text section(s).

Of course, one of them can have a main label so it can be run the normal way (without adjusting the exception handler setting), it is just that the both cannot.