Microprocessor Based
Systems
Lecture No 10 Defining Data
By Nasir Mahmood
This Lecture
The Assemble-Link-Execute Cycle
Defining DATA
Defining Arrays & Strings
Book Reading “Assembly Language for x86 Processors” 6th
Edition By Kip R. Irvine
Section 3.3 and 3.4
The Assemble-Link-Execute Cycle
The Assemble-Link-Execute Cycle Contd..
Step 1: A programmer uses a text editor to create an ASCII
text file named the source file.
Step 2: The assembler reads the source file and produces an
object file, a machine-language translation of the program.
Optionally, it produces a listing file. If any errors occur, the
programmer must return to Step 1 and fix the program.
Step 3: The linker reads the object file and checks to see if the
program contains any calls to procedures in a link library. The
linker copies any required procedures from the link library,
combines them with the object file, and produces the executable
file.
Step 4: The operating system loader utility reads the executable
file into memory and branches the CPU to the program’s
starting address, and the program begins to execute.
The Assemble-Link-Execute Cycle Contd..
Object File: Machine language translation of
the program
(Optional) Listing File: A printable copy of
source code, offset addresses, machine code
(Optional) Map File: List of segment groups in
program
Linker copies any included procedure from
link library and produces the final .exe file
Defining
Data…
Data Definition Statement
A data definition statement sets aside storage in
memory for a variable.
May optionally assign a name (label) to the data
Syntax:
[name] directive initializer [,initializer] . . .
value1 BYTE 10
All initializers become binary data in memory
7
Intrinsic Data Types (1 of 2)
BYTE, SBYTE
8-bit unsigned integer; 8-bit signed integer
WORD, SWORD
16-bit unsigned & signed integer
DWORD, SDWORD
32-bit unsigned & signed integer
QWORD
64-bit integer
TBYTE
80-bit integer
8
Intrinsic Data Types (2 of 2)
REAL4
4-byte IEEE short real
REAL8
8-byte IEEE long real
REAL10
10-byte IEEE extended real
9
Defining BYTE and SBYTE Data
Each of the following defines a single byte of storage:
value1 BYTE 'A' ; character constant
value2 BYTE 0 ; smallest unsigned byte
value3 BYTE 255 ; largest unsigned byte
value4 SBYTE -128 ; smallest signed byte
value5 SBYTE +127 ; largest signed byte
value6 BYTE ? ; uninitialized byte
10
Defining WORD and SWORD
Data
Define storage for 16-bit integers
or double characters
single value or multiple values
word1 WORD 65535 ; largest unsigned value
word2 SWORD –32768 ; smallest signed value
word3 WORD ? ; uninitialized, unsigned
word4 WORD "AB" ; double characters
11
Defining DWORD and SDWORD
Data
Storage definitions for signed and unsigned 32-bit
integers:
val1 DWORD 12345678h ; unsigned
val2 SDWORD –2147483648 ; signed
12
Little Endian Order
All data types larger than a byte store their
individual bytes in reverse order.
The least significant byte occurs at the first
(lowest) memory address.
Example:
val1 DWORD 12345678h
13
Example Code….
TITLE Add and Subtract, Version 2
; This program adds and subtracts 32-bit unsigned
; integers and stores the sum in a variable.
INCLUDE Irvine32.inc
.data
val1 DWORD 10000h
val2 DWORD 40000h
val3 DWORD 20000h
finalVal DWORD ?
.code
main PROC
mov eax,val1 ; start with 10000h
add eax,val2 ; add 40000h
sub eax,val3 ; subtract 20000h
mov finalVal,eax ; store the result (30000h)
call DumpRegs ; display the registers
exit
main ENDP
END main
15
Example Output
Program output, showing registers and flags:
EAX=00030000 EBX=7FFDF000 ECX=00000101
EDX=FFFFFFFF
ESI=00000000 EDI=00000000 EBP=0012FFF0
ESP=0012FFC4
EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0
OF=0
16
DECLARING ARRAYS: MULTIPLE
INITIALIZER
Syntax:
[name] directive initializer [,initializer] . . .
var1 BYTE 10
arrayl BYTE 10, 11, 12, 13
Defining Byte Arrays
Examples that use multiple initializers:
list1 BYTE 10,20,30,40
BYTE 50,60,70,80
BYTE 81,82,83,84
list3 BYTE ?,32,41h,00100010b
list4 BYTE 0Ah,20h,‘A’,22h
18
Defining Strings (1 of 3)
A string is implemented as an array of
characters
For convenience, it is usually enclosed in quotation
marks
It often will be null-terminated
Examples:
str1 BYTE "Enter your name",0
str2 BYTE 'Error: halting
program',0
str3 BYTE19 'A','E','I','O','U’
Defining Strings (2 of 3)
End-of-line character sequence:
0Dh = carriage return
0Ah = line feed
str1 BYTE "Enter your name: ",0Dh,0Ah
BYTE "Enter your address: ",0
newLine BYTE 0Dh,0Ah,0
20
Defining Strings (3 of 3)
To continue a single string across multiple
lines, end each line with a comma:
menu BYTE "Checking Account",0dh,0ah,0dh,0ah,
"1. Create a new account",0dh,0ah,
"2. Open an existing account",0dh,0ah,
"3. Credit the account",0dh,0ah,
"4. Debit the account",0dh,0ah,
"5. Exit",0ah,0ah,
"Choice> ",0
21
THE END