Invalid value when replacing array values โ€‹โ€‹in x86 assembly

I am working on quicksort in the x86 assembly, and I need to swap two elements of the array A [pivot] and A [j], but I can't even assign a value to the index of the array, not replace the elements.

An array is assigned as such:

A: .long 2,1,8,6,12 

My initial exchange scheme did not work at all, so I reduced it to this in order to understand where my problem was. I tried many ways to get the right results, but all either result in the wrong value or segmentation error.

  movl A(,%ebx,4), %eax #eax = A[pivot] movl A(,%edi,4), %edx #edx = A[j] #ebx = pivot = 0 #edi = j = 1 pushl %eax pushl $test7 #"A[pivot] = %d" call printf addl $8, %esp # A[0] = 2 pushl %edx pushl $test8 #"A[j] = %d" call printf addl $8, %esp #A[1] = 1 

This snippet returns:

 A[pivot] = 2 A[j] = -143535296 

A [pivot] = A [0] = 2, so that's right, but A [j] = A [1] = 1

Is this the correct way to reference array elements when% ebx and% edi are two indexes of the array to view their contents or change their values.

I canโ€™t understand what I'm doing wrong, any help would be appreciated.

edit: Also, if I use A (, [index], 4) as the printf argument, it displays the correct values.

edit1: I understand why my printf statements were incorrect, I changed the code and returned what seems to be the correct memory addresses. addr [A] = 134513652 and addr [A + 1] = 134513656. My original problem of changing array values โ€‹โ€‹still exists, but I continue to get a segmentation error when doing this:

  leal A(,%ebx,4), %ecx # ecx = addr[A[0]] movl A(,%edi,4), %edx # edx = A[1] movl %edx, (%ecx) # (ecx) = edx 
+4
source share
1 answer

You should read about esp convention calls. registers of registration of subscribers and callers. In many ia32 calling conventions, EAX, ECX, and EDX are the caller save register. This means that a call - in your case the first call to printf - can potentially change their meaning. This is called saving the caller because you, as the caller, are responsible for storing the value, for example, using push / pop instructions. Your example can also be fixed using one of the saved registers.

+1
source

Source: https://habr.com/ru/post/1444175/


All Articles