(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
04-29-24 04:11 PM
0 users currently in ROM Hacking.
Acmlm's Board - I3 Archive - ROM Hacking - Help in understanding how to do a bitfield properly. New poll | |
Add to favorites | Next newer thread | Next older thread
User Post
Xenesis

Blipper








Since: 11-19-05
From: Australia

Last post: 6280 days
Last view: 6280 days
Posted on 11-18-06 03:05 AM Link | Quote
Okay, so I'm learning how to read and write instructions in THUMB and ARM for GBA hacking.

Now, I'm coming to the point where I'd like to start trying out to make my own routines (or whatever they're called).

Now the THUMB Instruction set is quite helpful in reading and understanding what things do..but I don't quite understand how to assemble an instruction correctly by hand..

Now, I know that you've got two bytes per instruction eg, 2304 or whatever, which gives you 16 bits to work with. The GBA is little endian, so that would give the instruction 0423 in the ROM.

Now, I'm having trouble turning something like this:

Opcode Format
Bit Expl.
15-11 Must be 00011b for 'add/subtract' instructions
10-9 Opcode (0-3)
0: ADD Rd,Rs,Rn ;add register Rd=Rs+Rn
1: SUB Rd,Rs,Rn ;subtract register Rd=Rs-Rn
2: ADD Rd,Rs,#nn ;add immediate Rd=Rs+nn
3: SUB Rd,Rs,#nn ;subtract immediate Rd=Rs-nn
Pseudo/alias opcode with Imm=0:
2: MOV Rd,Rs ;move (affects cpsr) Rd=Rs+0
8-6 For Register Operand:
Rn - Register Operand (R0..R7)
For Immediate Operand:
nn - Immediate Value (0-7)
5-3 Rs - Source register (R0..R7)
2-0 Rd - Destination register (R0..R7)

Return: Rd contains result, N,Z,C,V affected (including MOV).
Execution Time: 1S

into a usable instruction. I don't really understand how to set the correct bits, as the way I know how to deal with bitfields is like this:

01 = Something
02 = Another Thing
04 = Whatever
08 = Majiggy

And you add the set bits together to get the first nibble. And the second nibble would be
10 = A
20 = B
40 = C
80 = D

And then you just continue adding the set bits you want, so like 88 = D Majiggy or whatever.

I know that this is stupidly long winded, but I'd like to understand the process properly. ;


(edited by Xenesis on 11-18-06 02:06 AM)
MathOnNapkins

1100

In SPC700 HELL


 





Since: 11-18-05

Last post: 6279 days
Last view: 6279 days
Posted on 11-18-06 05:17 AM Link | Quote
I would not recommend constructing these opcodes by hand, it would be better to use an assembler and a disassembler. Even reading these ARM/THUMB opcodes is a tedious process, let alone putting one together. Unless of course, your brain is really good at working with and seeing these bitfields.

To begin:

The particular set of details you have given in your post is like an instruction manual on how to construct a particular type of operation - and add or subtract instruction.

let's go through it bitfield by bitfield.

Bit Expl.
15-11 Must be 00011b for 'add/subtract' instructions

This indicates that in bits 11 through 15, the value 00011b must be present. This means bit 15 is not set, nor is bit 14 or bit 13, but bit 12 and 11 are set.

10-9 Opcode (0-3)
0: ADD Rd,Rs,Rn ;add register Rd=Rs+Rn
1: SUB Rd,Rs,Rn ;subtract register Rd=Rs-Rn
2: ADD Rd,Rs,#nn ;add immediate Rd=Rs+nn
3: SUB Rd,Rs,#nn ;subtract immediate Rd=Rs-nn
Pseudo/alias opcode with Imm=0:
2: MOV Rd,Rs ;move (affects cpsr) Rd=Rs+0

This indicates whether it is an add or a subtract operation, and whether you are adding a register to a register, or adding a register to an immediate (fixed) value. The psuedo/alias thing is just a note indicating that ADD Rd, Rs, #$00 is the same as MOV Rd, Rs. Example: 11b would mean SUB Rd, Rs, #nn

8-6 For Register Operand:
Rn - Register Operand (R0..R7)
For Immediate Operand:
nn - Immediate Value (0-7)

Using the information in bits 9-10, we have two choices in this bitfield. If we're adding register on register, then this indicates which register Rn is. example: 010b in this field would mean that Rn = R2. In the immediate case, it would add a fixed value ranging from 0 to 7 to Rs, then store it in Rd. Example: 110b in this field would mean we do the formula Rd = Rs + 6

5-3 Rs - Source register (R0..R7)

This sets which value s takes on in Rs. Example: in this field a value of 101b would mean that Rs = R5

2-0 Rd - Destination register (R0..R7)

similar to the previous field, this indicates what d is in Rd.

So let's say you wanted to do the operation SUB R1, R2, R3

field 11-15 must be 00011b
field 10-9 must be 01b
field 8-6 must be 011b
field 5-3 must be 010b
field 2-0 must be 001b

Put all that together that's:

00011010 11010001b (spaced for clarity)
which in hex is $1AD1
which would be D1 1A in the rom...

hope that clears things up for you a bit more.


(edited by MathOnNapkins on 11-18-06 04:21 AM)
Dwedit

Rope
フクト オン フォニクス








Since: 11-17-05
From: Chicago!

Last post: 6281 days
Last view: 6280 days
Posted on 11-21-06 12:15 AM Link | Quote
NO$GBA has a built in "Change Instruction" command, and it works pretty well.
Add to favorites | Next newer thread | Next older thread
Acmlm's Board - I3 Archive - ROM Hacking - Help in understanding how to do a bitfield properly. |


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.011 seconds; used 364.87 kB (max 431.61 kB)