简体   繁体   中英

How to use a library in a MIPS assembly program?

How to use a library(made in c/c++) in a MIPS assembly program compiled using QtSpim ( MIPS emulator) ? Using this forum I was able to do this using Intel assembly program by the help of a very talented person @rkhb. This was the solution that he provided:

how to use a library in masm or more specifcally a .lib file?

Now I want to do this same thing in MIPS assembly. Now since I am using an Intel processor and using QtSpim to run MIPS assembly programs, how can I do this while using QtSpim ?

To call a C compiled function from assembler (or vice versa) the registers must have the following content:

  • Registers 4-7 must contain the first 4 arguments
  • Using newer C compilers registers 8-11 contain 4 more arguments
  • Register 29 must be the stack pointer
  • More than 4 or 8 (depending on the compiler) must be stored on the stack (the n-th argument ist located at address (n-1)*4+(register 29) as far as I know; the first 4 or 8 words on the stack may be overwritten by the called routine
  • The routine is simply called using "JAL" or "JALR". Some GNU C compiled functions compiled with "-PIC" switch assume a "JALR" instruction with a certain register (28?) so that register contains the address of the function itself.
  • When returning from the function register 2 contains the value returned; register 3 the high 32 bits for 64-bit results

If SPIM does not support loading executable files but only source code you may create a statically linked executable file and perform a disassembly.

You may pass the disassembly to the SPIM simulator.

Please note that the "system call" instructions ("syscall" or "syscall #n") have a different meaning in different operating systems. If the library contains system calls than you cannot use library written for Linux in SPIM and vice versa.

--- Edit ---

Library functions compiled with "-pic" switch must be called using "JALR $t9", so register 25 ("t9") holds the address of the function that is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM