Page 1 of 1

MIPS Doubt

Posted: Sat Jan 05, 2013 9:22 pm
by nunks
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

Re: MIPS Doubt

Posted: Sun Jan 06, 2013 5:50 pm
by noname120
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 :)

Re: MIPS Doubt

Posted: Thu Jan 10, 2013 10:51 pm
by nunks
Well i havent given pointers alredy but i understood what you said. Thanks my test went just fine ;)

Re: MIPS Doubt

Posted: Wed Nov 13, 2013 6:43 am
by grief3r
$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

Re: MIPS Doubt

Posted: Sat Dec 28, 2013 12:59 am
by xerpi
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;
?

Re: MIPS Doubt

Posted: Sat Dec 28, 2013 1:34 am
by grief3r
sw means store word not load word


overall it's gonna loop 8 times and it's storing 8 values starting at adress 2000

Re: MIPS Doubt

Posted: Sat Dec 28, 2013 12:34 pm
by tomtomdu80
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^^

Re: MIPS Doubt

Posted: Sat Dec 28, 2013 11:33 pm
by m0skit0
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.

Re: MIPS Doubt

Posted: Sat Jan 04, 2014 10:36 am
by Acid_Snake
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).