0% found this document useful (0 votes)
50 views41 pages

NASM: Data and Bss Examples and Sample Problems: Machine-Level and Systems Programming

The summary provides the memory layout of 3 variables - var1, var2, and var3 - after executing an assembly language program on a little endian machine. It lists the hex values of each variable byte by byte, along with the size of each variable in parentheses.

Uploaded by

emnet81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views41 pages

NASM: Data and Bss Examples and Sample Problems: Machine-Level and Systems Programming

The summary provides the memory layout of 3 variables - var1, var2, and var3 - after executing an assembly language program on a little endian machine. It lists the hex values of each variable byte by byte, along with the size of each variable in parentheses.

Uploaded by

emnet81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

NASM: data and bss

Examples and
Sample Problems
ICS312
Machine-Level and
Systems Programming

Henri Casanova ([email protected])


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

tmp pixels i message buffer max


(4) (4) (2) (6) (8) (4)
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

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

tmp pixels i message buffer max


(4) (4) (2) (6) (8) (4)
Endianness?
00 00 00 FE
max dd 254
max

 In the previous slide we showed the above 4-byte memory


content for a double-word that contains 254 = 000000FEh
 While this seems to make sense, it turns out that Intel
processors do not do this!
 Yes, the last 4 bytes shown in the previous slide are wrong
 The scheme shown above (i.e., bytes in memory follow the
“natural” order): Big Endian
 Instead, Intel processors use Little Endian:
FE 00 00 00

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

In-register byte order and in-memory byte order, within a


single multi-byte value, are different!
Example
pixels times 4 db 0FDh
x dd 00010111001101100001010111010011b
blurb db “ad”, “b”, “h”, 0
buffer times 10 db 14o
min dw -19

 What is the layout and the content of the data


memory segment on a Little Endian
machine?
 Byte per byte, in hex
Example
pixels times 4 db 0FDh
x dd 00010111001101100001010111010011b
blurb db “ad”, “b”, “h”, 0
buffer times 10 db 14o
min dw -19

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

pixels x blurb buffer min


(4) (4) (5) (10) (2)
Practice #1
var1 dd -9
var2 db “dcba”
a1 times 3 dw 011b
wt db 0A3h, 0

 What is the layout and the content of the data


memory segment on a Little Endian
machine?
 Byte per byte, in hex
Practice #1
var1 dd -9
var2 db “dcba”
a1 times 3 dw 011b
wt db 0A3h, 0

16 bytes

F7 FF FF FF 64 63 62 61 03 00 03 00 03 00 A3 00

var1 var2 a1 wt
(4) (4) (6) (2)
Practice #2
x1 dw -22
msg db “ba”, 0
array times 2 dd 023o
wt dw 0,011b,020o

 What is the layout and the content of the data


memory segment on a BIG ENDIAN
machine?
 Byte per byte, in hex
Practice #2
x1 dw -22
msg db “ba”, 0
array times 2 dd 023o
wt dw 0,011b,020o

19 bytes

FF EA 62 61 00 00 00 00 13 00 00 00 13 00 00 00 03 00 10

x1 msg array wt
(2) (3) (8) (6)
Example
 Data segment:
L1 db 0AAh, 0BBh
L2 dw 0CCDDh
L3 db 0EEh, 0FFh
 Program:
mov eax, [L2]
mov ax, [L3]
mov [L1], eax

 What’s the memory content?


Solution
 Data segment:
L1 L2 L3
L1 db 0AAh, 0BBh
AA BB DD CC EE FF
L2 dw 0CCDDh
L3 db 0EEh, 0FFh
Solution
L1 L2 L3
AA BB DD CC EE FF

mov eax, [L2] ; eax = FF EE CC DD

mov ax, [L3] ; eax = FF EE FF EE

mov [L1], eax ; L1 points to EE FF EE FF


L1 L2 L3
EE FF EE FF EE FF Final memory content
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”

mov eax, first


inc eax
mov ebx, [eax]
mov [second], ebx
mov byte [third], 11o

What is the content of “data” memory after the code executes


on a Little Endian Machine?
Example
first db 00h, 04Fh, 012h, 0A4h
second dw 165
third db “adf”

mov eax, first


inc eax
mov ebx, [eax]
mov [second], ebx
mov byte [third], 11o

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”

mov eax, first


inc eax Put an address into eax
mov ebx, [eax] (addresses are 32-bit)
mov [second], ebx
mov byte [third], 11o

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”

mov eax, first


inc eax
mov ebx, [eax]
mov [second], ebx
mov byte [third], 11o

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”

mov eax, first


inc eax
mov ebx, [eax]
mov [second], ebx
mov byte [third], 11o

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”

mov eax, first


inc eax
mov ebx, [eax]
mov [second], ebx
mov byte [third], 11o

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”

mov eax, first


inc eax
mov ebx, [eax]
mov [second], ebx
mov byte [third], 11o

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)
Practice #3
 Consider the following program
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

 What is the layout of memory starting at address


var1 on a Little Endian Machine?
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

var1 var2 var3


(4) (3) (3)
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

B3 00 00 00 A3 17 12 62 63 61

var1 var2 var3


(4) (3) (3)
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

B3 00 00 00 A3 17 12 62 63 61 xx xx xx xx eax

var1 var2 var3


(4) (3) (3)
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

B3 00 00 00 A3 17 12 62 63 61 xx xx xx xx eax

var1 var2 var3


(4) (3) (3)
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

B3 00 00 00 A3 17 12 62 63 61 xx xx xx xx eax

var1 var2 var3 12 17 A3 00 ebx


(4) (3) (3)
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

B3 00 00 00 A3 17 12 62 63 61 xx xx xx xx eax

var1 var2 var3 12 17 A3 05 ebx


(4) (3) (3)
Practice #3
var1 dd 179
var2 db 0A3h, 017h, 012h
var3 db “bca”

mov eax, var1


add eax, 3
mov ebx, [eax]
add ebx, 5
mov [var1], ebx

05 A3 17 12 A3 17 12 62 63 61 xx xx xx xx eax

var1 var2 var3 12 17 A3 05 ebx


(4) (3) (3)
Practice #4
 Consider the following program
var1 db “b”,”ca”,0
var2 db 1,2,3,4
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

 What is the layout of memory starting at address


var1 on a Little Endian Machine?
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

var1 var2 var3


(4) (4) (4)
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

62 63 61 00 03 00 00 00 12 00 12 00

var1 var2 var3


(4) (4) (4)
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

62 63 61 00 03 00 00 00 12 00 12 00 xx xx xx xx eax

var1 var2 var3


(4) (4) (4)
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

62 63 61 00 03 00 00 00 12 00 12 00 xx xx xx xx eax

xx xx xx xx ebx
var1 var2 var3
(4) (4) (4)
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

62 63 61 00 03 00 00 00 12 00 12 00 xx xx xx xx eax

xx xx xx xx ebx
var1 var2 var3
(4) (4) (4)
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

62 63 61 00 03 00 00 00 12 00 12 00 xx xx xx xx eax

xx xx xx xx ebx
var1 var2 var3
(4) (4) (4)
Practice #4
var1 db “b”,”ca”,0
var2 times db 3,0,0,0
var3 times 2 dw 012h

mov eax, var3


mov ebx, var1
sub eax, 4
add ebx, [eax]
mov dword [ebx], 42

62 63 61 2A 00 00 00 00 12 00 12 00 xx xx xx xx eax

xx xx xx xx ebx
var1 var2 var3
(4) (4) (4)

You might also like