[英]C struct to complete assembly code for function
I was given the following code skeleton: 我得到了以下代码框架:
typedef struct node {
_______________ x;
_______________ y;
struct node *next;
struct node *prev;
} node_t;
node_t n;
void func() {
node_t *m;
m = ______________________;
m->y /= 16;
return;
}
with the following assembly code generated on an IA-32 linux machine: 在IA-32 linux机器上生成的以下汇编代码:
func:
pushl %ebp
movl n+12,%eax
movl 16(%eax),%eax
movl %esp,%ebp
movl %ebp,%esp
shrw $4,8(%eax)
popl %ebp
ret
And I need to fill in the blanks above. 我需要填写上面的空白。 I do believe that when I look at he lines: 我确实相信,当我看着他的台词时:
m->y /= 16; and shrw $4,8(%eax)
this is obviously a right shift on the 8th byte of the node_t *m. 这显然是node_t * m的第8个字节的右移。 So what I think is that there are 8 bytes before y so x is 8 bytes long (double). 所以我认为y之前有8个字节,所以x为8个字节长(双精度)。 Also, I think that the total size of the struct 'node' comes from the line 另外,我认为结构“节点”的总大小来自
movl n+12, %eax
because that's where the node_t *m is created. 因为那是创建node_t * m的地方。 I'm just not sure how to determine the size of this from that line. 我只是不确定如何从该行确定其大小。
I was also give this information 我也给了这个信息
It should also be noted that pointers are 4 bytes in size and alignment. 还应注意,指针的大小和对齐方式为4个字节。
In 32-bit mode, a pointer is 4 bytes, and typically aligned on a 4-byte boundary. 在32位模式下,指针为4字节,通常在4字节边界上对齐。 So n + 12
is actually loading & n.next
to %eax
. 因此, n + 12
实际上正在加载%eax
& n.next
在其& n.next
。 eg, if y
is an unsigned short
, there are 2 bytes of padding in the structure before next
. 例如,如果y
是 unsigned short
,则next
之前的结构中有2个字节的填充。
movl 16(%eax),%eax
is dereferencing with a 16-byte offset, to give: m = n.next->prev
movl 16(%eax),%eax
以16个字节的偏移量解引用,得到: m = n.next->prev
struct node
{
double x; /* bytes: 0 .. 7 */
unsigned short y; /* bytes: 8 .. 9 */
/* bytes: 10 .. 11 */ /* (padding) */
struct node *next; /* bytes: 12 .. 15 */
struct node *prev; /* bytes: 16 .. 19 */
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.