用汇编语言按1索引旋转数组

时间:2019-10-15 17:03:00

标签: assembly masm irvine32

我是汇编语言的新手,并且ecx寄存器存在问题,并且似乎在循环执行该程序的目的是

  1. 将10个有符号整数输入到DWORD数组中
  2. 打印数组
  3. 将数组右旋转1个索引[1,2,3,4]-> [4,1,2,3]
  4. 打印数组

我的代码在下面,我似乎在旋转它的过程中添加了第11个值,而且我不停地尝试尝试找出原因

   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

rotate数组现在可以工作,但是该算法似乎将第11个值添加到内存中。有人看到我的算法有问题吗?

0 个答案:

没有答案