Hi, I was wondering if anyone could clarify what this question is asking for?

Write a program with a loop and indexed addressing that exchanges every pair of values in an array with an even number of elements. Therefore, item i will exchange with item i+1, and item i+2 will exchange with item i+3, and so on.

I've read it so many times but I don't understand what is being asked. "every pair of values in an array" and what does it mean by the "number of elements"?

Imagine an array:

{1, 2, 3, 4, 5, 6}

Now imagine dividing it up into pairs:

{1, 2}
{3, 4}
{5, 6}

Then swap each pair:

{2, 1}
{4, 3}
{6, 5}

Put back together you have the final result:

{2, 1, 4, 3, 6, 5}

Which in pseudocode would be something like this:

a = {1, 2, 3, 4, 5, 6}
n = 6
i = 0

While (i < n)
    # Swappy swap
    temp = a[i]
    a[i] = a[i + 1]
    a[i + 1] = temp;

    # Jump to the next pair
    i = i + 2
Loop
commented: Thanks for clarifying :) +1

While Deceptikon's answer is excellent, I would want to ask if you understood what an array really is, in the assembly language you are using. The algorithm won't help you if you don't know how to declare an array in the first place.

Which processor are you targeting, and what assembler are you using? Those are always the place to start in this forum, because every assembler is different in how it does some things, and ones for different processors can be very different from each other.

deceptikon's answer really helped because I literally didn't understand what the question was asking for, but now I at least know what I'm trying to achieve.

I am kind of confused about arrays, but trying to learn through my textbook and examples from online. I know how to declare them, but Assembly is still really confusing and foreign to me compared to some of the high-level programming languages. The only thing I really know how to do so far is make loops, declare arrays, and perform mathematical operations on the array elements (like adding them up), but I'm not sure how to create/populate an array from scratch or how to move around the elements. Do you know of any good resources that would be helpful to me?

Oh and I'm using Visual Studio for MASM Assembly and targetting the x86 processor, if that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.