Put variables in a regsiter

Source: Internet
Author: User
#define max(a,b) ({     \        typeof (a) _a = (a);    \        typeof (b) _b = (b);    \        _a > _b ? _a:_b;        \        })#include "stdio.h"int main(void){        register int a asm("eax") = 2;        int b = 3;        register int c = 4;        printf("%d,%d",a,max(a,c));        return 0;}

In the above example, A, B, and C can store variables in registers rather than stacks? The answer is a and B. However, the advantage of a is that registers can be specified, while C can only be placed in registers.

Objdump-D.

00000000004005ac <main>:
4005ac: 55 push % RBP
4005ad: 48 89 E5 mov % RSP, % RBP
4005b0: 53 push % RBx
4005b1: 48 83 EC 18 sub $0x18, % RSP
4005b5: B8 02 00 00 00 mov $0x2, % eax //
4005ba: C7 45 E4 03 00 00 00 movl $0x3,-0x1c (% RBP) // B
4005c1: BB 04 00 00 00 mov $0x4, % EBX // C
4005c6: 89 45 E8 mov % eax,-0x18 (% RBP)
4005c9: 89 5d EC mov % EBX,-0x14 (% RBP)
4005cc: 8B 55 E8 mov-0x18 (% RBP), % edX
4005cf: 3B 55 ec cmp-0x14 (% RBP), % edX
4005d2: 7E 05

Add a typeof example to enrich the code. typeof is a method for obtaining the variable type during compilation,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.