Volatile usage Summary

Source: Internet
Author: User

In most cases, the variable is cached inRegisterIs a very valuable optimization method, if not, it is a pity. C and C ++ provide you with the opportunity to explicitly disable such cache optimization. If you declare that the variable uses the volatile modifier, the compiler will not cache the variable in the Register-the actual location of the variable in the memory will be accessed each time.

1. Volatile must be added to the variable modified in the interrupted service program for testing by other programs;

2. Volatile should be added to the labels shared by all tasks in a multi-task environment;

3. It is recommended to add volatile to the variable value corresponding to the Register, for example:

Volatile uint32 * Register; // register is the address of the Register

Another example:

* (Volatile uint32 *) (uintn) (apicbase + apic_register_icr_high_offset) = icrhigh;
* (Volatile uint32 *) (uintn) (apicbase + apic_register_icr_low_offset) = icrlow;

4. When multiple commands write values to the same register according to the sequence, if O2 is used for optimization, the program only writes the write of the last command! It is obviously wrong!

====================

Your own practice!

Use Cl test. c/FACS/Zi/O2 to compile the following program.

# Include "stdio. H"

Unsigned int Q;

Int main ()
{
// Volatile unsigned int * I;
Unsigned int * I;
Unsigned int P;

P = 0x55aa;

I = (unsigned int X) 0x310;
* I = P;
P = * I; // if there is no volatile, the compiler optimizes O2 and considers this step redundant. In the next step, 0x55aa is directly assigned to Q;

// However, If I is the address of a hardware register and you need to write a value to read the status value, then the Q value is not the correct status value, but 0x55aa! This is obviously incorrect.

Q = P;

Return 0;
}

Test_non_volatile.cod:

; Listing generated by Microsoft (r) Optimizing Compiler version 13.10.3077

Title Test. c
. 386 P
Include listing. inc
If @ version GT 510
. Model flat
Else
_ Text Segment para use32 public 'code'
_ Text ends
_ Data Segment DWORD use32 public 'data'
_ DATA ends
Const segment DWORD use32 public 'const'
Const ends
_ BSS segment DWORD use32 public 'bss'
_ BSS ends
$ Symbols segment byte use32 'destsym'
$ Symbols ends
$ Types segment byte use32 'descrip'
$ Types ends
_ TLS segment DWORD use32 public 'tls'
_ TLS ends
; Comdat _ main
_ Text Segment para use32 public 'code'
_ Text ends
Sxdata segment DWORD use32 'sxdata'
Sxdata ends
Flat group _ data, const, _ BSS
Assume Cs: flat, DS: flat, SS: Flat
Endif

Includelib libc
Includelib oldnames

_ Data Segment
Comm _ Q: DWORD
_ DATA ends
Public _ main
; Function compile flags:/ogty
; File E:/temp/test. c
; Comdat _ main
_ Text Segment
_ Main proc near; comdat

; 7: // volatile unsigned int * I;
; 8: Unsigned int * I;
; 9: Unsigned int P;
; 10:
; 11: P = 0x55aa;
; 12:
; 13: I = (unsigned int x *) 0x310;

00000 B8 10 03 00 00 mov eax, 784; 00000310 H

; 14: * I = P;

00005 C7 00 AA 55 00
00 mov dword ptr [eax], 21930; drawing 55aah

; 15: P = * I; // ignore this sentence!
; 16:
; 17: q = P;

0000b C7 05 00 00 00
00 AA 55 00 00 mov dword ptr _ q, 21930; then 55aah // directly assign 0x55aa

; 18:
; 19: Return 0;

00015 33 C0 XOR eax, eax

; 20 :}

00017 C3 RET 0
_ Main endp
_ Text ends
End

Test_volatile.cod:

; Listing generated by Microsoft (r) Optimizing Compiler version 13.10.3077

Title Test. c
. 386 P
Include listing. inc
If @ version GT 510
. Model flat
Else
_ Text Segment para use32 public 'code'
_ Text ends
_ Data Segment DWORD use32 public 'data'
_ DATA ends
Const segment DWORD use32 public 'const'
Const ends
_ BSS segment DWORD use32 public 'bss'
_ BSS ends
$ Symbols segment byte use32 'destsym'
$ Symbols ends
$ Types segment byte use32 'descrip'
$ Types ends
_ TLS segment DWORD use32 public 'tls'
_ TLS ends
; Comdat _ main
_ Text Segment para use32 public 'code'
_ Text ends
Sxdata segment DWORD use32 'sxdata'
Sxdata ends
Flat group _ data, const, _ BSS
Assume Cs: flat, DS: flat, SS: Flat
Endif

Includelib libc
Includelib oldnames

_ Data Segment
Comm _ Q: DWORD
_ DATA ends
Public _ main
; Function compile flags:/ogty
; File E:/temp/test. c
; Comdat _ main
_ Text Segment
_ Main proc near; comdat

; 7: Volatile unsigned int * I;
; 8: // unsigned int * I;
; 9: Unsigned int P;
; 10:
; 11: P = 0x55aa;
; 12:
; 13: I = (unsigned int x *) 0x310;

00000 B8 10 03 00 00 mov eax, 784; 00000310 H

; 14: * I = P;

00005 C7 00 AA 55 00
00 mov dword ptr [eax], 21930; drawing 55aah

; 15: P = * I;
; 16:
; 17: q = P;

0000b 8B 00 mov eax, dword ptr [eax] // Assembly explicitly assigns the value in memory I to P
Required D A3 00 00 00 00 mov dword ptr _ q, eax// Q can also get the true value in memory

; 18:
; 19: Return 0;

00012 33 C0 XOR eax, eax

; 20 :}

00014 C3 RET 0
_ Main endp
_ Text ends
End

 If we do not add the optimization option O2, the COD file is as follows, it can be seen that the assembly code is correct.

; Listing generated by Microsoft (r) Optimizing Compiler version 13.10.3077

Title Test. c
. 386 P
Include listing. inc
If @ version GT 510
. Model flat
Else
_ Text Segment para use32 public 'code'
_ Text ends
_ Data Segment DWORD use32 public 'data'
_ DATA ends
Const segment DWORD use32 public 'const'
Const ends
_ BSS segment DWORD use32 public 'bss'
_ BSS ends
$ Symbols segment byte use32 'destsym'
$ Symbols ends
$ Types segment byte use32 'descrip'
$ Types ends
_ TLS segment DWORD use32 public 'tls'
_ TLS ends
Sxdata segment DWORD use32 'sxdata'
Sxdata ends
Flat group _ data, const, _ BSS
Assume Cs: flat, DS: flat, SS: Flat
Endif

Includelib libc
Includelib oldnames

_ Data Segment
Comm _ Q: DWORD
_ DATA ends
Public _ main
; Function compile flags:/ODT
; File E:/temp/test. c
_ Text Segment
_ I $ =-8; size = 4
_ P $ =-4; size = 4
_ Main proc near

; 6 :{

00000 55 push EBP
00001 8B EC mov EBP, ESP
00003 83 EC 08 Sub ESP, 8

; 7: // volatile unsigned int * I;
; 8: Unsigned int * I;
; 9: Unsigned int P;
; 10:
; 11: P = 0x55aa;

00006 C7 45 fc aa 55
00 00 mov dword ptr _ p $ [EBP], 21930; 12755aah

; 12:
; 13: I = (unsigned int x *) 0x310;

Mongod C7 45 F8 10 03
00 00 mov dword ptr _ I $ [EBP], 784; 00000310 H

; 14: * I = P;

00014 8B 45 F8 mov eax, dword ptr _ I $ [EBP]
00017 8B 4D FC mov ECx, dword ptr _ p $ [EBP]
0001a 89 08 mov dword ptr [eax], ECx

; 15: P = * I;

0001c 8B 55 F8 mov edX, dword ptr _ I $ [EBP]
0001f 8B 02 mov eax, dword ptr [edX]
00021 89 45 FC mov dword ptr _ p $ [EBP], eax

; 16:
; 17: q = P;

00024 8B 4D FC mov ECx, dword ptr _ p $ [EBP]
00027 89 0d 00 00 00
00 mov dword ptr _ q, ECx

; 18:
; 19: Return 0;

0002d 33 C0 XOR eax, eax

; 20 :}

0002f 8B E5 mov ESP, EBP
00031 5d pop EBP
00032 C3 RET 0
_ Main endp
_ Text ends
End

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.