访问数组元素汇编语言(视窗) [英] accessing array's element in assembly language (windows)

查看:457
本文介绍了访问数组元素汇编语言(视窗)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在汇编语言的问题,我得到了访问数组元素...假设数组包含周一天......像周日,周一,周二,周三....我不得不进入第二个指标数组的......我该怎么做?

I've a problem in assembly language that i got to access element of an array... suppose an array contains weeks day... like sun,mon,tues,wed.... i have to access 2nd index of the array... How can I do this?

推荐答案

索引装配基本相同的C / C ++,除了一个区别:你必须知道你的数据元素的大小

Indexing in assembly is basically the same as C/C++ except for one difference: you have to know the size of your data elements.

例如,通过字节(或字符的字符串)组装数组循环,你可以做到以下几点:

For example, to loop through an array of bytes (or characters in a string) in assembly, you could do the following:

mov eax, 0
mov ecx, 0

loop_start:
  cmp ecx, ARRAY_LENGTH
  jge loop_end

  add eax, BYTE PTR myArray[ecx]

  add ecx, 1
  jmp loop_start
loop_end:

正如你所看到的,数组是通过一个元素在通过增加ECX(我使用的是作为一个计数器,它)时间循环。每个元素被加入到含有在循环结束时的总和eax中。请注意,我不得不添加BYTE PTR时,引用数组告诉我使用的是哪种类型的数据的汇编。

As you can see, the array is looped through one element at a time by incrementing ecx (which I'm using as a counter). Each element is added to eax which contains the sum at the end of the loop. Notice I had to add "BYTE PTR" when referencing the array to tell the assembler what type of data I'm using.

现在来看看这个code由它来完成同样的事情DWORD数据(4字节):

Now take a look at this code which does the same thing for DWORD data (4 bytes):

mov eax, 0
mov ecx, 0

loop_start:
  cmp ecx, ARRAY_LENGTH
  jge loop_end

  add eax, myArray[ecx*4]

  add ecx, 1
  jmp loop_start
loop_end:

只有两件事情改变了:我不再是必要的,因为,除非特别指出,汇编器假定您使用的是32位计算机上的32位数据类型使用BYTE PTR在这里;我还需要数组的索引改变为ECX * 4,因为阵列中的每个元素是4字节长。在32位机器上使用的大多数数据类型是在大小为32位,因此后面的例子将更加普遍。

Only two things changed: I no longer needed to use "BYTE PTR" here because, unless told otherwise, the assembler assumes you are using 32-bit data types on a 32-bit machine; I also needed to change the index of the array to "ecx*4" because each element in the array is 4 bytes long. Most data types used on 32-bit machines are 32 bits in size so the later example will be more common.

要回答你的具体问题,这里有一个方法遍历字符串数组并显示出来:

To answer your specific question, here is one way to loop through an array of strings and display them:

.data
  sunday    db "Sun",0
  monday    db "Mon",0
  tuesday   db "Tues",0
  wednesday db "Wed",0
  thursday  db "Thurs",0
  friday    db "Fri",0
  saturday  db "Sat",0

  daysOfWeek dd OFFSET sunday, OFFSET monday, OFFSET tuesday OFFSET wednesday
             dd OFFSET thursday, OFFSET friday, OFFSET saturday

.code
mov ecx, 0

loop_start:
  cmp ecx, 7
  jge loop_end

  mov eax, daysOfWeek[ecx*4]
  ; eax now contains the pointer to the
  ; next element in the array of days

  add ecx, 1
  jmp loop_start
loop_end:

由于在32位机器上的指针是32位宽,像对待DWORD值作为第二个例子。

Because pointers on a 32-bit machine are 32 bits wide, treat them like DWORDs as in the second example.

这篇关于访问数组元素汇编语言(视窗)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆