5. 执行 load 和 store 指令

1 概述

  • 经过 add beq jal jalr 指令的测试,模拟器主要流程都已打通,load & store 指令也容易实现。
  • 这一次的重点是完善指令解析部分代码——关注易于扩展。

1.1 当前 decode 的问题

  • instructions 的初始化工作,可以借助 riscv-opcodes 工程生成的文件
  • decode函数中,遍历指令的语句中 match 做了太多无效工作,指令类型的不同影响的是操作数的解析,把这部分可以放到执行函数中

2 load store 实现

2.1 用于测试load/stroe的程序

.section .data
# Define a data section with some initial values
data1:  .word 0x12345678  # 32-bit word
data2:  .word 0x9abcdef0  # 32-bit word
data3:  .byte 0x12        # 8-bit byte
pad:    .byte 0           # 8-bit byte
data4:  .half 0x3456      # 16-bit halfword

.section .text
.globl _start
_start:
    # Load the address of the data section into register x5
    la x5, data1            # x5 = address of data1

    # Load the value at data1 into register x6
    lw x6, 0(x5)            # x6 = *data1

    # Load the value at data2 into register x7
    lw x7, 4(x5)            # x7 = *data2

    # Load the byte at data3 into register x8
    lb x8, 8(x5)            # x8 = *data3

    # Load the halfword at data4 into register x9
    lh x9, 10(x5)           # x9 = *data4

    # Store the value in register x6 into a new memory location
    sw x6, 12(x5)           # *(data1 + 12) = x6

    # Store the byte in register x8 into a new memory location
    sb x8, 16(x5)           # *(data1 + 16) = x8

    # Store the halfword in register x9 into a new memory location
    sh x9, 18(x5)           # *(data1 + 18) = x9

    # End of the program
    j end                   # Jump to end (an infinite loop)

end:
    j end                   # Infinite loop to stop execution

2.2 添加的功能点

  • 给 Bus 添加读写字/半字的接口
  • 实现 memory 的读写功能
  • 添加通过 GPR 的 ABI name 访问寄存器的功能
  • 优化 decode 代码
  • 实现反汇编功能
  • 实现测试代码需要的指令
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容