You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "Using C or C++ in __asm Blocks | Microsoft Docs"
3
3
ms.custom: ""
4
-
ms.date: "11/04/2016"
4
+
ms.date: "06/25/2018"
5
5
ms.technology: ["cpp-masm"]
6
6
ms.topic: "conceptual"
7
7
dev_langs: ["C++"]
@@ -12,36 +12,39 @@ ms.author: "corob"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# Using C or C++ in __asm Blocks
15
-
## Microsoft Specific
16
-
Because inline assembly instructions can be mixed with C or C++ statements, they can refer to C or C++ variables by name and use many other elements of those languages.
17
-
18
-
An `__asm` block can use the following language elements:
19
-
20
-
- Symbols, including labels and variable and function names
21
-
22
-
- Constants, including symbolic constants and `enum` members
23
-
24
-
- Macros and preprocessor directives
25
-
26
-
- Comments (both **/\*\*/** and **//** )
27
-
28
-
- Type names (wherever a MASM type would be legal)
29
-
30
-
-`typedef` names, generally used with operators such as **PTR** and **TYPE** or to specify structure or union members
31
-
32
-
Within an `__asm` block, you can specify integer constants with either C notation or assembler radix notation (0x100 and 100h are equivalent, for example). This allows you to define (using `#define`) a constant in C and then use it in both C or C++ and assembly portions of the program. You can also specify constants in octal by preceding them with a 0. For example, 0777 specifies an octal constant.
33
-
34
-
## What do you want to know more about?
35
-
36
-
-[Using Operators in __asm Blocks](../../assembler/inline/using-operators-in-asm-blocks.md)
37
-
38
-
-[Using C or C++ Symbols_in __asm Blocks](../../assembler/inline/using-c-or-cpp-symbols-in-asm-blocks.md)
39
-
40
-
-[Accessing C or C++ Data in __asm Blocks](../../assembler/inline/accessing-c-or-cpp-data-in-asm-blocks.md)
41
-
42
-
-[Writing Functions with Inline Assembly](../../assembler/inline/writing-functions-with-inline-assembly.md)
Because inline assembly instructions can be mixed with C or C++ statements, they can refer to C or C++ variables by name and use many other elements of those languages.
19
+
20
+
An `__asm` block can use the following language elements:
21
+
22
+
- Symbols, including labels and variable and function names
23
+
24
+
- Constants, including symbolic constants and `enum` members
25
+
26
+
- Macros and preprocessor directives
27
+
28
+
- Comments (both __/\*\*/__ and __//__ )
29
+
30
+
- Type names (wherever a MASM type would be legal)
31
+
32
+
-`typedef` names, generally used with operators such as **PTR** and **TYPE** or to specify structure or union members
33
+
34
+
Within an `__asm` block, you can specify integer constants with either C notation or assembler radix notation (0x100 and 100h are equivalent, for example). This allows you to define (using `#define`) a constant in C and then use it in both C or C++ and assembly portions of the program. You can also specify constants in octal by preceding them with a 0. For example, 0777 specifies an octal constant.
35
+
36
+
## What do you want to know more about?
37
+
38
+
-[Using Operators in __asm Blocks](../../assembler/inline/using-operators-in-asm-blocks.md)
39
+
40
+
-[Using C or C++ Symbols_in __asm Blocks](../../assembler/inline/using-c-or-cpp-symbols-in-asm-blocks.md)
41
+
42
+
-[Accessing C or C++ Data in __asm Blocks](../../assembler/inline/accessing-c-or-cpp-data-in-asm-blocks.md)
43
+
44
+
-[Writing Functions with Inline Assembly](../../assembler/inline/writing-functions-with-inline-assembly.md)
`Class CString` is based on class template [CStringT Class](../atl-mfc-shared/reference/cstringt-class.md). `CString` is a `typedef` of `CStringT`. More exactly, `CString` is a `typedef` of an *explicit specialization* of `CStringT`, which is a common way to use a class template to define a class. Similarly defined classes are `CStringA` and `CStringW`.
27
+
`Class CString` is based on class template [CStringT Class](../atl-mfc-shared/reference/cstringt-class.md). `CString` is a **typedef** of `CStringT`. More exactly, `CString` is a **typedef** of an *explicit specialization* of `CStringT`, which is a common way to use a class template to define a class. Similarly defined classes are `CStringA` and `CStringW`.
28
28
29
29
`CString`, `CStringA`, and `CStringW` are defined in atlstr.h. `CStringT` is defined in cstringt.h.
30
30
@@ -56,10 +56,10 @@ This topic explains the following basic [CString](../atl-mfc-shared/reference/cs
At least one argument to the concatenation operators (+ or +=) must be a `CString` object, but you can use a constant character string (for example, `"big"`) or a `char` (for example, 'x') for the other argument.
59
+
At least one argument to the concatenation operators (+ or +=) must be a `CString` object, but you can use a constant character string (for example, `"big"`) or a **char** (for example, 'x') for the other argument.
The `Compare` method and the == operator for `CString` are equivalent. `Compare`, `operator==`, and `CompareNoCase` are MBCS and Unicode aware; `CompareNoCase` is also case-insensitive. The `Collate` method of `CString` is locale-sensitive and is often slower than `Compare`. Use `Collate` only where you must abide by the sorting rules as specified by the current locale.
62
+
The `Compare` method and the == operator for `CString` are equivalent. `Compare`, **operator==**, and `CompareNoCase` are MBCS and Unicode aware; `CompareNoCase` is also case-insensitive. The `Collate` method of `CString` is locale-sensitive and is often slower than `Compare`. Use `Collate` only where you must abide by the sorting rules as specified by the current locale.
63
63
64
64
The following table shows the available [CString](../atl-mfc-shared/reference/cstringt-class.md) comparison functions and their equivalent Unicode/MBCS-portable functions in the C run-time library.
Copy file name to clipboardExpand all lines: docs/atl-mfc-shared/cfixedstringt-example-of-a-custom-string-manager.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,44 +12,44 @@ ms.author: "mblome"
12
12
ms.workload: ["cplusplus"]
13
13
---
14
14
# CFixedStringT: Example of a Custom String Manager
15
-
The ATL library implements one example of a custom string manager used by class [CFixedStringT](../atl-mfc-shared/reference/cfixedstringt-class.md), called **CFixedStringMgr**. `CFixedStringT` is derived from [CStringT](../atl-mfc-shared/reference/cstringt-class.md) and implements a string that allocates its character data as part of the `CFixedStringT` object itself as long as the string is less than the length specified by the **t_nChars** template parameter of `CFixedStringT`. With this approach, the string does not need the heap at all, unless the length of the string grows beyond the size of the fixed buffer. Because `CFixedStringT` does not always use a heap to allocate its string data, it cannot use **CAtlStringMgr** as its string manager. It uses a custom string manager (**CFixedStringMgr**), implementing the [IAtlStringMgr](../atl-mfc-shared/reference/iatlstringmgr-class.md) interface. This interface is discussed in [Implementation of a Custom String Manager (Advanced Method)](../atl-mfc-shared/implementation-of-a-custom-string-manager-advanced-method.md).
15
+
The ATL library implements one example of a custom string manager used by class [CFixedStringT](../atl-mfc-shared/reference/cfixedstringt-class.md), called **CFixedStringMgr**. `CFixedStringT` is derived from [CStringT](../atl-mfc-shared/reference/cstringt-class.md) and implements a string that allocates its character data as part of the `CFixedStringT` object itself as long as the string is less than the length specified by the `t_nChars` template parameter of `CFixedStringT`. With this approach, the string does not need the heap at all, unless the length of the string grows beyond the size of the fixed buffer. Because `CFixedStringT` does not always use a heap to allocate its string data, it cannot use `CAtlStringMgr` as its string manager. It uses a custom string manager (`CFixedStringMgr`), implementing the [IAtlStringMgr](../atl-mfc-shared/reference/iatlstringmgr-class.md) interface. This interface is discussed in [Implementation of a Custom String Manager (Advanced Method)](../atl-mfc-shared/implementation-of-a-custom-string-manager-advanced-method.md).
16
16
17
-
The constructor for **CFixedStringMgr** takes three parameters:
17
+
The constructor for `CFixedStringMgr` takes three parameters:
18
18
19
-
-**pData:** A pointer to the fixed `CStringData` structure to be used.
19
+
-*pData:* A pointer to the fixed `CStringData` structure to be used.
20
20
21
-
-**nChars:** The maximum number of characters the `CStringData` structure can hold.
21
+
-*nChars:* The maximum number of characters the `CStringData` structure can hold.
22
22
23
-
-**pMgr:** A pointer to the `IAtlStringMgr` interface of a "backup string manager."
23
+
-*pMgr:* A pointer to the `IAtlStringMgr` interface of a "backup string manager."
24
24
25
-
The constructor stores the values of `pData` and **pMgr** in their respective member variables (`m_pData` and **m_pMgr**). It then sets the length of the buffer to zero, the available length equal to the maximum size of the fixed buffer, and the reference count to -1. The reference count value indicates the buffer is locked and to use this instance of **CFixedStringMgr** as the string manager.
25
+
The constructor stores the values of *pData* and *pMgr* in their respective member variables (`m_pData` and `m_pMgr`). It then sets the length of the buffer to zero, the available length equal to the maximum size of the fixed buffer, and the reference count to -1. The reference count value indicates the buffer is locked and to use this instance of `CFixedStringMgr` as the string manager.
26
26
27
27
Marking the buffer as locked prevents other `CStringT` instances from holding a shared reference to the buffer. If other `CStringT` instances were allowed to share the buffer it would be possible for the buffer contained by `CFixedStringT` to be deleted while other strings were still using the buffer.
28
28
29
-
**CFixedStringMgr** is a full implementation of the `IAtlStringMgr` interface. The implementation of each method is discussed separately.
29
+
`CFixedStringMgr` is a full implementation of the `IAtlStringMgr` interface. The implementation of each method is discussed separately.
30
30
31
31
## Implementation of CFixedStringMgr::Allocate
32
-
The implementation of **CFixedStringMgr::Allocate** first checks to see if the requested size of the string is less than or equal to the size of the fixed buffer (stored in the `m_pData` member). If the fixed buffer is large enough, **CFixedStringMgr** locks the fixed buffer with a length of zero. As long as the string length does not grow beyond the size of the fixed buffer, `CStringT` will not have to reallocate the buffer.
32
+
The implementation of `CFixedStringMgr::Allocate` first checks to see if the requested size of the string is less than or equal to the size of the fixed buffer (stored in the `m_pData` member). If the fixed buffer is large enough, `CFixedStringMgr` locks the fixed buffer with a length of zero. As long as the string length does not grow beyond the size of the fixed buffer, `CStringT` will not have to reallocate the buffer.
33
33
34
-
If the requested size of the string is greater than the fixed buffer **CFixedStringMgr** forwards the request to the backup string manager. The backup string manager is presumed to allocate the buffer from the heap. However, before returning this buffer **CFixedStringMgr** locks the buffer and replaces the buffer's string manager pointer with a pointer to the **CFixedStringMgr** object. This ensures that attempts to reallocate or free the buffer by `CStringT` will call into **CFixedStringMgr**.
34
+
If the requested size of the string is greater than the fixed buffer `CFixedStringMgr` forwards the request to the backup string manager. The backup string manager is presumed to allocate the buffer from the heap. However, before returning this buffer `CFixedStringMgr` locks the buffer and replaces the buffer's string manager pointer with a pointer to the `CFixedStringMgr` object. This ensures that attempts to reallocate or free the buffer by `CStringT` will call into `CFixedStringMgr`.
35
35
36
36
## Implementation of CFixedStringMgr::ReAllocate
37
-
The implementation of **CFixedStringMgr::ReAllocate** is very similar to its implementation of **Allocate**.
37
+
The implementation of `CFixedStringMgr::ReAllocate` is very similar to its implementation of `Allocate`.
38
38
39
39
If the buffer being reallocated is the fixed buffer and the requested buffer size is smaller than the fixed buffer, no allocation is done. However, if the buffer being reallocated is not the fixed buffer, it must be a buffer allocated with the backup manager. In this case the backup manager is used to reallocate the buffer.
40
40
41
-
If the buffer being reallocated is the fixed buffer and the new buffer size is too large to fit within the fixed buffer, **CFixedStringMgr** allocates a new buffer using the backup manager. The contents of the fixed buffer are then copied into the new buffer.
41
+
If the buffer being reallocated is the fixed buffer and the new buffer size is too large to fit within the fixed buffer, `CFixedStringMgr` allocates a new buffer using the backup manager. The contents of the fixed buffer are then copied into the new buffer.
42
42
43
43
## Implementation of CFixedStringMgr::Free
44
-
The implementation of **CFixedStringMgr::Free** follows the same pattern as **Allocate** and `ReAllocate`. If the buffer being freed is the fixed buffer, the method sets it to a zero-length locked buffer. If the buffer being freed was allocated with the backup manager, **CFixedStringMgr** uses the backup manager to free it.
44
+
The implementation of `CFixedStringMgr::Free` follows the same pattern as `Allocate` and `ReAllocate`. If the buffer being freed is the fixed buffer, the method sets it to a zero-length locked buffer. If the buffer being freed was allocated with the backup manager, `CFixedStringMgr` uses the backup manager to free it.
45
45
46
46
## Implementation of CFixedStringMgr::Clone
47
-
The implementation of **CFixedStringMgr::Clone** always returns a pointer to the backup manager rather than the **CFixedStringMgr** itself. This happens because every instance of **CFixedStringMgr** can only be associated with a single instance of `CStringT`. Any other instances of `CStringT` trying to clone the manager should get the backup manager instead. This is because the backup manager supports being shared.
47
+
The implementation of `CFixedStringMgr::Clone` always returns a pointer to the backup manager rather than the `CFixedStringMgr` itself. This happens because every instance of `CFixedStringMgr` can only be associated with a single instance of `CStringT`. Any other instances of `CStringT` trying to clone the manager should get the backup manager instead. This is because the backup manager supports being shared.
48
48
49
49
## Implementation of CFixedStringMgr::GetNilString
50
-
The implementation of **CFixedStringMgr::GetNilString** returns the fixed buffer. Because of the one-on-one correspondence of **CFixedStringMgr** and `CStringT`, a given instance of `CStringT` never uses more than one buffer at a time. Therefore, a nil string and a real string buffer are never needed at the same time.
50
+
The implementation of `CFixedStringMgr::GetNilString` returns the fixed buffer. Because of the one-on-one correspondence of `CFixedStringMgr` and `CStringT`, a given instance of `CStringT` never uses more than one buffer at a time. Therefore, a nil string and a real string buffer are never needed at the same time.
51
51
52
-
Whenever the fixed buffer is not in use, **CFixedStringMgr** ensures that it is initialized with a zero length. This allows it to be used as the nil string. As an added bonus, the `nAllocLength` member of the fixed buffer is always set to the full size of the fixed buffer. This means that `CStringT` can grow the string without calling [IAtlStringMgr::Reallocate](../atl-mfc-shared/reference/iatlstringmgr-class.md#reallocate), even for the nil string.
52
+
Whenever the fixed buffer is not in use, `CFixedStringMgr` ensures that it is initialized with a zero length. This allows it to be used as the nil string. As an added bonus, the `nAllocLength` member of the fixed buffer is always set to the full size of the fixed buffer. This means that `CStringT` can grow the string without calling [IAtlStringMgr::Reallocate](../atl-mfc-shared/reference/iatlstringmgr-class.md#reallocate), even for the nil string.
0 commit comments