Ics312 - Nasm - Data - BSSPPT BUENOS
Ics312 - Nasm - Data - BSSPPT BUENOS
(inverted)
ICS312
Machine-Level and
Systems Programming
ten bytes T 10
The DX data directives
One declares a zone of initialized memory
using three elements:
Label: the name used in the program to refer to
that zone of memory
A pointer to the zone of memory, i.e., an address
DX, where X is the appropriate letter for the size
of the data being declared
Initial value, with encoding information
default: decimal
b: binary
h: hexadecimal
o: octal
quoted: ASCII
DX Examples
L1 db 0
1 byte, named L1, initialized to 0
L2 dw 1000
2-byte word, named L2, initialized to 1000
L3 db 110101b
1 byte, named L3, initialized to 110101 in binary
L4 db 0A2h
1 byte, named L4, initialized to A2 in hex (note the ‘0’)
L5 db 17o
1 byte, named L5, initialized to 17 in octal (1*8+7=15 in decimal)
L6 dd 0FFFF1A92h (note the ‘0’)
4-byte double word, named L6, initialized to FFFF1A92 in hex
L7 db “A”
1 byte, named L7, initialized to the ASCII code for “A” (65d)
ASCII Code
Associates 1-byte numerical codes to
characters
Unicode, proposed much later, uses 2 bytes and
thus can encode 28 times more characters (room
for all languages, Chinese, Japanese, accents,
etc.)
A few values to know:
‘A’ is 65d, ‘B’ is 66d, etc.
‘a’ is 97d, ‘b’ is 98d, etc.
‘ ’ is 32d
DX for multiple elements
L8 db 0, 1, 2, 3
Defines 4 bytes, initialized to 0, 1, 2 and 3
L8 is a pointer to the first byte
L9 db “w”, “o”, ‘r’, ‘d’, 0
Defines a null-terminated string, initialized to
“word\0”
L9 is a pointer to the beginning of the string
L10 db “word”, 0
Equivalent to the above, more convenient to write
DX with the times qualifier
Say you want to declare 100 bytes all
initialized to 0
NASM provides a nice shortcut to do this, the
“times” qualifier
L11 times 100 db 0
Equivalent to L11 db 0,0,0,....,0 (100 times)
Data segment example
tmp dd -1
pixels db 0FFh, 0FEh, 0FDh, 0FCh
i dw 0
message db “H”, “e”, “llo”, 0
buffer times 8 db 0
max dd 254
28 bytes
28 bytes
FF FF FF FF FF FE FD FC 00 00 48 65 6C 6C 6F 00 00 00 00 00 00 00 00 00 00 00 00 FE
max
Little Endian
mov eax, 0AABBCCDDh
mov [M1], eax
mov ebx, [M1]
Registers Memory
eax [M1]
ebx
Little Endian
mov eax, 0AABBCCDDh
mov [M1], eax
mov ebx, [M1]
Registers Memory
eax AA BB CC DD
[M1]
ebx
Little Endian
mov eax, 0AABBCCDDh
mov [M1], eax
mov ebx, [M1]
Registers Memory
eax AA BB CC DD
[M1] DD CC BB AA
ebx
Little Endian
mov eax, 0AABBCCDDh
mov [M1], eax
mov ebx, [M1]
Registers Memory
eax AA BB CC DD
[M1] DD CC BB AA
ebx AA BB CC DD
Little Endian
mov eax, 0AABBCCDDh
mov [M1], eax
mov ebx, [M1]
Registers Memory
eax AA BB CC DD
[M1] DD CC BB AA
ebx AA BB CC DD
25 bytes
FD FD FD FD D3 15 36 17 61 64 62 68 00 0C 0C 0C 0C 0C 0C 0C 0C 0C 0C ED FF
00 4F 12 A4 A5 00 61 64 66 00 00 00 00 eax
00 00 00 00 ebx
first second third
(4) (2) (3)
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”
00 4F 12 A4 A5 00 61 64 66 xx xx xx xx eax
00 00 00 00 ebx
first second third
(4) (2) (3)
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”
00 4F 12 A4 A5 00 61 64 66 xx xx xx xx eax
00 00 00 00 ebx
first second third
(4) (2) (3)
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”
00 4F 12 A4 A5 00 61 64 66 xx xx xx xx eax
A5 A4 12 4F ebx
first second third
(4) (2) (3)
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”
00 4F 12 A4 4F 12 A4 A5 66 xx xx xx xx eax
A5 A4 12 4F ebx
first second third
(4) (2) (3)
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”
00 4F 12 A4 4F 12 09 A5 66 xx xx xx xx eax
A5 A4 12 4F ebx
first second third
(4) (2) (3)
Assembly is Dangerous
The previous example is really a terrible program
But it’s a good demonstration of why the assembly
programmer must be really careful
For instance, we were able to store 4 bytes into a 2-
byte label, thus overwriting the first 2 characters of a
string that merely happened to be stored in memory
next to that 2-byte label
Playing such tricks can lead to very clever programs
that do things that would be impossible (or very
cumbersome) to do with many high-level
programming language (e.g., in Java)
But you really must know what you’re doing
Typically such behaviors are bugs
x86 Assembly is Dangerous
Another dangerous thing we did in our assembly program was the
use of unaligned memory accesses
We stored a 4-byte quantity at some address
We incremented the address by 1
We read a 4-byte quantity from the incremented address!
This really removes all notion of a structured memory
Some architectures only allow aligned accesses
Accessing an X-byte quantity can only be done for an address that’s a
multiple of X!
bytes
words
dwords
qwords
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17
Conclusion
It’s important to understand the memory
layout