im a beginner at assembly, and im trying to write an assembly function that implements the largestdif function in this piece of c code (it receives 3 integers as arguments). it basically needs to find the minimum and maximum of the 3 arguments and then return the subtraction between the maximum and minimum:
#include <stdio.h>
extern int largestdif( int n1, int n2, int n3 );
int main( ) {
printf( "largestdif: %d\n", largestdif( 100, 30, 10 ) );
printf( "largestdif: %d\n", largestdif( 0, -1, 1 ) );
return 0;
}
heres my piece of code in assembly, looks kinda confusing but im hoping you can tell me whats wrong with this since its presenting segmentation fault error. i really have no idea whats left to do, it must have something to do with the registers not being called enough. cant seem to grasp an answer so yeah im hoping someone could give me a hand. thanks in advance.
.global largestdif
.text
largestdif:
push %ebp
mov %esp, %ebp
mov 8(%ebp), %ebx
push %ebx
mov 12(%ebp), %ecx
push %ecx
mov 16(%ebp), %edx
push %edx
#compare n1 and n2 to find the largest number
cmp %edx, %ecx
jl n1_menor_n2
#block else
cmp %edx, %ebx #compare first and third numbers
jl firstlower
#bloco else
mov %edx, %eax #already have largest number
jmp continue
firstlower:
mov %ebx, %eax #already have largest number
jmp continue
n1_menor_n2:
cmp %ecx, %ebx #compare second and third numbers
jl secondlower
#block else
mov %ecx, %eax # already have largest
jmp continue
secondlower:
mov %ebx, %eax # already have largest
jmp continue
continue:
#compare n1 and n2 to find the largest number
cmp %edx, %ecx
jg n1_maior_n2
#block else
cmp %edx, %ebx
jg firstlarger
#block else
sub %edx, %eax
jmp continue2
firstlarger:
sub %ebx, %eax
jmp continue2
n1_maior_n2:
cmp %ecx, %ebx
jg secondlarger
#block else
sub %ecx, %eax
jmp continue2
secondlarger:
sub %ebx, %eax #already have the subtraction
jmp continue2
continue2:
pop %edx
pop %ecx
pop %ebx
mov %ebp, %esp
pop %ebp
ret