How to inline ASM in C (gcc)In your code you will use the asm(); function, which you can use to inline asm in C. If you don't understand what inline means, it's basically using ASM in C.
First, set headers, module info and start main.
#include <pspkernel.h>
#include <pspdebug.h>
PSP_MODULE_INFO("Hello World in ASM", 0, 1, 1);
int main(void) {
When inlining ASM in C, you can set a variable in C, in out case we will call it msg, and use it in the ASM code.
char *msg = "Hello world\n";
Setup the psp screen.
pspDebugScreenInit();
Now, using the asm() function you can make the psp print this code...
asm("lui $4, %%hi(%0)\n"
"jal pspDebugScreenPrintf\n"
"addiu $4,$4,%%lo(%0)\n"
"nop\n"
: : "g" (msg));
lui, load upper immediate.
$4, $4 will be loaded later.
%%hi, this will grab the upper 16bits of (val).
$0, this is this first argument passed into the function.
jal, jump and load.
pspDebugScreenPrintf, the jal loads pspDebugScreenPrintf and that prints $4.
addiu, add immmediate unsigned.
$4,$4,%%lo(%0), $4 + %%lo(%0) (lower 16 bits of the first arguement passed into the function) = $4.
nop, no operation. This means that instead of setting $4 after jal, you can set before. This is used for aligning code.
: : "g" (msg));, loads the variable msg ($0) and ends the asm function. This requires -02 on gcc, if that isnt by default.
Return 0 and close main.
return 0;
}
Your code shoud look like this.
#include <pspkernel.h>
#include <pspdebug.h>
PSP_MODULE_INFO("Hello World", 0, 1, 1);
int main(void) {
char *msg = "Hello world!\n";
pspDebugScreenInit();
asm("lui $4, %%hi(%0)\n"
"jal pspDebugScreenPrintf\n"
"addiu $4,$4,%%lo(%0)\n"
"nop\n"
: : "g" (msg));
return 0;
}
Your Makefile.
TARGET = hello
OBJS = main.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Hello World in ASM
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Ta-da, it should say hello world!
Yes, this way probably does suck, but im still learning MIPS ASM myself

Big thanks to pspdev and siberian for helping me
