Computer Organization and Assembly Language (CS2411 & CSC2201)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Computer Organization

and Assembly Language


(CS2411 & CSC2201)
Class: BSCS 4
Lecture: 4
Lecturer Name: Kashif
Email: [email protected]

Szabist Larkana Campus 1


Lecture Outline
 Suggested Coding Standards
 Defining Data
 Multiple Initializers
 Defining Strings
 Defining Symbolic Constants
 Data-Related Operators and Directives

Szabist Larkana Campus 2


Suggested Coding Standards
 Some approaches to capitalization
 Capitalize nothing
 Capitalize everything
 Capitalize all reserved words, mnemonics and register names
 Capitalize only directives and operators
 MASM is NOT case sensitive: does not matter what case is used
 Other suggestions
 Use meaningful identifier names
 Use blank lines between procedures
 Use indentation and spacing to align instructions and comments
 Use tabs to indent instructions, but do not indent labels
 Align the comments that appear after the instructions
Szabist Larkana Campus 3
Intrinsic Data Types
 Assembler recognizes a basic set of intrinsic data types
 Describe types in terms of their size (byte, word, doubleword, and so on).

 BYTE, SBYTE  REAL4


 8-bit unsigned integer  IEEE single-precision float
 8-bit signed integer
 Occupies 4 bytes
 WORD, SWORD
 16-bit unsigned integer
 REAL8
 16-bit signed integer  IEEE double-precision
 DWORD, SDWORD  Occupies 8 bytes
 32-bit unsigned integer  REAL10
 32-bit signed integer
 IEEE extended-precision
 QWORD, TBYTE
 64-bit integer
 Occupies 10 bytes
 80-bit integer

Szabist Larkana Campus 4


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] . . .

val1 BYTE 10
 All initializers become binary data in memory
 Above example declares variable having name ‘val1’ of size Byte/8bits and
assigns value 10.

Szabist Larkana Campus 5


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 (will be assigned a value at runtime:

The DB directive can also define an 8-bit variable


val1 DB 255 ; unsigned byte
val2 DB -128 ; signed byte

• MASM does not prevent you from initializing a BYTE with a negative value, but it's considered poor style.
• If you declare a SBYTE variable, the Microsoft debugger will automatically display its value in decimal with a leading
sign.
Szabist Larkana Campus 6
Adding a Variable to the AddTwo Program
(Lab Work)
; AddTwoSum.asm - Chapter 3 example
.386
.model flat, stdcall
.stack 4096

.data
sum DWORD 0 Variable name “sum” of type double word having value 0

.code
main PROC
mov eax, 5
add eax, 6
mov sum, eax Moving data value from eax register to variable sum

main ENDP
END main
 The x86 instruction set does not let us add one variable directly to another (sum1+sum2 => ADD
sum1, sum2).
 But, it does allow a variable to be added to a register (SUM1+EAX => ADD EAX, SUM1).

Szabist Larkana Campus 7


Open a Watch window in VS2012
 Select Windows from the Debug menu (during a debugging session)
 Select Watch, and select one of the four available choices (Watch1,
Watch2, Watch3, or Watch4).
 Then, highlight the sum variable with the mouse and drag it into the Watch window.

Szabist Larkana Campus 8


Multiple Initializers OR Defining Byte Arrays

 In case of multiple initializers used in the same data definition.


 its label refers only to the offset of the first initializer.

 Example: list BYTE 10,20,30,40

 list is located at offset 0000, the value 10 is at offset 0000, 20 is at


offset 0001, 30 is at offset 0002, and 40 is at offset 0003.

Memory layout of a byte sequence.

Szabist Larkana Campus 9


Multiple Initializers OR Defining Byte Arrays Cont..

To continue the array of bytes begun with list, we can define additional bytes on
the next lines:
Example:
list BYTE 10,20,30,40
BYTE 50,60,70,80
BYTE 81,82,83,84

Other Examples that use multiple initializers

list3 BYTE ?,32,41h,00100010b

list4 BYTE 0Ah,20h,'A',22h

Szabist Larkana Campus 10


Defining Strings
 A string is implemented as an array of characters
 For convenience, it is usually enclosed in quotation marks “ ” OR ‘ ’
 It is often terminated with a NULL char (byte value = 0)
 Each character uses a byte of storage.
 are an exception to the rule that byte values must be separated by commas.

str1 BYTE "Enter your name", 0


str2 BYTE 'Error: halting program', 0
str3 BYTE 'A','E','I','O','U'
greeting BYTE "Welcome to the Encryption "
BYTE "Demo Program", 0

Szabist Larkana Campus 11


Defining Strings Cont..
 A string can be divided between multiple lines without having to supply a
label for each line:
 greeting1 BYTE "Welcome to the Encryption Demo program "
BYTE "created by Kip Irvine.", 0dh, 0ah
BYTE "If you wish to modify this program, please
"
BYTE "send me a copy.", 0dh,0ah,0
 The hexadecimal codes 0Dh and 0Ah are alternately called CR/LF (carriage-return
line-feed) or end-of-line characters.
 The line continuation character (\) concatenates two source code lines into a
single statement.
 It must be the last character on the line
 greeting1 \
BYTE "Welcome to the Encryption Demo program "

Szabist Larkana Campus 12


Multiple initializers or defining array
.386
.model flat, stdcall
.stack
.data
list1 BYTE 10, 20, 30, 40
BYTE 50, 60, 70, 80
BYTE 91, 91, 92, 93

list2 BYTE ?, 32, 41H, 'A', 1100101B


str1 BYTE "WELCOME"
str2 BYTE "HOW ARE YOU"
greeting1 BYTE "Welcome to the Demo program "
BYTE "created by BSCS4.",0dh,0ah
BYTE "If you wish to modify this program, please "
BYTE "send me a copy.",0dh,0ah,0
.code
main PROC

main ENDP
END main
Szabist Larkana Campus 13
DUP Operator
 Allocates storage for multiple data items using a integer expression.
 Likewise a counter.
 It is particularly useful when allocating space for a string or array.
 can be used with initialized or uninitialized data:
 Examples
Var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero
var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized
var3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK"

Szabist Larkana Campus 14


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
 Following Example myList starts at offset 0000. The addresses increment
by 2 because each value occupies 2 bytes
myList WORD 1,2,3,4,5

Szabist Larkana Campus 15


Defining DWORD and SDWORD Data

 The DWORD directive (define doubleword) and SDWORD directive


(define signed doubleword) allocate storage for one or more 32-
bit integers.
 val1 DWORD 12345678h ; unsigned
val2 SDWORD −2147483648 ; signed
val3 DWORD 20 DUP(?) ; unsigned array
 The legacy DD directive can also be used to define doubleword
data.
val1 DD 12345678h ; unsigned
val2 DD −2147483648 ; signed
 Array of 32-bit (Double Word)
 myList DWORD 1,2,3,4,5

 Figure shows that offset increases by 4

Szabist Larkana Campus 16


(Symbolic Constants) Equal-Sign (=) Directive

 It is created by associating an identifier (a symbol) with either an integer


expression or some text.
 Does not use any storage.
 Used only during the assembly of a program, hence cannot change at
runtime.
name = expression
COUNT = 500
mov ax, COUNT
 Can be redefined
COUNT = 5
moy al, COUNT ;AL 5
COUNT = 10
moy al, COUNT ;AL 10
COUNT = 100
moy aI, COUNT ;AL 100

Szabist Larkana Campus 17


EQU Directive
 name EQU expression expression must be a valid integer expression
 name EQU symbol symbol is an existing symbol name, already
defined with = or EQU
 name EQU <text> any text may appear within the brackets <. . .>

 useful when defining a value that does not evaluate to an integer.


 No Redefinition

 PI EQU <3.1416>
 Examples
matrix1 EQU 10 * 10
matrix2 EQU <10 * 10>

.data
M1 WORD matrix1 M1 WORD 100
M2 WORD matrix2 M2 WORD 10 * 10
Szabist Larkana Campus 18
TEXTEQU Directive

 Creates a text macro.


 There are three different formats:
 name TEXTEQU <text> assigns text
name TEXTEQU textmacro assigns the contents of an existing text macro
name TEXTEQU %constExpr third assigns a constant integer expression

 EXAMPLE
 rowSize = 5
count TEXTEQU %(rowSize * 2)
move TEXTEQU <mov>
setupAL TEXTEQU <move al,count>

 Therefore, the statement setupAL would be assembled as


mov al,10

Szabist Larkana Campus 19


Using symbolic constants
;using symbolic constants
.386
.model flat, stdcall
.stack
esckay=27
count=1
pi EQU <9.8>
presskey EQU <"Press any key to continue...",0>
array1 EQU 10*10
array2 EQU <10*10>
.data
prompt BYTE presskey
a1 BYTE array1
a2 BYTE array2
.code
main PROC
main ENDP
end main
Szabist Larkana Campus 20
Calculating the Sizes of Arrays and Strings

 Manually list BYTE 10,20,30,40


ListSize = 4
 Let the assembler calculate its value for you.
 The $ operator (current location counter) returns the offset.
 In the following example, ListSize is calculated by subtracting the offset of
list from the current location counter ($)
 list BYTE 10,20,30,40
ListSize = ($ - list)
 ListSize must follow immediately after list.
 Following code produces too large a value (24) for ListSize because the storage
used by var2
list BYTE 10,20,30,40
var2 BYTE 20 DUP(?)
ListSize = ($ - list)
Szabist Larkana Campus 21
Calculating size of arrays
calculating size of arrays
;

.386
.model flat, stdcall
.stack
include irvine32.inc

.data
list1 BYTE 10,20,30,40
listSize = ($-list1)

.code
main PROC
mov ax, listSize
call DumpRegs

main ENDP
END main

Szabist Larkana Campus 22


Lab Work
 Create assembly language programs using following registers:
 Al, ah, bl, bh, eax, ebx
 Apply arithmetic operations for decimal, binary, hexadecimal and octal numbers
 Declare variables in your programs of different types:
 Assign values from registers to variables
 Display results of variables in Watch window.
 Use BYTE, SBYTE, WORD, SWORD, SDWORD, QWORD, TBYTE
 Use $ operator to calculate array size of strings.
 DO MISTAKES IN CODING AND TRY TO LEARN!!
 Try store greater value in any variable than its capacity!!!
 Symbolic Integer Constants
 Write a program that defines symbolic constants for all seven days of the week.
Create an array variable that uses the symbols as initializers.

Szabist Larkana Campus 23

You might also like