tags:

views:

501

answers:

3

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?

+1  A: 

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.

e.James
+2  A: 

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.

UncleO
You misunderstood what I explained, because I never said that .pos adds to the position. If you look closely, you'll see that I'm only incrementing the position on instructions and not directives. And leaving room for later changes is not really relevant to the problem.
Nazgulled
You said, "Adding the 430 to our current position (13), put us at position 443." .pos 430 doesn't add 430.
UncleO
I see what you're saying now, you are right, I wasn't exactly typing what I thought I was. But, if "sum" starts at 430 like you said, we need 4 more bytes for it, thus 434 and then 4 more for "i", thus 438, where main starts. Why do you say it's 446?
Nazgulled
By the way, I've updated the first post to fix that... However, I used 438 and not 446 like you said. Waiting for your reply...
Nazgulled
I saw "long" and thought 8. Is it only 4? My mistake. But you still say in your question that the array holds "400 bytes ... and a few more (30)". It doesn't. The 430 bytes hold everything between pos 0 and pos 430, so the array is only 417 long. And the answer is still the same: the extra space is for defense. Defensive coding really is relevant in any programming.
UncleO
Once again, that's irrelevant... This code is for learning purposes, I'm not going to change it in the future where I'll need extra space for other stuff.
Nazgulled
A: 

somebody has a idea of how to pass from C code to Y86 with a command for Linux??

adsad