如何跳回分支语句?

发布于 2024-12-19 18:05:51 字数 764 浏览 3 评论 0原文

我在尝试弄清楚如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-12-26 18:05:51

自增数组运算符上方以及printemptyprintmissprinthit末尾放置一个标签, j 到标签。

例子:

   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

   LBL_Increment:
    increment array

   loop


    print empty:
    print
    j LBL_Increment
    print miss:
    print
    j LBL_Increment
    print hit:
    print
    j LBL_Increment

Put a label above the increment array operator and at the end of print empty, print miss, and print hit, j to the label.

Example:

   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

   LBL_Increment:
    increment array

   loop


    print empty:
    print
    j LBL_Increment
    print miss:
    print
    j LBL_Increment
    print hit:
    print
    j LBL_Increment
﹏半生如梦愿梦如真 2024-12-26 18:05:51

您确实应该为此使用函数调用

   Looping through array, 'ArrayCell' = value at current array location
    if equal ArrayCell, 0, JAL empty
    if equal ArrayCell, 1, JAL miss
    if equal ArrayCell, 2, JAL hit

   LBL_Increment:
    increment array

   loop


   empty:
    print " "
    JR $RA    // return to the instruction after the "JAL empty" instruction.
   miss:
    print "miss"
    JR $RA
   hit:
    print "hit"
    JR $RA

You should really be using function calls for this.

   Looping through array, 'ArrayCell' = value at current array location
    if equal ArrayCell, 0, JAL empty
    if equal ArrayCell, 1, JAL miss
    if equal ArrayCell, 2, JAL hit

   LBL_Increment:
    increment array

   loop


   empty:
    print " "
    JR $RA    // return to the instruction after the "JAL empty" instruction.
   miss:
    print "miss"
    JR $RA
   hit:
    print "hit"
    JR $RA
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文