Assembly 如何使用指针访问程序集中数组中的整数?
首先,这是一个家庭作业。我被告知使用push和pop创建带有这些参数的排序函数。它们是这样传入函数的,我不知道如何使用“int*list”访问其中的元素。我在过程中Assembly 如何使用指针访问程序集中数组中的整数?,assembly,x86,Assembly,X86,首先,这是一个家庭作业。我被告知使用push和pop创建带有这些参数的排序函数。它们是这样传入函数的,我不知道如何使用“int*list”访问其中的元素。我在过程中 int sorter (int* list, int count, int opcode) { __asm { mov eax, 0; zero out the result mov ebx, opcode; move opcode to ebx for comparison ; f
int sorter (int* list, int count, int opcode)
{
__asm
{
mov eax, 0; zero out the result
mov ebx, opcode; move opcode to ebx for comparison
; fill in your code here
mov ecx, 0; set the counter to 0
cmp ebx, 0x01; check ascendant or descendant
je ASCENDANT
jne DESCENDANT
ASCENDANT:
loop_start :
cmp ecx, count; condition for the outer loop
jge loop_end
push ecx
mov eax,
}
}
loop_start:
cmp ecx, count ; condition for the outer loop
jge loop_end ; jump if end of array
mov esi, list ; move pointer to esi
mov eax, [esi + 4 * ecx] ; move data that current index to eax
push ecx ; push ecx to the stack to save the index
inner_loop:
inc ecx ; increment eax
cmp eax, [esi + 4 * ecx]; compare eax with the next element in the array
jle swap ; if it is less than the current value than jump to swap
cmp ecx, count ; check if ecx reaches the end of array
jle inner_loop ; if not than go back to inner loop
pop ecx ; it ecx reaches the end than pop eax to go back to the old index of outer loop
mov[esi + 4 * ecx], eax ; exchange the value of current eax to the element that is compared
inc ecx ; increment ecx for outer loop
jmp loop_start ; jump back to loop start
swap: ; swap the smaller value with the current value
mov eax, [esi + 4 * ecx] ; swapping
jmp inner_loop ; jump back to inner loop
loop_end:
ret
将
list
的值(即int
s的地址)放入寄存器,并使用寄存器间接寻址:
mov esi, list
mov eax, [esi] ; read the first element
mov eax, [esi+4] ; read the second element
add esi, 8 ; move 2 elements ahead
mov eax, [esi] ; read the third element
; etc...
如果要交换由ecx
和edx
中的索引指定的数组中的两个元素,可以执行以下操作:
mov eax, [esi + ecx*4] ; eax = elem1
xchg eax, [esi + edx*4] ; swap eax and elem2
mov [esi + ecx*4], eax ; elem2 = old elem1
将
list
的值(即int
s的地址)放入寄存器,并使用寄存器间接寻址:
mov esi, list
mov eax, [esi] ; read the first element
mov eax, [esi+4] ; read the second element
add esi, 8 ; move 2 elements ahead
mov eax, [esi] ; read the third element
; etc...
如果要交换由ecx
和edx
中的索引指定的数组中的两个元素,可以执行以下操作:
mov eax, [esi + ecx*4] ; eax = elem1
xchg eax, [esi + edx*4] ; swap eax and elem2
mov [esi + ecx*4], eax ; elem2 = old elem1
x86 CPU支持索引寻址,如:
mov edx, list ;edx is a list ptr
mov ecx, index ;ecx ia an array index
mov eax, dword ptr[edx + ecx * 4] ;Load result to eax
x86 CPU支持索引寻址,如:
mov edx, list ;edx is a list ptr
mov ecx, index ;ecx ia an array index
mov eax, dword ptr[edx + ecx * 4] ;Load result to eax
非常感谢,但是如何将数组中较小的值与数组中某个索引的值进行交换呢?再次非常感谢!我有个问题。如果我将ecx(计数器)推到堆栈上,ecx的数据还会保留在堆栈上吗?因为我想保留外循环的索引,并在ecx完成内循环后将其取回?还有,有什么方法可以联系你吗?按
ecx
不会修改ecx
。我完成了气泡排序,但它一直崩溃,你能看一下并帮我处理吗?对不起,我对汇编非常陌生。现在我有了一个结构数组,每个元素都是(int-ID,char-name[20])。我得到了一个指针“char*list”。我把它交给了esi。我怎样才能读懂名字的每个字符。我试着这样做:‘moveesi,list’->‘addesi,4’->moval,byte ptr[esi+ecx]->‘incecx’->‘addesi,24’。这是一种正确的方法吗?非常感谢,但我如何将数组中较小的值与数组中某个索引的值交换?再次非常感谢!我有个问题。如果我将ecx(计数器)推到堆栈上,ecx的数据还会保留在堆栈上吗?因为我想保留外循环的索引,并在ecx完成内循环后将其取回?还有,有什么方法可以联系你吗?按ecx
不会修改ecx
。我完成了气泡排序,但它一直崩溃,你能看一下并帮我处理吗?对不起,我对汇编非常陌生。现在我有了一个结构数组,每个元素都是(int-ID,char-name[20])。我得到了一个指针“char*list”。我把它交给了esi。我怎样才能读懂名字的每个字符。我试着这样做:‘moveesi,list’->‘addesi,4’->moval,byte ptr[esi+ecx]->‘incecx’->‘addesi,24’。这是一种正确的方法吗?谢谢如果我找到一个较小的值,我怎么能将它与当前索引的值交换呢?感谢lot将两者的索引都保存在一个寄存器中,然后简单地将值加载到eax(或其他一些方便的寄存器)中,并将其存储在另一个地址:mov-eax[esi+ecx*4]:mov-ebx[esi+edx*4]:mov[esi+edx*4]、eax:mov[esi+ecx*4]、ebx
或类似地址。如果我找到一个较小的值,如何将其与当前索引的值交换?感谢lot将两者的索引保存在一个寄存器中,然后简单地将值加载到eax(或其他一些方便的寄存器)中,并将其存储在其他地址:mov-eax[esi+ecx*4]:mov-ebx[esi+edx*4]:mov[esi+edx*4]、eax:mov[esi+ecx*4]、ebx
或类似地址。jle-inner_-loop
看起来可疑。如果ecx
等于count
并且您跳回内部循环
,您要做的第一件事是增加ecx
并与数组外部的内容进行比较,但是如果我不增加ecx,它将第一次将其当前元素与自身进行比较。在检查“jle交换”后,我试图增加eax,但我遇到了一个无限循环jle内部循环
看起来可疑。如果ecx
等于count
并且您跳回内部循环
,您要做的第一件事是增加ecx
并与数组外部的内容进行比较,但是如果我不增加ecx,它将第一次将其当前元素与自身进行比较。在检查“jle交换”之后,我试图增加eax,结果遇到了一个无限循环