Architecture is defined by the instruction set (language) and operand locations (registers and memory).
    本章从汇编入手,从计算机语言入手,先重点介绍MIPS,后面还引入x86,展现了结构的宏大图景。重点在理解MIPS,而x86暂时作为了解补充。
    不同结构的指令集更像不同的方言,而不是不同的语言。因为指令集所要实现的功能是类似的,甚至是雷同的。

    指令结构 - add a, b, c - [mnemonic] [destination operand] [source operand]
    image.png共有R型、I型、J型,后面还引入了F型(浮点运算)
    地址结构 - By convention, memory is drawn with low memory addresses toward the bottom and high memory addresses toward the top. - 低地址在底部,高地址在顶部
    image.png大小端序不是一定的,目前一般是小端序,但是互联时要保证兼容性。
    Some MIPS processors are little-endian, and some are big-endian.

    MIPS is a reduced instruction set computer (RISC) architecture. Architectures with many complex instructions, such as Intel’s x86 architecture, are complex instruction set computers (CISC).
    CISC实现复杂指令,增加了硬件开销,减少了简单指令的速度,而且非常非常臃肿(要兼容的东西太多了)。但是用更少的指令就可以实现复杂功能,也为优化留下余地。
    书中倾向于RISC,但我个人认为真实应用场景下,完备的复杂的长的指令集(介于CISC和RISC)更加实用。而且MIPS有一点理想化和过时了,比如定长指令、延迟槽。

    jal - jump and link - 由于长度限制,无法覆盖所有地址,j和jal是伪直接指令。

    MIPS小知识
    The name $s1 is pronounced“register s1” or “dollar s1”.
    $s are called saved registers.需要保护
    $t are called temporary registers.可以被自由修改,一般暂存后很快被使用
    image.png
    MIPS generally stores variables in 18 of the 32 registers: $s0–$s7, and $t0–$t9.
    MIPS uses a byte-addressable memory. 可以定位到字节,如lb指令(那么自然可以定位到字)
    In MIPS, the caller conventionally places up to four arguments in registers $a0–$a3 before making the function call, and the callee places the return value in registers $v0–$v1 before finishing.如果不够,则利用堆栈。但是callee的堆栈是有一定规则和顺序的。(后面有图)
    调用函数不能影响主函数,不能修改$s0-$s7,$ra,stack,要保护重要数据。

    递归函数,掌握使用汇编写递归的方法,关键是理解 $a0–$a3和$v0–$v1的使用,关键是理解变量的保护和更新。并不是说汇编不可以递归(前提是主程序是合理的)。
    image.png
    关于CALL的堆栈部分约定
    image.png
    li是伪指令,相当于lui+ori。很多我无法理解的指令其实是伪指令,比如nop,在MARS编译器是可以使用的,也推荐使用。
    image.pngimage.png还有ror和rol

    Q1四条有启发性的原则,针对于MIPS
    A1
    Throughout the chapter, we motivate the design of the MIPS architecture using four principles articulated by Patterson and Hennessy:
    (1) simplicity favors regularity;
    简单喜欢规律-一致性
    (2) make the common case fast;
    加速常见情况
    (3) smaller is faster;
    越小越快
    (4) good design demands good compromises.
    好设计需妥协

    Q2关于龙芯自研指令集
    A2
    LoongArch 是全新的指令集。包含基础指令 337 条、虚拟机扩展 10 条、二进制翻译扩展 176 条、128 位向量扩展 1024 条、256 位向量扩展 1018 条,共计 2565 条原生指令。MIPS、X86、ARM、Risc-V 都将通过二进制模拟来运行,不再原生支持。区别只是 MIPS 与 LoongArch 近似度较高,效率会高一些。
    成熟的指令集不能搞高校那套RISV,必须要广要紧跟着应用,必须要有大的丰富的可靠的指令集。
    没有独立的指令集,谈不上有独立的硬件生态。实践证明MIPS做不到独立自主。

    product-积
    quotient-商
    reminder-余数
    heap-堆
    by convention-按照惯例
    exception-例外、异常