I entry code for some MIPS core. Basically what I do is to set the stackpointer to a address I set via the linker script and jump to my main. This works:
#[naked]
#[no_mangle]
#[link_section = ".init"]
unsafe fn _start() -> ! {
extern "C" {
static _STACK_START: usize;
}
// Set stackpointer and call main()
asm!("
move $$sp, $0
j $1"
:: "r"(&_STACK_START), "i"(main as extern "C" fn() -> !)
: "$$sp");
::core::intrinsics::unreachable();
}
However, I am confused why I cannot load the stack pointer directly using an immediate value (the address should be known statically):
asm!("
li $$sp, $0
j $1"
:: "i"(&_STACK_START), "i"(main as extern "C" fn() -> !)
: "$$sp");
Whenever I do it this way I get code wich loads 0 to the stackpointer:
a0000000: 00 00 1d 24 addiu $sp, $zero, 0
a0000004: 04 00 00 08 j 16 <mipswififw.6kl3m8ym-cgu.0+0x10>
Just as a reference the code that is generated by the working example. It does an unnecessary addiu even though optimization is on:
a0000000: 04 a0 01 3c lui $1, 40964
a0000004: 00 00 22 24 addiu $2, $1, 0
a0000008: 25 e8 40 00 move $sp, $2
a000000c: 06 00 00 08 j 24 <mipswififw.6kl3m8ym-cgu.0+0x18>
Any idea how to load the address of a static variable as an immediate? It is no problem to load the value of a static as immediate this way.