装配分段故障(阵列)

时间:2016-01-30 13:12:48

标签: arrays gcc assembly ia-32

初学者在这里组装(IA-32组装)。我在编译gcc时一直遇到段故障,我猜测它试图访问禁止的内存,但是我无法将其指向下来。任务是找到阵列的最大共同点。我想通过一个带有副函数gcd的数组来实现它(返回2个数字的gcd)。这是代码:

.intel_syntax noprefix 

.text

.global GCD

GCD:

push edi
push esi


mov edi, [ebp + 12] # number of elements in the array
mov esi, [ebp + 8] #array address 

mov eax, [esi] # place the first element of the array into eax
mov ecx, 1 # set the counter to 1

next_number: #accumulating the gcd of the entire array

cmp ecx, edi #if the counter reaches the max number of elements,
ja end       #end the the loop


push [esi + 4*ecx] #calling the gcd function, pushing the numbers on the 
push eax           #stack, so gcd can pick them up
call gcd 
add esp, 8


inc ecx            #increment the counter

jmp next_number    #next iteration


end:


  pop esi 
  pop edi 
  leave 
  ret

gcd方面的功能:

# The C algorithm for finding the gcd of 2 numbers implemented in IA-32
#   while(y)
#   {
#     r = x % y;
#     x = y;
#     y = r;
#   }
#   return x;

.intel_syntax noprefix

.text


.global gcd

gcd:

enter 0,0

mov eax, [ebp + 8]
mov ecx, [ebp + 12]

loop:

cmp ecx, 0
je end

xor edx, edx
idiv ecx

mov eax, ecx
mov ecx, edx

jmp loop


end:

leave
ret

0 个答案:

没有答案