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.

user668074
  • 141
  • 3

1 Answers1

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)

patthoyts
  • 501
  • 4
  • 11