RCSN 发表于 2024-6-15 21:25

《RISC-V 开放架构设计之道》- RISC-V开放架构设计简读

<div class='showpostmsg'><article data-content="[{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;3060-1621846615933&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h1&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;p5PQ-1621846617594&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;一、前言&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;PTQy-1718377266935&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;MexK-1718377266936&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;感谢eeworld电子工程世界提供《&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]},{&quot;text&quot;:&quot;RISC-V开放架构设计之道&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;bold&quot;},{&quot;type&quot;:&quot;color&quot;,&quot;value&quot;:&quot;#333333&quot;},{&quot;type&quot;:&quot;backgroundColor&quot;,&quot;value&quot;:&quot;rgb(255, 255, 255)&quot;},{&quot;type&quot;:&quot;fontFamily&quot;,&quot;value&quot;:&quot;SimHei&quot;},{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]},{&quot;text&quot;:&quot;》书籍测评,书中并没有过多繁琐介绍riscv的概念,也没有过多强调riscv的好处,更多介绍的是riscv的简洁、免费、开放的思想。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;7UDb-1718438509089&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;ixVc-1718438509081&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;如同书中一位大佬推荐所说的:这本方便的小书轻松地总结了RISC-V指令集架构所有的基本要素,是学生和从业者的完美参考指南。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Bsnd-1718438627592&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;tOZo-1718438627590&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;RISC-V,是比较大体、宽泛的概念,&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]},{&quot;text&quot;:&quot;但是&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;color&quot;,&quot;value&quot;:&quot;#4d4d4d&quot;},{&quot;type&quot;:&quot;backgroundColor&quot;,&quot;value&quot;:&quot;rgb(255, 255, 255)&quot;},{&quot;type&quot;:&quot;fontFamily&quot;,&quot;value&quot;:&quot;SimHei&quot;},{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]},{&quot;text&quot;:&quot;书中内容很简短,内容插入了不少插图以及RISCV的参考卡,说是参考指南一点也不过分;加之目录介绍很清晰,从riscv32的基础整数指令集RV32I、乘法和除法指令集M扩展、原子指令集A扩展、单双精度浮点数指令集RV32F/RV32D、压缩指令RV32C、以及汇编语言和向量RV32V。以及后续的RV64介绍。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;fpOb-1718438974333&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;gysQ-1718438974331&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;而刚好先楫的处理器作为典型标准的RISCV处理器,当然也支持上述的指令集,并且还有所扩充。以实际的硬件进行书籍分享是最好不过的思路了。比如先楫的HPM5300系列。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;Mqc6-1718439097476&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19912/WEBRESOURCE60168c14b311dbcac1fef06a2db3a29e&quot;,&quot;width&quot;:411,&quot;height&quot;:431},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;rGHx-1718439065289&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;YcWF-1718439065287&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;本系列文章更多是讲一些基础只知识,利用HPM5300EVK配合SEGGER Embedded Studio这个IDE通过查看机器码和汇编来分析RISCV的一些指令集,以此能对riscv有一定的了解兴趣。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;OMyi-1718439224951&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;9Vmi-1718439224949&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;本书内容目录比较多,系列文章定期分享RISCV的每个指令集基础。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;v7u7-1718439522867&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h1&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;daIb-1718439522865&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;二、导读&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;5CAH-1718440591487&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;7T0m-1718440591488&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;如果想要接触riscv,那么典型的reference card(参考卡)不可或缺,你会发现相比其他的指令集架构,riscv真的相对简洁了不少(基础的就只有两页),而且操作码7位,相当可自由扩充127个扩展指令,这也是riscv的开放自由思想。这里截图一小部分,需要了解的可以搜下riscv cheat sheet。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;1vsq-1718440895646&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Wd70-1718440895644&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;链接&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]},{&quot;text&quot;:&quot;:&quot;,&quot;marks&quot;:[]}]},{&quot;type&quot;:&quot;inline&quot;,&quot;id&quot;:&quot;rzRT-1718440910890&quot;,&quot;name&quot;:&quot;link&quot;,&quot;data&quot;:{&quot;href&quot;:&quot;https://www.cl.cam.ac.uk/teaching/1617/ECAD+Arch/files/docs/RISCVGreenCardv8-20151013.pdf&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;e69W-1718440910889&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;https://www.cl.cam.ac.uk/teaching/1617/ECAD+Arch/files/docs/RISCVGreenCardv8-20151013.pdf&quot;,&quot;marks&quot;:[]}]}]},{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;0eq9-1718440910891&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;5aEw-1718440937539&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19948/WEBRESOURCE0bec8e26312056cce5b07a21a3220366&quot;,&quot;width&quot;:480,&quot;height&quot;:51},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;FS0X-1718440549731&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h2&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;E8uI-1718440549732&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;(一)RV32I指令格式&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;zS2X-1718441935107&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19957/WEBRESOURCE16a5d490faada320169719e3f18836f7&quot;,&quot;width&quot;:379,&quot;height&quot;:258},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;UYuI-1718441635763&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;7LXY-1718441635764&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;如书中描述,RV32I 基础指令集的一页图形表示。 对于每幅图, 将有下划线的字母从左到右连接起来,即可组成完整的 RV32I 指令集。对于每一个图,集合标志{}内列举了指令的所有变体,变体用加下划线的字母或下划线字符_表示。特别的,下划线字符_表示对于此指令变体不需用字符表示。 例如,下图表示了这四个 RV32I 指令: slt, slti, sltu, sltiu:&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;vfHv-1718441977117&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19960/WEBRESOURCE92f493d7aa5a33183cb52cae46b72f20&quot;,&quot;width&quot;:479,&quot;height&quot;:51},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;qP2S-1718441977121&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;hQul-1718441977120&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;书中展示了6种基本指令格式,分别是用于寄存器-寄存器操作的 R 类型指令,用于短立即数和访存 load 操作的 I 型指令,用于访存 store 操作的 S 型指令,用于条件跳转操作的 B 类型指令,用于长立即数的 U 型指令和用于无条件跳转的 J 型指令。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;YGKX-1718444720004&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19964/WEBRESOURCE9fbb95a310a5ec6e8b01b5440e703585&quot;,&quot;width&quot;:720,&quot;height&quot;:137},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;ynmr-1718444720008&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;reql-1718444720007&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;这里使用最简单的U类型种的lui指令,配合利用HPM5300EVK配合SEGGER Embedded Studio来进行说明上述所说的指令格式是什么意思。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;ZLbt-1718445848742&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;RmYi-1718445848740&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;随便打开hpm_sdk一个例子,从左边的汇编窗口找到lui的汇编,比如以下:&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;gBDS-1718446169796&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;SkW5-1718446169795&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;可以看到,ses这个IDE对于汇编和机器码呈现还是是否直观的,对于不熟悉gdb调试又想查看汇编的开发者是十分友好的。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;roBR-1718446220175&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;6Y8P-1718446220173&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;对于左边的8000452a指的是存储到flash的机器码的flash地址,中间的E40007B7就是机器码,最右边就是汇编部分。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;color&quot;,&quot;value&quot;:&quot;#F33232&quot;},{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;0lkK-1718447084813&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;ASRc-1718447084811&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;hpm单片机支持压缩指令集RV32C,所以机器码有些会是16位,这对于固件空间的缩小利用是有一定帮助的。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;o2OV-1718446164273&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19974/WEBRESOURCE234f99bb64aabe81fa1fd2ee3b5f136b&quot;,&quot;width&quot;:637,&quot;height&quot;:170},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;tlpo-1718446137362&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;tiTY-1718446137361&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;可以分析下E40007B7这个机器码对应的Lui指令格式,如下图可看到&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;aV9z-1718447304056&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;6x3W-1718447304054&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;E40007B7的低7位代表操作码opcode,也就是lui的操作码(0b0110111),7到11bit是5位目的寄存器(0b01111),这很奇妙,5bit宽度刚好是32,对应的就是寄存器的长度(32bit),换成十六进制就是15,对应的通用寄存器就是X15,也就是别名a5寄存器。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;color&quot;,&quot;value&quot;:&quot;#F33232&quot;},{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;HN78-1718451250423&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;PS8R-1718451250421&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;高11位就是立即数,对应的十六进制就是0xE4000,最终根据该机器码可以反汇编得到:&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;color&quot;,&quot;value&quot;:&quot;#F33232&quot;},{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;MAto-1718451349772&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;opAR-1718451349771&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;把一个20位的立即数0xE4000加载到a5寄存器,对应的汇编指令就是:lui a5, 0xE4000,刚好可以对上ses IDE的汇编显示。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;color&quot;,&quot;value&quot;:&quot;#F33232&quot;},{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;JNII-1718447304344&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/19988/WEBRESOURCEd9401fe4faa080ca216cd8a3e5e2d1be&quot;,&quot;width&quot;:638,&quot;height&quot;:111},&quot;nodes&quot;:[],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;8tZ7-1718447304348&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h2&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;N3t9-1718447304347&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;(二)RISCV通用寄存器&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;pZwD-1718457345576&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;HmXB-1718457345574&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;无论是RISCV32还是RISCV64,他的通用寄存器的数量都是32个,分别是x0~x31,当然为了方便汇编编写,每个寄存器也有别名,在参考卡中如下:&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;ClLg-1718454934631&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/20024/WEBRESOURCE27ebea292a4b668da9a2fcab8b20836f&quot;,&quot;width&quot;:435,&quot;height&quot;:412},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/20024/WEBRESOURCE27ebea292a4b668da9a2fcab8b20836f&quot;,&quot;initialSize&quot;:{&quot;width&quot;:435,&quot;height&quot;:776},&quot;loading&quot;:false}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;v1cq-1718454298537&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;CfN9-1718454298536&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;RISC-V 有足够多的寄存器来达到两全其美的结果:既能将操作数存放在寄存器中,同时也能减少保存和恢复寄存器的次数。其中的关键在于,在函数调用的过程中不保留部分寄存器存储的值,称它们为临时寄存器;另一些寄存器则对应地称为保存寄存器。不再调用其它函数的函数称为叶函数。当一个叶函数只有少量的参数和局部变量时,它们可以都被存储在寄存器中,而不会“溢出(spilling)”到内存中。但如果函数参数和局部变量很多,程序还是需要把寄存器的值保存在内存中,不过这种情况并不多见。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;LusV-1718457355234&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;yq6d-1718457355233&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;这里说明下caller是调用者,调用(或执行)一个函数的代码段或者函数,是主动发起函数调用的一方,可以理解是\&quot;甲方\&quot;。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;SLp5-1718455086344&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;RhkJ-1718455086343&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;callee是被调用者,被调用的函数本身,是被动接收函数调用并且执行对应操作的一方,可以理解是\&quot;乙方\&quot;。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;wEBp-1718455142858&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;nxOU-1718455142856&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;从上面的表格知道,从通用寄存器上看,X0到X31,调用者需要保存的通用寄存器16个,剩下的是被调用者需要保存的寄存器。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;YMa6-1718455390449&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;AuIm-1718455390448&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;这里可以从函数调用阶段来进行说明通用寄存器的作用,配合利用HPM5300EVK配合SEGGER Embedded Studio,使用helloworld例子来说明。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;wo60-1718455627348&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;0MFm-1718455627346&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;函数调用基本分为以下几个阶段,在书中也提到:&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;HZiF-1718457257934&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/a/WEBRESOURCE2801bdef730fc2ac065a9d4df690323a&quot;,&quot;width&quot;:404,&quot;height&quot;:274},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;loading&quot;:false,&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/a/WEBRESOURCE2801bdef730fc2ac065a9d4df690323a&quot;,&quot;initialSize&quot;:{&quot;width&quot;:749,&quot;height&quot;:519}}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;vBHY-1718457247560&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;Hfvb-1718457247561&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;DkWN-1718457240150&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h3&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;SaLU-1718457240149&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;1、进入到被调用者函数的开始位置(caller需要保存的)&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;NHYn-1718455868666&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/20051/WEBRESOURCE761fa450d86cb2ba8487b616f9c244e0&quot;,&quot;width&quot;:858,&quot;height&quot;:127},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/20051/WEBRESOURCE761fa450d86cb2ba8487b616f9c244e0&quot;,&quot;initialSize&quot;:{&quot;width&quot;:858,&quot;height&quot;:402},&quot;loading&quot;:false}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;qca0-1718455600925&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h3&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;RMo3-1718455600924&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;2、将函数传参存储到指定寄存器上&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;WA3h-1718456096833&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;AtUl-1718456096831&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;这里就是上图的x10~x17,相比其他指令集架构参数依赖于内存,也就是需要保存在栈中,riscv反而可以通过寄存器保存参数,最多可以存储8个参数,当然超过的也会存储到内存中。一般应用还是推荐不要多余8个参数传参。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;aAra-1718456158214&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;mtel-1718456158212&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;那么我们进入到board_timer_create API中,传参是300和board_led_toggle函数指针。那么将会存储在x10和x11这两个寄存器上。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;OwAj-1718456297923&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/20067/WEBRESOURCEce0236aa64bf92748b7db8bf757d8b75&quot;,&quot;width&quot;:718,&quot;height&quot;:233},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/20067/WEBRESOURCEce0236aa64bf92748b7db8bf757d8b75&quot;,&quot;initialSize&quot;:{&quot;width&quot;:718,&quot;height&quot;:233},&quot;loading&quot;:false}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;sIPn-1718456391285&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/20071/WEBRESOURCEfa33c688a22d80af62e803851accfdfe&quot;,&quot;width&quot;:716,&quot;height&quot;:232},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/20071/WEBRESOURCEfa33c688a22d80af62e803851accfdfe&quot;,&quot;initialSize&quot;:{&quot;width&quot;:716,&quot;height&quot;:518},&quot;loading&quot;:false}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;EDrL-1718456391290&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;6gwS-1718456391288&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;可以看到,x10现在是300,是符合预期的,对于x11是0x8000669e,我们可以在SES IDE中的汇编窗口查到该函数地址。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;YMic-1718456501128&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;281T-1718456501126&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;0x8000669e对应的就是board_led_toggle,这也是符合预期的。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;FXwT-1718456493323&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/20077/WEBRESOURCEeb6e39962cf754234fccd81343c36afe&quot;,&quot;width&quot;:566,&quot;height&quot;:347},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/20077/WEBRESOURCEeb6e39962cf754234fccd81343c36afe&quot;,&quot;initialSize&quot;:{&quot;width&quot;:566,&quot;height&quot;:347},&quot;loading&quot;:false}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;UFLY-1718456462380&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h3&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;YJWK-1718456462379&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;3、获取函数局部资源变量保存在寄存器,并执行函数中的指令,可以看到寄存器窗口会发生不同的赋值变化&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;96r0-1718456714396&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h3&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;k5SJ-1718456714394&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;4、执行完毕之后将返回值存储到调用者能够访问的位置(X10寄存器),恢复寄存器,出栈释放局部资源&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;hbhQ-1718456846725&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h3&quot;},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;JOYo-1718456846723&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;5、返回进行下一个调用函数位置。&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;LBo5-1718457081072&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;BMyp-1718457081073&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;在ses IDE中也展示了main调用者主函数调用被调用者函数的一些汇编代码过程。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;YEkg-1718457113578&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;41Ma-1718457113576&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;gC46-1718457127808&quot;,&quot;name&quot;:&quot;image&quot;,&quot;data&quot;:{&quot;version&quot;:1,&quot;url&quot;:&quot;https://note.youdao.com/yws/res/c/WEBRESOURCEbfee27898b7f1a7dcf76e4ac6282307c&quot;,&quot;width&quot;:348,&quot;height&quot;:500},&quot;nodes&quot;:[],&quot;state&quot;:{&quot;loading&quot;:false,&quot;renderSource&quot;:&quot;https://note.youdao.com/yws/res/c/WEBRESOURCEbfee27898b7f1a7dcf76e4ac6282307c&quot;,&quot;initialSize&quot;:{&quot;width&quot;:583,&quot;height&quot;:761}}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;iElv-1718457127813&quot;,&quot;name&quot;:&quot;heading&quot;,&quot;data&quot;:{&quot;level&quot;:&quot;h1&quot;,&quot;style&quot;:{}},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;HvaT-1718457127811&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;三、总结&quot;,&quot;marks&quot;:[]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;FWve-1718457383409&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;rUMd-1718457383408&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;1、riscv的书籍众多,RISC-V开放架构设计之道这本书籍值得阅读,书中内容简洁但又不缺完整,而且丰富的参考卡可作为riscv的日常参考指南所用&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}},{&quot;type&quot;:&quot;block&quot;,&quot;id&quot;:&quot;vwat-1718457507356&quot;,&quot;name&quot;:&quot;paragraph&quot;,&quot;data&quot;:{&quot;version&quot;:1},&quot;nodes&quot;:[{&quot;type&quot;:&quot;text&quot;,&quot;id&quot;:&quot;KXLP-1718457507354&quot;,&quot;leaves&quot;:[{&quot;text&quot;:&quot;2、对于技术书籍,需要有一个实操机会可以加深印象,先楫的处理器内核遵守risv规范,具有多种扩展指令集,而其配套的SEGGER Embedded Studio这个IDE通过查看机器码和汇编,让一些不熟悉gdb调试的开发者,是一个值得推荐的实操平台。&quot;,&quot;marks&quot;:[{&quot;type&quot;:&quot;fontSize&quot;,&quot;value&quot;:18}]}]}],&quot;state&quot;:{}}]">
<p>一、前言</p>

<p>感谢eeworld电子工程世界提供《RISC-V开放架构设计之道》书籍测评,书中并没有过多繁琐介绍riscv的概念,也没有过多强调riscv的好处,更多介绍的是riscv的简洁、免费、开放的思想。</p>

<p>如同书中一位大佬推荐所说的:这本方便的小书轻松地总结了RISC-V指令集架构所有的基本要素,是学生和从业者的完美参考指南。</p>

<p>RISC-V,是比较大体、宽泛的概念,但是书中内容很简短,内容插入了不少插图以及RISCV的参考卡,说是参考指南一点也不过分;加之目录介绍很清晰,从riscv32的基础整数指令集RV32I、乘法和除法指令集M扩展、原子指令集A扩展、单双精度浮点数指令集RV32F/RV32D、压缩指令RV32C、以及汇编语言和向量RV32V。以及后续的RV64介绍。</p>

<p>而刚好先楫的处理器作为典型标准的RISCV处理器,当然也支持上述的指令集,并且还有所扩充。以实际的硬件进行书籍分享是最好不过的思路了。比如先楫的HPM5300系列。</p>

<p> &nbsp;</p>

<p>本系列文章更多是讲一些基础只知识,利用HPM5300EVK配合SEGGER Embedded Studio这个IDE通过查看机器码和汇编来分析RISCV的一些指令集,以此能对riscv有一定的了解兴趣。</p>

<p>本书内容目录比较多,系列文章定期分享RISCV的每个指令集基础。</p>

<p>二、导读</p>

<p>如果想要接触riscv,那么典型的reference card(参考卡)不可或缺,你会发现相比其他的指令集架构,riscv真的相对简洁了不少(基础的就只有两页),而且操作码7位,相当可自由扩充127个扩展指令,这也是riscv的开放自由思想。这里截图一小部分,需要了解的可以搜下riscv cheat sheet。</p>

<p>链接:<a href="https://www.cl.cam.ac.uk/teaching/1617/ECAD+Arch/files/docs/RISCVGreenCardv8-20151013.pdf">https://www.cl.cam.ac.uk/teaching/1617/ECAD+Arch/files/docs/RISCVGreenCardv8-20151013.pdf</a></p>

<p> &nbsp;</p>

<p>(一)RV32I指令格式</p>

<p> &nbsp;</p>

<p>如书中描述,RV32I 基础指令集的一页图形表示。 对于每幅图, 将有下划线的字母从左到右连接起来,即可组成完整的 RV32I 指令集。对于每一个图,集合标志{}内列举了指令的所有变体,变体用加下划线的字母或下划线字符_表示。特别的,下划线字符_表示对于此指令变体不需用字符表示。 例如,下图表示了这四个 RV32I 指令: slt, slti, sltu, sltiu:</p>

<p> &nbsp;</p>

<p>书中展示了6种基本指令格式,分别是用于寄存器-寄存器操作的 R 类型指令,用于短立即数和访存 load 操作的 I 型指令,用于访存 store 操作的 S 型指令,用于条件跳转操作的 B 类型指令,用于长立即数的 U 型指令和用于无条件跳转的 J 型指令。</p>

<p> &nbsp;</p>

<p>这里使用最简单的U类型种的lui指令,配合利用HPM5300EVK配合SEGGER Embedded Studio来进行说明上述所说的指令格式是什么意思。</p>

<p>随便打开hpm_sdk一个例子,从左边的汇编窗口找到lui的汇编,比如以下:</p>

<p>可以看到,ses这个IDE对于汇编和机器码呈现还是是否直观的,对于不熟悉gdb调试又想查看汇编的开发者是十分友好的。</p>

<p>对于左边的8000452a指的是存储到flash的机器码的flash地址,中间的E40007B7就是机器码,最右边就是汇编部分。</p>

<p>hpm单片机支持压缩指令集RV32C,所以机器码有些会是16位,这对于固件空间的缩小利用是有一定帮助的。</p>

<p> &nbsp;</p>

<p>可以分析下E40007B7这个机器码对应的Lui指令格式,如下图可看到</p>

<p>E40007B7的低7位代表操作码opcode,也就是lui的操作码(0b0110111),7到11bit是5位目的寄存器(0b01111),这很奇妙,5bit宽度刚好是32,对应的就是寄存器的长度(32bit),换成十六进制就是15,对应的通用寄存器就是X15,也就是别名a5寄存器。</p>

<p>高11位就是立即数,对应的十六进制就是0xE4000,最终根据该机器码可以反汇编得到:</p>

<p>把一个20位的立即数0xE4000加载到a5寄存器,对应的汇编指令就是:lui a5, 0xE4000,刚好可以对上ses IDE的汇编显示。</p>

<p> &nbsp;</p>

<p>(二)RISCV通用寄存器</p>

<p>无论是RISCV32还是RISCV64,他的通用寄存器的数量都是32个,分别是x0~x31,当然为了方便汇编编写,每个寄存器也有别名,在参考卡中如下:</p>

<p> &nbsp;</p>

<p>RISC-V 有足够多的寄存器来达到两全其美的结果:既能将操作数存放在寄存器中,同时也能减少保存和恢复寄存器的次数。其中的关键在于,在函数调用的过程中不保留部分寄存器存储的值,称它们为临时寄存器;另一些寄存器则对应地称为保存寄存器。不再调用其它函数的函数称为叶函数。当一个叶函数只有少量的参数和局部变量时,它们可以都被存储在寄存器中,而不会&ldquo;溢出(spilling)&rdquo;到内存中。但如果函数参数和局部变量很多,程序还是需要把寄存器的值保存在内存中,不过这种情况并不多见。</p>

<p>这里说明下caller是调用者,调用(或执行)一个函数的代码段或者函数,是主动发起函数调用的一方,可以理解是&quot;甲方&quot;。</p>

<p>callee是被调用者,被调用的函数本身,是被动接收函数调用并且执行对应操作的一方,可以理解是&quot;乙方&quot;。</p>

<p>从上面的表格知道,从通用寄存器上看,X0到X31,调用者需要保存的通用寄存器16个,剩下的是被调用者需要保存的寄存器。</p>

<p>这里可以从函数调用阶段来进行说明通用寄存器的作用,配合利用HPM5300EVK配合SEGGER Embedded Studio,使用helloworld例子来说明。</p>

<p>函数调用基本分为以下几个阶段,在书中也提到:</p>

<p> &nbsp;</p>

<p>1、进入到被调用者函数的开始位置(caller需要保存的)</p>

<p> &nbsp;</p>

<p>2、将函数传参存储到指定寄存器上</p>

<p>这里就是上图的x10~x17,相比其他指令集架构参数依赖于内存,也就是需要保存在栈中,riscv反而可以通过寄存器保存参数,最多可以存储8个参数,当然超过的也会存储到内存中。一般应用还是推荐不要多余8个参数传参。</p>

<p>那么我们进入到board_timer_create API中,传参是300和board_led_toggle函数指针。那么将会存储在x10和x11这两个寄存器上。</p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p>可以看到,x10现在是300,是符合预期的,对于x11是0x8000669e,我们可以在SES IDE中的汇编窗口查到该函数地址。</p>

<p>0x8000669e对应的就是board_led_toggle,这也是符合预期的。</p>

<p> &nbsp;</p>

<p>3、获取函数局部资源变量保存在寄存器,并执行函数中的指令,可以看到寄存器窗口会发生不同的赋值变化</p>

<p>4、执行完毕之后将返回值存储到调用者能够访问的位置(X10寄存器),恢复寄存器,出栈释放局部资源</p>

<p>5、返回进行下一个调用函数位置。</p>

<p>在ses IDE中也展示了main调用者主函数调用被调用者函数的一些汇编代码过程。</p>

<p> &nbsp;</p>

<p>三、总结</p>

<p>1、riscv的书籍众多,RISC-V开放架构设计之道这本书籍值得阅读,书中内容简洁但又不缺完整,而且丰富的参考卡可作为riscv的日常参考指南所用</p>

<p>2、对于技术书籍,需要有一个实操机会可以加深印象,先楫的处理器内核遵守risv规范,具有多种扩展指令集,而其配套的SEGGER Embedded Studio这个IDE通过查看机器码和汇编,让一些不熟悉gdb调试的开发者,是一个值得推荐的实操平台。</p>
</article>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

Jacktang 发表于 2024-6-20 07:28

<p>是初学者学习掌握RISC-V指令集架构所有的基本要素的好书</p>

freebsder 发表于 2024-6-26 14:43

<p>这个和arm有啥区别?除了不要钱。</p>

rtyu789 发表于 2024-9-21 22:19

freebsder 发表于 2024-6-26 14:43
这个和arm有啥区别?除了不要钱。

<p>指令集更加精简了,代码的执行效率可以更高,书中有详细RISC-V和x86,arm指令集的性能对比</p>

freebsder 发表于 2024-9-24 15:28

rtyu789 发表于 2024-9-21 22:19
指令集更加精简了,代码的执行效率可以更高,书中有详细RISC-V和x86,arm指令集的性能对比

<p>主要是现在硬件过剩,软件跟不上<img height="63" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/victory.gif" width="61" /></p>
页: [1]
查看完整版本: 《RISC-V 开放架构设计之道》- RISC-V开放架构设计简读