2

So I have been doing an assembly tutorial, and I got stuck in the very beginning.

Project name: asmtut.s

The Code:

.text

.global _start

start:
  MOV R0, #65
  MOV R7, #1

SWI 0

Right off the beginning I'm welcomed by 3 error messages after I try this line:

as -o asmtut.o asmtut.s

asmtut.s:6: Error: expecting operand after ','; got nothing
asmtut.s:7: Error: expecting operand after ','; got nothing
asmtut.s:9: Error: no such instruction: 'swi 0'

I'm confused, because this is the exact code in the tutorial, and there it works completely fine.

Can anyone help me what could cause this?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Kulacs
  • 51
  • 3
  • 4
    Which `as` variant do you use? Which tutorial do you follow? – Zeta Mar 14 '18 at 10:44
  • 1
    I suspect you're on an x86 Linux system, where `as` is expecting x86 instructions. `#` is the comment character, and x86 doesn't have an `swi` instruction. The tutorial probably ran `as` *on an ARM system*. On x86, install a cross-assembler. (And BTW, your label should be `_start:`, not `start:`. It would still work, though, because `ld` defaults to the start of the `.text` section as the entry point if there's not `_start` symbol. – Peter Cordes Mar 14 '18 at 11:03
  • Thank you, that would explain it. Much appreciated. I'm doing Derek Banas's assembly tutorial on youtube. https://www.youtube.com/watch?v=ViNnfoE56V8 – Kulacs Mar 14 '18 at 11:56

1 Answers1

3

You're trying to use an x86 assembler to assemble ARM code. They use different instruction sets and syntax.

The native gcc and as tools on your x86 Linux system will choke, just like if you tried to compile C++ with a Java compiler or vice versa. For example, # is the comment character in GAS x86 syntax, so mov r0, is a syntax error before it even gets to the point of noticing that r0 isn't a valid x86 register name.


You're following a tutorial for Assembly on Raspberry Pi (an ARM architecture) on a x86-based PC. Either run as on the Raspberry Pi, or install a cross-compile toolchain for Rasperry Pi/ARM.

Some Linux distros have packages that provide arm-linux-gnueabi-as and ...-gcc. For example, https://www.acmesystems.it/arm9_toolchain has details for Ubuntu.

To actually run the resulting binaries, you'd either run them on your ARM system, or you'd need an ARM emulator like qemu-arm. How to single step ARM assembly in GDB on QEMU? and How to run a single line of assembly, then see [R1] and condition flags have walkthroughs of doing that.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 2
    But note that you won't be able to run ARM programs on x86 without an emulator or similar. – fuz Mar 14 '18 at 14:58
  • @Zeta: I could post my version as a new answer if you want. I didn't think of that; usually on old questions there's an accepted or highly-voted answer that needs improvement, and I was in that mindset while looking for a duplicate. I don't personally do RPi development so leaving these links as part of a community-wiki answer for others to maintain seemed like a good idea. – Peter Cordes Mar 23 '20 at 06:55
  • @PeterCordes Yeah. Especially since its an ever-changing topic with new toolchains and such; hopefully the great picture doesn't change too much anymore. Just wanted to make sure that you gain the appropriate reputation. I'm fine with the answer being edited, it is, after all, a wiki answer :). – Zeta Mar 23 '20 at 07:01
  • Thanks, but I think I'll leave this one alone instead of cluttering the question with a 2nd answer. I don't need another couple of nerd points that badly :P – Peter Cordes Mar 23 '20 at 07:04