r/Assembly_language • u/tinymoat • 4d ago
Solved! Can't call subroutine from a linked in object file in GBA assembly
Hello, I recently decided to start learning assembly by programming the game boy advance. Most of the information about programming the gba I've found are for C programming so I'm trying to learn assembly by translating them to assembly. I'm specifically following the tonc turorials, with a development environment I mostly copied from the gba-bootstrap repository.
I got the first example from the tonc tutorial running on mgba (a gba emulator), but I did it with a unity build. The tonc book strongly advises against this and instead recommends you assemble the files separately and link them together instead.
Right now my working code looks something like this (shortened for brevity):
// src/header.s
// copied almost verbetim from gba-bootstrap
.section .gba_crt0, "ax"
.global entrypoint
.cpu arm7tdmi
.arm
entrypoint:
/*
* gba header boilerplate
* initialize the cpu and switch to thumb mode
*/
bl main
// If main returns, reboot GBA using SoftReset
swi #0x00
// src/main.s
.include "src/header.s"
.section .text
.global main
main:
/*
* Draw colors on the screen
*/
// while(1)
1:
b 1b
// unreachable
bx lrHere's the Makefile in it's entirety because I suspect the problem is there:
NAME := main
GAME_TITLE := "GBA_TEMPLATE"
GAME_CODE := "00"
UNAME_S := $(shell uname -s)
MAKEFLAGS += --no-print-directory
PREFIX := arm-none-eabi-
AS := $(PREFIX)as
LD := $(PREFIX)ld
CC := $(PREFIX)gcc
CXX := $(PREFIX)g++
OBJDUMP := $(PREFIX)objdump
OBJCOPY := $(PREFIX)objcopy
MKDIR := mkdir
RM := rm -rf
ELF := $(NAME).elf
ROM := $(NAME).gba
MAP := $(NAME).map
GBAFIX := gbafix/gbafix
ASFLAGS := -x assembler-with-cpp \
-mthumb -mthumb-interwork \
-ffunction-sections -fdata-sections
LDFLAGS := -mthumb -mthumb-interwork \
-Wl,-Map,$(MAP) -Wl,--gc-sections \
-specs=nano.specs -T src/gba.ld \
SOURCEDIR := src
BUILDDIR := build
SOURCES := $(wildcard $(SOURCEDIR)/*.s $(SOURCEDIR)/**/*.s)
OBJS := $(patsubst $(SOURCEDIR)/%.s,$(BUILDDIR)/%.s.o,$(SOURCES))
$(BUILDDIR)/%.s.o : $(SOURCEDIR)/%.s
@echo " AS $<"
@$(MKDIR) -p $(BUILDDIR)
@$(CC) $(ASFLAGS) -MMD -MP -c -g -o $@ $<
$(ROM) : $(ELF) $(GBAFIX)
@$(OBJCOPY) -O binary $< $@
@echo " GBAFIX $@"
@$(GBAFIX) $@ -t$(GAME_TITLE) -c$(GAME_CODE)
$(ELF) : $(OBJS) src/gba.ld
@echo " LD $@"
@$(CC) -o $@ $(BUILDDIR)/main.s.o $(LDFLAGS)
@#$(CC) -o $@ $(OBJS) $(LDFLAGS) #DOESN'T WORK
$(GBAFIX) : gbafix/gbafix.c
@echo " building GBAFIX"
@cd gbafix && make
.PHONEY: clean dump
clean:
@echo " clean"
@$(RM) $(ROM) $(ELF) $(MAP) $(BUILDDIR)
@cd gbafix && make clean
dump: $(ELF)
@$(OBJDUMP) -S $(ELF)
ifeq ($(UNAME_S), Darwin)
.PHONEY: run
run: $(ROM)
@echo " RUNNING $(ROM)"
@open -a "mgba" $(ROM)
endifThe linker script is copied verbatim from gba-bootstrap because I don't really understand linkers.
When I switch to linking together all the object files and don't include header in main I still get a main.gba file that runs in the emulator, but it doesn't run the code at my main label.
I don't know if I'm doing something wrong with assembly or if this is a game boy specific problem. What confuses me though is when I objdump my linked file I see the bl main call from the header file has the same address as where my main label ended up.
Any help at all with this would be appreciated! Thanks
1
u/MagicWolfEye 4d ago edited 4d ago
It's been more than a year I guess, so I don't quite remember everything probably.
I too am too lazy to learn about makefiles or whatever, so I build every program I write with a simple build.bat
This is the one I have here
As I said, I don't remember everything, I don't remember what -MMD etc. means
Some infos:
(I am one of those people who write in C and a little bit of a plus; where you use a C++ compiler but mostly write C code, that's why I have .cpp files)
code/thumb.cpp -> This is the regular code; that will compile to thumb instructions; it'S where your main goes and most of the logic of the game
code/arm.cpp -> This is where arm code goes; given that I have a HOT_CODE macro that automatically puts stuff into arm code; I don't really have anything in here except for the IrqHandler
My main starts with this
My asm.s is where the assembly code goes. This file starts with some special interrupt codes, you can look them up somewhere,
A bit important is also intrs.h. It contains this so that you can actually call them in your code
That's about it; it's a bit much but there is quite some debugging info in there.
-DSHOW_PROFILE and -DNDEBUG are obviously specific to my code
1
u/msthe_student 4d ago
Tried running it in an emulator with a debugger?
1
u/ProfitLow5177 4d ago
Did a bit of digging and it looks like the CPU is executing blank data as instructions (0x0000 can be executed as "LSL r0, r0, #0" in THUMB instruction).
Apparently this can happen if the program accesses uninitialised memory or corrupted pointers.
1
u/HowziCanReddit 4d ago edited 4d ago
Your entrypoint is running in ARM mode, but your main routine is assembled as Thumb code. A plain bl main from ARM mode cannot correctly switch to a Thumb function (the CPU will try to execute the Thumb instructions as ARM, leading to a crash or silent failure). Even if the linker resolves the address of main, the mode mismatch prevents the code from running correctly. The gba_crt0.s from gba-bootstrap switches the CPU to Thumb mode and then calls main via a bx-style trampoline. You need to replicate that logic in your header.s