3

Recently, I saw a video of someone writing ASM, they only mentioned that the code is for the x86_64 architecture, with no mention of the specific CPU. At university I was taught to look at the CPU's Instruction Set, OP Code, memory map, etc... (Intel 8051 Specifically) So I found it odd.

I'm interested in writing Assembly for my M-series MacBook, does ARM64 Assembly run on all ARM CPUs too?

chtz
  • 17,329
  • 4
  • 26
  • 56
HasanTheSyrian_
  • 140
  • 1
  • 6
  • 2
    Depends on what you mean be "architecture" and there's no 100% agreement on that since it is such a general term. So, the answer ranges from yes to no. In many architecture families there is backward compatibility so that older programs can continue to run on CPU's with upgraded instruction sets. Though there is some "forward" compatibility for some little things, there's generally much less attempt at that by those maintaining and extending instruction sets. – Erik Eidt May 08 '23 at 22:53

2 Answers2

6

Yes, correct. The instruction set architecture (ISA) specifies what the machine code looks like and what instructions it has. Some ISAs support multiple instruction sets with applications being able to switch between them. For example, x86 has three instruction sets: a 16 bit, a 32 bit, and a 64 bit instruction set. While very similar, code written for one of the three cannot run on the others without reassembly and slight changes.

Your Apple M1 processor has the ARMv8.4-A instruction set architecture. Of the three instruction sets (A32, T32, A64) specified in this architecture, it only supports A64. Code written for the Apple M1 will in principle run on any other processor with the same instruction set, but may fail due to different operating systems and libraries.

Note also that instruction set architectures frequently have extensions. Code that makes use of instruction set extensions only runs on processors that support this extension. For example, x86 has the popular SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, AVX, AVX2, AVX-512, ... extensions. ARMv8.4 too has extensions, but they are not really relevant for beginners.

fuz
  • 88,405
  • 25
  • 200
  • 352
0

Yes in the sense that if you provide enough detail for the specific instruction set then yes. If you say x86 for example, then of course no, as with most instruction sets, it evolved over time so instructions were added or perhaps changed over time. ARM certainly the same, ARM provides more architecture information to know what spec to refer to. Today perhaps if you say something like arm64 you might survive, but one expects that at some point in the future that will not be true. Already from fuz's answer "Your Apple M1 processor has the ARMv8.4-A" aarch64 started with ARMv8-A ARMv8.4-A is a different ISA. Now it may happen to share the same "instructions" but the variation is in the "architecture" side, I do not know the 64 bit side of arm like other arm variants. ARM and x86 you can usually build a binary that works across a number of targets since there is a lot of reverse compatibility, but obviously not every ARM will run "ARM" code likewise x86 and x86.

8051 and some others (6502 I think, maybe z80 maybe not, but not PIC, probably msp430) for various reasons are fixed from an instruction set perspective although...after intel stopped making them the 8051 evolved, and actually there are, forget the name, special registers that are very much not compatible from one 8051 to another. 8051s are still in use today, this web page passed through, well next to, vary many of them between you and me. The instructions may be compatible, but other parts of the implementation are not, and the whole thing needs to match to run "code" between them.

What language you use be it a high level compiled down eventually to machine code or starting at assembly language down to machine code. Is only part of the "run on all arm cpus" equation. Particuarly with ARM. The instruction set is just like the alphabet or the language. You can write a math text book in english or in spanish or russian, etc. And still have the same content and message and usefulness. It is the same book, just a different language and alphabet. Likewise you can write a biology book in english and it can be completely useless to a math class. The vast majority of the code has nothing to do with the processor core itself, it has to do with things outside the core, part of the system. Peripherals, operating system, etc. And those are not compatible from an mobile phone to a mac m1 to a raspberry pi. So while you could use the exact same instruction set from the same arm specification, the code produced can be completely incompatible with arm BASED processors with the same architecture and will not "run" on those arm processors, it will crash.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • With ARMv8 it's the same way as with x86: ARMv8 binaries run on ARMv8.1 and so on. The ISA is a base instruction set with a bunch of extensions. Each architecture level mandates the presence of some extensions, but there can also be additional extensions. No level has so far removed anything. However, the same cannot be said in the change form ARMv7 to ARMv8 or between other major architecture levels. – fuz May 21 '23 at 15:02