Thumblib3: 100% of your daily dose of vitamin CHAX

I know that International Hack Release Day is a few months away, but I had a brainwave recently that compelled me to write this. So:

THUMBLIB v3 (DOWNLOAD)

The previous version of THUMBLIB was a library for writing THUMB assembly within Event Assembler source files. This iteration of the concept is for wizards who like to write their code using the C programming language. An example taken from my Repair Items hack rewrite:


#define THUMBLIB_VOLATILE

#include "gbafe.h"
#include "thumblib3.h"

Unit* GetUnitStructFromEventParameter(s16 unitKind);
void RepairItems_Internal(Unit* unit);

THUMBLIB_FUNC void ASMC_RepairItems(Proc* eventEngineProc) {

  register unsigned* slots;
  register int slotOffset;

  register void* call;

  register signed unitKind asm("r0");
  register Unit* unit asm("r0");

  PUSH_LR();

  LDR_POOL(slots, &gEventSlot);
  MOV_I(slotOffset, sizeof(s16) * 1);
  LDSH(unitKind, slots, slotOffset);

  LDR_POOL(call, &GetUnitStructFromEventParameter);
  MOV_TO_LR(call);
  BL_LR();

  CMP_I(unit, NULL);
  BEQ(_End);

    LDR_POOL(call, &RepairItems_Internal);
    MOV_TO_LR(call);
    BL_LR();

  _End:;
  POP(call);
  BX(call);

}

There are a few things to note here:

The library has a function attribute macro THUMBLIB_FUNC that should be used for functions. It prevents the compiler from generating pushes/pops and other stuff, along with reducing the chance that things will be optimized out.

Before including the library, THUMBLIB_VOLATILE is defined, which marks all THUMBLIB assembly as volatile, further reducing the chance that the compiler will optimize things out.

You should specify the exact registers to use for a variable when they’re the inputs/outputs of a function call. You can leave out the register if it’s not important to use a specific one.

The optimizer/compiler will absolutely be mean and mangle your beautiful assembly, so be sure to check to make sure that anything generated is correct. I am not liable for anything that happens when using this library.

15 Likes

your actions are not endorsed by fire emblem universe as an organization

7 Likes