Tôi đang cố hiểu mã lắp ráp này liên quan đến mã C ở trên. Tôi không chắc chắn liệu mình có đi đúng hướng hay không vì vậy có lẽ ai đó có thể giúp tôi hiểu rõ hơn về điều này.Hiểu ngôn ngữ lắp ráp
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;
}
này mang mã máy sau:
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
Tôi cố gắng để quấn quanh đầu tôi và tôi đã chỉ mới bắt đầu suy nghĩ về lắp ráp để gợi ý về đề tài này sẽ thực sự thoải mái. Tôi có một vài câu hỏi tôi cần hỏi về mã lắp ráp này có thể giúp tôi hiểu về chồng:
(a) Val biến có được lưu trữ trên ngăn xếp không?
(b) Nếu vậy, tại những gì oset byte (tương đối so với% ebp) là nó được lưu trữ?
(c) Tại sao cần lưu trữ nó trên ngăn xếp?(a) Biến val2 được lưu trữ trên ngăn xếp?
(b) Nếu vậy, tại những gì oset byte (tương đối so với% ebp) là nó được lưu trữ?
(c) Tại sao cần lưu trữ nó trên ngăn xếp?(a) Điều gì (nếu có) được lưu trữ ở -24 (% ebp)?
(b) Nếu một cái gì đó được lưu trữ ở đó, tại sao nó là cần thiết để lưu trữ nó?(a) Điều gì (nếu có) được lưu trữ ở -8 (% ebp)?
(b) Nếu một cái gì đó được lưu trữ ở đó, tại sao nó là cần thiết để lưu trữ nó?
Cảm ơn trước :)
Câu hỏi của bạn chính xác là gì? – o11c
Tôi vừa mới cập nhật câu hỏi. Xin lỗi :) – drleifz
LƯU Ý: 'val2' được uninitialised.Sometimes. Và đôi khi là 'val'. – wildplasser