我在 C 和汇编代码中有一个开关条件代码。但在我看来,设置什么,edx 或 eax 或 ecx 似乎很随意?
如何区分 edx、epx、ecx、ebp?甚至教科书也没有给我足够的解释。
#include <stdio.h>
// Enumerated type creates set of constants
// numbered 0 and upward
typedef enum {MODE_A, MODE_B, MODE_C, MODE_D, MODE_E} mode_t;
int switchmode(int *p1, int *p2, mode_t action)
{
int result = 0;
switch(action) {
case MODE_A:
result = *p1;
*p1 = *p2;
break;
case MODE_B:
*p2 += *p1;
result = *p2;
break;
case MODE_C:
*p2 = 15;
result = *p1;
break;
case MODE_D:
*p2 = *p1;
/* Fall Through */
case MODE_E:
result = 17;
break;
default:
result = -1;
}
return result;
}
int main(int argc, const char * argv[])
{
int num1 = 10;
int num2 = 20;
printf("MODE_A: %d \n", switchmode(&num1, &num2, MODE_A)); // 10
printf("MODE_B: %d \n", switchmode(&num1, &num2, MODE_B)); // 40
printf("MODE_C: %d \n", switchmode(&num1, &num2, MODE_C)); // 20
printf("MODE_D: %d \n", switchmode(&num1, &num2, MODE_D)); // 17
printf("MODE_E: %d \n", switchmode(&num1, &num2, MODE_E)); // 17
printf("NULL: %d \n", switchmode(&num1, &num2, (mode_t) NULL)); // 20
return 0;
}
汇编代码是
Arguments: p1 at %ebp+8
p2 at %ebp+12
action at %ebp+16
Register: result in %edx (initialized to -1)
.L17: // MODE_E
movl $17, %edx
jmp .L19
.L13: // MODE_A
movl 8(%ebp), %eax
movl (%eax), %edx
movl 12(%ebp), %ecx
movl (%ecx), %eax
movl 8(%ebp), %ecx
movl %eax, (%ecx)
jmp .L19
.L14: // MODE_B
movl 12(%ebp), %edx
movl (%edx), %eax
movl %eax, %edx
movl 8(%ebp), %ecx
addl (%ecx), %edx
movl 12(%ebp), %eax
movl %edx, (%eax)
jmp .L19
.L15: // MODE_C
movl 12(%ebp), %edx
movl $15, (%edx)
movl 8(%ebp), %ecx
movl (%ecx), %edx
jmp .L19
.L16: // MODE_D
movl 8(%ebp), %edx
movl (%edx), %eax
movl 12(%ebp), %ecx
movl %eax, (%ecx)
movl $17, %edx
.L19: // default
movl %edx, %eax // set return value
我的问题是
我怎么知道什么是eax?这是论据之一吗?
如果是这样,我怎么知道哪个是 eax 和 ecx,哪个是 edx?