1

My program works fine on Ubuntu.

It encounters error when I compile it with gcc on a Solaris SPARC system.

I have several pieces of code like:

printf("endian_convert: %s\n", endian_convert);

asm("movl $8, %esi\n\t"
                "movl $.LC0, %edi\n\t"
                "movl $0, %eax");

This is the error I get on SPARC:

gcc -g -Wall -Werror -pedantic -Wextra src/utfconverter.c -o bin/utf 

/usr/ccs/bin/as: "/var/tmp//cc9czJEf.s", line 957: error: unknown "%"-symbol
/usr/ccs/bin/as: "/var/tmp//cc9czJEf.s", line 957: error: statement syntax
.......
/usr/ccs/bin/as: "/var/tmp//cc9czJEf.s", line 1058: error: unknown "%"-symbol
/usr/ccs/bin/as: "/var/tmp//cc9czJEf.s", line 1058: error: statement syntax
*** Error code 1 make: Fatal error: Command failed for target `utf'

So, the "%" symbol is considered as unknown on SPARC?

How can I fix this and make it working on SPARC?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Patrick
  • 293
  • 1
  • 5
  • 14
  • 3
    Don't post images of text! And that is no C90, but assembler. Check the temp-file! – too honest for this site Sep 19 '16 at 22:10
  • 1
    Try escaping: `%%esi`. – Kerrek SB Sep 19 '16 at 22:12
  • @KerrekSB: Wouldn't that add two `%` symbols? I'm not familar with x86 assembly, but could it be a problem with AT&T vs. Intel syntax? – too honest for this site Sep 19 '16 at 22:13
  • 1
    `asm` is not part of any standard C. You're probably want to learn about GCC's [extended ASM](https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Extended-Asm). –  Sep 19 '16 at 22:15
  • 1
    According to the screenshot you're using `gcc`, not "C90". Also your inline assembly statement is completely broken, if it works on Ubuntu its only a coincidence. – Ross Ridge Sep 19 '16 at 22:18
  • You'll need to produce an MCVE ([MCVE]) that reproduces the problem. Since you show line numbers in the 950-1060 range, you will need to do a lot of editing. You should show us just a few lines of code — 957-959 and 1056-1058, or only a few lines either side — that still generates the error you show. You must not show more than about 20 lines of code in total. Since you've not shown us those lines, we cannot really help you. – Jonathan Leffler Sep 19 '16 at 22:24
  • @RossRidge: gcc (before some very recent versions) implements C90 with GNU extensions by default. (Using `-pedantic` without specifying a standard is a bit odd -- but `-std=c90 -pedantic` will complain about the `asm`, which is of course an extension.) – Keith Thompson Sep 19 '16 at 22:56
  • Thanks for making the effort to turn this into a better question. But don't just leave the bad stuff in and add updates. Rewrite it to be what you should have posted in the first place. e.g. take out the image. Maybe shorten the block of error messages, since it's the same error message many times. Your goal should be to leave in any keywords or error messages that someone with the same problem might search on and find this, so some of your confused thought process is appropriate to leave in. Try to just add details that you should have mentioned in a way that reads well. – Peter Cordes Sep 19 '16 at 23:57

1 Answers1

4

(The original version of the question didn't mention that the errors were from a SPARC Solaris system, and just called it C90 because the old version of gcc installed on it defaulted to -std=c90, leading to error messages about things that are illegal in C90.)


Wait a minute, "works fine on Ubuntu but not on C90"? /usr/ccs/bin/as (in your screenshot) looks like Solaris. That + the hostname is a clue that this might be a SPARC machine, not x86 at all.


Obviously x86 assembly isn't valid SPARC assembly syntax. It's a different CPU architecture.

If you'd used gcc foo.c -S and looked at the resulting foo.s asm output file, you'd see that it was full of SPARC asm, except for text inserted literally by your asm statements.

SPARC syntax does use % decorators on register names, but the register names are different. e.g. add %i0, %i1, %o0 adds input registers i0 and i1, storing the result in output register o0. (Input as in function arg and output as in function result. SPARC uses a sliding window onto a large virtual register file that might or might not spill to memory, depending on whether the CPU microarchitecture is out of registers when the save instruction runs.)

Remember that these errors are from the Solaris assembler, not from gcc. You're using gcc but it's using the system assembler instead of the GNU assembler.

Anyway, I recommend rewriting your code into pure portable C, rather than using #ifdef __x86__ to keep using that inline asm, or writing a SPARC port of it.


BTW, your asm statement looks horrible. A different version of gcc might store a different constant at .LC0, breaking your code. More importantly, you're not using input/output constraints to tell the compiler what value is where. If you're assuming it's ok to set eax in asm inside a function, that's incorrect. The function can and will inline, and then your asm is just floating free in the middle of wherever your function inlined. See the end of this answer for links to some GNU C inline asm tutorials.

Also, you don't need inline asm to endian-convert. You will get better asm from using endian.h functions like uint32_t le32toh(uint32_t little_endian_32bits); which use gcc builtins or inline asm to get the compiler to make optimal assembly output itself.

See also https://gcc.gnu.org/wiki/DontUseInlineAsm, which applies even if you did know how to use it properly.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Sorry about the pictures and yes, it is SunOS sparc. I thought it was C90 because I got an Error: ISO C90 forbids mixed declarations and code for my c program before this error. – Patrick Sep 19 '16 at 23:09
  • @Patrick: Well, old versions of gcc default to being a C90 compiler if you don't use any `-std` options. It's relevant for gcc to tell you why something is an error, esp. when it's not an error in C99. (e.g. `for (int i = 0 ; ...)` is only valid in C99 and later). – Peter Cordes Sep 19 '16 at 23:25
  • @Patrick: I removed my downvote since you added a text version. It's still not a good question, since it doesn't contain all the information needed to know the answer for sure. Nothing in the question mentions SPARC or Solaris, so you're just lucky that the computer science department at Dalhousie used Solaris SPARC systems when was an undergrad, and I later had a job as a sysadmin for a few small beowulf clusters, including one we bought from Sun where we kept the default install of Solaris. I'm not at all surprised at everyone else's reaction in comments of total confusion. – Peter Cordes Sep 19 '16 at 23:35