## MIPS Doubt

nunks
Posts: 19
Joined: Sat Jan 22, 2011 2:30 pm

### MIPS Doubt

So i have this exercise and i wanted some help with it please

\$t4=0x12345678
\$t5=2000

L1: sw \$t4,0(\$t5)
sll \$t4,\$t4,4
bne \$t4,\$zero,L1

And they ask how is the memory altered throught the program.Also, one doubt i have is what does 0(\$t5) returns
[align=center][/align]

noname120
Developer
Posts: 775
Joined: Thu Oct 07, 2010 4:29 pm

### Re: MIPS Doubt

Here is the reversed version:

Code: Select all

``````unsigned int t4 = 0x12345678;
// t5 is a pointer on an array
unsigned int *t5 = 2000;

do
{
// Get the value at the address of \$t5
t4 = *t5;
// Ignore the 4bits on the left of the register
t4 = t4 << 4
// Change the pointer to the next number (in MIPS, numbers are 32bits = 4 bytes)
t5 += 4;
}
while (t4 != 0)
``````
This has really no sense because t5 = 2000 (not a pointer), but it is then used as a pointer with sw \$t4,0(\$t5) (get the value at address \$t5 and store it in \$t4)

Good luck

And next time, you can use this MIPS simulator to check the action of a program
Funny stuff
<yifanlu> I enjoy being loud and obnoxious
<yifanlu> rooting an android is like getting a hooker pregnant
<xerpi> I sometimes think I should leave all this stressing **** and be a farmer instead

nunks
Posts: 19
Joined: Sat Jan 22, 2011 2:30 pm

### Re: MIPS Doubt

Well i havent given pointers alredy but i understood what you said. Thanks my test went just fine
[align=center][/align]

grief3r
Posts: 361
Joined: Sat Nov 09, 2013 4:12 am

### Re: MIPS Doubt

\$t4=0x12345678
\$t5=2000

L1: sw \$t4,0(\$t5)
sll \$t4,\$t4,4
bne \$t4,\$zero,L1

ez it just stores t4 on adress 2000? and then shift left logical 4 bits, then it adds 4 to t5 and then jumps -3 adresses
PSV1001 2.61 FieldRunners
PSP1001 6.60 Pro-C
PSP 3001 6.20 Pro-C2

xerpi
HBL Collaborator
Posts: 139
Joined: Sat Apr 23, 2011 10:45 am
Location: Barcelona

### Re: MIPS Doubt

noname120 wrote:

Code: Select all

``````    // Get the value at the address of \$t5
t4 = *t5;``````
Shouldn't sw \$t4,0(\$t5) be :

Code: Select all

``````// Store the value of \$t4 to the address \$t5
*t5 = t4;
``````
?

grief3r
Posts: 361
Joined: Sat Nov 09, 2013 4:12 am

### Re: MIPS Doubt

sw means store word not load word

overall it's gonna loop 8 times and it's storing 8 values starting at adress 2000
PSV1001 2.61 FieldRunners
PSP1001 6.60 Pro-C
PSP 3001 6.20 Pro-C2

tomtomdu80
Buffer Overflow
Posts: 113
Joined: Tue Nov 20, 2012 6:39 pm
Location: France

### Re: MIPS Doubt

xerpi wrote:
noname120 wrote:

Code: Select all

``````    // Get the value at the address of \$t5
t4 = *t5;``````
Shouldn't sw \$t4,0(\$t5) be :

Code: Select all

``````// Store the value of \$t4 to the address \$t5
*t5 = t4;
``````
?
I think the same, and it has more sense^^

m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

### Re: MIPS Doubt

xerpi is correct indeed: sw \$t4,0(\$t5) means store content of \$t4 in the address pointed by \$t5, which in C is would be *t5 = t4.
grief3r wrote:sw means store word not load word
lw \$t4,0(\$t5) would be t4 = *t5.
I wanna lots of mov al,0xb

"just not into this RA stuffz"

Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

### Re: MIPS Doubt

I like this reverse better:

Code: Select all

``````u32 data = 0x12345678;
u32* dataptr = (u32*)2000;
do{
*dataptr = data;
data *= 16;
dataptr++;
}
while (data != 0);``````
things to be noted:
- whenever possible logical shifts should be converted to multiplications with a power of two operand for readability purposes (and because it's highly probable that the original code uses multiplications and the compiler converts it to shifts for speed).
- if t5 is a pointer there's no need to add 4 to it, the C compiler will treat dataptr++ as actually being dataptr+=4
- registers should be renamed for clarity purposes, having t4 and t5 as names is not clear at all in a code written in C