我有這個C代碼是反彙編(AT & T),我有兩個東西混淆。首先,我的理解是EBP-4應該是堆棧中的第一個局部變量(這裏是int i)。我明確在EBP-8這裏。爲什麼是這樣?彙編局部變量和寄存器
其次,在對它們進行算術運算之前,是否需要將值移入寄存器? (這是一個x86 32位機)例如:
0x08048402 <+21>: mov 0x8(%ebp),%eax //move parameter a into eax 0x08048405 <+24>: add %eax,-0x4(%ebp) //r += a
爲什麼不能此是:
0x08048405 <+24>: add 0x8(%ebp),-0x4(%ebp) //r += a
的C代碼:
int loop_w (int a, int b){
int i = 0;
int r = a;
while (i < 256){
r += a;
a -= b;
i += b;
}
return r;
拆卸:
Dump of assembler code for function loop_w:
0x080483ed <+0>: push %ebp
0x080483ee <+1>: mov %esp,%ebp
0x080483f0 <+3>: sub $0x10,%esp
---------------Above is for stack setup-----------------
0x080483f3 <+6>: movl $0x0,-0x8(%ebp) //I=0
0x080483fa <+13>: mov 0x8(%ebp),%eax //move parameter a into eax
0x080483fd <+16>: mov %eax,-0x4(%ebp) //move a into local var (r=a)
0x08048400 <+19>: jmp 0x8048414 <loop_w+39> //start while loop
0x08048402 <+21>: mov 0x8(%ebp),%eax //move parameter a into eax
0x08048405 <+24>: add %eax,-0x4(%ebp) //r += a
0x08048408 <+27>: mov 0xc(%ebp),%eax //move parameter b into eax
0x0804840b <+30>: sub %eax,0x8(%ebp) //a += parameter b
0x0804840e <+33>: mov 0xc(%ebp),%eax //move parameter b into eax
0x08048411 <+36>: add %eax,-0x8(%ebp) //i+=b
0x08048414 <+39>: cmpl $0xff,-0x8(%ebp) //compare i to 256
0x0804841b <+46>: jle 0x8048402 <loop_w+21> //continue loop if failed condition
=> 0x0804841d <+48>: mov -0x4(%ebp),%eax //move r into eax
0x08048420 <+51>: leave
0x08048421 <+52>: ret //return eax
End of assembler dump.
這一切都取決於編譯器是否在入口處或從堆棧分配後(後者更常見)分配堆棧起始處的變量。請注意,按照遞增的內存順序,我位於r之前(正常情況下)。 – user3344003