1

I'm trying to link assembly functions to a C code for exercise. Here's my assembly function, written in x86 assembly:

.code32

.section .text
.globl max_function
.type max_function, @function 
# i parametri saranno in ordine inverso a partire da 8(%ebp)

max_function:
    pushl %ebp              # save ebp
    movl %esp, %ebp         # new frame function
    movl $0, %edi           # first index is 0
    movl 8(%ebp), %ecx      # ecx is loaded with the number of elements
    cmpl $0, %ecx            # check that the number of elements is not 0
    je end_function_err    #if it is, exit

    movl 12(%ebp),%edx      # edx is loaded with the array base
    movl (%edx), %eax       # first element of the array

    start_loop:
    incl %edi               #increment the index
    cmpl %edi,%ecx          #if it's at the end quit
    je loop_exit
    movl (%edx,%edi,4),%ebx   #pick the value
    cmpl %ebx,%eax              #compare with actual maximum value
    jle start_loop              #less equal -> repeat loop
    movl %ebx,%eax              #greater -> update value
    jmp start_loop              #repeat loop

    loop_exit:
    jmp end_function            #finish

end_function:                   #exit operations
    movl %ebp, %esp
    popl %ebp
    ret

end_function_err:
    movl $0xffffffff, %eax            #return -1 and quit
    jmp end_function

It basically defines a function that finds the maximum number of an array (or it should be)

And my C code:

#include <stdio.h>
#include <stdlib.h>

extern int max_function(int size, int* values);

int main(){
    int values[] = { 4 , 5 , 7 , 3 , 2 , 8 , 5 , 6 } ;
    printf("\nMax value is: %d\n",max_function(8,values));
}

I compile them with gcc -o max max.s max.c.
I get a SegmentationFault when executing the code.
My suspect is that I don't access the value in a right manner, but I can't see why, even because I based my code on an example code that prints argc and argv values when called from the command line.

I'm running Debian 8 64-bit

13
  • 1
    You are breaking values in callee-save registers %edi and %ebx, so I guess it may cause some troubles. Commented Jun 7, 2016 at 13:59
  • 5
    And here I go again: the FIRST thing you need to learn when programming C/C++ is how to use the debugger. The second in this case is: Write your assembler function in C first, then produce disassembly to see how the ABI is done. Then write your own while being a bit less in the dark. Commented Jun 7, 2016 at 14:06
  • 1
    The 64-bit Linux ABI (and calling convention) can be found in this document . The 32-bit Linux ABI can be found in this document Commented Jun 7, 2016 at 14:31
  • 1
    One way to eliminate the need for EDI is to loop through the array from end to beginning. You could use ECX as a countdown loop counter. But if you don't do that then you'll need to push the contents of %edi (since it needs to be preserved in a function) and pop it at the end. The reason your result is wrong is because I think you have the comparison cmpl %ebx,%eax reversed. You may have meant cmpl %eax,%ebx . Your comparison and branch finds the minimum value. both these lines can be eliminated jmp end_function end_function: . Jumping to next instruction not very useful. Commented Jun 7, 2016 at 17:21
  • 1
    Yeah that was inverted. Thanks for the tip for both cmpl and jmp. It's kinda the first time I write an assembly function, I'm still learning my way around. Commented Jun 7, 2016 at 17:26

1 Answer 1

1

The problems were:

  • not preserving %ebx and %edi
  • not compiling for 32 bit (had to use -m32 flag for gcc)
  • cmpl operands were inverted

Thanks everybody, problem is solved. I'll focus more on debugging tools to (disassembling and running step by step was very useful)!

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to remove the .code32 directive from your assembly file. Without it you'd probably have gotten an assembler error because you didn't use -m32.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.