对准C /大会 [英] Alignment C/Assembly

查看:213
本文介绍了对准C /大会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,你们,我需要知道什么是A和B的值,在code A和B具有的#define定义的常量:

  typedef结构{
    INT X [A] [B]。
    大Y;
} STR1;typedef结构{
    字符数组[B]。
    INT吨;
    短S [A];
    长U;
} STR2;无效SETVAL(STR1 * P,STR2 * Q){
    长V1 = Q->吨;
    长V2 = Q-> U;
    P-> Y = V1 + V2;
}

SETVAL 程序时,会生成以下汇编code:

  SETVAL:movslq 8(%RSI),RAX%addq 32(%RSI),RAX%MOVQ%RAX,184(%RDI)RET


解决方案

该结构具有以下对齐要求:


  • 字符可在任何字节启动

  • 可在偶字节启动

  • INT 可在字节启动,被4整除

  • 在字节可能会启动,除尽

str1.y 字段是启动184 ,这意味着,该 str1.x 可容纳无论是 184 180 字节。

str2.t 字段是 INT 8 ,这意味着,该 str1.array 可以从 5 抱到 8 字节。

str2.u 字段是 32 ,这意味着,该 str2.S 可以从 14 抱到 20 字节。

这是 STR1 结构域图:

  + --------------- + --- + -------- +
| INT X [A] [B] | ? |长Y |
+ --------------- + --- + -------- +
| 184 | 8 |
------------------- + + -------- +

和这是 STR2 字段图:

  + --------------- + --- + ------- + --------- --- + --- + -------- +
|字符数组[B] | ? |诠释T |短S [A] | ? |长U |
+ --------------- + --- + ------- + ------------ + --- + ---- ---- +
| 8 | 4 | 20 | 8 |
------------------- + ------- + + + ---------------- ---- ---- +

在这之后,你应该解决以下系统:

  180°= A * B< = 184
5℃; = B&下; = 8
14< = A * 2 = 20 // 7< = A< = 10

答案是: A = 9 B = 5

Hey you guys I need to know what are the values of A and B, in the code A and B are constants defined with #define:

typedef struct {
    int x[A][B];
    long y;
} str1;

typedef struct {
    char array[B];
    int t;
    short S[A];
    long u;
} str2;

void setVal(str1 *p, str2 *q) {
    long v1 = q->t;
    long v2 = q->u;
    p->y = v1+v2;
}

The following assembly code is generated for the setVal procedure:

setVal:

movslq 8(%rsi), %rax

addq 32(%rsi), %rax

movq %rax, 184(%rdi)

ret

解决方案

The structure has the following alignment requirements:

  • a char may start at any byte
  • a short may start at even byte
  • an int may start at byte, divisible by four
  • a long may start at byte, divisible by eight

The str1.y field is a long and starts at 184, this implies, that str1.x may hold either 184 or 180 bytes.

The str2.t field is an int and starts at 8, this implies, that str1.array may hold from 5 to 8 bytes.

The str2.u field is a long and starts at 32, this implies, that str2.S may hold from 14 to 20 bytes.

This is the diagram for str1 structure fields:

+---------------+---+--------+
|  int x[A][B]  | ? | long y |
+---------------+---+--------+
|        184        |    8   |
+-------------------+--------+

And this is the diagram for str2 fields:

+---------------+---+-------+------------+---+--------+
| char array[B] | ? | int t | short S[A] | ? | long u |
+---------------+---+-------+------------+---+--------+
|         8         |   4   |        20      |    8   |
+-------------------+-------+----------------+--------+

After that, you should solve the following system:

180 <= A * B <= 184
5 <= B <= 8
14 <= A * 2 <= 20 // 7 <= A <= 10

The answer is: A = 9, B = 5

这篇关于对准C /大会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆