This article uses three sample questions to take you five minutes to understand the DUP memory allocation calculation
Basic concept explanation
When defining data, we will encounter the following instructions. To start calculating, we need to figure out how many bytes they represent:
- Define the number of bytes :
Keyword | abbreviation | Alias | Number of defined bytes |
---|---|---|---|
define byte | DB | BYTE | 1 |
define word | DW | WORD | 2 |
define double word | DD | DWORD | 4 |
- mDUP(x1, x2, x3...xn) :
Takem*n
one space, and assign initial values x1, x2, x3...xn to each m space in turn.
example
After the following statements are assembled, what is the number of bytes in the storage unit allocated for the variable BUF?
BUF DW 10H DUP(3 DUP(2,10H),3,5)
BUF DWORD 10H DUP(3 DUP(2,5),3,4,5)
- 2*16*(3*2+1+1) = 256 = 100H
Analysis: DW define word defines each space as2
a byte, a total of10H
= 16 spaces. After that, each space was allocated again: a3
small space + 13
+ 1 space5
. The two bytes allocated in each small space are2
and10H
- 4*16*(3*2+1+1+1) = 576 = 240H
Analysis: DWORD define double word defines each space as4
a byte, a total of10H
= 16 spaces. After that, each space was allocated: a3
small space+13
+14
+1+15
. The two bytes allocated in each small space are2
and5
exercise
BUF DW 10H DUP(3 DUP (2,?), 1, 2 DUP(?), 100 )
What is the number of bytes of the memory space allocated for the variable BUF?
answer
2 * 16 * (32 + 1 + 21 + 1)= 320 = 140H
Pay attention to the conversion of decimal and hexadecimal
During the calculation process, it is best to convert to decimal calculation and then convert the result back to hexadecimal (if necessary)