last day (15 days later) » 

1:30 AM
0
A: How to convert a high level language statement involving arrays into MIPS 32 bit?

cocoakrispies93This is just me working out my own homework problem with the help of advice from commenters. Earlier revisions of this answer included a wrong version that loaded from J[6] instead of storing and overuse of temps. This is the pseudo-code: x1 = d-e # store subtraction x2 = x1 * 4 ...

 
Missing scaling of the index into a byte offset, so scale $t3 before you do the add.
Also stores & loads are a bit backwards. There is no need to load J[6] as it is a target of an assignment. You are correct to use 24($s1) but it (a) must be done last, the final aspect of the assignment, and (b) using a store instruction (not a load instruction).
the 0($t4) will be right once $t3 is scaled before the add. However, that address 0($t4) is the source of what to store into the target J[6], so that 0($t4) should be used in a load instruction (not a store).
 
What does scale mean? So it's sw $t2, 24($s1) and sw $s4, 0($t2)? @ErikEidt
 
Scaling is multiplication.
 
My professor doesn't allow multiplication yet so I guess that's why I don't understand scaling, he's informing me to just do adding until it's multiplied. So would that just be addi $t4, $t4, 4? or add $t4, $t4, $t4 four times? @ErikEidt
 
I have edited my answer several times, please re-read it. We don't have to use multiplication to multiply by 4 but the general concept of scaling is to multiply by the element size to convert from array index to byte offset for doing proper pointer arithmetic on byte addressable machines. For 32-bit word elements, index 1 is byte offset 4; index 2 is byte offset 8, and index i is byte offset i * 4, etc..
Ok, good update, getting closer! Need to change first sw to lw, pick a new register and use that in the succeeding sw.
 
1:30 AM
@cocoakrispies93, please don't take the feedback personally. I empathise that stackoverflow isn't particularly beginner-friendly. It's designed as a repository of specific questions and their specific answers. Not all questions can be asked here. In this case you asked how to solve x, received an answer, then added a refinement to say "I actually want to solve y". I understand that to a new-comer that might seem fine, but it's not appropriate for stackoverflow. Here's a nice guide that covers how to ask homework questions.
 
You only need one temp register. It's ok to overwrite an old temporary with a new one when you no longer need it, e.g. lw $t3, 0($t3) if you computed K + (d-e)*4 in $t3
Also, in your actual code, $t4 isn't an index, it's a pointer. Because you added an index to the array base address. "K's 0th index" is just be 0. You actually want the address of K[0], in C terms &K[0] = K.
 
Looks like you finally got there! That code looks right. Take Peter's statement about your code comment seriously: pointer + offset is another pointer.
 
@PeterCordes and @ErikEidt the only thing I'm not sure if I understood is lw $t3, 0($t3) #Load K's index back into J[6] for this is $t3 different from 0($t3) because of pointers? My C is very rusty, I got lazy with Java and Python after learning C/C++
 
@cocoakrispies93: Before that lw, $t3 holds the pointer you calculated, &K[d-e] i.e. K + d - e. After that lw, it will hold K[d-e] (the value at that address). The comment is nonsense; that instruction doesn't involve J[6], and the load result is the value from k[idx], not an index.
Over-using temporaries isn't a bug; if you have tons of registers it's not wrong to use them, it's just harder to read and not normal style for hand-written code. Common for compilers, though. But IDK why you're still using $t5, especially if you're going to mention over-using temporaries in your answer. (Also IDK why you kept two separate version of the same code block. At least you removed the =, not == comment; IDK why that was ever relevant since you didn't have a version that did only loads and used beq or xor/sltu to booleanize.)
 
I'm not sure what beq or xor/sltu is
Just so I can keep the edits in case the new revision isn't as good and I want to revert back or quickly see what changed.
I believe the final answer has been updated but I'm not sure, I'm not sure how to implement the pseudo-code, did I get it right that time? @ErikEidt
 
1:30 AM
Yes, your first assembly section is good -- except for the sw $s1, 0($t3) -- that doesn't match anything in the C statement or pseudo code, so just remove it. (In fact what that particular sw line would do is store the address of J (at index 0) into K[d-e], and, nothing like that is desired.) By count, the pseudo code break down of the C statement has only one load and one store, and the assembly should match load & store counts. (Your second assembly section is not so good, so stick with the first!)
 
Okay, gotcha! So would lw $s1, 24($s1) dereference J[6]? @ErikEidt
 
Yes, it would be a dereference for reading (pay attention to direction: read vs. write, i.e. load vs. store, right hand of = vs. left hand of =). However, a problem with that particular instruction is that it would also overwrite your J pointer in $s1, so you would loose J for subsequent usages; if you wanted to read J[6] and still keep J for later use, then target a different register as in lw $t7, 24($s1). And hopefully, you know that you don't need to read-dereference of J[6] in this scenario, from the C and pseudo code.
If the C code read J[6] += 100 you would have to both read-dereference J[6] and write-dereference J[6] with an add 100 in between the two dereferences. But since the = in posted question is a simple assignment, we don't read-dereference J[6], only write-dereference, and after the value to put there has been computed.
 
I think I understand, sorry about all of this it's hard to find information on dereferencing and scaling at such a low level, all I'm seeing is far more advanced topics in assembly IDE's with commands that haven't been taught yet
 

last day (15 days later) »