Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
.data
array1 DWORD 10d,20d,30d,40d,50d,60d,70d,80d,90d
.code
main PROC
mov ESI, OFFSET array1 ;ESI now points to the first item of array1
mov EDI, SIZEOF array1
add EDI, OFFSET array1
sub EDI, TYPE array1 ;EDI now points to the last item of array1

mov ECX, LENGTHOF array1
shr ECX, 1 ;now ecx is half the length of the array1
L1: mov EAX, [ESI] ;in this loop we reverse the items of the array
mov EBX, [EDI]
mov [EDI],EAX
mov [ESI],EBX
add ESI, TYPE array1
sub EDI, TYPE array1
LOOP L1

mov ECX, LENGTHOF array1;here we just print the array
mov ESI, OFFSET array1
L2: MOV EAX, [ESI]
call WriteInt
call Crlf
add ESI, TYPE array1
LOOP L2
exit
main ENDP
END main

This is the code i have to add to my project but im not sure how i convert it into c++ to work.
Posted
Comments
ZurdoDev 11-Nov-15 15:28pm    
You re-write it to get it into C++.
Richard MacCutchan 11-Nov-15 15:37pm    
Without knowing what it is supposed to do it is difficult to make a suggestion.
YeezyGod 11-Nov-15 16:16pm    
Its suppose to Use a loop with indirect or indexed addressing to reverse the elements of an integer array in place using the SIZEOF, TYPE, and LENGTHOF operators.
Richard MacCutchan 11-Nov-15 16:34pm    
That should not be too difficult to write in C/C++ then.
Sergey Alexandrovich Kryukov 11-Nov-15 21:49pm    
C++?!
—SA

1 solution

This code assumes you have the _countof macro defined.

https://msdn.microsoft.com/en-us/library/ms175773.aspx[^]

C++
#include <stdio.h>

// array1 DWORD 10d,20d,30d,40d,50d,60d,70d,80d,90d

static long words[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};

int main(int argc, char *argv[])
{
    // swap the array.
    unsigned count = _countof(words);
    unsigned half = _count / 2;
    for (unsigned i = 0; i < half; i++)
    {
        long swap = words[i];
        words[i] = words[count - i - 1];
        words[count - i - 1] = swap;        
    }

    // print the array.
    for (unsigned i = 0; i < half; i++)
    {
        printf(" %ld", words[i]);
    }
    return 0;
}
</stdio.h>


You know, it would be simpler to just print the numbers in reverse order.

C++
#include <stdio.h>

static long words[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};

int main(int argc, char *argv[])
{
    // print the array.
    unsigned i = _countof(words);
    while (i > 0)
    {
        printf(" %ld", words[--i]);
    }
    return 0;
}
</stdio.h>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900