View Single Post
Old 18th August 2014, 01:30 AM   #2
DoesntKnowHowToPlay
Newbie
 
DoesntKnowHowToPlay's Avatar
 
Join Date: Jul 2014
Posts: 6
DoesntKnowHowToPlay
Default

I don't have experience with 65c816 ASM, so I don't know how much help I'll be able to provide, but:

Quote:
Also, I don't really know how to create compiler variables, like I'm pitifully attempting to do with FREESPACE. My usual ";"-style comments don't seem to fare well with the compiler either, and I don't know what the alternative would be.
Hashtags (#) work to denote comments, although only at the start of a line. As for defining compiler variables, I'm not sure if you can; maybe someone else knows?

Quote:
However, any of you could easily see precisely why this won't compile. I'm not sure when to use "mov" and when to use "ldr" (one for values and the other for loading values at registers, respectively?).
"mov" is used to move the value of one register into another, or to place an immediate byte value in one register. "ldr" is used to load a word (4 bytes) from a pointer stored in a register into another (possibly the same) register, or to load a word stored in ROM. A few examples:

Code:
mov r0, #0x64
mov r1, #0x77

#r0 = 64, r1 = 77

mov r0, r1

#r0 = 77, r1 = 77

ldr r0, .ArbitraryValue
ldr r1, .ArbitraryPointer
b Next

.align 2
.ArbitraryValue: .word 0x12345678
.ArbitraryPointer: .word 0x08000010

#r0 = 0x12345678, r1 = 0x08000010

Next:
ldr r0, [r1, #0x0]
ldr r2, [r1, #0x4]
ldrh r3, [r1, #0x0]
ldrb r4, [r1, #0x0]

#r0 = 0xAD09E484
#r1 = 0x08000010
#r2 = 0x988B2411
#r3 = 0x0000E484
#r4 = 0x00000084
Quote:
I understand that r14 (or lr) is used to return from jumping somewhere, but I'm still not sure how to actually JUMP to a location I want in the first place. I tried using "bl" but the compiler keeps telling me that it's out of range; why would this be the case if it's a LONG jump??
"bl" is indeed branch long but it actually does have distance restrictions, and you are likely to hit those if you insert code in FR free space and call functions the game uses. The typical way to get around that for what you're doing is to use the opcode "bx", like so (offsets are fabricated but the hook must be 4-aligned):

Code:
.org 0x12344
ldr r1, .FunctionAddr
bx r1

.FunctionAddr: .word 0x08C80001


.org 0xC80000
#meaningful code that does useful stuff
ldr r2, .ReturnAddr
bx r2

.align 2
.ReturnAddr: .word 0x0801234d
Another option is to repoint some tables and insert the routine closer to where you're hooking in; in my experience the first 3 or so megabytes are bl-safe.

Last edited by DoesntKnowHowToPlay; 18th August 2014 at 01:32 AM.
DoesntKnowHowToPlay is offline  
Likes Jambo51 liked this post