Search code examples
assemblyx86masmirvine32

Rotate an array with Assembly


I'm having errors with getting my program to build or run. I want to loop the array and move the first element to the end and basically move every element up by one in the index. I know I need indexing but thats the confusing syntax part for me.

Please help!

INCLUDE Irvine32.inc

.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword

.data
array dword 10h,20h,30h,40h
arraySize dword lengthof array

.code
main proc

  mov ecx, 0

  loop_start:
    cmp ecx, 7
    jge loop_end

    mov eax, array[ecx*4]
    ; Use Irvine's WriteHex to display value in register eax
    call WriteHex
    call Crlf
    add ecx, 1
    jmp loop_start
  loop_end:

  INVOKE  ExitProcess, 0
main endp
end main

Any help is appreciated!


Solution

  • I recommend starting by writing your intended algorithm in a higher-level language:

    void rotateArray(uint32_t *arr, size_t num)
    {
      uint32_t temp = arr[0];
      for (size_t i = 0; i<num-1; i++)   //loop is essentially a memmove()
        {
          arr[i] = arr[i+1];
        }
      arr[num-1] = temp;
      return;
    }
    

    This should be straightforward to compile, even for a meatbag human.