5 ms·
> ProTip: Hexadecimal literals are prefixed with 0x. I love the idea that someone could get to this page and not already know that! Also this nicely highlight
by IshKebab 3y ago
> ProTip: Hexadecimal literals are prefixed with 0x.
I love the idea that someone could get to this page and not already know that!
Also this nicely highlights my pet peeve with assembly:
add rd, rs1, rs2 # rd = rs1 + rs2
It's very difficult to remember which parameter is the destination etc. IMO it would be much nicer if assembly had just a little more syntax for that sort of thing. E.g.
rd = add rs1, rs2
t0 = li 5
Just so the destination register is obvious. Ah well, nobody's going to do that. Assembly parsing is a total mess; there's no official grammar or anything - it's just "what GCC/LLVM do".
- Findecanor 3y ago> I love the idea that someone could get to this page and not already know that! Might be worth noting because it is different in other assembly languages. In e.g. 6502 and MC680x0 asm, you'd first prefix with # to differentiate between an address (to load from and/or store to) and an "immediate" constant number, and then $ to denote hexadecimal. In x86 assembly (Intel style), you instead suffix the number with the letter h. > rd = add rs1, rs2 There are assembly languages for a few architectures that did/have that form or a similar one. Itanium assembly had instead the form: add r1 = r2, r3
- JAlexoid 3y agoAssembly is a language, that can be extended. Assembly++? But at some point doing simple C makes more sense, than adding syntactic sugar to Assembly.
- epcoa 3y ago> there's no official grammar Most architectures have something. Sparc had one (I still have the manual), PPC, 68k. I would even say x86 does as well but you can’t force its adoption, what AT&T and GNU wants to do on their own can’t be prevented. AT&T I suppose had the goal of making it all consistent, but I’m not sure if that was an improvement. Though I know of its vocal defendants. RISC-V might be the exception more than anything, but they have a defacto syntax used throughout the spec. Analog Devices DSPs and Itanium are examples with the = token. http://laplace.physics.ubc.ca/vnp4/intel/docs/asm_lan.pdf http://laplace.physics.ubc.ca/vnp4/intel/docs/asm_lan.pdf https://www.nxp.com/docs/en/reference-manual/M68000PRM.pdf https://www.nxp.com/docs/en/reference-manual/M68000PRM.pdf
- camel-cdr 3y agoThere is an assembly manual: https://github.com/riscv-non-isa/riscv-asm-manual/blob/master/riscv-asm.md https://github.com/riscv-non-isa/riscv-asm-manual/blob/maste...
- epcoa 3y agoThat document is grossly unfinished and doesn’t even appear to specify syntax outside pseudo ops and a few other things. In a few places it refers to the output of objdump, which I think is close enough to saying “whatever gcc does”.
- IshKebab 3y agoThat's not a specification for the grammar at all. It's just a guide for how to use RISC-V assembly. Not what we're talking about.
- jcranmer 3y agoIf you want fun, there's the x86 assembly syntax where the destination is the first register, and the x86 assembly syntax where the destination is the last register. One is the syntax as is used in official documentation (the Intel and AMD manuals), most reverse engineering tools, etc. The other is the syntax most commonly used in practice because it's what gcc defaults to and actually isn't documented (which gets into a problem when you start running into what gcc figured was the best way to adapt AVX-512 EVEX stuff into assembly). So there's a lot of times where I'm staring at x86 assembly and going "wait, which version is this? the one that does destination first or destination second?"
- epcoa 3y ago> and the x86 assembly syntax where the destination is the last register. ITYM AT&T :). The idea is that the basic grammar is common across architectures to help compiler backend authors. The historical reason for the ordering is because that’s how it was on the PDP-11, the “mother” assembly. And all AT&T/GNU versions preserve this ordering regardless of the vendor format. > The other is the syntax most commonly used in practice It didn’t always used to be this way. In the dark ages before NASM, MASM was a top warez. And depending on what you’re doing I don’t think Intel syntax is uncommon, gas will even accept it for the most part these days. > "wait, which version is this? the one that does destination first or destination second?" There must be some mnemonic to associate sigil vomit with destination last. Shitty sigils come out the ass?
- brucehoult 3y ago> The historical reason for the ordering is because that’s how it was on the PDP-11 True, I think. I mean, that certainly was the case for PDP-11 and VAX asm. 68000 too (pretty much a 32 bit, 16 register PDP-11). Whether that was the actual reason is more debatable. > And all AT&T/GNU versions preserve this ordering regardless of the vendor format. False. GNU `as` puts the destination register first for all of Arm32, Arm64, MIPS, PowerPC, RISC-V. Every RISC ISA, as far as I know. Except for store instructions, where the source register is first.
- 3y ago
- dvh 3y agoHaystack needle
- edgyquant 3y agoAssembly is meant to be 1:1 with machine code, which makes writing an assembler extremely easy as long as you know the architecture. Machine code doesn’t have things like equal signs, it’s literally just a series of bytes (an opcode and operands) If you want equal signs, use C
- epcoa 3y agoMachine code doesn’t have mnemonics either. What’s your point? There are assembly syntaxes with equal signs (The Itanium though ultimately a failure was not obscure and there are a handful of DSP ISAs that use this syntax). Not my cup of tea but your argument is specious. > Assembly is meant to be 1:1 with machine code, And that is nonsense as well. RISC-V is a perfect example as it has plenty of pseudo ops. (Or do you actually think that the literal machine code bytes of an add instruction are 0x41 0x44 0x44? - they’re not) Equal signs or not, macro assemblers have plenty of ergonomic conveniences layered above machine code. How does an equal sign break any concordance with assembly and the underlying machine code anyway?
- edgyquant 3y agoIt isn’t my definition so your entire rant here is just being argumentative and defensive for no reason. Just because some fringe ISAs have equal signs in their assembler doesn’t change anything either. Assembly is meant to map directly to the way the machine code is written, and ran, so having [opcode] [operand(s)] makes perfect sense and quality of life/syntactic sugar beyond very simple things like variables (which don’t add some crazy ast or other abstractions that make it a compiler) do not make sense for the tool an assembler is.
- epcoa 3y ago> fringe ISAs Itanium fringe? You’re clueless and have no credibility. We’re talking about an = instead of a , … you’re needlessly bringing up “crazy ASTs”, so much for being argumentative. And defensive? You seem mixed up, I’m not the one even advocating for the damn things. But your argument is ignorant and foolish. > Assembly is meant to map directly to the way the machine code is written This is just false. https://github.com/netwide-assembler/nasm/blob/master/asm/assemble.c https://github.com/netwide-assembler/nasm/blob/master/asm/as... Simple?
- _a_a_a_ 3y ago> rd = add rs1, rs2 I dunno, how about rd = rs1 + rs2
- inkyoto 3y agoBecause from the CPU perspective, «+» is ambiguous as there is not one «addition» but many: - signed add - unsigned add - add and carry There are a few others in other ISA', then there are the operand sizes (byte, half-word, word, long word etc) and the «+» operator does not capture the operand size nor the specifics whereas - rd = rs1 addu8 rs2 makes the intention clear: «add the lower 8 bits from rs2 to rs1, don't set the sign bit when overflown and store the product in rd». Moreover, the «+» operation is commutative and the CPU instructions are not, e.g. - rd = rs + #10 and - rd = #10 + rs mean two completely different things for the CPU and the latter does not even have an encoding for it. The assembly processor is not the right place to place the smarts in to figure out the programmer's intention, either, as it is a very straightforward 1:1 assembly syntax to the ISA encoding translator.
- _a_a_a_ 3y agoGood post! ok, for rd = rs1 addu8 rs2 use rd = rs1 +u8 rs2 etc. The + stands out, more clearly indicating addition. (to me anyway) As for commutativity of +, it's not necessarily true. It depends entirely on what underlying operation + denotes. It's perfectly reasonable to use it for string concatenation, and that clearly isn't commutative. But if it's not in the case of an ISA, that's fine, just have the assembler reject it.
- lifthrasiir 3y agoObviously that doesn't generalize for opcodes with 0, 1, 3 or more input operands. As a concrete example, consider a fused multiply-add operation: `rd = fma rs1, rs2, rs3` is consistent, but how would you convert it to an infix notation?
- _a_a_a_ 3y ago
- lifthrasiir 3y agoThat's a valid idea for RISC-V, but not always applicable. For example x86 `ADD` only accepts two operands, so `add rax, rbx` would add `rbx` to `rax` in place. `rax = add rax, rbx` would be a "faithful" translation, but `rax = add rbx, rcx` would be invalid (AFAIK there is still no three-operand version of scalar `ADD` in x86). `rax = add rbx` would be less redundant but way more confusing. Another alternative is to mark the output in the operand position. Say, `add rd!, rs1, rs2`. I relate this to x86 `LEA` which is effectively an arithmetic operation but encoded like a load operation, hence a weird syntax `lea rax, [rbx + 42]` even though no memory access happens. Maybe that might be useful in the other way around.
- LegionMammal978 3y ago> (AFAIK there is still no three-operand version of scalar `ADD` in x86) Indeed, a 3-operand ADD is one of the extensions planned for Intel APX. But unless you need the output flags, an ordinary LEA suffices for adding two registers and storing the result in a third.
- andromeduck 3y agoI've been pushing to open source a VLIW assembler/codec generator with some syntax like this but alas may not stay long enough to see it through.