Assembly 在汇编语言中按1个索引旋转数组
我是汇编语言新手,在ecx寄存器和循环方面遇到了问题。看来,这个程序的目的是Assembly 在汇编语言中按1个索引旋转数组,assembly,masm,irvine32,Assembly,Masm,Irvine32,我是汇编语言新手,在ecx寄存器和循环方面遇到了问题。看来,这个程序的目的是 在DWORD数组中输入10个有符号整数 打印阵列 将阵列向右旋转1个索引[1,2,3,4]->[4,1,2,3] 打印阵列 下面是我的代码,在旋转代码的过程中,我似乎增加了第11个值,我忍不住挠头想知道为什么 TITLE RotateArray (RotateArray.asm) ;This program will ask the user to input 10 values into a DWORD arr
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
旋转数组现在可以工作了,但是该算法似乎给内存增加了第11个值。有人看到我的算法有什么问题吗?因为你没有保留或重置ECX,它不是你需要的@PrintArray或L2值:我修复了循环,谢谢。现在算法工作了,这让我很高兴,但现在我有了第11个值。因为您运行
L4
循环10次,然后在循环后有另一个WriteInt
。