We had a piece of code in C in one class where we needed to convert it to Y86 and this was written on the board by some guy with the teacher's correction of course.
However, I'm confusing the memory locations and .pos directives on the initial part of the code:
int array[100], sum, i;
int main() {
sum = 0;
for(i = 0; i < 100; i++) {
array[i] = i;
sum += array[i];
}
}
.pos 0
irmovl Stack, %esp
rrmovl %esp, %ebp
jmp main
array:
.pos 430
sum: .long 0
i: .long 0
main:
// (rest of the code that doesn't really matter here)
What I understand from this code is this:
It starts in position 0 (.pos 0), the irmovl instruction takes 6 bytes, so, the next rrmovl instruction starts at position 6 and that instruction takes 2 bytes, we are now at position 8.
The jmp instruction takes 5 bytes starting at 8, we are now at position 13.
Now it's tame to save stack space to hold the 100 integers for the array and to do that we use .pos 430 to hold at least 400 bytes (4 bytes * 100 integers) and 17 more (the next position minus the current one, 430-13=17).
We're now at position 430 and we need to save 4 more bytes to hold sum and another 4 to hold i, which puts at position 438.
At position 438 is where the main code of our program will start.
I think I got everything right, my only question is simple:
Why did we use .pos 430 to hold space for the 100 integers? We should only need exactly 400 bytes to hold all of them. Wouldn't .pos 413 (since the previous position was 13 and we need 400 bytes for the 100 integers, thus 413) be enough and more correct than .pos 430?
What am I missing?