Reputation:
I am trying to take in some user input that takes in a length of an array, and the user is able to assign the numbers for every index of that array, and then print it out. The Java code I basically want to do is:
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] array = new int[N];
for (int i=0; i<N; i++) {
array[i] = sc.nextInt();
}
Upvotes: 1
Views: 178
Reputation: 26646
The index needs to be scaled to make a byte offset, which can then be used together with the base address of the array. Why? Because each integer takes 4 bytes, and in memory, each of those 4 bytes has its own address. The next integer in the array (e.g. at i+1
) starts at an address that is numerically 4 higher than the last one (e.g. at i
). So, we scale indexes by the size of the array element to make a byte offset to access an indexed element of the array.
There's no point to copy $v0
into $s1
, it will store into memory just fine from $v0
.
You've changed the loop from a for-loop in Java to a do-while in assembly, which means that if the user inputs a count of 0 it will work in the Java version, but break in the assembly version, in that it will still ask for at least one input. The Java version will follow this pattern:
int i = 0;
while ( i < N ) {
<for-loop-body>
i++;
}
So, loop-test, loop-body, increment, loop-test, loop-body, increment...
By the syscalls, looks like you're using MARS or QtSpim, and neither of these uses a branch delay slot by default. I guess you are selecting the option for that?
The approach of putting the increment in the branch delay slot will also loop one more time than you're expecting: in the Java version, the increment will happen before the loop exit test, but in the assembly version, after.
It's a very good approach that you started with pseudo code — that often offers considerable clarity that is hard to have writing in assembly from scratch without an algorithm. But you should try to follow your pseudo code more literally, rather than making arbitrary changes in taking it to assembly code.
Upvotes: 1