在子例程中使用 TRAP 例程? - LC3总成
因此,我已经完成了一个汇编程序,该程序请求一个字符,读取它,回显它,重复四次,然后在连续的字符串中打印出这些字符。然后我想尝试子例程(我们在课堂上还没有学过),所以我制作了以下子例程来将换行符打印到控制台:
PRINT_NEWLINE ;procedure to print a newline
AND R0,R0,#0 ;clear output register
LD R0,NEWLINE ;load newline into output regiester
TRAP x21 ;print it out
RET ;jump to address in R7
它被“调用”如下:
JSR PRINT_NEWLINE
运行这个之后,我注意到一些东西奇怪的是,程序似乎在第一次调用 PRINT_NEWLINE
后停止了。然后我意识到 TRAP 保存了下一条指令的地址,在本例中是 RET,在用于子程序链接的寄存器 R7 中。这将覆盖 JSR
保存在 R7
中的地址。所以它看起来停止的原因是在 TRAP 例程完成后,它加载了我的 RET 指令。实际上,由于 TRAP
,将 pc 计数器更改为其自身。有点像无限循环。
这一切都很好,我明白发生了什么,但是有没有办法可以在子例程中使用系统 TRAP 例程,同时仍然使用系统 JSR 指令?
当然,我可以在 JSR PRINT_NEWLINE
调用之后手动将指令的地址存储到不同的寄存器中,然后在 PRINT_NEWLINE
子例程的末尾,JMP
到该寄存器中的地址。
但是,这对我来说似乎是错误的,我是一名程序员,因此我很懒,宁愿只是享受别人的劳动成果并使用 JSR
和 RET< /代码>。
那么是否可以在子例程中调用 TRAP 例程并仍然使用 RET 从该子例程“返回”?如何实现这一目标?
谢谢!
So, I have finished an assembly program that asks for a character, reads it, echos it, repeats that four times, then prints out those characters in a consecutive string. I then wanted to experiment with subroutines (which we haven't learned about in class yet), so I made the following subroutine to print a newline character to the console:
PRINT_NEWLINE ;procedure to print a newline
AND R0,R0,#0 ;clear output register
LD R0,NEWLINE ;load newline into output regiester
TRAP x21 ;print it out
RET ;jump to address in R7
It gets "called" like this:
JSR PRINT_NEWLINE
After running this, I noticed something weird, the program seemed to stop after the first call to PRINT_NEWLINE
. Then I realized that TRAP saves the address of the next instruction, which in this case is RET
, in R7
, the register used for subroutine linking. This overwrites the address which was saved in R7
by JSR
. So the reason it appears to stop is that after the TRAP
routine finishes, it loads my RET
instruction. Which is actually, because of the TRAP
, changing the pc counter to itself. Sort of like an infinite loop.
That's all great, and I understand what's going on, but is there a way I can use system TRAP
routines inside of my subroutine, while still using the system JSR
instruction?
I could, of course, manually store the address of the instruction after the JSR PRINT_NEWLINE
call into a different register, and then at the end of my PRINT_NEWLINE
subroutine, JMP
to the address in that register.
But, that seems wrong to me and, I'm a programmer, therefore I'm lazy, and would rather just enjoy the fruits of other's labor and use the combination of JSR
and RET
.
So is it possible to call a TRAP
routine in a subroutine and still use RET
to "return" from that subroutine? How would one accomplish this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在调用
TRAP
之前将R7
的值保存在其他位置,并在之后恢复它。您也需要对嵌套子例程调用执行此操作。大多数 RISC 架构要求用户保存某种形式的返回地址;它们不像 x86 处理器那样在硬件中实现内存堆栈。You will need to save the value of
R7
somewhere else before callingTRAP
and restore it afterward. You will need to do that for nested subroutine calls as well. Most RISC architectures require some form of return address saving by the user; they do not implement a memory stack in hardware like x86 processors do.