Value in address changes without altering it in mips
So, I have an array with size 10, and I want to create a sparse array(which means everytime an element of the array has a non zero value, store its position and the value in the sparse array, example: array = {0,0,1,0,5}, sparseArray = {2,1,4,5} ), the problem is that for some reason the value of $t4 (and also of ($t2)) changes and i can't understand why, for example when i try to print the array after i call createSparse subroutine, it is completely changed.
createSparse:
li $t0, 0 #t0: counter for iteration in the for loop for checking all the array elements
li $t1, 0 #t1: counter for the length of the sparse array
move $t2, $a1 #t2: contains the address for the first element of the array
move $t3, $a2 #t3: contains the address for the first element of the sparse array
loop2:
beq $t0, 10, return2 #start of the for iteration.
lw $t4, ($t2) #load the value of the array temporarily in $t4
beq $t4, 0, continue #check if $t4 == 0
sw $t0, ($t3) #if it is not zero store the position it had in the array in the sparseArray
addi $t3, $t3, 4 #go to the next element of the sparseArray
sw $t4, ($t3) #store the value
addi $t3, $t3, 4 #go to the next element of the sparse array
addi $t1, $t1, 2 #length of the sparse array increased by 2
addi $t0, $t0, 1 #t0 += 1;
addi $t2, $t2, 4 #t2 += 4;
j loop2
continue: #if the value of the element of the array we checked equals zero then we go to check the next element of the array
addi $t0, $t0, 1
addi $t2, $t2, 4
j loop2
return2:
move $v0, $a2
move $v1, $t1
jr $ra
mips spim qtspim
add a comment |
So, I have an array with size 10, and I want to create a sparse array(which means everytime an element of the array has a non zero value, store its position and the value in the sparse array, example: array = {0,0,1,0,5}, sparseArray = {2,1,4,5} ), the problem is that for some reason the value of $t4 (and also of ($t2)) changes and i can't understand why, for example when i try to print the array after i call createSparse subroutine, it is completely changed.
createSparse:
li $t0, 0 #t0: counter for iteration in the for loop for checking all the array elements
li $t1, 0 #t1: counter for the length of the sparse array
move $t2, $a1 #t2: contains the address for the first element of the array
move $t3, $a2 #t3: contains the address for the first element of the sparse array
loop2:
beq $t0, 10, return2 #start of the for iteration.
lw $t4, ($t2) #load the value of the array temporarily in $t4
beq $t4, 0, continue #check if $t4 == 0
sw $t0, ($t3) #if it is not zero store the position it had in the array in the sparseArray
addi $t3, $t3, 4 #go to the next element of the sparseArray
sw $t4, ($t3) #store the value
addi $t3, $t3, 4 #go to the next element of the sparse array
addi $t1, $t1, 2 #length of the sparse array increased by 2
addi $t0, $t0, 1 #t0 += 1;
addi $t2, $t2, 4 #t2 += 4;
j loop2
continue: #if the value of the element of the array we checked equals zero then we go to check the next element of the array
addi $t0, $t0, 1
addi $t2, $t2, 4
j loop2
return2:
move $v0, $a2
move $v1, $t1
jr $ra
mips spim qtspim
I don't really get what you mean by "the value of $t4 (and also of ($t2)) changes and i can't understand why", considering that the code you've posted contains instructions that alter the values of both$t2
and$t4
.
– Michael
Dec 30 '18 at 16:03
@Michael what i do is i load in t4 the value stored in t2 and then i store it in the sparse array t3, i dont change t4 or t2 anywhere, coud you please explain how i change them? Im pretty new to mips and i havent found a way to clear up thingss anywhere.
– Giannis Merikas
Dec 30 '18 at 16:21
"i load in t4 the value stored in t2". Every time you do that you're changing$t4
. And you're changing$t2
when you increment it.
– Michael
Dec 30 '18 at 16:34
@Michael Ohh ok, then how do i it without altering it?
– Giannis Merikas
Dec 30 '18 at 17:49
add a comment |
So, I have an array with size 10, and I want to create a sparse array(which means everytime an element of the array has a non zero value, store its position and the value in the sparse array, example: array = {0,0,1,0,5}, sparseArray = {2,1,4,5} ), the problem is that for some reason the value of $t4 (and also of ($t2)) changes and i can't understand why, for example when i try to print the array after i call createSparse subroutine, it is completely changed.
createSparse:
li $t0, 0 #t0: counter for iteration in the for loop for checking all the array elements
li $t1, 0 #t1: counter for the length of the sparse array
move $t2, $a1 #t2: contains the address for the first element of the array
move $t3, $a2 #t3: contains the address for the first element of the sparse array
loop2:
beq $t0, 10, return2 #start of the for iteration.
lw $t4, ($t2) #load the value of the array temporarily in $t4
beq $t4, 0, continue #check if $t4 == 0
sw $t0, ($t3) #if it is not zero store the position it had in the array in the sparseArray
addi $t3, $t3, 4 #go to the next element of the sparseArray
sw $t4, ($t3) #store the value
addi $t3, $t3, 4 #go to the next element of the sparse array
addi $t1, $t1, 2 #length of the sparse array increased by 2
addi $t0, $t0, 1 #t0 += 1;
addi $t2, $t2, 4 #t2 += 4;
j loop2
continue: #if the value of the element of the array we checked equals zero then we go to check the next element of the array
addi $t0, $t0, 1
addi $t2, $t2, 4
j loop2
return2:
move $v0, $a2
move $v1, $t1
jr $ra
mips spim qtspim
So, I have an array with size 10, and I want to create a sparse array(which means everytime an element of the array has a non zero value, store its position and the value in the sparse array, example: array = {0,0,1,0,5}, sparseArray = {2,1,4,5} ), the problem is that for some reason the value of $t4 (and also of ($t2)) changes and i can't understand why, for example when i try to print the array after i call createSparse subroutine, it is completely changed.
createSparse:
li $t0, 0 #t0: counter for iteration in the for loop for checking all the array elements
li $t1, 0 #t1: counter for the length of the sparse array
move $t2, $a1 #t2: contains the address for the first element of the array
move $t3, $a2 #t3: contains the address for the first element of the sparse array
loop2:
beq $t0, 10, return2 #start of the for iteration.
lw $t4, ($t2) #load the value of the array temporarily in $t4
beq $t4, 0, continue #check if $t4 == 0
sw $t0, ($t3) #if it is not zero store the position it had in the array in the sparseArray
addi $t3, $t3, 4 #go to the next element of the sparseArray
sw $t4, ($t3) #store the value
addi $t3, $t3, 4 #go to the next element of the sparse array
addi $t1, $t1, 2 #length of the sparse array increased by 2
addi $t0, $t0, 1 #t0 += 1;
addi $t2, $t2, 4 #t2 += 4;
j loop2
continue: #if the value of the element of the array we checked equals zero then we go to check the next element of the array
addi $t0, $t0, 1
addi $t2, $t2, 4
j loop2
return2:
move $v0, $a2
move $v1, $t1
jr $ra
mips spim qtspim
mips spim qtspim
asked Dec 29 '18 at 16:18
Giannis MerikasGiannis Merikas
1
1
I don't really get what you mean by "the value of $t4 (and also of ($t2)) changes and i can't understand why", considering that the code you've posted contains instructions that alter the values of both$t2
and$t4
.
– Michael
Dec 30 '18 at 16:03
@Michael what i do is i load in t4 the value stored in t2 and then i store it in the sparse array t3, i dont change t4 or t2 anywhere, coud you please explain how i change them? Im pretty new to mips and i havent found a way to clear up thingss anywhere.
– Giannis Merikas
Dec 30 '18 at 16:21
"i load in t4 the value stored in t2". Every time you do that you're changing$t4
. And you're changing$t2
when you increment it.
– Michael
Dec 30 '18 at 16:34
@Michael Ohh ok, then how do i it without altering it?
– Giannis Merikas
Dec 30 '18 at 17:49
add a comment |
I don't really get what you mean by "the value of $t4 (and also of ($t2)) changes and i can't understand why", considering that the code you've posted contains instructions that alter the values of both$t2
and$t4
.
– Michael
Dec 30 '18 at 16:03
@Michael what i do is i load in t4 the value stored in t2 and then i store it in the sparse array t3, i dont change t4 or t2 anywhere, coud you please explain how i change them? Im pretty new to mips and i havent found a way to clear up thingss anywhere.
– Giannis Merikas
Dec 30 '18 at 16:21
"i load in t4 the value stored in t2". Every time you do that you're changing$t4
. And you're changing$t2
when you increment it.
– Michael
Dec 30 '18 at 16:34
@Michael Ohh ok, then how do i it without altering it?
– Giannis Merikas
Dec 30 '18 at 17:49
I don't really get what you mean by "the value of $t4 (and also of ($t2)) changes and i can't understand why", considering that the code you've posted contains instructions that alter the values of both
$t2
and $t4
.– Michael
Dec 30 '18 at 16:03
I don't really get what you mean by "the value of $t4 (and also of ($t2)) changes and i can't understand why", considering that the code you've posted contains instructions that alter the values of both
$t2
and $t4
.– Michael
Dec 30 '18 at 16:03
@Michael what i do is i load in t4 the value stored in t2 and then i store it in the sparse array t3, i dont change t4 or t2 anywhere, coud you please explain how i change them? Im pretty new to mips and i havent found a way to clear up thingss anywhere.
– Giannis Merikas
Dec 30 '18 at 16:21
@Michael what i do is i load in t4 the value stored in t2 and then i store it in the sparse array t3, i dont change t4 or t2 anywhere, coud you please explain how i change them? Im pretty new to mips and i havent found a way to clear up thingss anywhere.
– Giannis Merikas
Dec 30 '18 at 16:21
"i load in t4 the value stored in t2". Every time you do that you're changing
$t4
. And you're changing $t2
when you increment it.– Michael
Dec 30 '18 at 16:34
"i load in t4 the value stored in t2". Every time you do that you're changing
$t4
. And you're changing $t2
when you increment it.– Michael
Dec 30 '18 at 16:34
@Michael Ohh ok, then how do i it without altering it?
– Giannis Merikas
Dec 30 '18 at 17:49
@Michael Ohh ok, then how do i it without altering it?
– Giannis Merikas
Dec 30 '18 at 17:49
add a comment |
1 Answer
1
active
oldest
votes
I found the solution after all, so I'm going to post it in case someone has the same problem.
The addresses that are stored in registers $t2 and $t3 are overlapping each other in ram so when i store $t0 in ($t3) it erases what was stored in ($t2) and it stores $t0 instead.
So if you wanna get around this problem, instead of writing:
sw $t0, ($t3)
You should write
sw $t0, 40($t3)
So now since the array has length of 10 x 4 bytes. You now store it in the next 40 bytes of ram and it doesnt erase what is stored in the first 40 bytes of ram.
If anyone has something else to add, it would be really helpful.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53971220%2fvalue-in-address-changes-without-altering-it-in-mips%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found the solution after all, so I'm going to post it in case someone has the same problem.
The addresses that are stored in registers $t2 and $t3 are overlapping each other in ram so when i store $t0 in ($t3) it erases what was stored in ($t2) and it stores $t0 instead.
So if you wanna get around this problem, instead of writing:
sw $t0, ($t3)
You should write
sw $t0, 40($t3)
So now since the array has length of 10 x 4 bytes. You now store it in the next 40 bytes of ram and it doesnt erase what is stored in the first 40 bytes of ram.
If anyone has something else to add, it would be really helpful.
add a comment |
I found the solution after all, so I'm going to post it in case someone has the same problem.
The addresses that are stored in registers $t2 and $t3 are overlapping each other in ram so when i store $t0 in ($t3) it erases what was stored in ($t2) and it stores $t0 instead.
So if you wanna get around this problem, instead of writing:
sw $t0, ($t3)
You should write
sw $t0, 40($t3)
So now since the array has length of 10 x 4 bytes. You now store it in the next 40 bytes of ram and it doesnt erase what is stored in the first 40 bytes of ram.
If anyone has something else to add, it would be really helpful.
add a comment |
I found the solution after all, so I'm going to post it in case someone has the same problem.
The addresses that are stored in registers $t2 and $t3 are overlapping each other in ram so when i store $t0 in ($t3) it erases what was stored in ($t2) and it stores $t0 instead.
So if you wanna get around this problem, instead of writing:
sw $t0, ($t3)
You should write
sw $t0, 40($t3)
So now since the array has length of 10 x 4 bytes. You now store it in the next 40 bytes of ram and it doesnt erase what is stored in the first 40 bytes of ram.
If anyone has something else to add, it would be really helpful.
I found the solution after all, so I'm going to post it in case someone has the same problem.
The addresses that are stored in registers $t2 and $t3 are overlapping each other in ram so when i store $t0 in ($t3) it erases what was stored in ($t2) and it stores $t0 instead.
So if you wanna get around this problem, instead of writing:
sw $t0, ($t3)
You should write
sw $t0, 40($t3)
So now since the array has length of 10 x 4 bytes. You now store it in the next 40 bytes of ram and it doesnt erase what is stored in the first 40 bytes of ram.
If anyone has something else to add, it would be really helpful.
answered Dec 31 '18 at 18:12
Giannis MerikasGiannis Merikas
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53971220%2fvalue-in-address-changes-without-altering-it-in-mips%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I don't really get what you mean by "the value of $t4 (and also of ($t2)) changes and i can't understand why", considering that the code you've posted contains instructions that alter the values of both
$t2
and$t4
.– Michael
Dec 30 '18 at 16:03
@Michael what i do is i load in t4 the value stored in t2 and then i store it in the sparse array t3, i dont change t4 or t2 anywhere, coud you please explain how i change them? Im pretty new to mips and i havent found a way to clear up thingss anywhere.
– Giannis Merikas
Dec 30 '18 at 16:21
"i load in t4 the value stored in t2". Every time you do that you're changing
$t4
. And you're changing$t2
when you increment it.– Michael
Dec 30 '18 at 16:34
@Michael Ohh ok, then how do i it without altering it?
– Giannis Merikas
Dec 30 '18 at 17:49