Advertising (This ad goes away for registered users. You can Login or Register)

Black Friday Sale: Playstation Plus 1 Year subscription is $39.99 (33% off) on Amazon and other retailers, while supplies last.

MIPS Doubt

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
User avatar
nunks
Posts: 19
Joined: Sat Jan 22, 2011 2:30 pm

MIPS Doubt

Post by nunks » Sat Jan 05, 2013 9:22 pm

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
addi $t5,$t5,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
Advertising
[align=center]Image[/align]

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

Re: MIPS Doubt

Post by noname120 » Sun Jan 06, 2013 5:50 pm

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 :D

And next time, you can use this MIPS simulator to check the action of a program :)
Advertising
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

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

Re: MIPS Doubt

Post by nunks » Thu Jan 10, 2013 10:51 pm

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

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

Re: MIPS Doubt

Post by grief3r » Wed Nov 13, 2013 6:43 am

$t4=0x12345678
$t5=2000

L1: sw $t4,0($t5)
sll $t4,$t4,4
addi $t5,$t5,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

Post by xerpi » Sat Dec 28, 2013 12:59 am

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

Post by grief3r » Sat Dec 28, 2013 1:34 am

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

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

Re: MIPS Doubt

Post by tomtomdu80 » Sat Dec 28, 2013 12:34 pm

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^^

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

Re: MIPS Doubt

Post by m0skit0 » Sat Dec 28, 2013 11:33 pm

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
Image
"just not into this RA stuffz"

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

Re: MIPS Doubt

Post by Acid_Snake » Sat Jan 04, 2014 10:36 am

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

now, the questions asked:
nunks wrote:how is the memory altered throught the program
memory is altered starting from position 2000 all the way to 2032
nunks wrote:what does 0($t5) returns
it will return 0x12345678 as that is what is written there the first time it loops
noname120 wrote: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)
what makes no sense is your post, it may not be a pointer on the PSP, but this is generic MIPS, it could be a pointer on another system or it could just be made up to teach MIPS itself, also just because it's in decimal doesn't mean it's not a pointer, they are both two different ways of showing the same data and are both treated the same way at low level (the machine doesn't make any distinction between decimal and hex).

Post Reply

Return to “Programming”