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 > Using ASM mixed wih C (external files)

#16234 - x86asm - Wed Feb 11, 2004 1:37 am

Hi, I'm surprised at how easy ARM7 assembly is, I would like to try and use it along with C, just to try programming an embedded system. I have a few questions (I use VisualHAM) and prefer to use external ASM files (less red tape)

1. How do I declare say a 32-bit variable in an ASM source file, I tried .DCD but didnt work for me :*(

2. How would I reference external variables in C source files (using the .EXTERN keyword?)

3. How do you get the C compiler to compile THUMB code instead of ARM code?

These are the only two stumbling blocks I have come across (kind of embarassing actually :) ), but with your guys help I can get back on track.
_________________
Hello Everyone :D

#16240 - poslundc - Wed Feb 11, 2004 3:04 am

x86asm wrote:
Hi, I'm surprised at how easy ARM7 assembly is, I would like to try and use it along with C, just to try programming an embedded system. I have a few questions (I use VisualHAM) and prefer to use external ASM files (less red tape)

1. How do I declare say a 32-bit variable in an ASM source file, I tried .DCD but didnt work for me :*(


Declare a label where you want your variable, then use the .word directive. Check out the GAS ARM asm reference (downloadable from http://re-eject.gbadev.org/) for a bunch of other directives.

Quote:
2. How would I reference external variables in C source files (using the .EXTERN keyword?)


Code:
   ldr   r0, GLOBALS      @ r0 <- pointer to myCVariable
   ldr   r0, [r0]      @ r0 <- myCVariable
GLOBALS:
   .word   myCVariable


Quote:
3. How do you get the C compiler to compile THUMB code instead of ARM code?


Use the -mthumb command-line switch when calling gcc with your input files. To write Thumb code in assembler, precede your code with the .thumb directive.

Cheers,

Dan (did I win?).

#16242 - torne - Wed Feb 11, 2004 3:17 am

One further thing to remember: if you want to be able to call thumb from ARM and vice versa, then all your C modules need to be compiled with -mthumb-interwork, and you must use interworking return sequences in your assembly code. Read the ARM/Thumb procedure call standard (and previous posts in this forum) for details.

#16296 - x86asm - Wed Feb 11, 2004 10:49 pm

poslundc wrote:
x86asm wrote:
Hi, I'm surprised at how easy ARM7 assembly is, I would like to try and use it along with C, just to try programming an embedded system. I have a few questions (I use VisualHAM) and prefer to use external ASM files (less red tape)

1. How do I declare say a 32-bit variable in an ASM source file, I tried .DCD but didnt work for me :*(


Declare a label where you want your variable, then use the .word directive. Check out the GAS ARM asm reference (downloadable from http://re-eject.gbadev.org/) for a bunch of other directives.

Quote:
2. How would I reference external variables in C source files (using the .EXTERN keyword?)


Code:
   ldr   r0, GLOBALS      @ r0 <- pointer to myCVariable
   ldr   r0, [r0]      @ r0 <- myCVariable
GLOBALS:
   .word   myCVariable


Quote:
3. How do you get the C compiler to compile THUMB code instead of ARM code?


Use the -mthumb command-line switch when calling gcc with your input files. To write Thumb code in assembler, precede your code with the .thumb directive.

Cheers,

Dan (did I win?).


I what you suggested and it worked, but trying it in VisualHAM's debugger, it crashes with illegal instruction, I guess I need to jump somewhere cause its trying to execute what is in the variable. Thanks alot posluns, explains alot ;)
_________________
Hello Everyone :D

#16297 - poslundc - Wed Feb 11, 2004 11:18 pm

Yeah, you need to stick your variables somewhere close enough to your code that they can be referenced local to the program counter but still somewhere they won't get executed. :P

So usually that means sticking them after the end of a function or routine, after you bx lr.

Dan.

#16348 - x86asm - Fri Feb 13, 2004 2:18 am

poslundc wrote:
Yeah, you need to stick your variables somewhere close enough to your code that they can be referenced local to the program counter but still somewhere they won't get executed. :P

So usually that means sticking them after the end of a function or routine, after you bx lr.

Dan.


Hey thats fixes everything thanks a bunch! BTW, I'm a high school student looking to enter the software programming field and I noticed that on your page you were educated at McMaster U, how was it there and how is the software programming job market here in Toronto?

Thanks ;)
_________________
Hello Everyone :D

#16352 - poslundc - Fri Feb 13, 2004 3:05 am

x86asm wrote:
BTW, I'm a high school student looking to enter the software programming field and I noticed that on your page you were educated at McMaster U, how was it there and how is the software programming job market here in Toronto?


Ugh, you don't want to know... :P Go to college instead and maybe get yourself a real job. Or not. It depends on what you're interested in doing, I guess.

If you really want to know the ugly details, send me an e-mail and I'll relate it to you.

Dan.