gpt4 book ai didi

c - 在汇编中实现返回最大数和最小数之差的 C 函数

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:11 25 4
gpt4 key购买 nike

我是汇编初学者,我正在尝试编写一个汇编函数来实现这段 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

最佳答案

您的代码在做什么:

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 代码甚至显示了它!

相反,使用 minmax 的默认值并将非默认值与 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

我已尽力优化它,但我承认几乎可以肯定进一步优化此代码。

关于c - 在汇编中实现返回最大数和最小数之差的 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50243368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com