我是程序集的初学者,我试图编写一个汇编函数,在这段c代码中实现largestdif函数(它接收3个整数作为参数)。它基本上需要找到3个参数的最小值和最大值,然后返回最大值和最小值之间的减法:
#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;
}
下面是我在汇编中的代码片段,看起来有点令人困惑,但我希望你能告诉我这是错误的,因为它出现了分段错误错误。我真的不知道还有什么需要做的,它必须与寄存器没有足够的调用有关。似乎无法掌握答案,所以我希望有人能帮助我。提前谢谢。
.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
答案 0 :(得分:1)
您的代码正在做什么:
if (n2 < n1) {
if (n3 < n2) min = n3;
else min = n2;
} else {
if (n3 < n1) min = n3;
else min = n1;
}
if (n2 > n1) {
if (n3 > n2) return min - n3;
else return min - n2;
} else {
if (n3 > n1) return min - n3;
else return min - n1;
}
正如您可能已经意识到的,无论您使用何种语法,这种方法在x86 ASM中都是不必要的复杂。 C代码甚至可以显示它!
相反,使用min
和max
的默认值并将非默认值与min
和max
进行比较会更有效,然后返回结果减法:
int min = n1, max = n3;
if (min > n2) min = n2;
if (min > n3) min = n3;
if (max < n1) max = n1;
if (max < n2) max = n2;
return max - min;
相同数量的必需比较,但请注意消除else
分支,这使代码更简单。转换为一些简单的汇编代码:
.text
.globl largestdif
largestdif:
pushl %ebp
movl %esp, %ebp
movl 16(%ebp), %edx # d = n1, min = n1
movl 12(%ebp), %ecx # c = n2
movl 8(%ebp), %eax # a = n3, max = n3
# Find min.
cmpl %ecx, %edx # if (n1 <= n2) {}
jle .min_not_n2 # else
xchgl %ecx, %edx # swap(&n1, &n2) // n1 < n2 is true after this
.min_not_n2:
cmpl %eax, %edx # if (n1 <= n3) {}
jle .min_not_n3 # else
xchgl %eax, %edx # swap(&n1, &n3) // n1 < n3 is true after this
.min_not_n3:
# Find max.
cmpl %ecx, %eax # if (n3 >= n2) {}
jge .max_not_n2 # else
xchgl %ecx, %eax # swap(&n2, &n3) // n3 > n2 is true after this
.max_not_n2:
# n1 > n3 is absolutely false at this point.
# The swaps above ordered things into the relation
# n1 <= n2 <= n3 (%edx <= %ecx <= %eax),
# so n1 <= n3 must be true.
# Return max - min.
subl %edx, %eax
movl %ebp, %esp
popl %ebp
ret
我尽力优化它,但我承认,几乎可以肯定,甚至可以进一步优化此代码。
答案 1 :(得分:0)
这里没有分段错误。我想主要的错误是不遵守调用约定。该计划本质上是一个C程序。所以你必须遵循C calling convention "cdecl"。函数largestdif
必须返回寄存器EBX,ESI,EDI,EBP 不变。如果在函数中使用这些寄存器,则必须在更改它们之前保留它们,并在离开函数之前将其恢复。
在更改之前,您没有保留EBX 。变化
...
mov 8(%ebp), %ebx
push %ebx
...
到
...
push %ebx
mov 8(%ebp), %ebx
...