我想了解與上面的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