我需要编写一个汇编程序,在 -2<=x<=8 范围内找到 x^3 -14x^2 +56x -64 的最大值,最大值应该在程序末尾的一个寄存器中.
我用 C 编写了代码,但我不确定如何自己将其转换为汇编。在这一点上我真的很困惑。顺便说一句,我必须自己转换它。我不能使用 gcc 转换为程序集。这是给 SPARC 的
#include<stdio.h>
int main()
{
int i;
int ans;
for (i = -2; i < 9; i++){
ans = (i * i * i) - (14 * i * i) + (56 * i) - 64;
}
}
我试图编写程序集。有人可以批评它并告诉我我的方向是否正确。另外,我将如何测试最大数量?
main:
save %sp, -96, %sp
ba test
mov -2, %a_r
loop:
mov %a_r, %o0 !a_r moved into o0
mov %a_r, %o1 !a_r moved into o1
call .mul !they are multiplied and stored in o0
call .mul !they are multiplied again and stored in o0
mov %o0, r0 !results stored in r0
mov %a_r, %o0 !a_r moved into o0 and o1
mov %a_r, %o1
call .mul !they are multiplied and stored in o0
mov 14, %o1
call .mul !o0 result is multiplied by 14 and stored in r1
mov %o0, r1
mov 56, %o0 !56 moved into o0
mov %a_r, %01 !a_r moved into o1
call .mul !they are multiplied and stored in r2
mov %o0, r2
Sub r0,r1,r0 !r0-r1 stored in r0
Add r0,r2,r0 !r0+r2 stored in r0
Sub r0,64,r0 !r0-64 stored in r0
add %a_r, 1, %a_r !a_r + 1
test:
cmp %a_r %b_r ! a_r<=8?
ble loop