How to use C definitions in ARM assembler

How can I use external definitions such as LONG_MIN and LONG_MAX in ARM assembler code?

Say my_arm.h looks like this:

int my_arm(int foo); 

Let's say I have my_main.c as follows:

 ... #include <limits.h> #include "my_arm.h" ... int main (int argc, char *argv[]) { int foo=0; ... printf("My arm assembler function returns (%d)\n", my_arm(foo)); ... } 

And my_arm.s looks like this:

  .text .align 2 .global my_arm .type my_arm, %function my_arm: ... ADDS r1, r1, r2 BVS overflow ... overflow: LDR r0, LONG_MAX @ this is probably wrong, how to do it correctly? BX lr @ return with max value 

The second in the last line, I'm not sure how to boot correctly, I vaguely remember reading somewhere that I needed to define LONG_MAX in .global, but I can no longer find a link to a working example.

I am compiling with arm-linux-gnueabi-gcc version 4.3.2

===================

UPDATE: rate offers! Unfortunately, I am still having syntax issues.

First I made a small header file mylimits.h (now in the same directory as .S)

 #define MY_LONG_MIN 0x80000000 

in my_arm.S I added the following:

 ... .include "mylimits.h" ... ldr r7, =MY_LONG_MIN @ when it was working it was ldr r7, =0x80000000 ... 

Two problems with this approach.

First, the biggest problem: the MY_LONG_MIN character is not recognized ... so something else is wrong.

Secondly: the syntax for .include does not allow me to include <limits.h> , I would add that it seems a bit kludgy in mylimits.h, but I suppose this is normal :)

Any pointers?

I have access to the ARM Systems Developers Guide Designing and Optimizing System Software [2004] and the ARM Architecture Reference Guide [2000], my goal is XScale-IXP42x Family rev 2 (v5l) though.

+7
c assembly arm gnu limits
source share
5 answers

Often the .s lowercase extension implies that assembler should not be passed through the c preprocessor, while the .s uppercase extension implies that it should. It is up to your compiler to follow this convention, though (usually gcc ports), so check its documentation.

(EDIT: note that this means you can use the #include directives, but remember that most of the files you would include would normally not be valid for assembler (unless they consist entirely of #define ), so you you may need to write your own title, which is)


edit 5 years later:

Note that the armcc v5 compiler follows this behavior on Linux, but not on windows.

+11
source share

If you use gcc and its assembler, it's simple: name the file with the final .S , then add #include <limits.h> at the beginning and use wherever you need a constant, for example. ldr r0, SOMETHING ; I have been doing tests with x86, as this is what I have, but the same thing works, since it is a gcc function.

+2
source share

What I did was:

in my_main.c

 #include <limits.h> ... int my_LONG_MAX=LONG_MAX; 

then in my_arm.S

 ldr r8, =my_LONG_MAX ldr r10, [r8] 

It looks confusing, and it (plus the benefits of portability are questionable in this approach).

There must be a way to access LONG_MAX directly in the assembly. I gladly accept this method as a complete answer.

+1
source share

I saw just the power of gcc, the assembler vs gas source will let you do C like in assembler. This is actually a bit scary when you are faced with situations where you have to use gcc as the front end for gas to make something work, but this is another story.

0
source share

use -cpreproc for the armasm option and add

 #include "my_arm.h" 

in my_arm.s.

he works for keil arm

0
source share

All Articles