I want to learn assembler. Do I? If so x86 or arm?

I see many links to godbolt any people seeing clever things in the output.

Should I learn this magic to have better understanding of Zig.

If so should I learn this dying x86 or rising star arm? I work on m1 mac.

Inspired by https://9gag.com/gag/a9yMpYm

2 Likes

Both?

If you want to have a useful understanding of assembly in the modern world you’ll end up learning a bit of both eventually.

That being said, learn whatever your local physical machine uses. That way you’ll get to run the assembly you write on the bare metal of your actual physical sitting-in-front-of-you processor, which is totally awesome and fun.

(Beware, this much fun might cause you to spontaneously grow a grey neckbeard.)

8 Likes

Average assembly enjoyer:

(I agree with @neurocyte’s suggestion).

5 Likes

Typical staff engineer I would say

If so should I learn this dying x86 or rising star arm? I work on m1 mac.

I’d suggest:

  • learn RISC-V as a clean, reasonable example of an understandable CPU arch
  • learn about intel sytnax vs AT&T sytnax, this is MAJOR stupid gotcha
  • pickup bits of x86 and ARM as you go/as you need them
9 Likes

RISC-V RISC-V - Wikipedia Simple, free instruction set. Can be produced from Zig.

I find that the best way to learn assembly is by reverse engineering. This is equivalent to learning to program by reading other people’s code. It’s an incredibly useful skill to have, and literally every program is available for you to reverse engineer. There is more material on reverse engineering for x86, and also more programs. Even in Apple silicon, I believe most programs are still x86, being run by Apple’s builtin emulator. I recommend R4ndom’s tutorial, which is for x86.

1 Like

WASM is a good introduction to assembly, I think.

https://webassembly.github.io/spec/core/appendix/index-instructions.html

1 Like

As an old guy who started programming with assembly language, I always advise learning this. You will gain a better and deeper understanding of how a computer works. But which one is difficult to say. I think after a while you’ll know what interests you. To get started, I would normally recommend flat assembler. But it probably doesn’t run on the Mac so I can’t give a recommendation. Except definitely try. :+1: :smile:

2 Likes

Full reverse engineering is probably a better exercise, but it’s also good to just read and understand the assembly output for programs you know the source for.

I have enjoyed playing around with the QBE code generation backend recently. QBE uses a simple intermediate representation and generates very nice readable assembly in my opinion. The only downside is that for x86 output it uses AT&T syntax, but the syntax differences are easy to get used to IMO.

If you know C then cproc is a nice C11 compiler that uses QBE for code generation. A nice things about QBE and cproc is that their source code is also fairly small and readable[1].

I don’t have an Apple computer, but at first glance it seems that QBE has amd64_apple and arm64_apple targets.


  1. The variable names used in QBE’s source may stretch some people’s definition of readable. ↩︎

What happened to your grey neckbeard? :joy_cat:

Shaved off, makes me look old. :rofl:

2 Likes

Yeah, mine quickly turned from black to grey, then to white. My wife says it gives me a “distinguished” look – I guess that’s just another word for “old” :slight_smile: .

1 Like

Just do it. Why spend time debating which one to learn? It’s not like if you learn x86 it will permanently ruin your ability to learn anything else. Just start on what interests you.

1 Like

MASM is worth looking into, especially if youre into Win32 programming.

The question is not entirely irrelevant, since x86 is CISC while ARM is RISC. The latter should in theory be easier to understand.

1 Like

RISC vs CISC is not a meaningful distinction. No ISA today is meant for a small and simple in-order machine unless it’s meant to go in your thermometer.

ARM has BSL, BIF, and BIT. These are all the same ternary conditional, but with the arguments in a different order, so that the destination register is different. I don’t know what “RISC” means to you but this definitely does not qualify.

And there is no difference in ease of understanding between ARM and x86. Actually, if I had to pick, I’d say ARM is probably more difficult. x86 is straightforward and just has a lot of facilities that make sense and there is a blessed way to accomplish most things you’d want to do. ARM is a bit more… creative.

3 Likes

I suggest to start with whatever your Machine uses.

It is that assembly you get to see, if you run your programs in debuggers or profilers.

I love to play with small examples on Compiler Explorer. It gives easy access to documentation of the used assembly instructions. Just remember to use zigs cross compilation capabilities and add your target, like -target arm-macos

2 Likes

The only RISC-like instruction set I know of is the x86 uops lol.

As we edging closer to “what is RISC” definitions war, obligatory link: What is RISC?

1 Like

A few weeks ago I decided to learn some x86-64 assembly just for fun, and I wanted to see if I could utilise the zig build system to make linking and testing easy. As it turns out, zig does in fact make it very easy to create a static library out of your assembly code, and write tests in zig which link to that static library!

Overall I would definitely recommend diving into assembly, it was very eye opening (and fun!) for me. Just pick a flavour that works natively on your machine and go for it :grin:

I’ll leave a link to my project here since you (and anyone else that stumbles across it) might find it helpful to see an example of invoking nasm and running tests using build.zig.

7 Likes