mardi 13 mai 2014

c - confus au sujet des emplacements de mémoire du présent code d'Assemblée Y86 - Stack Overflow


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?




I don't think pos adds to the position. It is a directive to place code there.


So the "array" starts at position 13, and "sum" starts at 430. That makes main start at 438, and leaves only 417 for the array.


It's always a good idea to leave a little room to make changes later. If you later wanted to increase the array or add another instruction, you would have to adjust the pos directives throughout the code. It also saves sum from being clobbered if there is a mistake accessing the array. Padding to 430 is more defensive.




Let me start by saying that I'm no expert in Y86. I have, however, written a good deal of assembly code.


You are probably correct that .pos 413 would be exact (and correct). I imagine that the student or the teacher just left "a bunch of space" in order to make room for the irmovl ... jump instructions, so as to avoid having to calculate exactly how much room was needed, as you have done.


Your way of thinking about it is correct, and it shows an understanding of the material that your instructor ought to be happy about.



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?



I don't think pos adds to the position. It is a directive to place code there.


So the "array" starts at position 13, and "sum" starts at 430. That makes main start at 438, and leaves only 417 for the array.


It's always a good idea to leave a little room to make changes later. If you later wanted to increase the array or add another instruction, you would have to adjust the pos directives throughout the code. It also saves sum from being clobbered if there is a mistake accessing the array. Padding to 430 is more defensive.



Let me start by saying that I'm no expert in Y86. I have, however, written a good deal of assembly code.


You are probably correct that .pos 413 would be exact (and correct). I imagine that the student or the teacher just left "a bunch of space" in order to make room for the irmovl ... jump instructions, so as to avoid having to calculate exactly how much room was needed, as you have done.


Your way of thinking about it is correct, and it shows an understanding of the material that your instructor ought to be happy about.


0 commentaires:

Enregistrer un commentaire