#7809 - Lupin - Wed Jun 25, 2003 1:31 pm
I watched my compiled gcc source and I found this bit of instructions (right at the beginning of the func):
mov ip, sp
stmfd sp!, {r4, r5, r6, r7, fp, ip, lr, pc}
sub fp, ip, #4
mov r0, #256
strh r0, [fp, #-34] @ movhi
now, I'm wondering an little bit, because of 2 things:
1) What value does fp store? ip/sp aren't initialized, i copied the code right after the function label...
2) I thought the second number/register in these brackets is something like an byte-index or so, but why does the code use an negative index?
this code should put 256 into an member of an data structure. Even though I defined the structre in my C-code, it seens that it doesn't need to get declared within the asm instructions...
How could I handle something like structs in ASM?
#7831 - tepples - Thu Jun 26, 2003 6:27 am
Lupin wrote: |
I watched my compiled gcc source and I found this bit of instructions (right at the beginning of the func):
mov ip, sp
stmfd sp!, {r4, r5, r6, r7, fp, ip, lr, pc}
sub fp, ip, #4
mov r0, #256
strh r0, [fp, #-34] @ movhi
now, I'm wondering an little bit, because of 2 things:
1) What value does fp store? ip/sp aren't initialized, i copied the code right after the function label...
|
'sp' is the stack pointer. GCC seems to be using 'fp' as some sort of stack frame pointer (analogous to 'bp' of i386?).
Quote: |
2) I thought the second number/register in these brackets is something like an byte-index or so, but why does the code use an negative index? |
The 'fp' seems to point to the end of the stack frame, which means that offsets to structures would be negative.
Quote: |
Even though I defined the structre in my C-code, it seens that it doesn't need to get declared within the asm instructions... |
A struct is compiled to a set of offsets; its structure doesn't necessarily show up in any object file.
Quote: |
How could I handle something like structs in ASM? |
Define the name of each field as an offset from the beginning of the struct. Some assemblers have macros to do this automatically.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7839 - Lupin - Thu Jun 26, 2003 1:05 pm
tepples, could you perhaps give me an little example of an very basic struct wich shows how to use them, I don't understand how to handle this, because I never worked with stack stuff before (or maybe an nice tutorial about this?)
#7873 - tepples - Fri Jun 27, 2003 7:43 am
I'm not that experienced with doing complicated stuff in assembly language. I just write my programs in C (the optimizer in DKA R5b3 is decent) and save the assembly for BIOS calls or 32x32=64 bit multiplies.
To get ideas, try writing struct code in C and see what it turns into in assembly (gcc -S instead of gcc -c will output the assembly file that gcc sends to as).
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7898 - Lupin - Fri Jun 27, 2003 7:53 pm
yeah, I got the code I posted above from gcc asm output, but I still don't understand it, because it uses the stack (I at least think so :))...
You multiply 32x32=64? Do you really need that high precission on an gba, since the gba doesn't support 64bit by hardware that might be slow I thought.
#7901 - tepples - Fri Jun 27, 2003 8:23 pm
Lupin wrote: |
yeah, I got the code I posted above from gcc asm output, but I still don't understand it, because it uses the stack (I at least think so :))... |
Try writing struct code that uses a global variable. Try writing struct code that uses a data structure passed by reference. Then look at the asm that GCC generates. Don't skip straight to code that uses a struct on the stack.
Quote: |
You multiply 32x32=64? Do you really need that high precission on an gba, since the gba doesn't support 64bit by hardware that might be slow I thought. |
Sometimes I need 32x32=middle 32 bits or 32x32=upper 32 bits. The normal mul instruction gives the lower 32 bits.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#7909 - Lupin - Sat Jun 28, 2003 10:25 am
i see, the stack is the place where function variables/structures go, right?
#7925 - funkeejeffou - Sat Jun 28, 2003 7:20 pm
Stack is just an adress pointing at the end of IWram memory. You use it to store temporarly variables by using the "push" or "pop" instruction.
Push to write to IWram the value contained in a register
Pop to restore a register with the value in the stack.