4

I'm trying to learn assembly using the raspberry pi. I have code that compiles using as but will not compile with gcc. I thought that as was the backend for gcc assembly so I'm confused why it is not working. It says that udiv is undefined.

test.s

            .global _start
_start:     
            MOV     R4, #3
            MOV     R1, #999

            UDIV    R2, R1, R4

            MOV     R7, #1
            SVC     0

Compiling with as -o test.o test.s; ld -o test test.o works ok, Compiling with gcc test.s -o test fails.

  • 1
    Gcc may pass options to as which enable/disable specific features, e.g. FPU opcodes. Check -mfloat-abi gcc.gnu.org/onlinedocs/gcc/ARM-Options.html – Janka Jul 2 at 0:31
  • A Raspbery Pi is the right platform for learning :-) But your question does not belong essential to Raspberry Pi. It is a general programming question and I believe you will get better and quicker answers at stackoverflow.com – Ingo Jul 2 at 11:06
  • @Ingo, I guess so, but I felt that assembly related stuff is very system specific, so I might get best results from this board. – user668074 Jul 2 at 14:06
3

You need to tell gcc the architecture when just assembling like this. So gcc -march=native -o test test.s tells it to assemble for your native architecture (arm on a RPi).

This will yield link errors about multiple definitions of _start and crt1.o. Gcc expects to link in the C runtime which actually provides _start normally and that then calls main. You can prevent this by passing -nostdlib to the link stage so finally you should be ok using:

gcc -march=native -nostdlib -o test test.s

To see what gcc actually does with this, add -v and it will show the full as command with all necessary options (-march=armv7ve -mfloat-abi=hard -mfpu=vfp -meabi=5 on my Pi 2)

  • When I used -march=native it didn't work, but when I used -march=armv8-a it did. – user668074 Jul 4 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.