C-language extern references variables in assembly, arbitrary conversion type

Source: Internet
Author: User
Tags array definition

Today, a small problem, in C language reference to the assembly of the variable, what will be the result of the variables in the assembly is not like the C language type of int type constraints, can be treated as any type of data, then to the C language we should be treated as what type.

In other words, define VAR in the Assembly, reference in C, we must declare the var external variable with extern, then what type does extern follow? is extern int var? or extern short Var


Instance:

One

Assembler: (yes, just so short)

. global Var #注意现在的汇编器不再要求被C语言引用的变量名前加下划线. Datavar:.fill 10,4,9 #填充10个单元, 4 bytes per unit, with a value of 9 per cell

C Language:

#include <stdio.h> extern int var; The C language compiler now references assembler variables that can be used with the same name as the assembly variable, and written as _var instead of void Main () {printf ("sizeof (VAR) =%d\n", sizeof (VAR));p rintf ("%d\n", var ); }

The output of the program is

sizeof (int) =49

The explanation is simple,

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6B/98/wKioL1UyThyhEVK1AABn_JIymqM381.jpg "title=" 01.JPG "alt=" Wkiol1uythyhevk1aabn_jiymqm381.jpg "/>

For example, I've only drawn four bytes, and I've populated 10 units of 4 bytes per unit, meaning that each 9 accounts for four bytes. , and the C language is declared as extern int var; the first four bytes are taken out as an int variable, of course 9.

Two

Keep the C language program as in the previous one,

extern int var;

Change the Assembly statement to

. Fill 10,2,9 #10个单元, each unit is filled with 9, each unit size is 2B

This means that each unit is 2 bytes, each unit fills a 9, we do not change the C language statement, then the Var will still be considered as an int 4-byte variable, still the first 4 bytes taken as the value of Var. Let's speculate on what the result will be.

First, each 9 takes up two bytes, the memory graph should be like this, (only 4 bytes are drawn, not painted at the back)

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6B/98/wKioL1UyTtjCtjvIAABkoAsAul4794.jpg "title=" 02.JPG "alt=" Wkiol1uyttjctjviaabkoasaul4794.jpg "/>

Take out 4 bytes at once, it is 0x00090009, this thing to get the windows to bring the calculator to calculate is the decimal 589833, this is our forecast value. Let's see if the program results are the same as we guessed.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6B/98/wKioL1UyT76zt-bEAABmJmoXbCM037.jpg "title=" 03.JPG "alt=" Wkiol1uyt76zt-beaabmjmoxbcm037.jpg "/>


Yes, that is to say that the variables we define in the assembly, come to the C language can be arbitrarily declared as any type, from the output of sizeof can also be seen, we put Var declared how big, it is how big.

Three

Let's try something else, the assembly statement remains the same as the previous one, and remains

. Fill 10,2,9 #10个单元, each unit is filled with 9, each unit size is 2B

We change the C language statement to declare VAR as

extern Long long var; 8-byte integer

Of course, the output statement is slightly changed, because it is a long long integer, so output printf ("%lld\n", Var) so.

#include <stdio.h> extern long long var; void Main () {printf ("sizeof (VAR) =%d\n", sizeof (VAR));p rintf ("%lld\n", Var);}

We still guess the result, too many bytes I do not draw, according to the above thinking, this time take out 8 bytes as a long variable var value, then should be 0x0009000900090009, as the decimal is 2533313445691401.

Look at the program run results:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6B/9C/wKiom1UyUK_jBcMdAABNgTifnuA454.jpg "title=" 04.JPG "alt=" Wkiom1uyuk_jbcmdaabngtifnua454.jpg "/>

Predictions are correct.

Four

Again, declare var as a char variable.

#include <stdio.h> extern char var; #声明为charvoid Main () {printf ("sizeof (VAR) =%d\n", sizeof (VAR));p rintf ("%c\n", Var);

The output statement is correspondingly changed to%c

The assembly is slightly changed to fill the value, because 9 is a tab of the ASCII code, output you can not see. We changed it to fill 43.

. Fill 10,2,43 #10个单元, each unit fills in 43 (memory is actually stored in binary), each unit size is 2B

Note that the hexadecimal representation of 43 is 0x2b, which is the plus "+" of the ASCII code,

The memory grows like this

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6B/98/wKioL1UyVFeRALi9AABqDKISKjM708.jpg "title=" 05.JPG "alt=" Wkiol1uyvferali9aabqdkiskjm708.jpg "/>

C language will think that Var is a char type, take out a byte as the value of Var, that is, to the 0x2b to Var, we put Var with%c output, is the output of 0X2B this ASCII code represents the character. is the "+" sign.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6B/98/wKioL1UyVPTxY4n6AAA1Qy3PZAc866.jpg "title=" 06.JPG "alt=" Wkiol1uyvptxy4n6aaa1qy3pzac866.jpg "/>


Five

Well, it's not over anymore, so let's end this article with an array definition.

The assembly language code is the same as the previous

. Fill 10,2,43

C Language modified to:

#include <stdio.h> typedef char CHARARR[20]; Chararr is a type alias, and each variable defined with it is a 20-character array of extern chararr var; VAR is defined as the Chararr type, which is an array of char types with 20 elements. void Main () {printf ("sizeof (VAR) =%d\n", sizeof (VAR)), int i=0;while (i<=19) {printf ("[%c]", var[i]);//With [] It is easier to see the output phenomenon i++;} printf ("\ n");}

We let Var become an array, containing 20 elements, just to include the 10 unit X2 bytes in the assembly by 20 bytes.

And then use the loop to output one after the other, why do you do this without%s, because there are many bytes in the array is 0x00, which is the string termination flag. %s will be interrupted.

The results are as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6B/9C/wKiom1UyWXKiWre3AABj5Z9H3Ec038.jpg "title=" 07.JPG "alt=" Wkiom1uywxkiwre3aabj5z9h3ec038.jpg "/>

As you can see, the 20-bit ASCII code 0x2b 0x00 0x2b 0x00 .... This sequence of arrays is output.


Finished, c and assembly language combination is not strong to the point of unimaginable, arbitrary manipulation of memory data.



This article is from the "mirage1993" blog, make sure to keep this source http://mirage1993.blog.51cto.com/2709744/1635436

C-language extern references variables in assembly, arbitrary conversion type

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.