Data Types and Memory Allocation
Data Types and Memory Allocation
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
db
where
MyVar is variable name
1 of 11
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
.DATA
EE FF
1234
56789ABC
00000000
byte0
word2
var4
var8
BYTE
WORD
DWORD
DWORD
0EEh, 0FFh
1234h
56789ABCh
0
.CODE
_start:
B8 00000002 R
A3 00000008 R
C3
2 of 11
eax,304002h
24/08/2015 10:26
00301005: A3 08 40 30 00
0030100A: C3
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
mov
ret
At the end:
The byte sequence of 304002h was reversed when the value was stored in memory.
Note that command switch /base:0x300000 was used to change the base address of the executable image:
LINK /base:0x300000 /debug /subsystem:console /entry:_start /out:little_endian.exe little_endian.obj
There are two popular methods of storing integers: big endian and
little indian.
Big endian method is the most natural:
the biggest (i.e. most significant) byte is stored first, then the
next biggest, etc.
IBM mainframes, most RISC processors and Motorola processors all
use this big endian method.
However, Intel-based processors use the little endian method, in
which the least significant byte is stored first.
WORD
DWORD
Value(*)
Big endian
Little
endian
1234
12 34
34 12
47D5A8
00 47 d5 a8
a8 d5 47 00
DWORD 56789ABC 56 78 9a bc bc 9a 78 56
(*)
Normally, the programmer does not need to worry about which format
is used, unless
1. Binary data is transfered between different computers e.g. over
a network.
All TCP/IP headers store integers in big endian format
3 of 11
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
Big Endian:
Little Endian:
DB
DW
DD
DD
'y'
25159
542803535
1.234
DB
DB
DB
DB
DB
'B'
'y'
'e'
0DH
0AH
can be written as
message
DB
4 of 11
24/08/2015 10:26
message
DB
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
9. Multi-byte Definitions
Multiple definitions can be cumbersome to initialize data structures such as arrays
For example, to declare and initialize an integer array of 8 elements
values
DW
0, 0, 0, 0, 0, 0, 0, 0
What if we want to declare and initialize to zero an array of a lot more elements?
Assembler provides a better way of doing this by DUP directive:
values DW 8 DUP (0)
.DATA
value
sum
marks
message
char1
DW
DD
DW
DB
DB
0
0
10 DUP (?)
'The grade is:',0
?
;
;
;
;
;
;
;
;
label
name
-------value
sum
marks
message
char1
memory
offset
------0
2
6
26
40
C data type
--------------------char
int, unsigned int
float, long
double
internal intermediate float value
5 of 11
Description
BYTE, DB (byte)
DWORD, DD (doubleword = 4
bytes)
Allocates 6-byte (48-bit) integers. These values are normally used only as pointer variables on the
80386/486 processors.
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
QWORD, DQ (quadword = 8
bytes)
Allocates 10-byte (80-bit) integers if the initializer has a radix specifying the base of the number.
Bytes
BYTE, SBYTE
WORD, SWORD
DWORD, SDWORD 4
FWORD
QWORD
TBYTE
10
6 of 11
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
resb
resw
resd
1
100
1
PTR operator re-casts the DWORD-sized memory location pointed by num[ index ] expression into a WORD-sized value.
7 of 11
; Immediate to register
; Immediate to memory direct
; Immediate to memory indirect
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
; Register moves
mov
mem, ax
; Register to memory direct
mov
mem[bx], ax ; Register to memory indirect
mov
ax, bx
; Register to register
mov
ds, ax
; General register to segment register
; Direct memory moves
mov
ax, mem
mov
ds, mem
You can exchange data between registers or between registers and memory, but not from memory to memory:
xchg
xchg
xchg
ax, bx
memory, ax
mem1, mem2
; Put AX in BX and BX in AX
; Put "memory" in AX and AX in "memory"
; Illegal, can't exchange memory locations!
The rules for operands in the XCHG instruction are the same as those for the MOV instruction...
...except that XCHG does not accept immediate operands.
8 of 11
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
Without the XCHG instruction, we need a temporary register to exchange values if using only the MOV instruction.
WORD 1000h
WORD 2000h
.CODE
mov
xchg
mov
ax, [val1]
ax, [val2]
[val1], ax
; AX = 1000h
; AX = 2000h, val2 = 1000h
; val1 = 2000h
al, ah
For example, the following XCHG converts the data in AX into the other endian form.
Pentium provides BSWAP instruction to do similar conversion on 32-bit data:
BSWAP 32-bit register
Note: BSWAP works only on data located in a 32-bit register.
BSWAP swaps bytes of its operand. For example,
bswap eax
Sign-extend
AL to AX
AX to DX:AX
9 of 11
EAX to EDX:EAX
24/08/2015 10:26
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
SBYTE
SWORD
SDWORD
-5
+5
-5
al, mem8
;
;
;
;
;
;
;
;
;
ax, mem16
ax, mem16
eax, mem32
Sign extending instructions efficiently convert unsigned values as well, provided the sign bit is zero.
This example, for instance, correctly widens mem16 whether you treat the variable as signed or unsigned.
The processor does not differentiate between signed and unsigned values.
For instance, the value of mem8 in the previous example is literally 251 (0FBh) to the processor.
It ignores the human convention of treating the highest bit as an indicator of sign.
The processor can ignore the distinction between signed and unsigned numbers because binary arithmetic works the same in either
case.
The programmer, not the processor, must keep track of which values are signed or unsigned, and treat them accordingly.
BYTE
WORD
al, mem8
ah, ah
251
251
mov
sub
sub
mov
10 of 11
24/08/2015 10:26
mov
movsx
bx, 0C3EEh
ebx, bx
movzx
dx, bl
http://www.c-jump.com/CIS77/ASM/DataTypes/lecture.html
;
;
;
;
;
MOVSX and MOVZX instructions usually execute much faster than the equivalent CBW, CWD, CWDE, and CDQ.
11 of 11
24/08/2015 10:26