2

I am new to assembly language and am having a problem with the ecx register and looping it seems, the purpose of this program is to

  1. Input 10 signed integers into a DWORD array
  2. Print the array
  3. Rotate the array right by 1 index [1,2,3,4] -> [4,1,2,3]
  4. Print the array

My code is below and I seem to have added an 11'th value during the process of rotating it, and I can't stop scratching my head tryna figure out why

   TITLE RotateArray (RotateArray.asm)
;This program will ask the user to input 10 values into a DWORD array
;then proceed to rotate it to the right by one index then print both versions
;Tanner Boyle, Oct 15, 2019.

INCLUDE Irvine32.inc

.data
array DWORD 10 DUP(?)                                   ;Uninitialized Array                                              
prompt BYTE "Please enter a signed integer: ", 0
comma BYTE ", ", 0

.code
;************************* MAIN ***************************************
main PROC


mov edi, OFFSET array                                   ;Set EDI to address of array
mov ecx, 10                                             ;Set ECX to size of array


;****************************************** Number Collection ***********************************
L1: 

    mov edx, OFFSET prompt                              ;Ask for anumber
    Call WriteString
    Call ReadInt
    mov [edi], eax                                      ;Move the number into the array
    add edi, TYPE array                                 ;OFFSET array by its type

loop L1


;******************************************* Print **********************************************

mov edi, OFFSET array                                   ;Set EDI to address of array
mov ecx, 10                                             ;Set ECX to size of array


mov al, '['                                         ;Write a [ to indicate start of the array
    call WriteChar
    sub ecx, 1                                          ;Subracting so you dont print the last value for the comma

L2:
    mov eax, [edi]                                      ;Moving the value of array to eax
    call WriteInt                                       ;Writing it as a byte
    mov edx, OFFSET comma                               ;Moving the h, comma and space to edx
    call WriteString                                    ;Write it
    add edi, TYPE array                                 ;Adding edi the type of array to shift the values

loop L2                                                 ;Loop

    mov eax, [edi]                                      ;Moving the last value of the array
    call WriteInt

    mov al, ']'                                         ;Writing the closing bracket
    call WriteChar
    call CrLf

;******************************************* Rotation *******************************************



    mov edi, OFFSET array                                   ;Set EDI to address of array
    mov ecx, 10                                             ;Set ECX to size of array

    mov eax, [edi]                                      ;Move index 0 into eax to store as a temp
L3:

     mov edx, [edi]                                     ;Move into a second temp the value at edi
     mov [edi], eax                                     ;Move the temp into the value at edi
     mov eax, edx                                       ;Set the first temp to the second temp
     add edi, TYPE array

loop L3

     mov edi, OFFSET array
     mov [edi], eax                                     ;Set the temp to the first value


;******************************************* Print **********************************************

    mov al, '['                                         ;Write a [ to indicate start of the array
    call WriteChar
    sub ecx, 1                                          ;Subracting so you dont print the last value for the comma

    mov edi, OFFSET array                                   ;Set EDI to address of array
    mov ecx, 10                                             ;Set ECX to size of array

L4:
    mov eax, [edi]                                      ;Moving the value of array to eax
    call WriteInt                                       ;Writing it as a byte
    mov edx, OFFSET comma                               ;Moving the h, comma and space to edx
    call WriteString                                    ;Write it
    add edi, TYPE array                                 ;Adding edi the type of array to shift the values

loop L4                                                 ;Loop

    mov eax, [edi]                                      ;Moving the last value of the array
    call WriteInt

    mov al, ']'                                         ;Writing the closing bracket
    call WriteChar
    call CrLf
    exit
main ENDP

END main

Fixed output

The rotate array now works, however the algorithm seems to add a 11th value to memory. Anyone see any issue with my algorithm?

0 Answers0