For example in assemble code we have /add rd rs rt/ but in machine code we have /opcode rs rt rd/, where the destination register is at last position. Why MIPS arrange their code like this ? I know in machine code the rd is put at last position because some instruction does not have it, so why they don't write assemble code like add rs rt rd ?
2 Answers 2
The choice for the bit ordering in machine language (the binary representation) is made on technical grounds. In a sense, it is totally arbitrary, because only the assembler application and the CPU instruction decoder need to know it. The performance (speed) of the assembler is totally unimportant, so the dominant reason for a particular bit ordening is the ease at which it is implemented in hardware.
The choice for operand ordering in assembly language is made with readability in mind. Most programming languages work from right to left, with the leftmost position for the destination. This is an argument to put the destination register first, which is how it is done in most assembly languages (but alas, not in all, or worse: not for all instructions).
Assembly language is a deliberate, designed, abstraction of machine code. There is no requirement to have any specific relationship between machine code and assembler.
Further, the encoding of different groups of machine instructions is intended to help the implementation of the hardware. In more recent processors (e.g. 1980's onwards) machine instructions may be expressed in assembler in very different ways from the underlying instruction. For example MIPS move register to register can be expressed in machine code as "Add RS to 0 putting the result in RD". In this case there may be no move instruction in the processors machine code, the assembler instruction is just a convenience to help the assembler programmer (or compiler writer trying to understand how to target that instruction set).
So the rationale for the assembler might be to make it read in a consistent way; maybe assembler mirrors higher level languages, e.g.
RD = RS op RT.
In MIPS, the machine instructions are encoded to try to ensure that a specific instruction parameter, say the destination of the operation, RD, is always in the same bit position within the instruction. There may be other fields which need to use the same bits as other parameters, for example the machine instruction for a branch could re-interpret the bits used in other instructions for a source register, as the branch offset.
So the instruction set designers choose bit positions for instruction code and parameters to make their hardware simpler, cheaper or faster.
The designer of the assembler language can use different constraints, typically to help the programmer write code, or to make it simpler, cheaper or faster to write the assembler.
The abstraction between the two may be very useful; reflecting the different machine instruction encoding (i.e. the position of the bits for the parameters) might make the assembly language harder to learn or use reliably.