学习日志
Assignment 1:程序代码与方向状态
Here is the program for Assignment 1, along with a note on tracking the direction of movement.
Assignment 1 program
# First configure every pin as a GPIO output
_start:
# 1. Base address of the PB GPIO registers
lui a0, 0x2000
# 2. Configure PB0 through PB7 as outputs
lui a1, 0x11111
addi a1, a1, 0x111
sw a1, 0x30(a0)
# 3. Set the initial PB_DAT value / light position
addi a1, zero, 1
# 4. Set the turning thresholds and track the current direction
# Avoid bouncing only between PB7 and PB6 on the return journey
addi t0, zero, 1
addi t1, zero, 128
addi t2, zero, 0 # 0 = go_right, 1 = go_left
loop:
# 5. Write each new state to PB_DAT
sw a1, 0x40(a0)
# 6. Delay so the movement is visible
lui a2, 0x3f00
delay:
addi a2, a2, -1
bne a2, zero, delay
# 7. Turn at endpoints; otherwise keep the current direction
beq a1, t0, go_right
beq a1, t1, go_left
bne t2, zero, go_left
go_right:
addi t2, zero, 0
slli a1, a1, 1
j loop
go_left:
addi t2, zero, 1
srli a1, a1, 1
j loopTranscription note: the go_right and go_left labels, clipped at the left edge of the screenshot, have been completed. The screenshot has a line through addi t1, zero, 128; the copyable version above retains it because the upper-bound comparison still uses t1.
Why does t2 need to remember the direction?
The key register to notice in this program is `t2`: it remembers the light's current direction of movement.
Without tracking and checking that direction, using only the two endpoint checks can appear to work while moving from PB0 toward PB7. On reaching PB7 and returning, however, the light can get stuck bouncing between PB7 and PB6.
When the light state is 10000000, a1 equals t1. The instruction beq a1, t1, go_left branches to go_left, which shifts the state right to 01000000. The program then returns to loop and writes that state to PB_DAT.
On the next iteration, 01000000 is neither the PB0 endpoint nor the PB7 endpoint. Without the direction check bne t2, zero, go_left, neither beq branches, so execution falls through to go_right and shifts the state left again to 10000000.
The light then keeps alternating between 10000000 and 01000000.
The endpoint checks decide when to turn; `t2` remembers which way to keep moving afterward.
Original screenshot

这里是 Assignment 1 的程序代码,以及对移动方向状态的记录。
Assignment 1 程序代码
# 首先配置每个引脚的工作模式,把它们设为 GPIO 输出
_start:
# 1. PB GPIO 寄存器基地址
lui a0, 0x2000
# 2. 配置 PB0~PB7 全为输出模式
lui a1, 0x11111
addi a1, a1, 0x111
sw a1, 0x30(a0)
# 3. 给 PB_DAT 一个初始值,确定灯光的初始位置
addi a1, zero, 1
# 4. 设置转向临界值,并用一个寄存器记录当前移动方向
# 防止到达 PB7 后返回时,只在 PB7 和 PB6 之间循环
addi t0, zero, 1
addi t1, zero, 128
addi t2, zero, 0 # 0 = go_right,1 = go_left
loop:
# 5. 循环,把每次得到的新状态写入 PB_DAT
sw a1, 0x40(a0)
# 6. 延时,方便人眼观察
lui a2, 0x3f00
delay:
addi a2, a2, -1
bne a2, zero, delay
# 7. 判断是否需要转向;未到端点时,沿当前方向继续移动
beq a1, t0, go_right
beq a1, t1, go_left
bne t2, zero, go_left
go_right:
addi t2, zero, 0
slli a1, a1, 1
j loop
go_left:
addi t2, zero, 1
srli a1, a1, 1
j loop整理说明:补全了截图左侧截断的 go_right、go_left 标签。截图中 addi t1, zero, 128 一行带有划线;上面的可复制版本保留了这行,因为后面的上界判断仍使用 t1。
为什么需要 t2 记录方向?
在这个程序里,重点要注意 `t2`:它记住了灯光当前的移动方向。
如果不记录、检查这个方向,只依靠两个端点的判断,从 PB0 向 PB7 移动的过程看起来不会有问题;但到达 PB7 后返回时,就可能一直在 PB7 和 PB6 之间循环。
当灯光状态为 10000000 时,a1 等于 t1,执行 beq a1, t1, go_left,程序进入 go_left,把状态右移成 01000000,再回到 loop 写入 PB_DAT。
下一轮,01000000 既不是 PB0 的端点状态,也不是 PB7 的端点状态。如果没有 bne t2, zero, go_left 这条方向判断,两个 beq 都不跳转,程序就会顺序进入下面的 go_right,把状态重新左移成 10000000。
于是,灯光就在 10000000 和 01000000 这两个状态之间来回。
端点判断负责决定“什么时候转向”,`t2` 则负责记住“转向以后继续往哪边走”。
原稿截图
