News for hackers. Stuff that matters.
Re:ARM Assembly Is Too High Level: ROR and RRX by simias on Wednesday September 26, 2018 @04:34AM (#18073946)
I don't quite understand the objection here, this is fairly elegant in my opinion, you have a single opcode used to encode several operations by using "special cases". In my experience it's rather common in RISC IAs.

If the author doesn't like this they shouldn't look into MIPS because it goes well beyond that. You see, MIPS has a special "R0" register that's always 0 (AARCH 64 does as well by the way) so you can always use it as a placeholder in other instructions.

As such, there's no real MOVE instruction, it's just an assembler mnemonic that assembles down to `OR $target, $src, $R0`. NOP? It's by convention `SLL $R0, $R0, 0` (which has the nice property of being an instruction encoding as "0x00000000"). You want to negate a number? `SUB $target, $R0, $src".

Since all instructions are 32bit wide you can't load a 32bit immediate value in a single instruction, instead the assembler's "LI" mnemonic generates a pair of instructions (LUI/ORI) for large immediate values (ARM prefers PC-relative loads).

You have a whole bunch of mnemonics in MIPS that are just aliases around other instructions. I always thought it was pretty clever.

In summary you can have this assembler listing:

    1:
        sll $0, $0, 0
        or  $t0, $t1, $0
        li  $t0, 0xabcdef
        sub $t0, $0, $t1
        j   1b
That will disassemble to:

        nop
        move    t0,t1
        lui     t0,0xab
        ori     t0,t0,0xcdef
        j       0x0
        neg     t0,t1
The only operation here that I would qualify as "high level" is the reordering of the "neg" instruction into the delay slot (note that j is no longer the last instruction). Everything else is very straightforward substitution and if the assembler didn't support these mnemonics we could implement them with very trivial macros.

Note that even x86 assemblers do that to some extent, for instance "nop" assembles down to an instruction with no side effect (typically `xchg eax, eax`). Furthermore there are a bunch of mnemonics for the same encoding, for instance JAE (jump if above or equal), JNB (jump if not below) and JNC (jump if not carry). Overall instruction encoding is also massively more complicated in x86 (and even more for amd64) so the assembler needs to handle many more corner cases than the simple substitutions of ARM and MIPS. As a brain teaser, consider the following similar looking amd64 instructions that load the 32bit value pointed at by a register (the only difference is that the first one dereferences the pointer in %rax, the second in %r12):

        mov %eax, (%rax) ; assembles to 89 00
        mov %eax, (%r12) ; assembles to 41 89 04 24
I can't even be bothered to walk you through this but basically it has to do with the fact that %r12 happens to be encoded as %rsp + 8 (because registers r8 to r15 are effectively a hack since x86 only supported 8 GPRs) and %rsp has special semantics in this addressing mode which mandate a different, longer encoding otherwise you end up with an ambiguous instruction.

Yeah, I think in retrospect we can give ARM a pass for their ROR shenanigans.

Re:ARM Assembly Is Too High Level: ROR and RRX by XlogicX on Wednesday September 26, 2018 @10:45AM (#18076448)
I love x86 complications (ARM is way more elegant in comparison), and I WILL be bothered to walk through mov %eax, (%r12), or 41 89 04 24 ---- 41 - being the amd64 prefix that 'unlocks' rax to being interpreted as r12 ---- 89 - still the same MOV instruction as the first one in mov %eax, (%rax) ---- 04 - The ModR/M byte that specifies the %eax part, and [--][--] as the 'Effective Address', which is another way of saying that we need a SIB byte because this (%r12) doesn't already have a simple encoding with the ModR/M byte alone (the same would be needed even if it was a simple (%esp)). ---- 24 - A 'Scaled Index' of none and as simias stated, RSP (but R12 in the context of the 41 prefix for this instruction). ---- All of this is easier to visualize when using the ModR/M and SIB tables in Volume II of the Intel manual. In my copy of the manual it's pretty early on in Chapter 2 (2.1.5 Addressing-Mode Encoding of ModR/M and SIB Bytes))
Re:ARM Assembly Is Too High Level: ROR and RRX by xelxebar on Wednesday September 26, 2018 @07:04AM (#18074664)
Flattening the ROR and RRX special cases seems pretty straightforward to me as well. That's why I thought that the punch line was at the end of the post though:

ror r0, #0

gets assembled to

mov r0, r0.

I see how both are nops and assume they affect state congruently, but why is one nop encoding preferred over another? Does the architecture do something desirable when encountering the MOV incarnation vs. any other?

Re:ARM Assembly Is Too High Level: ROR and RRX by palotasb on Wednesday September 26, 2018 @07:29AM (#18074787)
The point is that the binary encoding is the same for ROR and MOV, and only the disassembly of the binary is special cased: if it has a shift, it's disassembled into the ROR text with the shift otherwise it is displayed as a MOV. RRX is a special case to ROR as ROR is to MOV.

The ARM Instruction Set PDF I'm looking at only lists MOV as a real instruction -- one with a distinct opcode -- out of the above three.

Re:ARM Assembly Is Too High Level: ROR and RRX by XlogicX on Wednesday September 26, 2018 @08:31AM (#18075218)
It's MOV and LSL that have the same binary encoding; everything is identical with exception to the imm5 vs the hardcoded 0's. MOV and LSL share the same op2 field of '00'. ROR and RRX share the op2 field of '11'. ROR and MOV have a similar binary encoding, but the op2 field is distinctly different in this case.

As a note, I'm basing this off of the v7-A and v7-R manual.

Re:ARM Assembly Is Too High Level: ROR and RRX by tropo on Wednesday September 26, 2018 @09:37AM (#18075757)
Well, there's the problem: the v7-A and v7-R manual.

That manual uses unified assembly syntax, meaning that old ARM and Thumb are described together. Because of irregularities in Thumb, the old ARM instructions end up being described in a way that is needlessly verbose. You lose the insight into how the opcodes are actually decoded.

Look at an older manual. ARMv5 will do nicely. There, you can see that the MOV instruction is mostly described by 2 bits. (one condition code is stolen) In an even older ARM, such as ARMv4 I think, it really is just 2 bits.

Re:ARM Assembly Is Too High Level: ROR and RRX by xelxebar on Wednesday September 26, 2018 @11:37PM (#18081993)
Unless I'm missing something, the encodings are different at bits 6 and 7. Looking at xlogicx's post, MOV has them set to 0 while ROR has them at 1.

On a higher level, this post got me to realize that there could be ops (in this case nops) that have equivalent effects but are encoded as different instructions. Even though registers might state-change equivalently, I can see that the internal processor state might mutate differently. Maybe one nop encoding is faster, and maybe the caches get hit differently, etc. As an assembler, what reasons might there be to prefer one encoding over another?

When designing an architecture, I can imagine putting a "fast nop" in the instruction encoder that essentially just short circuits around it. Is this something that's done in practice?

Re:ARM Assembly Is Too High Level: ROR and RRX by mfgmfg on Wednesday September 26, 2018 @07:50AM (#18074921)
Interestingly, older ARM architectures (ARMv6-M), do not support RRX
Re:ARM Assembly Is Too High Level: ROR and RRX by masklinn on Wednesday September 26, 2018 @08:56AM (#18075427)
Do they not support the feature or do they just not have a convenient mnemonic for it?
Re:ARM Assembly Is Too High Level: ROR and RRX by davemp on Wednesday September 26, 2018 @08:56AM (#18075426)
> Since all instructions are 32bit wide you can't load a 32bit immediate value in a single instruction, instead the assembler's "LI" mnemonic generates a pair of instructions (LUI/ORI) for large immediate values (ARM prefers PC-relative loads).

For example:

    ldr rn,=0xCAFEBABE
Will actually be assembled as:

    ldr rn,[pc,#(literals - .)]

  literals:
    .word 0xCAFEBABE
IMO it really is odd that some instruction to the assembler are not direct translations. In theory it can cause some issues if the developer makes irresponsible assumptions based on the program text. The mnemonics make life easier for folks who are familiar with the assembler, but harder for someone poking around doing some detective work.
Re:ARM Assembly Is Too High Level: ROR and RRX by megaremote on Wednesday September 26, 2018 @05:04AM (#18074109)
> I don't quite understand the objection here, this is fairly elegant in my opinion, you have a single opcode used to encode several operations by using "special cases".

Sounds like CISC.

Re:ARM Assembly Is Too High Level: ROR and RRX by kccqzy on Wednesday September 26, 2018 @05:39AM (#18074292)
I don't think this characterizes an instruction set as CISC at all. In any case, having those "special cases" means that if an operation can be subsumed by another operation, the former is just an alias of the latter on the instruction encoding level, thereby reducing the actual number of instructions. Think of it as syntactic sugar.

I still find this classic to be the best explanation of the technical characteristics of CISC/RISC: https://userpages.umbc.edu/~vijay/mashey.on.risc.html

Here's a short summary:

Most RISCs:

- Have 1 size of instruction in an instruction stream

- And that size is 4 bytes

- Have a handful (1-4) addressing modes

- Have NO indirect addressing in any form (i.e., where you need one memory access to get the address of another operand in memory)

- Have NO operations that combine load/store with arithmetic, i.e., like add from memory, or add to memory.

- Have no more than 1 memory-addressed operand per instruction

- Do NOT support arbitrary alignment of data for loads/stores

- Use an MMU for a data address no more than once per instruction

- Have >= 5 bits per integer register specifier

- Have >= 4 bits per FP register specifier

Re:ARM Assembly Is Too High Level: ROR and RRX by simias on Monday October 08, 2018 @03:24AM (#18164901)
IMO RISC is more a philosophy than a technical term, your definition is something that was created post-facto to try and come with a definition. It's more like "all currently accepted RISC IAs have the following characteristics" but I disagree that they're an appropriate definition. For instance:

>Have 1 size of instruction in an instruction stream and that size if 4 bytes

So that means that Thumb isn't RISC because it has 16 bits instructions and a few double-width opcodes? Even though its instruction set if effectively even more restricted than ARM? That doesn't make sense to me.

>Do NOT support arbitrary alignment of data for loads/stores

MIPS has SWL/SWR LWL/LWR, does that count? I suppose you could say that RISC has no support for arbitrary alignment in regular load and store instructions but again, is that really enough to disqualify an IA? What if I made a tweaked MIPS CPU with an identical instruction set with the only difference being that unaligned LW/SW would work as intended instead of raising an exception, would it stop being RISC?

>Have >= 5 bits per integer register specifier, Have >= 4 bits per FP register specifier

That actually disqualifies ARM32 as far as I can tell, since it only has 16GPRs encoded using 4 bits. I fail to see how this small encoding detail is relevant to RISC anyway. Maybe it just meas that you need at least 32GPRs?

Wikipedia has a much broader (and IMO more reasonable) definition of RISC:

>Various suggestions have been made regarding a precise definition of RISC, but the general concept is that such a computer has a small set of simple and general instructions, rather than a large set of complex and specialized instructions.

By this definition an instruction such as "Floating-point Javascript Convert to Signed fixed-point, rounding toward Zero" is very much un-risc-y.

Re:ARM Assembly Is Too High Level: ROR and RRX by Symmetry on Wednesday September 26, 2018 @08:26AM (#18075183)
Yes, but by that classic definition ARM is certainly one of the CISCiest of the RISC architectures with its condition codes, barrel shifter, auto-increment loads and stores, and load/store multiple. And x86 is one of the RISCiest of the CISCs with its lack of indirect memory access. All of which might represent a sort of semantic happy medium.
Re:ARM Assembly Is Too High Level: ROR and RRX by bonzini on Wednesday September 26, 2018 @05:30AM (#18074247)
No, it's very different.

CISC has different encodings for MOV and OR (and often subtle differences, e.g. OR updates the flags and MOV doesn't). On RISC processors, which have MOV as a special case of OR, the assembler accepts MOV at the source code level but the processor does not have to implement a separate MOV instruction at the binary level. Therefore the instruction set is indeed reduced.

ARM's peculiarity is that the fundamental ALU operation is "R1 op (R2 shiftop R3)" or "R1 op (R2 shiftop #nn". But it's still not a CISC design, it's just that the barrel shifter is at a different place in the ALU and that shows in the instruction encoding. Apart from this quirk the ideas from the previous paragraph apply just as well to ARM.

Re:ARM Assembly Is Too High Level: ROR and RRX by jlouis on Wednesday September 26, 2018 @06:17AM (#18074469)
The basic idea of RISC was to reduce the complexity of individual instructions. Rather than having one instruction, you had several, but each of those would be easier to decode and execute. And, as history has shown, it was also quite a bit faster to execute such simplified instructions.

Hence, assembling a single mnemonic into two or more instructions is fairly common on RISC architectures. But the instruction set at the machine level is still simple and direct, even if the assembler expands.

As a fun fact, modern x86 will often just microcode complex instructions inside the CPU to several simple mu-ops and then execute those. In turn, they are just doing the same work as the assembler, but in hardware. It is necessary for backwards compatibility, but it is hardly elegant.

Re:ARM Assembly Is Too High Level: ROR and RRX by simias on Wednesday September 26, 2018 @05:25AM (#18074221)
I don't understand how you reach this conclusion, if anything it's the opposite of CISC: instead of having highly specialized instructions you have a single generic instruction that can be used in all sorts of context. Note that this is very different from x86 "overloaded" encoding where a single mnemonic can have a million different encodings doing wildly different things depending on the operands, prefixes, lunar phase and/or operating mode[1], in this case there's no additional complexity to the "OR" implementation besides the adjunction of an R0 register (which is not specific to this opcode). You can still describe the encoding and functionality in a single line[2].

[1] https://svkt.org/~simias/up/20180926-112325_x86-mov.png

[2] https://svkt.org/~simias/up/20180926-112224_or-encoding.png

Re:ARM Assembly Is Too High Level: ROR and RRX by masklinn on Wednesday September 26, 2018 @06:52AM (#18074605)
It sounds like the exact opposite: you're reducing the actual instruction set, just providing convenient shorthands for common cases at the assembly level.
Re:ARM Assembly Is Too High Level: ROR and RRX by pjc50 on Wednesday September 26, 2018 @05:19AM (#18074194)
But why does anyone care where the boundary is drawn, especially based on entirely subjective opinions about whether one thing is more complex than another?
Re:ARM Assembly Is Too High Level: ROR and RRX by jlarcombe on Wednesday September 26, 2018 @12:38PM (#18077713)
AArch64 has a zero register too, I believe.