Quote:
Originally Posted by Knight of Duty
How do i make my routine read from a table? I think it would be different when hard coded or calling via script.
|
You need to know the format of the table before you do anything. Most of the time tables in ROM are very easy, while tables in RAM require some sort of field for Table length. This is because
In ROM, it's rather simple to read pointer and data it look like this:
Code:
main:
mov r2, #0x0
loop:
ldr r0, .TableStart
lsl r1, r2, #0x2 @data length (I'm assuming it's a pointer, so multiply by 4)
add r0, r0, r1
ldr r0, [r0] @new pointer in table
lsr r1, r0, #0x18
cmp r1, #0xFF @end of table, you may want to use 00 00 00 00 as the ending bytes, however.
beq end
@right now r0 contains a valid pointer in the table
@you can bl to any other sub routines that utilize it here
next:
add r2, r2, #0x1 @increment table counter
b loop
In RAM, depending on the contents of the table you may need a table counter. For example, in my Roaming Pokemon data structure, it was not always 100% chance that the last X bytes would be "00" and on top of that I want to be able to see if an Xth one is there (I don't want to be loading garbage data). So a table counter is very nice. However, if you're just writing something like a short table of strings, you can determine the end of the table because 0xFF is the string terminating byte.
Code:
main:
ldr r3, .TableLength
mov r2, #0x0
loop:
cmp r3, r2 @check if we're finished the table
beq end
ldr r0, .TableStart
lsl r1, r2, #0x2 @data length (I'm assuming it's a pointer, so multiply by 4)
add r0, r0, r1
@right now, r0 contains a pointer to the start of the current data
@you can bl to any other sub routines that utilize it here
next:
add r2, r2, #0x1 @increment table counter
b loop
You'll notice that I didn't need to check if it was the end of the table because I already know the length of the table. 1 extra byte in RAM space is saving up operation time. Worth? Yes.
Finally, the last and probably the best way is if you already know which index you want.
Code:
main:
ldr r0, .TableStart
mov r1, #0x[data length]
mov r2, #0x[date index]
mul r1, r1, r2
add r0, r0, r1 @start of the new data
Hope this helps. If you have questions, feel free to ask here :)