我想了解与上面的C代码有关的汇编代码。我不确定我是否在正确的轨道上,所以也许有人可以帮助我更好地理解这一点。了解汇编语言
int silly(int n, int *p)
{
int val, val2;
if (n > 0)
val2 = silly(n << 1, &val);
else
val = val2 = 0;
*p = val + val2 + n;
return val + val2;
}
我们得到以下的机器代码:
silly:
pushl %ebp // Here I am making space for the function on the stack
movl %esp,%ebp // Moving the stack pointer where the base pointer is
subl $20,%esp // Subtracting 20 from the stack pointer to allocate more space
pushl %ebx // Pushing the %ebx register on top of the stack
movl 8(%ebp),%ebx // Getting the first argument(which is n) and store it in register %ebx
testl %ebx,%ebx // The first if-statement which compares if n > 0
jle .L3 // Jump if less or equal - meaning if n < 0 then jump to .L3
addl $-8,%esp // Add -8 to %esp to allocate more space
leal -4(%ebp),%eax // Storing the first local variable (which is val) in %eax
pushl %eax // Pushing the register %eax on top of the stack
leal (%ebx,%ebx),%eax // n + n and stores it as 2n in %eax
pushl %eax // Pushing register %eax on top of the stack (Which I find strange
// considering that I've just pushed %eax onto the stack above
call silly // Call the function silly
jmp .L4 // Jump to .L4 (Unconditionally)
.p2align 4,,7 // Don't know what this means.
.L3: // .L3 is the else-statement
xorl %eax,%eax // Basically making %eax = 0
movl %eax,-4(%ebp) // Moving the value in %eax which is 0 to the first local variable
// meaning val = 0
.L4: // .L4 is the section after the else-statement
movl -4(%ebp),%edx // Getting val again and now storing it in %edx
addl %eax,%edx // Adding what is in %eax (which is 0) to %edx
movl 12(%ebp),%eax // Getting the second parameter (*p) and storing it in %eax
addl %edx,%ebx // Adding value from %edx to %ebx - meaning val + n
movl %ebx,(%eax) // Moving what is in %ebx and storing it in memory location of %eax
movl -24(%ebp),%ebx // Getting the second local variable (val2) and moving it to %ebx
movl %edx,%eax // Move val to %eax - and the return value will be in %eax
movl %ebp,%esp
popl %ebp
ret
我想换我解决这个头,我刚开始想组装等主题的指针将是非常好的。我有几个我需要问这个汇编代码,可以帮助栈我的理解的问题:
(a)是存储在堆栈上的变量val?
(b)如果是这样,在什么字节oset(相对于%ebp)被存储?
(c)为什么需要将它存储在堆栈中?(a)变量val2是否存储在堆栈中?
(b)如果是这样,在什么字节oset(相对于%ebp)被存储?
(c)为什么需要将它存储在堆栈中? (a)什么(如果有的话)存储在-24(%ebp)?
(b)如果有东西存储在那里,为什么需要存储它? (a)什么(如果有的话)存储在-8(%ebp)?(0127)
(b)如果有东西存储在那里,为什么需要存储它?
感谢提前:)
你的问题到底是什么? – o11c 2014-10-09 21:36:10
我刚刚更新了这个问题。对不起:) – drleifz 2014-10-09 21:41:38
注意:'val2'是未初始化的。有时。有时候'val'也是如此。 – wildplasser 2014-10-09 22:34:45