#7164 - hnager - Wed Jun 11, 2003 3:29 am
I'm new to assembly and would like to be able to work with it inline in my c/c++ - is this possible with gcc/g++? currently I've been using the most basic of makefiles:
path=C:\GBA\bin
g++ -o main.elf main.cpp -lm
objcopy -O binary main.elf main.bin
pause
what would I need to do differently inorder to use inline asm?
#7165 - DekuTree64 - Wed Jun 11, 2003 4:59 am
Makefiles shouldn't have anything to do with it, aside from setting the instruction set (ARM or THUMB), which I think is ARM by default. Are you getting compiling errors or what? If you just need to know how to use inline ASM, it done like
Code: |
void func()
{
asm volatile("
mov r0, #5
" : : : "r0");
}
|
If I remember right (haven't used it in a while), after the first : you put the output arguments, like "=m" (var1), "=m" (var2), and then use str r0, %0 for the first one, or str r0, %1 for the second. Then after the second : you put the input arguments like "r" (var1), and then access them like ldr r0, %2 (assuming you still had %0 and %1 being the output args). Then after the third : you put all the registers you changed, so the compiler knows what you're doing.
That's about all I know on how to use it, since I've moved over to writing ASM things in pure ASM, and then just calling them from C code. That way I know exactly what's going on (I've heard GCC wastes a lot of time when setting up for inline ASM blocks)
#7194 - hnager - Wed Jun 11, 2003 10:50 pm
Elabprate more on what you mean by writing pure ASM and calling them from C. This may be more what I'm looking to do...I want an easier way to handle sprites (graphics/palette) and I've seen some of this accomplished via asm - but dont exacly understand how it works. As a side question - any good examples of this floating around - hopefully handling 16 color sprites aswell as 256?
thanks.
#7196 - DekuTree64 - Thu Jun 12, 2003 12:30 am
Make a .s file and compile it just like you would a C file. Define functions like
.global funcName
.arm (or .thumb)
.align 4 (or 2 if it's thumb)
.thumb_func (if it's thumb, otherwise remove this line)
funcName:
And then write your function. Pretty simple. I think you can make it a .S (not .s) and it will run the C preprocessor on it, so you can use #define and stuff. Never tried it myself, but I've heard that's how it works.
Oh, and you can have different instruction sets in the same file. Whenever you put .arm in a file, from then on it will be treated as ARM, and if you put .thumb, it will be treated as THUMB. Just be sure to return with a bx lr if you want your code to be interworkable (where it can be called from ARM or THUMB and return properly either way)
#7197 - hnager - Thu Jun 12, 2003 2:00 am
hmmm, this may be a little more than I can take in right now - in a lot of cases I've seen asm compiled along with c by means of the makefile and extern variables. I'll just keep on working on asm tutorials and see if my lack of asm knowledge is why I'm confused about the methods used for importing raw files and palettes.
Thanks.
#7237 - DekuTree64 - Thu Jun 12, 2003 7:28 pm
Code now, worry later. That's the best way to learn stuff like this. Just put that first block of stuff into a .s file, and write a little function. Just do something like
Code: |
.global Five
.arm
.align 4
Five:
mov r0, #5
bx lr
|
And in your C file, put extern int Five();, call it, and print the return value. If it prints a 5, then it works, and you'll be much less worried about it^^
Whatever is in r0 is the return value, BTW.
Also, I thought ARM was much easier to learn with than THUMB, so try it first. Less restrictions, so less unexpected errors to worry about.