【发布时间】:2013-04-26 08:26:52
【问题描述】:
我正在尝试制作一个将未知数量的 int 相加的汇编程序,例如
sum(int a,int b, ...)
我的代码是
.globl notnull
notnull:
leal 4(%esp),%ebx
jmp next2
next:
leal 4(%ebx),%ebx
next2:
cmp $0,(%ebx)
je end
movl (%ebx),%eax
jmp next
end:
ret
我用这个程序测试它:
#include <stdio.h>
extern int notnull();
int main()
{
int x=notnull(3,2,1,0);
printf("3,2,1,0 = %d\n",x);
x=notnull(2,1,0);
printf("2,1,0 = %d\n",x);
x=notnull(1,0);
printf("1,0 = %d\n",x);
x=notnull(0);
printf("0 = %d\n",x);
x=notnull();
printf("_ = %d\n",x);
return 0;
}
Wich 给了我这个输出:
3,2,1,0 = 1 (#1)
2,1,0 = 1 (#2)
1,0 = 1 (#3)
0 = 8 (#4)
_ = 8 (#5)
我想要的是程序在没有变量时返回 0(参见 #5),并使其工作而不必将 0 作为最后一个数字。
notnull(3,2) 的完美输出是 2 和 notnull()=0
【问题讨论】:
-
notnull(1,2) 将使 4(%esp)=1 和 8(%esp)=2 和 12(%esp) 未使用。如何确定 12(%esp) 是否为空/无?