Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Help with C++ to MIPS translating

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
iHaTeYoU2
Expert Cheater
Reputation: 0

Joined: 26 Dec 2007
Posts: 151
Location: California

PostPosted: Thu Sep 25, 2014 11:41 pm    Post subject: Help with C++ to MIPS translating Reply with quote

C++
Code:
#include <iostream>
using namespace std;

int main()
{
    int a[] = {41,42,43,44,45,46};
    int i;
    for (i = 0; i < 5; i++)
    {
        if (a[i] < 42 || a[i] > 43)
        {
            cout << "Out of range" << endl;
        }
        else if (a[i] == 42)
        {
            cout << a[i] << endl;
        }
        else
        {
            cout << "Bingo!" << endl;
        }
    }
}


MIPS
Code:
     
#t0 - base address
#s0 - i
#t1 - offset/address of element

       .data
a:     .word 41,42,43,44,45 #int a[] = {41,42,43,44,45}
range: .asciiz "Out of range" #string literal
bingo: .asciiz "Bingo" #string literal
endl:  .ascii "\n"
       .test

main:   la $t0, a #loads the base address of 'a' into $t0?
        li $s0, 0 #sets i=0

loop:    mul $t1, $s0, 4 #?
         add $t1, $t1, $t0 #?
         lw $t2, ($t1) #?

...


Hi CEF, this is my progress so far in translating some of the C++ code into MIPS. I'm currently stuck at not knowing how to translate the for-loop and the only clue my professor gave to me was that block of statement for the loop in MIPS which I don't really understand. If anyone here would be kind enough to guide me through this, I'd highly appreciate it.

Thanks!
Confused


Last edited by iHaTeYoU2 on Sun Sep 28, 2014 6:30 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25791
Location: The netherlands

PostPosted: Fri Sep 26, 2014 4:53 pm    Post subject: Reply with quote

I have absolutely no experience with MIPS (only some basic ARM) so probably can't help you there

I assume your code is incomplete because I can't make out how that loop would loop (no reference to loop )

but to fill in some question marks: (assuming it's similar to ARM)
Code:

mul $t1, $s0, 4 #?

t1 gets the result of s0 multiplied by 4 (which makes sense as the description states that it will hold the offset of the element)
offset 0 is 0, offset 1 is 4 offset 2 is 8

So, I am assuming word is 4 bytes in your architecture ?

Code:

add $t1, $t1, $t0 #?

t1 gets the result of t1+t0

t0 hold the base address if the array 0
t1 holds (before execution) the offset into the array (0, 4, 8, ...)

so, t1 gets the address of the element into that array

Code:

lw $t2, ($t1) #?

t2 gets the word value stored at the address t1 points to

So, at that point t2 holds the value of the array, now do some conditional checks and then "b"ranch to the proper location

when done checking increment s0 with 1
and if so is smaller or equal than 4 jump(branch) to loop: (4 because the array only has 4 entries as opposed to the c++ code which has 5)

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
iHaTeYoU2
Expert Cheater
Reputation: 0

Joined: 26 Dec 2007
Posts: 151
Location: California

PostPosted: Sun Sep 28, 2014 6:29 am    Post subject: Reply with quote

Dark Byte wrote:
...


Thanks, your explanation cleared up my questions for that part!
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites