如何跳回分支语句?
我在尝试弄清楚如何在 Mips/Mars 架构中实现这一点时遇到了很多麻烦。
我正在制作一款战舰游戏,并将棋盘存储为可容纳 100 个整数的数组。
我需要循环播放器的数组并将每个“单元”中存储的信息转换为图形数据,以便向用户显示棋盘。
我的麻烦源于这样一个事实:向用户显示的字符基于数组每个单元格中的值。
如果值为 0(空)- 打印“[ ]”,如果为 1(猜测且为空)则打印-“[O]”,如果为 2(猜测并命中)则打印-“[X]”。
因此,当我循环遍历数组中的每个单元格时,我需要检查值并分支到适当的打印函数。
我的问题是,如果我分支到 print 语句,如何跳回分支语句所在的位置?
伪代码:
Looping through array, 'ArrayCell' = value at current array location
branch if equal ArrayCell, 0, print empty
branch if equal ArrayCell, 1, print miss
branch if equal ArrayCell, 2, print hit
increment array
print empty:
print then jump back to loop
print miss:
print then jump back to loop
print hit:
print then jump back to loop
打印后如何跳回分支语句以保留您在数组中的位置?
非常感谢!
I'm having a lot of trouble trying to figure out how to implement this in the Mips/Mars architecture.
I'm making a Battleship game and storing the board as an array that can hold 100 ints.
I need to loop through the player's array and convert the information stored in each 'cell' to graphical data for the purpose of displaying the board to the user.
My troubles stem from the fact that the characters being displayed to the user are based on what values are in each cell of the array.
If the value is 0 (empty) - print '[ ]', if 1 (guessed and empty) print - '[O]', and if 2 (guessed and hit) print - '[X]'.
So as I'm looping through each cell in the array, I need to check the value and branch to the appropriate print function.
My problem is that if I branch to the print statement, how do I jump back to where the branch statement was?
Pseudo code:
Looping through array, 'ArrayCell' = value at current array location
branch if equal ArrayCell, 0, print empty
branch if equal ArrayCell, 1, print miss
branch if equal ArrayCell, 2, print hit
increment array
print empty:
print then jump back to loop
print miss:
print then jump back to loop
print hit:
print then jump back to loop
How do you jump back to the branch statement after printing to preserve where you are in the array?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
自增数组
运算符上方以及printempty
、printmiss
和printhit
末尾放置一个标签,j
到标签。例子:
Put a label above the
increment array
operator and at the end ofprint empty
,print miss
, andprint hit
,j
to the label.Example:
您确实应该为此使用函数调用。
You should really be using function calls for this.