C,大会:认识开关状态,EDX ECX EAX [英] C, Assembly : understanding the switch condition, edx eax ecx
问题描述
C,大会:认识开关状态,EDX ECX EAX
C, Assembly : understanding the switch condition, edx eax ecx
我在C开关条件code和汇编code。
不过好像很随意对我有什么设置EDX或EAX或ECX?
我如何告诉EDX,EPX,ECX,EBP之间的差异?即使是教科书不给我足够的解释。
I have a switch-condition code in C and assembly code. But it seems very arbitrary to me what to set edx or eax or ecx? How do I tell the difference between edx, epx, ecx, ebp? Even the textbook does not give me the sufficient explanation.
#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;
}
和组装code是
参数:在P1的%ebp + 8
在P2的%ebp + 12
在EBP%+ 16的动作
注册:导致EDX%(初始化为-1)
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?
My question is how do I know what is eax? Is it one of the arguments? If so, how di I know which is the eax and ecx and which is edx?
在此先感谢!
推荐答案
在大多数组装code,无论是在%eax中是从函数的返回值。这就是为什么在.L19块无论是EDX%被移动到%eax中。回首过去,无论你设置结果
来的东西中,code举动,重视到%edx中,这样,当code跳到.L19的值将是默认情况下,放在%eax中。
这看起来像一个32位系统,因此它具有8个通用寄存器:
In most assembly code, whatever is in %eax is the value returned from the function. That is why in the .L19 block whatever was in %edx is moved into %eax. Looking back, wherever you set result
to something, the code moves that value into %edx so that when the code jumps to .L19 the value will be placed in %eax by default.
This looks like a 32-bit system, so it has 8 general-purpose registers:
- %EBX,%ecx中,%EDX,%ESI,和%EDI是寄存器,大多存储的任何数据是必要的他们在任何给定的时间保持。
- %eax中拥有一个函数,如返回值调用它的功能将被期待在%eax中该函数的返回值它更改%eax中之前。
- %EBP和ESP%的,基本上管理和栈各种函数的调用上分配空间的特殊功能寄存器。
要看到你的论点被放置,看看那里的数据是在code被引用。例如,在.L13的程序放置的%ebp + 8(其为p1)插入%eax中,然后在该地址(* p1)为宿值到%EDX。所以,EDX%现在拥有的价值由P1指向。
寄存器不要拿着函数的自变量。不要以为%edx中包含的参数之一开关模式()
默认情况下。参数始终放置在堆栈中。
To see where your arguments are being placed, look at where that data is being referenced in the code. For example, at .L13, the program places %ebp+8 (which is p1) into %eax, then places the value at that address (*p1) into %edx. So, %edx now holds the value pointed to by p1.
The registers DO NOT hold the arguments to the function. Don't think that %edx contains one of the arguments to switchmode()
by default. Arguments are always placed on the stack.
这篇关于C,大会:认识开关状态,EDX ECX EAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!