0

So i have declared a [100][2] empty array in the data section, and towards the end of my program i use this array in an if statement. I was wondering how i go about translating this from C to mips assembly This is the if statement in C

if ((x == arr[i][0] && y == arr[i][1]) || (arr[i][0] == x && arr[i][1] == y - 1) || (arr[i][0] == x && arr[i][1] == y + 1) || (arr[i][0] == x - 1 && arr[i][1] == y) || (arr[i][0] == x + 1 && arr[i][1] == y))
            gameOver++;

I understand this is going to take quite a bit of space in the text section in assembly. Would there be an alternative route to take to implement this in mips? I am unsure where to even start with these lines of C. Any help would be appreciated, thanks!

My concern is writing the portion arr[i][0]

dominic99
  • 11
  • 2
  • 1
    For starters notice you have `x == arr[i][0]` and `arr[i][0] == x` which are of course the same so you only need to do that once :) Then notice you can factor out `arr[i]`. Finally reverse the conditions to skip the `gameOver++`. – Jester Nov 28 '22 at 21:48
  • @Jester how would i go about writing the array portion in mips? – dominic99 Nov 28 '22 at 21:52
  • That depends on the size of the elements in the array. – user3386109 Nov 28 '22 at 22:00
  • 1
    Rewrite using pointer arithmetic in C, then translate that to mips. – Jester Nov 28 '22 at 22:00
  • You could get a head start by writing it in C and compiling it into assembly. – Ted Lyngmo Nov 28 '22 at 22:04
  • Try decomposing what you're doing there rather than looking for a total and custom solution in one post. The if-statement and the array access are two separate questions. You should be able to find answers to each of those question here, and then apply those solutions to your particular situation. – Erik Eidt Nov 28 '22 at 22:11
  • As far as taking a lot of text space, I don't know why you'd be worried about that. However, your expression is rich with common subexpressions, so can be optimized to repeat less and be smaller by applying the [optimization](https://en.wikipedia.org/wiki/Common_subexpression_elimination). – Erik Eidt Nov 28 '22 at 22:13
  • I understand how to write a conditional statement in mips, what i am confused about is implementing an array into that. In essence, how do i write “arr[n][0]” – dominic99 Nov 29 '22 at 01:33
  • [2D Array in MIPS](https://stackoverflow.com/q/4881233), and google many other MIPS 2d array questions that have other examples in answers. And/or look at MIPS GCC `-Og` compiler output, e.g. on https://godbolt.org/ - [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Nov 30 '22 at 05:49

0 Answers0