2

I've have a raspberry pi that I use for University purposes. Since at home I don't have a monitor, I decided to code in assembly from my laptop, using Lubuntu.

The problem is, even if this works when I assemble it on the raspberry, when I run the command as -g -o $1.o $1.asm && gcc -g -o $1 $1.o from my laptop, I get error messages from the assembler.

Here is the code

@ test this code
.global main
main:

mov r7,#1
svc #0

Here is the assembler response

wsearch.asm: Assembler messages:
wsearch.asm:1: Error: junk at end of line, first unrecognized character is `@'
wsearch.asm:5: Error: expecting operand after ','; got nothing
wsearch.asm:6: Error: no such instruction: `svc '

I don't know what I'm doing wrong since doing this exact processes on the raspberry works perfectly.

lurker
  • 56,987
  • 9
  • 69
  • 103
fran9782
  • 29
  • 2
  • A file encoding issue? UTF-8 or similar? – Michael Chourdakis Mar 23 '19 at 12:11
  • 3
    Your laptop is probably x86, so by default it will compile x86 assembly, not ARM. – zch Mar 23 '19 at 12:13
  • @MichaelChourdakis how could I check for that? – fran9782 Mar 23 '19 at 12:13
  • @zch is there a fix for that? – fran9782 Mar 23 '19 at 12:14
  • With a hex editor. – Michael Chourdakis Mar 23 '19 at 12:14
  • You could set up cross-compilation toolchain and maybe with an emulator, but it would be probably much easier to just SSH onto your Raspberry and work there. – zch Mar 23 '19 at 12:16
  • @MichaelChourdakis do you mean a text editor? Still I don't understand how to fix an eventual encoding issue – fran9782 Mar 23 '19 at 12:17
  • @zch Uhmm, the problem is that I only have my laptop. No spare keyboard, and no screen. The only thing I can do is to plug the raspberry, but that isn't much help – fran9782 Mar 23 '19 at 12:18
  • Run `as --version` and look for the last line `This assembler was configured for a target of`. What does that say? Should be something with `arm` and not `x86`. – Jester Mar 23 '19 at 12:22
  • @Jester it says `x86_64-linux-gnu' – fran9782 Mar 23 '19 at 12:23
  • That is your problem then. You need to install a cross compiler toolchain targeting ARM. – Jester Mar 23 '19 at 12:25
  • Great! Do you suggest anything in particular? – fran9782 Mar 23 '19 at 12:27
  • Not sure what lubuntu has, but look for a package `gcc-arm-linux-gnueabi` or similar. – Jester Mar 23 '19 at 12:28
  • 1
    Once you get it installed make sure to run it using `arm-linux-gnueabi-gcc` or `arm-linux-gnueabi-as` as appropriate otherwise you will still be running your normal x86 toolchain. Also note that the binary produced will be for arm of course so you can't run it on your machine directly. Try using `qemu` for that. – Jester Mar 23 '19 at 12:30
  • Thanks a lot, you were very helpful, I managed to solve it – fran9782 Mar 23 '19 at 12:33
  • @fran9782 To reply "I only have a laptop": you can use some remote protocol, for example SSH (to command line, or with X forwarding) or VNC(to GUI), both you just need a network connection, and you can conveniently "reuse" your keyboard. – Geno Chen Mar 23 '19 at 13:08
  • apt-get install gcc-arm-linux-gnueabi binutils-arm-linux-gnueabi – old_timer Mar 23 '19 at 13:21
  • and then the command line will be arm-linux-....whatever...-as just type arm- then hit tab and examine what shows up and pick one. – old_timer Mar 23 '19 at 13:22
  • @Jester I installed everything, and finally it assembles the file. I installed qemu but when I do `qemu-arm wsearch` it prints `/lib/ld-linux.so.3: No such file or directory`. I don't understad why since running the "ls" command you can clearly see that the file is there... Any suggestion? – fran9782 Mar 23 '19 at 13:56
  • It may be there but that's again probably the x86 version. You will need the arm version and you'll have to point qemu to it. – Jester Mar 23 '19 at 16:09

1 Answers1

1

As mentionned by @zch, you cannot compile ARM assembly with your current toolchain: it is (probably) configured to take x86 assembly as input, to generate an x86 object file.

To achieve your goal, you have to install a cross-compilation toolchain. In my opinion, the simplest way is to install build-essential and gcc-arm-linux-gnueabihf.

$ sudo apt install build-essential gcc-arm-linux-gnueabihf 

And after installation, you should have a new toolchain, prefixed with arm-linux-gnueabihf-. In your case, you would use arm-linux-gnueabihf-as -g -o $1.o $1.asm && arm-linux-gnueabihf-gcc -g -o $1 $1.o

There are plenty of way to do this, but I found the following references to be relevant:

Aif
  • 11,015
  • 1
  • 30
  • 44
  • 3
    Answers on Stack Overflow should be self-contained. Please at least paraphrase the gist of the instructions given in the tutorials in your answer in case the links go down. I am going to retract my downvote once you did so. – fuz Mar 23 '19 at 13:32