Introduction to MIPS ASM A tutorial by Arthurtilly

Introduction to MIPS ASM A tutorial by Arthurtilly

Previous knowledge Before you start this tutorial, it is EXTREMELY important that you understand all of the following: ● ● ● Hexadecimal numbers Bytes and bits ROM / RAM

What is MIPS ASM? MIPS ASM is one of the many different types of assembly languages, and it is the one that is used by Super Mario 64. Assembly is the second lowest level of programming language after machine code. In fact, ASM is almost one-to-one with machine code. But luckily, ASM is formatted to make it slightly easier to understand than a bunch of 0’s and 1’s. This tutorial aims to help people wishing to make better SM 64 ROM hacks by teaching them how to use MIPS ASM to code their own custom objects and many other things. This is the first tutorial in the series, and it will provide a basic explanation of how MIPS ASM works.

ASM as a programming language If you have had any experience with normal programming languages (Java, C, Python, etc. ) then you should know roughly how they are set out. You can make variables, if-statements and loops. But ASM is much more limited in what it can do. In fact, ASM can pretty much only do the following: ● ● Read or write anywhere in RAM Read or write to one of the few “registers” it uses Perform simple arithmetic Jump to somewhere else in the code With so few options, it seems hard to be able to do much with ASM, which is why it’s harder to learn than most programming languages. But the very basic functionality makes it much quicker to learn as well.

How SM 64 uses ASM Super Mario 64 uses ASM in mostly everything. For example, every behaviour command has an ASM function it uses. Every collision type calls its own ASM function when Mario’s standing on it. But we’ll be looking mostly at the functions called by the 0 x 0 C command in behaviour scripts. Here’s Mario’s behaviour script; everything between the 08 and 09 commands is executed every frame. (Don’t worry if you don’t understand this, I’ll be going over behaviour scripts in tutorial 4. 5. 21 CCC 0: 00 00 21 CCC 4: 10 05 00 00 21 CCC 8: 11 01 01 00 21 CCCC: 11 03 00 01 21 CCD 0: 23 00 00 25 00 A 0 21 CCD 8: 08 00 00 00 21 CCDC: 0 C 00 00 00 80 2 C B 1 C 0 21 CCE 4: 0 C 00 00 00 80 29 CA 58 21 CCEC 0 C 00 00 00 80 2 C B 2 64 21 CCF 4: 09 00 00 00 (Blue: command, green: parameter, red: ignored)

How SM 64 uses ASM The parameters passed by the 0 x 0 C commands in the main loop are RAM addresses for ASM functions. The first one is an unused debug function, the second one controls movement and the third controls interaction with objects. The one we’re interested in is the debug function at 0 x 802 CB 1 C 0. Since Mario’s object is always loaded, this function is executed every frame, meaning it’s great for experimenting with ASM.

Calculating ROM addresses But with this RAM address, how do we know where the ASM should go? We have the RAM address for the function, but this doesn’t help us find where we assemble the code into. Luckily, SM 64 loads everything in ROM from 0 x 1000 to about 0 x. EE 000 into RAM with an offset of 0 x 80245000. So for any RAM address between 0 x 80246000 to about 0 x 80333000, we can simply subtract 0 x 80245000 to get the ROM address. For our debug function, we do 0 x 802 CB 1 C 0 - 0 x 80245000 to get the ROM address of 0 x 861 C 0.

Writing ASM code We’re now ready to start writing some ASM code: . orga 0 x 861 C 0 The. orga command simply tells the assembler where to start writing in the ROM.

Writing ASM code Now let’s add some very important commands: . orga 0 x 861 C 0 ADDIU SP, 0 x. FFE 8 SW RA, 0 x 14(SP) LW RA, 0 x 14(SP) JR RA ADDIU SP, 0 x 18 These commands are vital for pretty much any ASM function, and we’ll look at what they do later. But for now, let’s look at the structure of this ASM code.

Understanding ASM code Let’s look at the different types of objects in the code: . orga 0 x 861 C 0 ADDIU SP, 0 x. FFE 8 SW RA, 0 x 14(SP) LW RA, 0 x 14(SP) JR RA ADDIU SP, 0 x 18 The blue objects are the commands, the red objects are registers and the green objects are immediate hex values. The hex values are simple enough, but what do the other letters mean?

Understanding ASM code Let’s see what the letters mean: ADDIU - Add Immediate Unsigned SW - Save Word LW - Load Word JR - Jump to Register RA - Return Address SP - Stack Pointer The blue commands and registers all stand for commands. Now let’s put our code into the ROM!

Assembling ASM code Copy and paste this code into a new text file, and give it the “. asm” extension. . orga 0 x 861 C 0 ADDIU SP, 0 x. FFE 8 SW RA, 0 x 14(SP) LW RA, 0 x 14(SP) JR RA ADDIU SP, 0 x 18 Now we have our ASM file, but how do we assemble it? Download Armips and open it.

Assembling ASM code Now load your SM 64 extended ROM and your ASM file into Armips, and click “Assemble”. Now load your ROM into an emulator. You’ll notice…. . . it doesn’t crash! The code we just wrote is a “function wrapper”, and is required for the subroutine to work correctly. If it is not present, the game will crash. In the next tutorial, we’ll look at the different registers in MIPS ASM, and what they do. Goodbye for now!
- Slides: 13