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 lr
Here'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)
endif
The 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