You are on page 1of 20

Computer Organization

&
Assembly Language Programming
CH 3:
Assembly Language Fundamentals
Outline
Defining Data
Intrinsic Data Types
Data Definition Statement
Adding a Variable to the AddT wo Program
Defining BYTE and SBYTE Data
Defining WORD and SWORD Data
Defining DWORD and SDWORD Data
Defining QWORD Data
Defining Packed BCD (TBYTE) Data
Defining Floating-Point Types
A Program That Adds Variables
Little-Endian Order
Declaring Uninitialized Data
Basic Concepts Computer Organization and Assembly Language
slide 2/43
Intrinsic Data Types

The assembler recognizes a basic set of intrinsic data types, which describe types in
terms of their size (byte, word, doubleword, and so on), whether they are signed, and
whether they are integers or reals.
Type Usage
BYTE 8-bit unsigned integer
SBYTE 8-bit signed integer
WORD 16-bit unsigned integer
SWORD 16-bit signed integer
DWORD 32-bit unsigned integer
SDWORD 32-bit signed integer SD stands for signed double
FWORD 48-bit integer
QWORD 64-bit integer Q stands for quad
TBYTE 80-bit (ten-byte) integer T stands for Ten-byte

Basic Concepts Computer Organization and Assembly Language


slide 3/43
Defining Data
 A data definition statement allocates storage in memory for variables
 [name] directive initializer [, initializer]
 There must be at least one initializer even if it is zero.
 If you prefer to leave the variable uninitialized (assigned a random value), ? the
symbol can be used as the initializer.
 All initializers are converted to binary data by the assembler

Basic Concepts Computer Organization and Assembly Language


slide 4/43
Defining BYTE and SBYTE Data

 The BYTE (define byte) and SBYTE (define signed byte) directives
allocate storage for one or more unsigned or signed values. Each initializer
must fit into 8 bits 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 ? ; no initial value

Basic Concepts Computer Organization and Assembly Language


slide 5/43
,

Multiple Initializers
If a definition has multiple initializers, label is offset for first data item

 .data
list BYTE 10, 20, 30, 40

Offset 0000 0001 0002 0003


Value 10 20 30 40

Basic Concepts Computer Organization and Assembly Language


slide 6/43
.

 Not all definitions need labels

 .data

list BYTE 10, 20, 30, 40


BYTE 50, 60, 70, 80
BYTE 81, 82, 83, 84

Offset 0000 0001 0002 0003 0004 0006


Value 10 20 30 40 50 60

Basic Concepts Computer Organization and Assembly Language


slide 7/43
Defining Strings (1/2)
 Enclose a sequence of characters in quotation marks.
 Most common way to end a string is a null byte (0): Called a null-
terminated string,

 greeting1 BYTE “Good afternoon”, 0


is same as
 greeting1 BYTE ‘G’, ‘o’, ‘o’, … 0

Basic Concepts Computer Organization and Assembly Language


slide 8/43
Defining Strings (2/2)

 can be spread over several lines:

 greeting2
BYTE “Welcome to Encryption”
BYTE “Demo program”
BYTE “created by Kip Irvine”,\
BYTE “If you wish to modify this”
“ program, please”
BYTE “send me a copy”, 0dh, 0ah

Basic Concepts Computer Organization and Assembly Language


slide 9/43
DUP Operator

 The DUP operator allocates storage for multiple data items, using a
constant expression as a counter. It is particularly useful when allocating
space for a string or array, and can be used with initialized or uninitialized
data:
 BYTE 20 DUP(0) ; 20 bytes, all equal to zero
 BYTE 20 DUP(?) ; 20 bytes, uninitialized
 BYTE 4 DUP("STACK") ;
 "STACKSTACKSTACKSTACK"

Basic Concepts Computer Organization and Assembly Language


slide 10/43
Defining WORD and SWORD Data

 The WORD (define word) and SWORD (define signed word) directives
create storage for one or more 16-bit integers:
 word1 WORD 65535 ; largest unsigned value
 word2 SWORD -32768 ; smallest signed value
 word3 WORD ? ; uninitialized, unsigned
 The legacy DW directive can also be used:
 val1 DW 65535 ; unsigned
 val2 DW -32768 ; signed

Basic Concepts Computer Organization and Assembly Language


slide 11/43
Reserved Words
 Array of Words Create an array of words by listing the elements or using
the DUP operator. The following array contains a list of values:
myList WORD 1,2,3,4,5
 The DUP operator provides a convenient way to initialize multiple words:
array WORD 5 DUP(?)

Basic Concepts Computer Organization and Assembly Language


slide 12/43
Defining DWORD and SDWORD Data

 The DWORD (define doubleword) and SDWORD (define signed doubleword)


directives allocate
 storage for one or more 32-bit integers:
 val1 DWORD 12345678h ; unsigned
 val2 SDWORD 2147483648 ; signed
 val3 DWORD 20 DUP(?) ; unsigned array
Array
 myList DWORD 1,2,3,4,5

Basic Concepts Computer Organization and Assembly Language


slide 13/43
.
 Defining QWORD Data
 The QWORD (define quadword) directive allocates storage for 64-bit (8-byte)
values:
quad1 QWORD 1234567812345678h

Basic Concepts Computer Organization and Assembly Language


slide 14/43
s
 Little Endian Order
 x86 processors store and retrieve data from memory using little endian order (low
to high). The least significant byte is stored at the first memory address allocated
for the data. The remaining bytes are stored in the next consecutive memory
positions.

Basic Concepts Computer Organization and Assembly Language


slide 15/43
Adding Variables to the AddSub Program

.data
val1 DWORD 10000
val2 DWORD 40000
val3 DWORD 20000
finalVal DWORD ?
.code
main PROC
mov eax,val1 ; start with 10000
add eax,val2 ; add 40000
sub eax,val3 ; subtract 20000
mov finalVal,eax ; store the result (30000)
call DumpRegs ; display the registers
exit
main ENDP
END main
Basic Concepts Computer Organization and Assembly Language
slide 16/43
Question set
1. Create an uninitialized data declaration for a 16-bit signed integer.
2. Create an uninitialized data declaration for an 8-bit unsigned integer.
3. Create an uninitialized data declaration for an 8-bit signed integer.
4. Create an uninitialized data declaration for a 64-bit integer.
5. Which data type can hold a 32-bit signed integer?
6. Declare a 32-bit signed integer variable and initialize it with the smallest possible
negative decimal value. (Hint: Refer to integer ranges in Chapter 1.)
7. Declare an unsigned 16-bit integer variable named wArray that uses three
initializers.
8. Declare a string variable containing the name of your favorite color. Initialize it as a
nullterminated string.
9. Declare an uninitialized array of 50 unsigned doublewords named dArray.
10. Declare a string variable containing the word “TEST” repeated 500 times.
11. Declare an array of 20 unsigned bytes named bArray and initialize all elements to
zero.
Basic Concepts Computer Organization and Assembly Language
slide 17/43
Your task

Declaring Uninitialized Data

Basic Concepts Computer Organization and Assembly Language


slide 18/43
Assignment 1
 Write a program that defines symbolic constants for
all of the days of the week and 1st statement must be
your name . Create an array variable that uses the
symbols as initializers.
 Using the AddSub program as a reference, write a
program that subtracts three integers using only 16-
bit registers. Insert a call DumpRegs statement to
display the register values.
(Show result of both question also attach screenshots of
your code )

Basic Concepts Computer Organization and Assembly Language


slide 19/43
 .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

Basic Concepts Computer Organization and Assembly Language


slide 20/43

You might also like