gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > C to ASM

#23602 - ProblemBaby - Fri Jul 16, 2004 1:51 am

Hi iam trying to convert my whole GBA library to ASM
to make it smaller and faster:D

but since iam new ASM I need to ask a couple of things
1.
First when I wrote my code in ASM it went bigger then the C-code
but then I wrote .thumb. And Yeah it got much smaller. But it didnt work=/ First I got compile errors from this kind of line: mov r0, r0, lsl #2
why? well I changed it and wrote lsl r0, r0 #2
after that I didnt get errors but the emulator (no$gba) said the rom-image has crashed.. I just wonder why??? are the instructions not equvalent (dont know how it is spelled)

2.
at GbaGuys asm tutorials I read that it was possible to include the defines that was written in C well I can include the file but i got undefined blah blah when I try to use them. I included it like this:
.include "gba_regs.h", why??

Thanks in advance

#23603 - poslundc - Fri Jul 16, 2004 2:02 am

ProblemBaby wrote:
Hi iam trying to convert my whole GBA library to ASM
to make it smaller and faster:D


That is a very bad reason. In almost any game, 90% of of the processor's time is spent on 10% of the code. It is wasteful to optimize the 90% of the code that is only being run 10% of the time. ASM is much better suited to speed critical tasks. But anyway.

Quote:
but since iam new ASM I need to ask a couple of things
1.
First when I wrote my code in ASM it went bigger then the C-code
but then I wrote .thumb. And Yeah it got much smaller. But it didnt work=/ First I got compile errors from this kind of line: mov r0, r0, lsl #2
why? well I changed it and wrote lsl r0, r0 #2
after that I didnt get errors but the emulator (no$gba) said the rom-image has crashed.. I just wonder why??? are the instructions not equvalent (dont know how it is spelled)


ARM and Thumb instruction sets are very different and have different rules to them. You cannot expect your ARM code to assemble properly just by changing the .arm directive to .thumb; you need to recode it.

Quote:
2.
at GbaGuys asm tutorials I read that it was possible to include the defines that was written in C well I can include the file but i got undefined blah blah when I try to use them. I included it like this:
.include "gba_regs.h", why??


Take a look in your gba_regs.h file. See all those pointers and data types? Those things don't exist in assembly, they exist in C. C and assembly are two very different languages. That's why.

Dan.

#23615 - DekuTree64 - Fri Jul 16, 2004 7:56 am

Yeah, you can write a plenty fast game in pure C almost all the time. ASM is generally only needed when you're doing crazy stuff like 3D, or for sound mixers that just have a whole lot of repetitive work to do and usually waste a whole lot of time doing it if written in C.

That said, I think it's a great idea to rewrite your library in assembly^_^ Not for the purpose of speeding it up, but for learning and having fun doing it. Small hunks of code like generic libraries tend to be more self-contained and easy to work with than writing a whole game, but by the time you finish it you should be used to the ASM style enough to start thinking more in algorithms than struggling with individual instructions
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#23625 - ProblemBaby - Fri Jul 16, 2004 12:25 pm

But it is much 3d/math in my lib and run a bit too slow I think and of course i want to learn asm..

1.
isnt ldr, str, ldrh etc.. the same things if I switch or wahts the difference?
Is it two totally different instruction sets???
where can I read about it??

2.
why is it possible to compile? he said in his tutorials that you can include your old c header. but it doesnt work so...
what should I do then?[/quote]

#23629 - poslundc - Fri Jul 16, 2004 2:17 pm

ProblemBaby wrote:
1.
isnt ldr, str, ldrh etc.. the same things if I switch or wahts the difference?


There are different restrictions on the registers you can use, and possibly the addressing modes as well.

Quote:
Is it two totally different instruction sets???
where can I read about it??


http://www.arm.com/documentation/ARMProcessor_Cores/index.html (the GBA uses an ARM7TDMI)
http://www.gbadev.org/docs.php?section=arm

Quote:
2.
why is it possible to compile? he said in his tutorials that you can include your old c header. but it doesnt work so...
what should I do then?


C and assembler are two different languages. If you wrote code in Visual Basic and just #included it into a C program, would you expect it to work?

You can use the #include and #define preprocessor statements if you name your assembly file with a capital .S extension (which tells gcc to run your file through the preprocessor first), but that's about it.

Dan.

#23636 - Miked0801 - Fri Jul 16, 2004 5:50 pm

lol - reminds me of when I had a C64. I was reading from Compute's Gazette magazine and they had some assembly listings for a game. I thought cool and started typing the assembly into the basic interpretor. It didn't work too well :)

#23643 - ProblemBaby - Fri Jul 16, 2004 9:41 pm

poslundc wrote:


C and assembler are two different languages. If you wrote code in Visual Basic and just #included it into a C program, would you expect it to work?


No and I dont expect it to compile without errors either.

thanks for the links!

#23646 - poslundc - Fri Jul 16, 2004 10:35 pm

It compiles because #define directives are only read by the preprocessor.

I can use the following #define in C code if I want:

Code:
#define fakeconstant lksj df02 31;2312jore ?lksfja( lksjdf


... and it will compile just fine so long as I don't use fakeconstant anywhere.

Dan.