在Assembly中获取数组元素的索引

时间:2015-10-22 09:13:32

标签: arrays assembly x86 masm irvine32

让我从这开始说,我知道我不应该粗暴强迫程序,但我有点陷入僵局,目的是使用一个数组的元素基本上“排序”另一个数组的元素。

假设:

start = 1

字符:H,A,C,E,B,D,F,G 链接:0,4,5,6,2,3,7,0

所以你从第一个元素A开始,它也是链接数组中的数字4,它指向字符数组中的字母B,依此类推。字符按字母顺序存储在一个新的数组中,我遇到的麻烦是在每一步之后得到字符数组的索引号,也许代码会显示我遇到麻烦的地方

   INCLUDE Irvine32.inc

   start = 1

   .data

    chars BYTE 'H','A','C','E','B','D','F','G'
links DWORD 0,4,5,6,2,3,7,0
array BYTE 0,0,0,0,0,0,0,0

.code 
main PROC
mov al, chars +1
mov array, al
mov esi, start          ; moves the start location into the esi register
mov eax, [links + 4]    ; moves the second element of the array into eax
mov dl, chars[eax]      ; moves the character element of the array chars into dl
mov array[esi], dl      ; moves the character into the array
inc esi
mov eax, [links + 16]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 8]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 20]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 12]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 24]
mov dl, chars[eax]
mov array[esi], dl
inc esi
mov eax, [links + 28]
mov dl, chars[eax]
mov array[esi], dl
inc esi

    main ENDP
    END main

所以我想如果我知道如何在“链接”数组指向它之后获取数组元素的索引,我想我可以把它放到循环中,我只需要知道如何做到这一点。

1 个答案:

答案 0 :(得分:2)

  mov al, chars +1
  mov array, al
  mov esi, start          ; moves the start location into the esi register
  mov ebx, offset links
Again:
  mov eax, [ebx + esi*4]  ; moves the next element of the array into eax
  mov dl, chars[eax]      ; moves the character element of the array chars into dl
  mov array[esi], dl      ; moves the character into the array
  inc esi

通过测试ESI寄存器,重复此代码所需的次数。

您可以通过从0开始而不是1来改进此代码。它将消除顶部的2行。它需要修改链接的定义。

links DWORD 1,4,5,6,2,3,7,0