CS 2733 Computer Organization II
|
Answer: In your case there are two copies archived, received about 2 minutes apart. The archiving seems to be working fine, but sometimes the email does not work -- I don't know why. One student complained about a workstation clock that was 5 hours fast, so if this happens to you, let me know.
Answer: I'm not sure why you think that addUp should add the registers $s0, $s1, and $s2.
addUp is supposed to add its three input parameters, which under MIPS convensions would be $a0, $a1, and $a2, NOT $s0, $s1, and $s2 as you say above.
In preparation for the call to writeArray from within main, following MIPS parameter convensions, you must load $a0 with the starting address of A, and load $a1 with the value of n. The writeArray function would mess up $a0, so unless you save its value, the same loads into $a0 and $a1 are needed in preparation to call the computeArray function from within main.
From within computerArray, there is a call to the addUp function. In preparation for this call, you must load the values of A[i], B[i], and i into registers $a0, $a1, and $a2. Of course, addUp just adds these and returns the sum in $v0.
Finally, from within computeArray, there is another call to writeArray. Here you must load $a0 and $a1 with the starting address of C and the value of n. In this case, you must not use a la instruction to get the starting address of C, since C should be allocated on the stack.
Wheh!
Answer: The MIPS convention of passing the first four parameters in registers $a0, $a1, $a2, and $a3 is there so that different individuals can write MIPS functions separately and have them work with one another. It is an artificial convention. It makes no difference what use the registers had previously been put to. In the current case, you are supposed to mirror the given C code faithfully:
Initially, the call from main of writeArray(A, n) means that the address of A is loaded into $a0 and the value of n is loaded into $a1. As the function computeArray starts executing, the value of n is still in $a1. This makes no difference. Just before you make the call addUp(A[i], B[i], i), the values of A[i}, B[i], and i must be loaded into $a0, $a1, and $a2. It makes no difference that previously $a1 held the value of n. The value of n will be overwritten. (Wheh!)
If you are writing a short MIPS program by yourself, there might be little or no benefit to following MIPS parameter conventions. (I asked you to follow them here for practice.) For example, suppose you are passing a single parameter, call it n, to a function. Then you load n into $a0 and call the function. Suppose the function called prints a message, and calls another function with the same parameter. In this case you must use $a0 as part of the syscall, so you must save the value of n somewhere, and then load it into $a0 again before the next function call. If you didn't need to follow MIPS conventions, you could save some loads.
Answer: No problem. The submissions are stored by CS account name, and we have that nicely cross-referenced with your real name (finger in Unix, or Roster by Username). We won't even notice the absence of your name!
Answer: I don't know where you're getting 31200004. Start with addi $t1, $0, 4. The opcode is 8hex = 810. The code for $t1 is 9 and of course $0 is 0. Remember that in the machine language, the target register comes last. Thus the fields of this instructions are:
names (bits) | op (6) | rs (5) | rt (5) | immediate (16) |
---|---|---|---|---|
contents (decimal) | 8 | 0 | 9 | 4 |
contents (binary) | 001000 | 00000 | 01001 | 0000000000000100 |
contents (by 4's) | 0010 00 | 00 000 | 0 1001 | 0000 0000 0000 0100 |
So the hex digits are: 20090004. This instruction remains unchanged no matter where it appears in memory, so the fact that it is loaded at 00400020 makes no difference.
Only two instructions depend on location information: first j done, which needs to use the absolute address: 00400020hex + 3210 = 00400020hex + 20hex = 00400040hex. The jump instruction keeps this number (without the two low order binary 0's) in the low order 26 bits of the instruction itself, so it has 400040hex >> 2 = 100010hex in the low 26 bits. Putting in the opcode for a jump gives the whole instruction in hex: 08100010hex.
The instruction la $t1, A depends on the starting address of the array A, which is 10010000hex. This is a pseudo instruction in which the 32-bit constant must be assembled from two instructions. First a lui $t1, 0x1001 and then an ori $t1, $t1, 0x0000. These when converted to machine language (with some effort) become 3C091001hex and 35290000hex. (This last ori instruction is just oring 0, so it could be deleted in this special case.)
sqrt.d $f10, $f2 #f10 = sqrt(n)
if I run it on pandora I get:
pandora2.cs.utsa.edu{ighigiu}16: spim -file sqrtn.s SPIM Version 6.4a of January 12, 2002 (etc.) spim: (parser) parse error on line 9 of file sqrtn.s sqrt.d $f16, $f2 #f10 = sqrt(n) ^
Answer: The spim on the Linux machines is a later version, for a more advanced model of MIPS, and has some extra features including extra instructions, in particular, sqrt.d. (Some other parts are just different, but not an addition.) The third edition of your text is oriented toward this more recent verions of spim.