Language…
4 users online: Alex No, CONLUSH666, Rykon-V73,  Tahixham - Guests: 257 - Bots: 366
Users: 64,795 (2,377 active)
Latest user: mathew

ASM Resources and Discussion

  • Pages:
  • 1
  • 2
ASM Resources and Discussion

Hello SMWC. It's apparent that many people on here are quite new to or don't know where to get started with assembly hacking, so this thread is here to serve as a resource for learning and understanding N64 ASM code.

Assembly or ASM is the low level code that the central processing unit reads and executes to make all the magic happen, and it's something that you should be somewhat familiar with in order to make edits to the way a game (namely SM64) works.


An excerpt from the function that prints HUD elements.


The code above is probably rather daunting to those who are new to ASM (it was to me when I started), but with a bit of patience and some memorization, you could be coding like a b0ss in no time. The first thing you should know is that the emulator of choice for viewing and testing ASM code is Nemu64, for its wonderful debugging capabilities. Nemu64 is not fond of VL-Tone's extended rom for one reason or another, so one idea here would be to do your assembly editing/testing with an original SM64 rom and then port your finished code over to your project rom.

I'll be compiling a list of tutorials/references/software that should aid in N64 ASM hacking here. If you have anything that you think should be added to the list, let me know.

Tutorials:
R4300i tutorial by Tarek
Simple debugging demonstration with Nemu64 by yoshielectron
LemASM demonstration by Skelux
MIPS Instruction Reference from University of Idaho


Assemblers:

B64si Beta
Command line assembler for ram/rom, comes with notepad++ syntax highlighter

By shyguyhex - Official thread

CajeASM Stable
Command line assembler, comes with a frontend and notepad++ syntax highlighter

By Tarek701 - Official thread

LemASM - GUI assembler by Lemmy, old but commonly used and gets the job done
Renegade64 - by Viper187
sgASM w2 - by shyguyhex

Other ASM related tools:
jalFinder - by shyguyhex (cmd line utility that scans rom for JALs to provided functions, probably only useful under special circumstances)
ASM address calculator - by shyguyhex



ASM Tips

Tip 1: When coding in ASM, always remember the "negative rule". If an immediate value is greater than 0x7FFF, then the value is negative!

If we want to load 32767 (decimal) to T0, we would first believe that we have to write:

LUI T0, 0x8001

But this is false. As 0x8001 is over 0x7FFF, we actually load -32767 into T0, which we didn't want! To solve this, you simply multiply 0x8001 with 0xFFFF and we get 0x7FFF. This value is not over 0x7FFF and so we now have 32767. So:

LUI T0, 0x7FFF

is correct.

Tip 2: The above rule also applies to load/stores. Remember, that each load/store aligns a 16-bit value to 32-bit! So, if the value is negative, it subtracts one from the upper half!

Let's say we want to load a value from 0x8033B218. It's the current coin amount and a halfword value. Your first thought would be most likely:

Code
LUI T0, 0x8033
LH T1, 0xB218(T0)


However, the above would actually load the value from 0x8032B218 and not the expected 0x8033B218. So, why's that? The instruction LUI simply loads 0x8033 to the upper half of T0; T0 = 0x80330000. LH however loads the lower half, but needs to shift it to the lower half obviously. As 0xB218 is over 0x7FFF, it's negative. Now, doing a shift to left means that MIPS still needs to keep the negative value and this means that the result would be 0xFFFFB218. This negative value is then added to 0x80330000, resulting in: 0x8032B218.

So, to solve this little problem we simply add 1 to the upper half "0x8033". So, we get 0x8034. So, later the game would subtract 1 and successfully loads the value from 0x8033B218.

Tip 3: The delay slot below Branches and Jump instructions is executed BEFORE the jump/branch instruction. Sometimes the delay slot can be useful. If there's nothing important to put in there, always remember to write a NOP!

To show off an example:

JAL 0x802D48D4
ORI A0, R0, 0x000A

This would first do ORI and then a JAL. Sometimes this can save a lot of code;

Code
LUI A0, 0x254D
ORI A0, 0x44A5
LUI A1, 0x254A
ORI A1, 0x222A
JAL 0x802D48D4
NOP


could be shortened to:
Code
LUI A0, 0x254D
LUI A1, 0x254A
ORI A0, 0x44A5
JAL 0x802D48D4
ORI A1, A1, 0x254A
I'm so confused on how to make asm coding it's too hard for me even the tutorials that I saw. I can't do anything I might need a lot of help on asm coding since I'm new to it.
Originally posted by mosky2000
I'm so confused on how to make asm coding it's too hard for me even the tutorials that I saw. I can't do anything I might need a lot of help on asm coding since I'm new to it.


The real solution to every problem is:
Learning and Practice. No tutorial in this entire world could make you an ASM Expert. The tutorials and books are just there to give you an introduction. The rest is upon you. The more you practice, the better you get. It's the same like modelling, music, etc. Some do have gifted talents, some don't. Those who have some gifted talents are lucky but also need to learn the theoretical and background part. And those who aren't gifted need to learn more, but in the end know a lot about how it works.

Good points there. You can't expect to become a good ASM programmer over night. Baby steps are key.
I see well I'll try to figure out this and soon I will know how to asm code by the time the year ends.
So, hello everyone. I'm remaking my old MIPS ASM Tutorial and this time a bit less theory and more practice. If anyone is interested, click here:

MIPS ASM Tutorial

Obviously it's not done yet. But it's enough to learn the basics of MIPS.

Can someone please give me a text file with asm codes so I can try them out and see how it works? Also how do you put the behavior of any object with the asm code in it? (includes custom objects too)
Originally posted by mosky2000
Can someone please give me a text file with asm codes so I can try them out and see how it works? Also how do you put the behavior of any object with the asm code in it? (includes custom objects too)


Well, I can give you Skelux's HideHUD ASM Code. (It can be assembled with CajeASM):

http://pastebin.com/j6WvRwVz

Also, I'm not quite sure what exactly you want to do. Do you want to write a custom behavior? Usually you change the 0x0C commands in the behavior pointing to the address where your ASM code is located. Give me a few more details and I may understand what exactly you're looking for.

Originally posted by mosky2000
Can someone please give me a text file with asm codes so I can try them out and see how it works? Also how do you put the behavior of any object with the asm code in it? (includes custom objects too)


To make a behavior call an asm function, you would put a 0C command in a behavior script; for example:
0C000000 80400000
This will make the script interpreter execute whatever asm function is at 80400000 in ram.

And then your asm would look something like this:
Code
addiu sp sp -40 ; allocate stack space
sw ra 0x14(sp)  ; save return address @ sp+0x14
lui t0 0x8036   ; t0 = 0x80360000
lw t0 0x1160(t0); t0 = value at 0x80361160, which will be a pointer to the current object
sw t0 0x18(sp)  ; save pointer to your object @ sp+0x18
nop ; \
nop ; | code which effects your object can go here
nop ; /
lw ra, 0x14(sp); load return address into register ra
jr ra          ; done
addiu sp sp 40 ; free stack space
Originally posted by Tarek701
Originally posted by mosky2000
Can someone please give me a text file with asm codes so I can try them out and see how it works? Also how do you put the behavior of any object with the asm code in it? (includes custom objects too)


Well, I can give you Skelux's HideHUD ASM Code. (It can be assembled with CajeASM):

http://pastebin.com/j6WvRwVz

Also, I'm not quite sure what exactly you want to do. Do you want to write a custom behavior? Usually you change the 0x0C commands in the behavior pointing to the address where your ASM code is located. Give me a few more details and I may understand what exactly you're looking for.



I want to write a custom behavior for a custom object like a robot as an enemy, but I'm barely starting to use asm coding for my hack so I can try it out.
Originally posted by shyguyhex
Originally posted by mosky2000
Can someone please give me a text file with asm codes so I can try them out and see how it works? Also how do you put the behavior of any object with the asm code in it? (includes custom objects too)


To make a behavior call an asm function, you would put a 0C command in a behavior script; for example:
0C000000 80400000
This will make the script interpreter execute whatever asm function is at 80400000 in ram.

And then your asm would look something like this:
Code
addiu sp sp -40 ; allocate stack space
sw ra 0x14(sp)  ; save return address @ sp+0x14
lui t0 0x8036   ; t0 = 0x80360000
lw t0 0x1160(t0); t0 = value at 0x80361160, which will be a pointer to the current object
sw t0 0x18(sp)  ; save pointer to your object @ sp+0x18
nop ; \
nop ; | code which effects your object can go here
nop ; /
jr ra          ; done
addiu sp sp 40 ; free stack space


Do I copy the from here to the Lemasm program where the code usually starts, but one thing that I can't understand is what the codes does like what will happen to the object(s) will it bounce, rotate or do other stuff like that?


So this is basically what the entire language is going to look like when it's finished.

I would also like to add a keyword like "crc_restore" (or "crc" because all of the keywords are 3 letters long) which would auto restore the checksum at 0x10 after patching. If anyone can hook me up with how the crc algorithm works I'd really appreciate it #tb{^V^}

Edit: just found an app called RN64crc which should do the job
Originally posted by shyguyhex
@mosky

Here's a sample asm behavior using the template I posted which will make an object slowly rotate:
Code
addiu sp, sp -40 ; allocate stack space
sw ra, 0x14(sp)  ; save return address @ sp+0x14
lui t0, 0x8036   ; t0 = 0x80360000
lw t0, 0x1160(t0); t0 = value at 0x80361160, which will be a pointer to the current object
sw t0, 0x18(sp)  ; save pointer to your object @ sp+0x18
lh t1, 0xD6(t0)  ; t1 = halfword @ object+0xD6 (object y rotation)
addiu t1, t1, 0x0100 ; add 0x100 to t1
sh t1, 0xD6(t0) ; store t1 at current object y rotation
jr ra          ; return ~
addiu sp, sp 40 ; and free stack space


Try writing this code at 00100740 in rom to replace the sign's behavior




Edit: just noticed that a lot of other objects use the code at 00100740 (it's a routine for collision handling), so you'll see other random stuff rotating as well #w{:>}


Cool thanks for posting the asm code, by the way please do more I can't wait to see what you have in store right now.
I'm happy to see you satisfied, mosky. But please avoid writing your replies in the middle of the quote. This just confuses people and others have to search all through the quote to find your reply. Also avoid quoting huge texts. Better you quote a specific part of the text your reply is referring to.

My apologies if this is the wrong thread but wasn't there a tutorial on how to use c and if so, can it still do everything asm can?
Originally posted by Ctoanus
My apologies if this is the wrong thread but wasn't there a tutorial on how to use c and if so, can it still do everything asm can?


C can't accomplish near as much as ASM. also, by learning C rather than ASM, you won't be able to debug later and will only be able to code small objects.
it basically only allows you to chain functions from the original together.
Originally posted by Kazeshin
Originally posted by Ctoanus
My apologies if this is the wrong thread but wasn't there a tutorial on how to use c and if so, can it still do everything asm can?


C can't accomplish near as much as ASM. also, by learning C rather than ASM, you won't be able to debug later and will only be able to code small objects.
it basically only allows you to chain functions from the original together.

Thanks for the information. Which assembler do you recommend?
Originally posted by Ctoanus

Thanks for the information. Which assembler do you recommend?


cajeasm is fastest to get your codes into the rom. i use that one to code my objects and so on.
LemASM is good for small edits or to read stuff out.
Nemu64 is the best choice for debugging purposes.
  WHY I HAVEN'T SEEN THIS THREAD BEFORE?
After a productive day, I've finally learned the basics of MIPS Assembly, and all that i can say is... ... Tarek i love you #ab{:S}

    See my hack development here!: Super Mario and the missing memories.
Originally posted by Mariohacker14
WHY I HAVEN'T SEEN THIS THREAD BEFORE?
After a productive day, I've finally learned the basics of MIPS Assembly, and all that i can say is... ... Tarek i love you #ab{:S}


Oh thx. Finally someone other than kaze and shyguy who realizes that I actually made an assembler and a mips tutorial, lol.

So, if you do stuff with MIPS, you better use my CajeASM;
http://www.smwcentral.net/?p=viewthread&t=73708

I'm making a new mips tutorial currently (yet again) which is gonna be way better. The current tutorials here are good enough, but I believe there are one or two mistakes in them.

Originally posted by Kazeshin

C can't accomplish near as much as ASM. also, by learning C rather than ASM, you won't be able to debug later and will only be able to code small objects.
it basically only allows you to chain functions from the original together.


I agree. If you code in ASM, you have a way better control of what's going on. However, you could code big objects in C too if it weren't for the huge compiled MIPS output you get once you compile your code. Currently I've re-made the n64toolchain (completely re-compiled gcc for cross-compiling to mips, etc. + extended headers a lot, rewritten the linker) and the output from my Shop Guy seems to be way smaller than the older toolchain from 2008 by GZRT and works wonderful.

About the debugging part:
Well, that's most likely why I always say, in case you code in C, to have a good knowledge of MIPS too. If you have, you can use Nemu64 and jump to the compiled MIPS code and debug it and look what's wrong. C makes things easier, that's for sure, but it can get a real pain if you're doing it without having some ASM knowledge.

I'm getting interested in learning how to code in ASM, Just downloaded CajeASM 7.24 and well I saw this video also:
https://www.youtube.com/watch?v=DnFxpRwT0Gc

My question is, I see you use a highlighter, how can I implement it to my notepad?(I only downloaded it for that purpose hehe)

and I would like to have some sample codes in order to see how they work in SM64, for example a custom object, how to make a model rotate, or move, detect collision, etc

I'm really interested on this, I'll do whathever to learn ASM, I just want to have some help at start #w{:>}
Working on SM64: A New Adventure
https://www.youtube.com/watch?v=ZxVwAPAQdnc
http://i.imgur.com/qblZTfb.png
  • Pages:
  • 1
  • 2