#6018 - funkeejeffou - Thu May 15, 2003 9:54 am
I've been coding for a week now on asm and i can get some graphics routines running fine now, but independantly(a routine for bresenham, another to draw an image...).
Since I'm new to asm and that I've been mostly coding in C, some questions went to me :
- how do we represent negative numbers?
- how do we create variables?
- how do we switch the proc to thumb or arm mode?
- how do we place our variables in iwram, ewram(ie is there an equivalent to malloc)?
- how do we place code in iwram, ewram?
- how can you represent the equivalent of a C structure in asm?
- what are pools?
- do you use an asm editor(wich one?)
- what is the general structure and organisation of a large program?
I know it is a lot to answer, but I've been browsing the asm tutorials as well as the arm docs, and I still can't answer precisely those.
If someone could please just guide me, I'm actually passing a 3D engine from c to asm and it is not so simple :)
Thanks to anyone who will answer me.
PS : I'm using goldroad 1.7
#6025 - DekuTree64 - Thu May 15, 2003 4:57 pm
- how do we represent negative numbers?
It's called two's compliment. It means the number looks the same as positive, except the bits are opposites, except you have ato add one to the bitwise-opposite to get the negative. -1 (in 8-bit binary) would be 11111111, because if you add 1 to that, it overflows and becomes 0. Likewise, -5 would be 11111011, because +5 is 101, so invert that and you get 11111010, but if you add 101 to that, you get 11111111, which is-1, not 0.
I hope that made some sort of sense^^;
- how do we create variables?
Use the stack pointer. It's called sp, or r13. Just different names for the same register. It's full-descending, so the word it currently points to is considered used, so don't overwrite it. And decending means it tharts at the top and grows down, so like to declare 2 4-byte of variables, you'd use sub r13, r13, #8, and then use str/ldr rWhatever, [r13], and str/ldr rWhatever, [r13, #4] to access them. You have to keep track of how many things you've pushed onto the stack at the current point in your code.
Global vars are accessed by labels though, so use like
ldr rWhatever, =gVar //the address of the variable
and then
ldr rWhatever. [rWhatever]
to get the value in it. I've never actually created a global var in ASM though, but I think it would be done like
.section .iwram (or ewram)
.global v1
v1:
.word 0 (the initial value)
.global v2
v2:
.word 1 (initialize to 1)
But keep in mind that's ust guessing, so don't blame me if it doesn't work^^
- how do we switch the proc to thumb or arm mode?
At the start of the functin, use .ARM or .THUMB to tell it which instruction set the following code uses. When calling thumb functions, use ldr rWhatever, =address
orr rWhatever, rWhatever, #1
bx rWhatever
The lower bit of the address you bx to tells it which instruction set to use.
- how do we place our variables in iwram, ewram(ie is there an equivalent to malloc)?
The stack is in IWRAM, and EWRAM is free to do whatever you want with, though I'm pretty sure you can use .section .ewram to put global vars there the same as IWRAM. I treat is like a giant 256K stack, and I have a memory manager to claim a little of it for allocating little bits at a time when needed.
- how do we place code in iwram, ewram?
.section .iwram (or .ewram) at the start of the function
- how can you represent the equivalent of a C structure in asm?
As far as I know, you just use an array-type thing and keep track of the member offsets yourself
- what are pools?
That's where literals are stored, like for accessing global vars. At the end of your function, put
.pool
gVar1:
.word gVar1
gVar2:
.word gVar2
then ldr rWhatever, =gVar1 loads the word at that label there. The linker replaces the names with the actual addresses of the variables.
Actually you don't need to do that explicitly, the assembler does it for you. You would need to do it yourself though if you have a long function so the pool at the end is out of range of ldr's immediate value range at the start of the function. Then you could put the pool right in the middle, and just branch over it when your code gets there.
I wouldn't swear to anything I just said though, anybody want to verify it?
- do you use an asm editor(wich one?)
I use MSVC++, cause it has automatic indenting, but you could use notepad or anything else.
- what is the general structure and organisation of a large program?
Never written a whole program in ASM.
Hope hat helps, I was half guessing some of the time, but I'm pretty familiar with the ways of ARM ASM though, so it's probably all at least close to right^^
#6119 - funkeejeffou - Sat May 17, 2003 2:31 pm
Thanks a lot you too for your replies, i can now visualise a little bit more how am i gonna code the engine.
Just some other questions and i'll stop bother you :
When i want to test if a 32bits variable is negative, should i check if it is more than 0x80000000 (ie 2power31), or can the assembler figure it out itself if i write
"cmp r0, #0"
"ble loop"
Also, if i want to use local variables, i've seen that i must declare them within a "textarea":
@textarea 0x300000(Ram adress)
var1 ....
var2 ....
@endarea
the problem is that if in another functiun i have new local variables, defining this :
@textarea 0x300000(Ram adress)
var3 ....
var4 ....
@endarea
would make var3 equivalent to var1 as well as vr4 equivalent to var2
Should I code a functiun equivalent to malloc so that i keep trace of how much memory i have still left in each type of ram?
What does bx exactly do? Does it make the jump to the label and set the end bit to its inverse value(set to 1 if 0, set to 0 if 1), so that the proc is switched to thumb or arm?
Still can't figure out how to switch the CPU mode, putting
"@arm" or "@thumb", doesn't seem to be enough, and "@thumb" generates compilation errors(don't know why as the code compiles in arm mode...)
Thanks again Torne and Dekuthree64 for your attention, hope you'll help me again.
PS : Are you too coding in GNU ASM, your not using the goldroad syntax (Goldroad doesn't have ".section" stuff, right?)