【问题标题】:Get index of array element in Assembly获取Assembly中数组元素的索引
【发布时间】:2015-10-22 09:13:32
【问题描述】:

首先让我说我确实知道我不应该强制程序,但我有点陷入僵局,目的是使用一个数组的元素基本上“排序”另一个数组的元素。

给定:

开始 = 1

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

因此,您将从第一个元素 A 开始,它也是 links 数组中的数字 4,它指向 chars 数组中的字母 B,依此类推。字符按字母顺序存储在一个新数组中,我遇到的麻烦是在每一步之后获取 chars 数组的索引号,也许代码会在我遇到问题的地方显示更多

   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

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

【问题讨论】:

  • 只需将偏移量存储到寄存器中即可。另外,使用索引寻址模式。

标签: arrays assembly x86 masm irvine32


【解决方案1】:
  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

【讨论】:

    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2011-12-14
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多