CSO002L1

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 280

CSO 002L1

COMPUTER SYSTEM
ORGANIZATION WITH
ASSEMBLY LANGUAGE
COMPUTER SYSTEM
ORGANIZATION
Prelim Midterm Finals
Kaizen 2006 - 2007
TERMS
PRELIM
Week 1 Microprocessor
Week 2 Basic Structure of hardware and
Software
Week 3 Register
Week 4 System Operation
Week 5 - Editor
Microprocessors
Topics:
1.Microprocessor
2.History of Microprocessors
3.Types of Microprocessors
4.Number System
* Decimal
* Binary
* Octal
* Hexadecimal

Objectives
To define what is a microprocessor.
To discuss the history of microprocessor.
Identify the types of microprocessor
To recall numbers systems.

Microprocessors
The brain or engine of the PC is
the processor (sometimes
called microprocessor), or
central processing unit (CPU).
The CPU performs the system's
calculating and processing.

Brief History
Intel is generally credited with creating the first microprocessor in
1971 with the introduction of a chip called the 4004.

All PC-compatible systems use either Intel processors or Intel-
compatible processors from a handful of competitors
Example: AMD or Cyrix.

Late 1970s the two most popular processors for PCs were not from Intel
(although one was a clone of an Intel processor). Personal computers of
that time primarily used the Z-80 by Zilog and the 6502 by MOS
Technologies.

Z-80 was noted for being an improved and less expensive clone of the
Intel 8080 processor, similar to the way companies today such as AMD,
Cyrix, IDT, and Rise Technologies have cloned Intel's Pentium
processors.

Types of Microprocessor
Processors can broadly be divided into the categories of:
CISC, RISC, hybrid, and special purpose.

Complex Instruction Set Computers (CISC)

have a large instruction set, with hardware support for a
wide variety of operations. In scientific, engineering, and
mathematical operations with hand coded assembly
language (and some business applications with hand
coded assembly language), CISC processors usually
perform the most work in the shortest time.

Reduced Instruction Set Computers (RISC)
have a small, compact instruction set. In most business
applications and in programs created by compilers from
high level language source, RISC processors usually
perform the most work in the shortest time.

Hybrid
processors are some combination of CISC and RISC
approaches, attempting to balance the advantages of
each approach.

Special purpose
processors are optimized to perform specific functions.
Digital signal processors and various kinds of co-
processors are the most common kinds of special
purpose processors.

Function of Microprocessor
To coordinate the functioning of the
various components of the system by
generating and responding to control
signals and to perform various arithmetic
and logic functions.
Number Systems
Decimal, Binary, Octal and Hexadecimal
Numeric Systems.
Conversions among the different
systems
Decimal Numbering systems

Base: 10
Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9


Example: 5234
10


10
3
= 1000 10
2
= 100 10
1
= 10 10
0

= 1
5 2 3 4

5,234 = 5 x 1000 + 2 x 100 + 3 x 10 + 4 x
1

Binary Numbering systems
Base: 2
Digits: 0, 1
binary number: 110101
2

positional powers of 2: 2
5
2
4
2
3
2
2
2
1
2
0

decimal positional value: 32 16 8 4 2 1
binary number: 1 1 0 1 0 1
Binary to Decimal
Conversion
To convert to base 10, add all the values
where a one digit occurs.
Ex: 110101
2

positional powers of 2: 2
5
2
4
2
3
2
2
2
1
2
0

decimal positional value: 32 16 8 4 2 1
binary number: 1 1 0 1 0 1
32 + 16 + 4 + 1 = 53
10

Ex: 101011
2

positional powers of 2: 2
5
2
4
2
3
2
2
2
1

2
0

decimal positional value:
binary number:

Ex: 101011
2

positional powers of 2: 2
5
2
4
2
3
2
2
2
1

2
0

decimal positional value: 32 16 8 4
2 1
binary number: 1 0 1 0 1
1
32 + 8 + 2 + 1 = 43
10

The Division Method. Divide by 2 until you reach zero,
and then collect the remainders in reverse.
Ex 1: 56
10
= 111000
2


2 ) 56 Rem:
2 ) 28 0
2 ) 14 0
2 ) 7 0
2 ) 3 1
2 ) 1 1
0 1

Ex 2: 35
10
=

2 ) Rem:
2 )
2 )
2 )
2 )
2 )
Answer: 35
10
=
2



Character Representation
Ex: Find the binary ASCII and decimal ASCII values for
the & character.


Rightmost Leftmost Three Bits
Four Bits 000 001 010 011 100 101 110 111
0000 NUL DLE Space 0 @ P ` p
0001 SOH DC1 ! 1 A Q a q
0010 STX DC2 " 2 B R b r
0011 ETX DC3 # 3 C S c s
0100 EOT DC4 $ 4 D T d t
0101 ENQ NAK % 5 E U e u
0110 ACK SYN & 6 F V f v
0111 BEL ETB ' 7 G W g w
1000 BS CAN ( 8 H X h x
1001 HT EM ) 9 I Y I y
1010 LF SUB * : J Z j z
1011 VT ESC + ; K [ k {
1100 FF FS , < L \ l |
1101 CR GS - = M ] m }
1110 SO RS . > N ^ n ~
1111 SI US / ? O _ o DEL

Character Representation
ASCII Table
From the chart:
& = 0100110 (binary ASCII value)

Convert the binary value to decimal:
0100110
2
= 32 + 4 + 2 = 38
10


Therefore:
& = 38 (decimal ASCII value)

Octal Numbering systems
Base: 8
Digits: 0, 1, 2, 3, 4, 5, 6, 7
Octal number: 1246
8

powers of : 8
4
8
3
8
2
8
1
8
0

decimal value: 4096 512 64 8 1
Octal number: 1 2 4 6

Octal to Decimal
Conversion
To convert to base 10, beginning with the
rightmost digit multiply each nth digit by
8
(n-1)
, and add all of the results together.

Ex: 1246
8

positional powers of 8: 8
3
8
2
8
1
8
0

decimal positional value: 512 64 8 1
Octal number: 1 2 4 6
512 + 128 + 32 + 6 = 678
10

Ex: 10352
8

positional powers of 8: 8
4
8
3
8
2
8
1

8
0

decimal positional value:
Octal number:

The Division Method. Divide by 8 until you
reach zero, and then collect the remainders in
reverse.
Ex 1: 4330
10
= 10352
8


8 ) 4330 Rem:
8 ) 541 2
8 ) 67 5
8 ) 8 3
8 ) 1 0
0 1


Ex 2: 810
10
=

8 ) 810 Rem:
8 )
8 )
8 )


Answer: 810
10
=
8



Hexadecimal Numbering
systems
Base: 16
Digits: 0, 1, 2, 3, 4, 5, 6, 7,8,9,A,B,C,D,E,F
Hexadecimal number: 1F4
16

powers of : 16
4
16
3
16
2
16
1
16
0

decimal value: 65536 4096 256 16 1
Hexadecimal number: 1 F 4
Four-bit Group Decimal Digit Hexadecimal Digit
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F

Hexa to Decimal Conversion
To convert to base 10, beginning with the
rightmost digit multiply each nth digit by
16
(n-1)
, and add all of the results together.

Ex: 1F4
16

positional powers of 16: 16
3
16
2
16
1
16
0

decimal positional value: 4096 256 16 1
Hexadecimal number: 1 F 4
256 + 240 + 4 = 500
10

Ex: 7E
16

positional powers of 16: 16
3
16
2
16
1

16
0

decimal positional value:
Hexa number:

The Division Method. Divide by 16 until
you reach zero, and then collect the
remainders in reverse.
Ex 1: 126
10
= 7E
16

16) 126 Rem:
16) 7 14=E
0 7


Ex 2: 810
10
=

16 ) 810 Rem:
16 )
16 )


Answer: 810
10
=
16



Binary to Octal Conversion
Since the maximum value represented in 3 bit is equal
to:
2
3
1 = 7

i.e. using 3 bits we can represent values from 0 7
which are the digits of the Octal numbering system.

Thus, three binary digits can be converted to one
octal digit and visa versa.


Three-bit Group Decimal Digit Octal Digit
000 0 0
001 1 1
010 2 2
011 3 3
100 4 4
101 5 5
110 6 6
111 7 7
Ex :
Convert 742
8
=
2


7 = 111
4 = 100
2 = 010

742
8
= 111 100 010
2
Ex :
Convert 10100110
2
=
8


110 = 6
100 = 4
010 = 2 ( pad empty digits with 0)

10100110
2
= 246
8
Four-bit Group Decimal Digit Hexadecimal Digit
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F

Ex :
Convert 3D9
16
=
2


3 = 0011
D = 1101
9 = 1001

3D9
16
= 0011 1101 1001
2
Ex :
Convert 10100110
2
=
16


0110 = 6
1010 = A

10100110
2
= A6
16
To convert between Octal to Hexadecimal
numbering systems and visa versa convert
from one system to binary first then convert
from binary to the new numbering system

Ex :
Convert E8A
16
=
8

1110 1000 1010
2

111 010 001 010 (group by 3
bits)

7 2 1 2

E8A
16
= 7217
8


Ex :
Convert 752
8
=
16

111 101 010
2
(group by 4 bits)

0001 1110 1010

1 E A

752
8
= 1EA
16


Ex :
Convert 752
8
=
16

111 101 010
2
(group by 4 bits)

0001 1110 1010

1 E A

752
8
= 1EA
16


Basic Structure of hardware and
Software
Topics:

Internal Hardware Features
External Hardware Features
Program Organization
Memory Organization
Basics of Assembly Programming
SK Editor
Using Debug


Objectives
To familiarize with the basic structure of
hardware and software
To discuss programming model and
memory organization
To discuss assembly programming using
debug.
Basic Structure of Hardware
and Software
Hardware is the physical part of the
computer system

Software - A program or set of instructions
that controls the operation of a computer.
Distinguished from the actual hardware of the
computer.


Internal Hardware Features
1. Processor
2. Memory
3. Registers
External Hardware Features
Computers Input/Output
example:
1. Keyboard
2. Monitor
3. Disk
4. CD-ROM

Program Organization
Software consists of the operating system and
various programs and data files stored on disk.
To execute (or run) a program:
1. the system copies it from external device
into internal memory.
2. the processor executes the program
instructions.
3. The registers handle the requested
arithmetic, data movement, and addressing.
Memory Organization
Bit the fundamental building block of
computer storage
- off means 0, on means 1

Bytes consists of eight bits for data and one
bit for parity



0 0 0 0 0 0 0 0 1
Data bits
parity
According to the rule of parity, the number of bits
that are on in each byte must always be odd.
Related Bytes
A program can treat a group of one or
more related bytes as a unit of data, such
as time or distance. A group of bytes that
defines a particular value is commonly
known as a data item or field.

Word a 2-byte (16-bit) data item
Doubleword A 4-byte (32-bit) data item
Quadword An 8-byte (64-bit) data item
Paragraph a 16-byte (128 bit) area
Kilobyte (KB) The number 2
10
equals,
1,024, which happens to be the value K,
for kilobyte.
Megabyte (MB) The number 2
20
equals
1,048,576, or 1 megabyte.
Basics of Assembly
Programming
Programming Language
A programming language is a notation for
expressing instructions to be carried out
by the computer. It is a medium of
communication between the human and
the machine, and often between one
human being and another.
Classification of programming
Languages
High-Level Language A high-level language is an English-like
language that provides for more natural expression of
algorithms.
Examples: BASIC, Pascal, COBOL, & Prolog

Mid-level Language It is a computer language which combines
low-level efficiency with high-level language structure,
abstraction and portability.
Examples: C & MODULO-2
Low-level Language A low-level language is considered to be
machine-oriented since it is closely follows the built in
instruction set of the underlying computer.
Examples: Assembly, machine languages.



Assembly Language
It is a low-level language that provides a
kind of shorthand notation called
mnemonic (memory aid) for the actual
machine language instruction. It is a much
easier to remember SUB AX<BX than
0010101111000011 which is the actual
instruction code that the machine uses.
SideKick Editor
It is a text editor used in coding the source
code. SideKick or SK editor can display
the ASCII code for coding assembly
language program.
Debug
BUG - It is a computer terminology for error or
mistake in a program or computer system and
the term debugging is a methodical process of
finding and reducing the number of bugs or
defects in a computer program.

DEBUGGER It is a program tool that provides
an environment for testing load modules. Load
modules are executable programs
Registers
Topics:
1. Register
2. General Purpose Registers
3. Pointer Registers
4. Segment Registers
5. Index Registers
6. Flag Registers




Objectives
1. To define what is a register.
2. Identify the types of registers.
3. Identify the function of each register.

Register Set

Registers are fast memory, almost
always connected to circuitry that allows
various arithmetic, logical, control, and
other manipulations, as well as possibly
setting internal flags.

General-Purpose Registers
AX - Accumulator Register mostly used for
calculations and for input/output


BX - Base Register Only register that can be used
as an index
AH AL
AX
EAX
BH BL
BX
EBX
CX - Count Register used for the loop
instruction



DX - Data Register input/output and used
by multiply and divide


CH CL
CX
ECX
DH DL
DX
EDX
Pointer Registers
IP - Instruction Pointer 16-bit number that
points to the offset of the next instruction
SP - Stack Pointer 16-bit number that points
to the offset that the stack is using
BP - Base Pointer used to pass data to and
from the stack

Segment Registers
CS - Code Segment 16-bit number that
points to the active code-segment
DS - Data Segment 16-bit number that points
to the active data-segment
SS - Stack Segment 16-bit number that
points to the active stack-segment
ES - Extra Segment 16-bit number that points
to the active extra-segment

Index Registers
SI - Source Index used by string operations as
source
DI - Destination Index used by string
operations as destination

Flag Registers
Abr.

Name

bit n

Description

OF Overflow
Flag
11 indicates an overflow when set
DF Direction
Flag
10 used for string operations to check direction
IF Interrupt
Flag
9 if set, interrupt are enabled, else disabled
TF Trap Flag 8 if set, CPU can work in single step mode
SF Sign Flag 7 if set, resulting number of calculation is negative
ZF Zero Flag 6 if set, resulting number of calculation is zero
AF Auxiliary
Carry
4 some sort of second carry flag
PF Parity Flag 2 indicates even or odd parity
CF Carry Flag 0 contains the left-most bit after calculations
System Operation

Topics:
Disk Operating System
Debug
Loading of Debug from DOS
Loading SK through DOS





Objectives
To recall common DOS commands.
To discuss the procedures in starting and
quitting Debug.
To familiarize with the Debug commands.
To know the rules in using debug
commands.





System Operation
Common Disk Operating System Commands

1. CLS - clear 5. DEL - delete
2. CD change dir 6. REN - rename
3. DIR - directory 7. MD make directory
4. COPY - copy 8. RD remove directory


DEBUG
Debug is a software that is classified as
debugger which is used for testing and
debugging executable programs.

- It displays all program code and data in
hexadecimal format and any data that you
enter into memory must also in hex form.
Advantages of Debug
1. It is free.
2. It is universally available.
3. It is simple to use.
4. It requires relatively little
memory.
Debug Commands
1. Q (Quit)
Finishes the Debug session and exits back to
DOS environment
Ex: -Q

2. H (Hexarithmetic)
Shows the sum and difference of two 4-bit
hexadecimal numbers, coded as H <hex
value> <hex value>
Ex: - 000C 0008
3. R (Register)
Allows you to display all registers and their
values. It also shows the next instruction
and permits you to change the value of a
particular register.
Ex: -R
-R CX

4. E (Enter)
Enables you to key in data or machine
instructions into memory beginning at a
specific location address.
Ex: -E 0200


5. D (Display or Dump)
Displays the content of a portion memory in
hex and ASCII forms starting with the
given address.
Ex: D 0200

6. A (Assemble)
Allows you to create program in mnemonic
or symbolic code. It also translates this
assembly source statements that you
create into machine code.
Ex: -A 0100


7. T (Trace)
Runs the program in a single-step mode. It
also displays the new values of the
registers and the next instructions to be
executed.
Ex: -T

8. G (Go)
Runs the program as a whole in memory and
displays the output.
Ex: -G

9. U (Unassemble)
Lists all the instructions contained in the program
beginning at the given address. You can also
specify the last address location.
Ex: -U 0100
-U 0100 0109

10. N (Name)
Gives a name to your program, coded as N
<path><filename>. The base name of the
filename must be eight characters long and
the extension name is .COM
Ex: N A:SAMPLE>COM

11. W (Write)
Saves the program onto the disk storage.

Ex: -W


Rules of Debug Commands
It is not case sensitive
It assumes that all numbers given are in
hexadecimal format.
You can enter a space only when it is
needed to separate parameters of a
particular command.
You should specify segments and
offsets with a colon, in the form
<segment>:<offset>.
Starting Debug
1. Key in the word debug and
press enter. A hyphen (-)
appears on the screen, that is
the DEBUG prompt. DEBUG
is now ready to accept
commands.

C:\>debug
-
Quitting and Closing Debug
To close Debug and go back to
DOS environment: type Q and
press ENTER key.

C:\>debug
-q
C:\>
Loading SK Through DOS
From the C:\> prompt type SK then the
SideKick main menu will appear. From
the menu select Notepad or press F2.

C:\>sk


Basic Assembly Instructions Used in
Debug
1. MOV (Move data)
It copies and transfers data between two
registers, or between an immediate data to a
register.

Format: MOV <register>,<register>
MOV <register>, <immediate data>




Example: MOV AX,BX
MOV CX, 5083
MOV CL,DL
MOV BL,33

2. ADD (Add Data)
It is used to get the sum of two registers or a
register and an immediate data, and
stores the result to the left most register.


Format: ADD <register>,<register>
ADD <register>,<immediate data>

Example: ADD CX,BX
ADD AX,0308
ADD AL,BL
ADD DH, 95

3. SUB (Subtract Data)
It is used to get the difference of two registers or
a register and an immediate data, and stores
the result to the left most register.
Format: SUB <register>,<register>
SUB <register>,<immediate data>
Example: SUB CX,BX
SUB AX,0308
SUB AL,BL
SUB CL,95
3. MUL (Multiply Data)
It is used to get the product of the given register
and AX register, and stores the result to AX
register. If the product is greater than 16 bits,
the overflow is stored in DX register.
Format: MUL <register>

Example: MUL CX


5. DIV (Divide Data)
It is used to divide the value of a given register
and AX register, and stores the quotient to AX
and the remainder to DX registers
respectively.

Format: DIV <register>

Example: DIV BX
6. INC (Increment by one)
It is used to increase the value of the register by
one (1).

Format: INC <register>

Example: INC AX
INC CH

7. DEC (Decrement by one)
The opposite of INC, instead of increasing, it
decreases the value of the register by one
(1)

Format: DEC <register>

Example: DEC AX
DEC CH

8. LOOP (Loop Until Complete)
It controls the execution of a program segment in
a specified number of times. The CX register
should contain a count value before starting
the loop and automatically decrements by one
(1). If CX is not equal to zero (0), it transfers
to its operand address which points to the
start of the loop; otherwise it drops through to
the next instruction.
Format: LOOP <offset address>

Example: LOOP 0108
Sample Debug Programs
Displaying a smiley character on the screen.

-A 0100
1358:0100 mov ah,02 ;request display character
1358:0102 mov dl,01 ;character to display
1358:0104 int 21 ;call interrupt service
1358:0106 int 20 ;end
13587:008
-G


Program terminated normally
-
Using control characters (0A Line Feed and 0D Carriage Return) in
a screen display.
-A 0100
1358:0100 MOV AH, 02 ;request display character
1358:0102 MOV DL,54 ;character T
1358:0104 INT 21 ;call interrupt service
1358:0106 MOV DL, 0A ;line feed character
1358:0108 INT 21 ;call interrupt service
1358:010A MOV DL,0D ;carriage return character
1358:010C INT 21 ;call interrupt service
1358:010E MOV DL,49 ;character I
1358:0110 INT 21 ;call interrupt service
1358:0112 MOV DL,0A ;line feed character
1358:0114 INT 21 ;call interrupt service
1358:0106 MOV DL,0D ;carriage return character
1358:0118 INT 21 ;call interrupt service
1358:011A MOV DL,50 ;character P
1358:011C INT 21 ;call interrupt service
1358:011E INT 20 ;end


Displaying the same character (lowercase z) thirty times using
loop on the screen.

-A 0100
1358:0100 MOV CX,001E ;SETTING THE NUMBER OF LOOOPS
1358:0103 MOV AH,2 ;REQUEST DISPLAY CHARACTER
1358:0105 MOV DL,7A ;CHARCTER TO DISPLAY
1358:0107 INT 21 ;CALL INTERRUPT SERVICE
1358:0109 LOOP 0107 ;GO TO OFFSET 0107
1358:010B INT 20 ;END
Editor
Topics:
1. Editor
2. Types of language translator
3. Advantages of assembly language

Objectives
To identify the different editors used in
assembly language.
Differentiate the different types of
language translator.
Enumerate the advantages of assembly
language.

Editor
Text Editors used in Turbo Assembler

1. Edit

2. Sidekick

3. Pilot


Turbo Assembler
Types of language
Translator



1. Compiler Is a translator program that
transforms an entire source program
composed of high/mid-level language
statements into an object program
consisting of machine language
executable code.
High/Mid-level language
Source Program
COMPILER
Machine language
Instruction
2. Interpreter processes the source program on a
statement-by-statement basis. It translates
high/mid-level language statements and then
executes it one statement at a time.



2. Assembler is a language translator that
transforms assembly language to machine
language instruction.

High/Mid-level language
Source Program
INTERPRETER
Machine language
Instruction
Assembly language
Source Program
ASSEMBLER
Machine language
Instruction
Assembly Language

It is a low-level language that provides a kind
of shorthand notation called
mnemonic(memory aid) for the actual
machine language instruction. It is much
easier to remember SUB, AX, BX than
00101111000011 which is the actual
instruction code that machine uses

Advantages of Assembly
language
It provides more control over
handling particular hardware
requirements.
It generates smaller, more compact
executable modules.
It results in faster execution.


Tasm Turbo Assembler


T-link t - Turbo Assembler Linker

Linker is a computer program for high, mid, and
low levels of programming language that
completes the process by converting the object
code into executable machine code.
STRUCTURE OF AN ASSEMBLY
LANGUAGE PROGRAM
TITLE MYPROG.ASM
DOSSEG
. MODEL SMALL
. STACK 0100h
. DATA
X DB Hello everyone! $
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 09h
MOV DX, OFFSET X
MOV AX, 4C00h
INT 21h
END
INSTRUCTIONS FOR
INITIALIZING THE PROGRAM
TITLE MYPROG.ASM
DOSSEG
. MODEL SMALL
. STACK 0100h
. DATA
. CODE
MOV AX, @DATA
MOV DS, AX
INSTRUCTIONS FOR
CONCLUDING THE PROGRAM
MOV AX, 4C00h
INT 21h
END
EXAMPLES OF ASSEMBLY LANGUAGE
PROGRAMS
Displaying a character (uppercase A) on screen.

TITLE MYPROG.ASM ; name of the program
DOSSEG
. MODEL SMALL
. STACK 0100h
. DATA
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 02h ; request display
character
MOV DL, A ; character to display
MOV AX, 4C00h ; end
INT 21h
END
Displaying character (lowercase b) twice on screen.


TITLE MYPROG.ASM ; name of the program
DOSSEG
. MODEL SMALL
. STACK 0100h
. DATA
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 02h ; request display character
MOV DL, b ; character to display
INT 21h ; call interrupt service
INT 21h ; call interrupt service
MOV AX, 4C00h ; end
INT 21h
END
Using control characters (0A Line Feed and 0D Carriage return) in a screen display.

TITLE MYPROG.ASM ; name of the program
DOSSEG
. MODEL SMALL
. STACK 0100h
. DATA
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 02h ; request display character
MOV DL, T ; character to display
INT 21h ; call interrupt service
MOV DL, 0Ah ; line feed character
INT 21h ; call interrupt service
MOV DL, 0DH ; carriage return character
INT 21h ; call interrupt service
MOV DL, I ; character I
INT 21h ; call interrupt service
MOV DL, 0Ah ; line feed character
INT 21h ; call interrupt service
MOV DL, 0Dh ;carriage return character
INT 21h ; call interrupt service
MOV DL, P ; character P
INT 21h ; call interrupt service
MOV AX, 4C00h ; end
INT 21h
END
Displaying different characters (A to Z) using LOOP on the
same line.

TITLE MYPROG.ASM
DOSSEG
. MODEL SMALL
. STACK 0100h
. DATA
. CODE
MOV AX,@DATA
MOV DS,AX
MOV CX,001Ah ; setting the number of loops
MOV AH,02h ; request display character
MOV DL,A ; character A
B: INT 21h ; call interrupt service
INC DL ; add to 1 to the value of DL
LOOP B ; go to label B
MOV AX,4C00h ; end
INT 21h
END
MIDTERM
Week 7- Instructions And Directives
Week 8 Introduction to Assembly Language
Week 9 Using SK Editor
Week 10 Keyboard handling
Week 11 Screen Handling
Instructions And Directives
Topics:
Instructions
Categories of Instructions
Directives
Types of Memory Model


Objectives
To familiarize with the different instructions.
To identify the categories of instructions
To identify the different directives and their
corresponding functions.
To identify the different memory modes.
Instructions
- These are statements that will
be translated into machine
language and executed by the
computer.
Examples of instructions
MOV ADD SUB
MUL DIV INC
DEC LOOP CMP

Categories of Instructions
1. DATA TRANSFER
To move data in the memory and in the CPU
registers.
Format:
Memory to register
Register to register
Register to stack
Stack to register
Syntax: Mov Destiny, Source
Example: mov ax,bx

Note: the list of symbolic instructions are for Intel processor
family

There are two very important
details to note about the mov
instruction
1. There is no memory to memory move
operation.

2. You cannot move immediate data into a
segment register.




LDS: Load Data Segment Register
LEA: Load Effective Address
LES Load Extra Segment Register
LODS: Load String
LSS: Load stack Segment Register
MOV: Move data
MOVS: Move String
MOVSX: Move with sign-extend
MOVZX: Move with Zero-extend
STOS: Store string
XCHG: Exchange
XLAT: Translate

2. ARITHMETIC

Syntax: Adc destiny, source
Example: Adc bh,bl

ADD: add binary numbers ADC: Add with carry

DEC: Decrement by 1 INC: Increment by 1

DIV: Unsigned Divide IDIV: Signed (integer) divide

MUL: Unsigned multiply IMUL: Signed multiply

SUB: Subtract binary values SBB: Subtract with borrow

XADD: exchange and add
3. COMPARISON
Syntax: CMP destiny, source
Example: cmp ax,bx

CMP: compare
CMPSn: Compare string
CMPXCHG: Compare and exchange


4. LOGICAL OPERATIONS
Syntax: AND destiny,source
Example: And ax,bx

AND: Logical AND
NOT: Logical NOT
OR: Logical OR
XOR: Exclusive XOR

5. INPUT/OUTPUT
Syntax: OUT destiny,source
Example: in ax,dx

IN: Input byte or word
INSn: Input string
OUT: Output byte or word
OUTSn: Output string
6. JUMP(conditional)
Syntax: ja label
Example: ja start

JA: Jump if Above
JAE: Jump if above/equal
JB: Jump if below
JBE: Jump if below/equal
JE: Jump if Equal
JG: Jump if Greater
JGE: Jump if Greater/Equal
JL: Jump if Less
JNE: Jump if Not Equal

JNG: Jump if Not Greater
JNGE: Jump if Not Greater/Equal
JNL: Jump if Not Less
JNLE: Jump if Not Less/Equal
JNZ: Jump if Not Zero
JLE: Jump if Less/Equal
JNA: Jump if Not Above
JNB: Jump if Not Below
JNBE: Jump if Not Below/Equal
JZ: Jump if Zero

JC: Jump if Carry
JCXZ: Jump if CX is Zero
JNO: Jump if No Overflow
JO: Jump if Overflow
JNP: Jump if No Parity
JP/JPO: Jump if Parity Odd
JPE: Jump if Parity Even
JS: Jump if Sign
JNS: Jump if No Sign



7. JUMP (Unconditional)

CALL: Call a procedure
INT: Interrupt
IRET: Interrupt Return
JMP: Unconditional Jump
RET: Return
RETN/RETF: Return Near/Return Far


8. LOOPING
Syntax: loop label
Example: loop a

LOOP: Loop until complete
LOOPE: Loop While Equal
LOOPZ: Loop While Zero
LOOPNE: Loop While Not Equal
LOOPNZ: Loop While Not Zero
9. STACK INSTRUCTIONS
Syntax: pop destiny
Example: pop ax

POP: Retrieve the content of the Stack
PUSH: Store the content of the Stack
POPF: Pop Flags off the Stack
PUSHF: Push Flags onto Stack
Directives
- These are statements that give information to
the assembler.
- Sometimes called pseudo-ops (short term for
pseudo operations and pronounced assu-da-
op).
- Unlike instructions, directives are not
translated is not machine language but they are
necessary for your program to assemble
properly.
Examples of Directives
TITLE DOSSEG .MODEL
.STACK .DATA .CODE
Most Common Directives
1. TITLE It creates a title (up to 60
characters) of a source listing.

Format:


Example: TITLE PROGRAM1.ASM

TITLE <TEXT>
2. DOSSEG It tells the
assembler to ignore all other
requests and to adopt the DOS
segment sequence stack,
data and code.
Format:


Example: DOSSEG
DOSSEG
3. .MODEL It specifies and initializes the memory
model before defining any segment.

Format:




Example: .MODEL TINY
.MODEL SMALL
.MODEL MEDIUM
.MODEL SMALL <MEMORY-MODEL>
Types of Memory Model
MEMORY MODEL NO. OF DATA SEGMENT
NO. OF CODE SEGMENT
Tiny 0 0
Small 1 1
Medium 1 More than 1
Compact More than 1 1
Large More than 1 More than 1
4. .STACK It defines the size of the stack.
The default size is 1,024 bytes which you
can overrule.

Format:



Example: .STACK 0100h
.STACK <SIZE>

5. .DATA It defines and marks
the beginning of data segment.

Format:



Example: .DATA
.DATA
6. .CODE It defines and marks
the code segment which
consists of a set of
instructions.
Format:



Example: .CODE
.CODE
7. END It is placed at the last
line of the source code.

Format:



Example: END
END
8. For Defining Data
DIRECTIVE LENGTH DESCRIPTION
DB 1 Define Byte
DW 2 Define Word
DD 4 Define Doubleword
DF 6 Define Farword
DQ 8 Define Quadword
DT 10 Define Tenbytes
Introduction to Assembly
Language
Topics:
Requirements for Coding Assembly
language
Reserved Words
Identifiers


Objectives
To know the requirements in coding
assembly language
To identify the different reserved words.
To know the categories of reserved words
To identify the two types of identifiers.
To know the general guidelines in coding
assembly program.

Introduction to Assembly
Language Using Turbo
Assembler
Requirements for Coding
Assembly language
Comment
-Is a part of a program that is ignored by the
assembler. Though Optional, the use of
comments throughout a program can improve
its readability and clarity especially in
assembly language where the purpose of a
set of instructions is often ambiguous.
-It begins with a semicolon(;)
Ways to include comment
Any Statement whose first non-blank character is a
semicolon.
Example: ; This program displays Good Day!

At the end of an instruction.
Example:
MOV AX, 8053h ; initializes the value of ax to 8053h
Reserved Words
These are words in which the assembler
assigns a special meaning and it cannot
be used as identifiers. They are reserved
for their own purpose to be used only
under special condition.
Categories of Reserved Words
Instructions
Directives
Operators
Pre-defined Symbols

Identifiers
An identifier is a user-defined
name (Variable) that you apply to
an item in your program that you
expect to reference.
Types of Identifiers
Name It refers to the address of a data item.
Example: x db 0
msg db Aloha! &
Label It refers to the address of an instruction or
procedure.
Example: mov dl, 41h
A: int 21h
add dl, 20h
int 21h
sub dl, 20h
loop A

Rules in Constructing Valid Identifier
It must be letters (A..Z, a..z), numbers (0-9)
&/or special characters like underscore(_),
question mark(?) and at sign (@).
* It must always start with a letter.
It must not use reserved words.
It must not exceed to 31 characters.
Examples of valid identifiers: neym u2u2
num_1 msg8
Examples of Invalid identifiers: title num-1
4ever F-4
Statement
An assembly language statement program
consists of a set of statements. The two
types of statements are instructions and
directives.
General Guidelines for coding statement
A statement may begin anywhere on the line
Each line can only contain one statement
Unlike C language, assembly is not case
sensitive. You can use either upper or
lowercase letters.
Take note that statements can have two (2)
operands, a single and even none at all.

Examples: ADD AX,BX ;uses 2 operands
DEC CX ; uses single operand
RET ; no operand
String
-is used for directive data such as persons
name or simply a message. It must end with
dollar ($) symbol and defined in double
quotation marks ( ). DB is the conventional
format for defining string of any length.
Example:

Neym db Marys Internet Caf $
Numeric Constant
-They are used to define arithmetic values and
memory addresses. It is identified with a radix
specifier such as d for decimal, b for binary and
h for hexadecimal.
Example:
Msg db Good day!, 0ah, 0dh, $
Is the same with
Msg db Good day!, 10d, 13d $
Msg db Good day!, 00001010b, 00001101b, $
Using SideKick

Assembling and Linking
Procedures
Topics:
Using SideKick
Creating Assembly Language
Structure of Assembly language
Assembling and Linking Assembly
Language

Objectives
To familiarize with the SideKick
Environment as text editor.
To know the procedures in invoking
Sidekick and quitting from the SideKick
window.
To know the procedures in creating and
saving assembly program.
To know the procedures in assembling,
linking and running assembly program.
Using Sidekick as Editor
Sidekick
- is an editor used for DOS
programming like assembly
language.
Creating Assembly Language
Program
Step 1: Open the text editor.
C:\tasm>SK
Step2: Type the name of the
program with .asm as file
extension name.
Example: c:program1.asm
Step3: You may now start typing
your source code.


Exiting from the Sidekick
Window
* Press escape (esc )key to go
back to DOS environment.
Structure of an Assembly Language Program
TITLE PROGRAM1.ASM
DOSSEG
.MODEL SMALL
.STACK
.DATA
X DB HELLO THERE!$
.CODE
MOV AX, @DATA
MOV DS,AX

MOV AH,O9H
MOV DX,OFFSET X
INT 21H

MOV AX,4C00H
INT 21H
END
Initializes the program
Concludes the program
Body of the program
Assembling and Linking
Procedures
Assembling:

From the c:\tasm> prompt type the following the
filename and its extension name then press enter
key.

Example: c:\tasm>program1.asm


Linking:
After a successful assembly (I.e., no errors
encountered), type tlink, press space bar,
filename and its extension name then
press enter key

Example: c:\tasm>tlink program1.obj

Executing:

To run the program, just type the filename
only at c:\tasm>.

Example: c:\tasm>program1
To recapitulate the overall process:
1. Open the text editor.
2. Save the program.
3. Start keying your program.
4. Exit the editor and go back to DOS environment by
pressing the Esc key.
5. Assemble the program by typing tasm
filename.asm.
6. If there are no errors, do the next step otherwise,
repeat steps 1-5 and rectify the program.
7. Link the object file by typing tlink filename.obj &
press enter key.
8. Type filename and press enter key to run the
program.

KEYBOARD HANDLING
Topics:

Interrupt and Service
Character Output
Character Input
String Display
String Input
Objectives
To identify the function/service number
used to input/output character.
To identify the function/service number
used to input/output string.
To identify the corresponding interrupt
used in character and string input/output.
Interrupt execute commands
Service set the command
Interrupt and Service
Character Output
Int 21h / 02
This function display one character on the
screen.

AH = 02h
DL = holds the character to display on the
screen.
Example:

. MODEL SMALL
. STACK 0100h
. DATA
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 02h ; request display character
MOV DL, j ; character to display
INT 21h ; call interrupt service
INT 21h ; call interrupt service
MOV AX, 4C00h ; end
INT 21h
END

Character Input
Int 21h / 1 Keyboard input
This function waits for you to
type a character on the keyboard. It
echoes the character to the screen and
returns the ASCII character in the AL
register.

AH = 01
AL = character read from the keyboard



Example:
.model small
.stack
.data
.code
mov ah, 01h ; request for character input
int 21h

Character Input
Int 21h / 7 keyboard input without echo
This function reads a character
from a keyboard but doesnt display the
character on the screen and wait for
control break.
AL = character read from the keyboard

Character Input
Int 21h / 8 keyboard input without echo
This function works the same
way with service 7 but do not respond to
control break.
AL = character read from the keyboard

Example:
.MODEL SMALL
.STACK 0100H
.DATA
MSG DB INPUT ANY CHARACTER: $
MSG2 DB 0AH, 0DH, The character you type is: $
x db 0
.CODE
MOV AX,@DATA
MOV DS, AX
MOV AH,09H
MOV DX,OFFSET MSG1
INT 21H

MOV AH,01H ;REQUEST FOR CHARACTER INPUT
INT 21H
MOV X,AL

MOV AH,09H
MOV DX,OFFSET MSG2
INT 21H

MOV AH,02H
MOV DL,X
INT 21H

MOV AX,4C00H
INT 21H
String Display
Int 21h / 09
This function displays the string pointed by
the DS:DX pair of registers. The end of
the string should be marked with the $
sign.

DS:DX points to address of the string
String Display
Int 21h / 09h
Display string in the data area followed by a
dollar sign ($ or 24h) delimiter, which the
operation uses to end the display.

Example:

Nick_name db Your nickname: $ ;display string
Example fragment of a program
MOV AH,09H ;request display
LEA DX, NICK_NAME ;load address of prompt
INT 21H ;call interrupt service
String Input
Int 21h / 0A
This function reads the string from the
keyboard.

DS:DX points to address of the string
Example:



. MODEL SMALL
. STACK 0100h
. DATA
NEYM DB 08h, ?, 08h DUP ($)
Q DB What is your name? $
A1 DB 0Ah, 0Dh, What a nice name ! $
A2 db 0Ah, 0Dh, Im glad to meet you ! $
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 09h
MOV DX, OFFSET Q
MOV AH, 0Ah ; request for string input
MOV DX, OFFSET NEYM ; load address
INT 21h
MOV AH, 09h
MOV DX, OFFSET A1
INT 21h
MOV AH, 09h
MOV DX, OFFSET NEYM+2
INT 21h
MOV DX, OFFSET A2
INT 21h
MOV AX, 4C00h
INT 21h
END
DISPLAYING THE INPUT STRING
The following instructions display the input string
starting at the third memory address.

MOV AH, 09h ; request for string output
MOV DX, OFFSET X+2 ; LOAD ADDRESS
INT 21h
Sample program that demonstrates
string and displays it again on screen.
. MODEL SMALL
. STACK 0100h
. DATA
NEYM DB 08h, ?, 08h DUP ($)
Q DB What is your name? $
A1 DB 0Ah, 0Dh, What a nice name ! $
A2 db 0Ah, 0Dh, Im glad to meet you ! $
. CODE
MOV AX, @DATA
MOV DS, AX
MOV AH, 09h
MOV DX, OFFSET Q
MOV AH, 0Ah ; request for string input
MOV DX, OFFSET NEYM ; load address
INT 21h
MOV AH, 09h
MOV DX, OFFSET A1
INT 21h
MOV AH, 09h
MOV DX, OFFSET NEYM+2
INT 21h
MOV DX, OFFSET A2
INT 21h
MOV AX, 4C00h
INT 21h
END

Screen Handling
Topics:

The Monitor

GRAPHICS

Objectives
To identify the instructions and directives
used for screen handling applications
To create an assembly program with the
application of graphics.

THE MONITOR

A typical video screen has eighty (80)
columns numbered from 0 to 79 and
twenty five (25) rows numbered from o to
24. The columns and rows provide a grid
of addressable locations at any one of
which the cursor can be set. The table
below shows the examples of a cursor
locations
Screen
Location
COLUMN ROW COLUMN ROW
In hexadecimal value In hexadecimal value
Upper Left
Corner
0 0 00H 00H
Upper Right
Corner
79 0 4FH 00H
Center of
Screen
40 12 28H 0CH
Lower Left
Corner
0 24 00H 18H
Lower Right
Corner
79 24 4FH 18H
Screen Features
Clearing the Screen in Assembly
Approach
Int 10h / 06h
Handles the process of clearing the
screen and scrolling.

AH = Function in 06H
AL = number of lines to scroll, or 00H for the full screen
BH = attribute value (color blinking etc.)
CX = starting row column
DX = ending row column

sample code shows how to create the
clear screen:


MOV AX,0600h ;AH=06h (Scroll), AL=00h (Full Screen)
MOV BH,07h ;0 (Black Background), 7 (White Text Color)
MOV CX,0000h ;CH=00h (Row), CL=00h (Column)
MOV DX,184Fh ;DH=18h (Row), DL=4Fh (Column)
INT 10h ;Call interrupt service

SETTING THE CURSOR
POSITION
Int 10h / 02
Used to set the cursor position.

AH = 02

MOV AH, 02h ; request to set the cursor position
MOV BH, 002h ; page number 0
MOV DH, 0Ah ; Row = 10 in decimal
MOV DL, 08h ; Column = 8 in decimal
INT 10h ; call interrupt service

Alphanumeric
DISPLAYING THE STRING FLOWERS AT THE CENTER OF THE SCREEN
.MODEL SMALL
.STACK 0100h
.DATA
A DB FLOWERS$
.CODE
MOV AX,@DATA
MOV DS,AX
MOV AX,0600h ; AH=00h (Scroll0, AL=00h (Full Screen)
MOV BH,07h ; 0 (Black Background), 7 (White Text Color)
MOV CX,0000h ; CH=00h (Row), CL 00h (Column)
MOV DX,184Fh ; DH=18h (Row), DL=4Fh (Column)
INT 10h ; Call interrupt Service
MOV AH,02h ; request to set the cursor position
MOV BH,00h ; page number 0
MOV DH,0Ch ; Row = 12 in decimal
MOV DL,24h ; Column = 36 in decimal
INT 10h ; Call interrupt service
MOV AH,09H ; request display string
LEA DX,A ; load address of the string
MOV AX, 4C00h
INT 21h
END

Graphics
TEXT AND BACKGROUND COLORS

BACKGROUND COLOR TEXT COLOR HEX CODE MEANING
BL R G B I R G B

0 0 1 0 1 0 0 0 28 Green Background Color
Gray Text Color


1 0 1 0 1 0 0 0 0A8 Green Background Color
Gray Black Color
Blinking

0 1 1 0 0 0 0 1 61 Brown Background Color
Blue Text Color

1 1 1 0 0 0 0 1 0E1 Brown Background Color
Blue Text Color
Blinking

DESIGNING PROGRAMS USING
PROCEDURE











Procedures make it possible to break a
large program down into smaller pieces so
that each piece can be shown to work
correctly without the reference to any of the
others.
DEFINING A PROCEDURE
Every procedure is defined using PROC and
ENDP directives. Since these are
directives, they dont generate machine
language instructions; rather, PROC and
ENDP are directions to the assembler,
making the beginning and end of a
procedure. In assembly, the first declared
procedure in the program is considered
the major calling procedure.
Format & Example
Format:

<procedure-name> PROC
<instruction-1>
<instruction-2>

<instruction-n>
<procedure-name> ENDP
Example:

MAIN PROC
CALL P1
CALL P2
CALL QUITPROGRAM
MAIN ENDP
CALLING PROCEDURE

To call a procedure

Format:
CALL <procedure-name>
FINALS
Week 13 File and Disk handling
Week 14 Sample Programs in Assembly Language
Week 15 Math Operation
Week 16 In-line Assembly
Week 17 Case Study
FILE AND DISK HANDLING
Topics:
Common DOS File Handle Services
File Attributes
Modes of Accessing Files

Objectives:
To identify and familiarize with the different
services for file and disk handling.
To create a program using the different
services in file and disk handling.
To identify the different file attributes
To apply the different modes of accessing
files.
Common DOS File Handle
Services


INT 21H / 39H

Create Subdirectory
This service creates a subdirectory as does
the system command MKDIR. To use it,
load DX with the address of an ASCIIZ
string containing the drive and directory
pathname.
AH = 39H
DS:DX = points to ASCIIZ
If CX = 1, AX has error

Example fragment of a program:
ASCstrg DB n:\pathname, 00H ;ASCIIZ string

MOV AH,39H ;request create subdirectory
LEA DX,ASCstrg ;Address of ASCIIZ string (DS:DX)
INT 21H
Remove Subdirectory
This service deletes a subdirectory, just as
does the system command RMDIR. Note
that you cannot delete the current (active)
directory or a subdirectory containing files.
Load DX with the address of an ASCIIZ
string containing the drive and directory
pathname.

AH = 3AH
DS:DX = points to ASCIIZ string
IF CX = 1, AX has error
INT 21H / 3AH

Example fragment of a program:
ASCstrg DB n:\pathname,00H ;ASCIIZ string

MOV AH,3AH ;request delete subdirectory
LEA DX,ASCStrg ;address of ASCIIZ (DS:DX)
INT 21H ;call interrupt service
INT 21H / 56H
Rename File or Directory
This service name a file or directory from
within a program. Load DX with the
address of an ASCIIZ string containing the
old drive, path and name of the file or
directory to be renamed. Load DI
(combined as ES:DI) with the address of
an ASCIIZ string containing the new drive,
path and name, with no wild cards.

Drive numbers, if used, must be the same in
both strings. Because the paths need not
be the same, the operation can both
rename a file and move it to another
directory on the same drive.
Example fragment of a program:
Oldstring db n:\oldpath\oldname,00h
Newstring db n:\newpath]newname,00h

MOV AH,56H ;request rename file/directory
LEA DX,Oldstring ;DS:DX
LEA DI,Newstring ;ES:DI
INT 21H / 5BH
Create a new File
This service creates a file only if the
named file does not already exist;
otherwise it is identical to function 3CH
(create file). You could use function 5BH
whenever you dont want to overwrite an
existing file.
INT 21H / 3CH
Create File

AH = 3CH
DS:DX = points to ASCIIZ string
CX = attribute
File Attributes
BIT ATTRIBUTE
0 Read only file
1 Hidden file
2 System file
3 Volume label
4 Subdirectory
5 Archive file
INT 21H / 43H
Get/Set File Attribute
You can use this operation either to get or
set a file attribute in the directory. This
operation requires the address of an
ASCIIZ string containing the drive, path
and filename for the requested file. (Or
use the default directory if no path is
given.)
Example fragment of a program:
ASCstrg db n:\pathname,00H ;ASCIIZ string

MOV AH,43H ;request
MOV AL,00H ;get attribute
LEA DX,ASCstrg ;ASCIIZ string (DS:DX)
INT 21H ;call interrupt service
INT 21H / 3BH
Change Directory
This service changes directory.

AH = 3BH
DS:DX = points to ASCIIZ string
If CX = 1, AX has error
Modes for Accessing Files
Access mode Means
0 Read Only
1 Write Only
2 Both Read and Write
INT 21H / 3DH
Open File
This function opens a file.

AH = 3DH
DS:DX = points to ASCIIZ string
AL = MODE
INT 21H / 3EH
Close File
This service closes a file.

AH = 3EH
BX = file handle
If CX = 1, AX has error
INT 21H / 41H
Delete File
This service deletes a file (but not read-
only) from within a program. Load the
address in DX of an ASCIIZ string
containing the device path and filename,
with no wild cards references.
Example fragment of a program:
ASCstrg db n:\pathname,00H ;ASCIIZ string

MOV AH,41H ;request delete file
LEA DX,ASCSTRG ;address of ASCIIZ string (DS:DX)
INT 21H ;call interrupt service
Sample Programs In Assembly
Language
Topic:
Designing programs using procedure
Basic Stack Instruction
Loop Instruction

Objectives:
Create an assembly program with
procedures.
Create an assembly program using stack
instruction.
Create an assembly program using loop
instruction.
DESIGNING PROGRAMS USING
PROCEDURE
DEFINING A PROCEDURE
Every procedure is defined using PROC and
ENDP directives. Since these are directives,
they dont generate machine language
instructions; rather, PROC and ENDP are
directions to the assembler, making the
beginning and end of a procedure. In assembly,
the first declared procedure in the program is
considered the major calling procedure.
Format:
<procedure-name> PROC
<instruction-1>
<instruction-2>

<instruction-n>
<procedure-name> ENDP

Example:

MAIN PROC
CALL P1
CALL P2
CALL QUITPROGRAM
MAIN ENDP
CALLING PROCEDURE

To call a procedure

Format:

CALL <procedure-name>

Example:
.model small
.code
org 100h

start: jmp main
x db assembly$
y db language$
z db program$

main proc near
mov dx,offset x
call proc1
call proc2
mov dx,offset y
call proc 1
call proc2
mov dx,offset z
call proc1
call proc2
main endp
proc2 proc near
mov ah,2
mov dl,13
int 21h
mov dl,10
int 21h
ret
proc2 endp

proc1 proc near
mov ah,9
int 21h
ret
proc1 endp

end start
Basic Stack Instruction
Push Instruction
puts/places data onto the stack segment

Pop Instruction
takes out data from the stack

Format:
push reg16
pop reg16
push reg32 (3)
pop reg32 (3)
push segreg
pop segreg (except CS) push memory
pop memory
The first two instructions push and pop a
16 bit general purpose register. This is a
compact (one byte) version designed
specifically for registers

The second pair of instructions push or
pop an 80386 32 bit general purpose
register.
The third pair of push/pop instructions let
you push or pop an 80x86 segment
register.

The fourth pair of push/pop instructions
allow you to push or pop the contents of a
memory location.

Example:
.model small
.code
org 100h

start: mov cx,5
x: mov ah,1
int 21h
add al,20h
push ax
loop x
mov ah,2
mov dl,13
int 21h
mov dl,10
int 21h
mov cx, 5
y: pop dx
int 21h
loop y
int 20h
end start
Loop Instruction
Loop allows a part of the program to be
repeated for several times, the number of
loops depend on the value of the CX
register.

- each time the loop is executed, CX is
decremented and the looping stops when
CX becomes zero.
Format:
LOOP label

Example:
LOOP start
This will cause the program flow
to go to the label start for CX times.
Example:
.model small
.code
org 100h
start:mov cx,20
mov ah,2
mov dl, j
z: int 21h
loop z
end start

Math Operation
BCD
ASCII
Objectives:
To create a program using addition,
subtraction, multiplication and division of
Binary Coded Decimal.
To create a program that converts the
addition, subtraction of ASCII codes to
BCD.
To identify the instructions and directives
for addition, subtraction of BCD.
BCD
binary coded decimals
Binary coded decimal (BCD) is a
method for implementing lossless decimal
arithmetic (including decimal fractions) on
a binary computer. The most obvious uses
involve money amounts where round-off
error from using binary approximations is
unacceptable. Some early computers used
BCD exclusively.


Decimal digits (0-9) can be encoded in a nibble
(half a byte), with some left over bit patterns
(hexadecimal A-F). In BCD operations, the
processor performs ordinary binary
computations, then adjusts the result to conform
to BCD.
For example, if you add the binary number 5 (bit
pattern 0101) to binary number 6 (bit pattern
0110), you get the binary result of 11 (bit pattern
1011, or hexadecimal B). With BCD arithmetic,
the processor would adjust the result to make it
into a valid BCD result (which in this case would
be bit pattern 0001 0001).

BCD arithmetic includes BCD addition, BCD
subtraction, BCD multiplication, BCD division, and
BCD negate.

The Intel 80x86 series uses a two step approach
for BCD arithmetic. Instead of having separate
BCD instructions, the normal binary addition and
subtraction instructions are used, then hardware
instructions are used to adjust the results to
correct BCD results.

There are instructions for both packed and
unpacked adjustments.

The advantage of this approach is greater
flexibility (more addressing modes and choices
of arithmetic operations because of the use of
regular binary integer instructions in the first
step).

The disadvantage of this approach is that it is
slower and takes more memory.

ASCII
American Standard Character Information
Interchange.

Refer to ASCII Table
Adding ASCII Numbers

Consider the effect of the following three examples of
adding ASCII numbers:


Ex. 1 35H Ex.2 38H Ex.3 39H
+32H 34H +39H
Total 67H 6CH 72H


Example 1.

The sum of 35h and 32h is 67h. AAA
checks the rightmost digit (7). Because it
is not between A and F, nor is the AF flag
set, AAA clears the leftmost digit (6) to 0.
Example 2. The sum of 38H and 34 H is 6CH.
Because the right most digit (C) is between A
and F, AAA performs the following:
AH AL
00 0C
Adds 6 to AL 00 72
Adds 1 to H 01 72
Clears leftmost digit of AL 01 02 (sum = 12)
Example 3. The sum of 39H and 39H is 72H.
Although the rightmost digit (2) is not between
A and F, the AF flag is set because of the carry
into the tens position. AAA performs the
following:
AH AL
00 72
Adds 6 to AL 00 78
Adds 1 to AH 01 78
Clears leftmost digit of AL 01 08 (sum = 18)
Subtracting ASCII NUMBERS
Example 1. ASCIIvalue1 contains 39h and ASCII value2
contains 39h. Subtracts ASCIIvalue2 (35h) from
ASCIIvalue1 (39h). AAS does not need to make an
adjustment because the rightmost hex digit is less than
the hex A.


AX AF CF
MOV AL,ASCVALUE1 ; 0039
SUB AL,ASCVALUE2 ; 0004 0 0
AAS ; 0004 0 0
OR AL,30h ; 0034
Example 2. Subtracts ASCIIvalue1 (39h) from ASCII value2
(35h). Because the rightmost digit of the result is hex C,
AAS subtracts 6 from AL, subtracts 1 from AH, and sets
the AF and CF flags:


AX AF CF
MOV AL,ASCVALUE2 ; 0035
SUB AL,ASCVALUE1 ; 00FC 1 1
AAS ; FFO6 1 1

The answer, which should be -4, is FF06h, its tens
complement; that is, decimal -10 + 6 = -4.
Multiplying ASCII Numbers
INSTRUCTION COMMENT AX CL
. . . ; Initial values 0035 39
AND CL, 0FH ; Convert CL to 09 0035 09
AND AL, 0FH ; Convert CL to 05 0005 09
MUL CL ; Multiply AL by CL 002D 09
AAM ; Convert AX to unpacked BCD
OR AX 3030H ; Convert AX to ASCII 3435
Dividing ASCII Numbers
INSTRUCTION COMMENT AX CL
. . . ; Initial values 3238 37
AND CL,0FH ; Convert to unpacked BCD 3238 07
AND AX,0FH0FH ; Convert to unpacked BCD 0208
AAD ; Convert to binary 00IC
DIV CL ; Divide by 7 0004
CONVERTING BINARY DATA TO
ASCII FORMAT
The following example converts binary number
0DF1H to ASCII format:

ASCVALUE DB 4 DUP ( ) ; Data Items
BINVALUE DW 0DF1H
. . .
MOV CX, 0010 ; Division factor
LEA SI, ASCVALUE ;Address of SCVALUE
MOV AX, BINALUE ; Get binary amount
L20:
CMP AX,CX ; Value < 10?
JB L30 ; yes, exit
XOR DX,DX ; Clear upper quotient
DIV CX ; Divided by 10
OR DL,30H
MOV [SI],DL ; Store ASCII character
DEC SI
JMP L20
L30:
OR AL,30H ;Store last quotient
MOV [SI], AL ; as ASCII character
C20: mov al,[SI] ;get a byte
Shr al,04 ;shift off right hex digit
Lea bx,xlatab ;set table address
Xlat ; translate hex
Stosb ;store AL in DISPAREA
Mov al,[SI] ;get other half of byte
And al,0fh ;clear left hex digit
Xlat ;translate hex
Stosb ;store AL in DISPAREA
Inc SI ; incr address of IOBUFFER
Lea bx,IOBUFFER+512 ;
Cmp SI,BX ;end of IOBUFFER?
Jb c20 ;if no, repeat
Lea bp,disparea ;if yes, display
Mov cx,1024 ;hex characters
Call D10DISPLAY
ret
C10CONVRT endp
; display data:
; - - - - - - - - - ;uses AX, BX, DX
D10DISPLAY proc near ;BP, CX set on entry
MOV AX,1301 ;request display
MOV BX,0016H ;page:attribute
MOV DX,0500H ;row:column
RET
D10DISPLAY end p
End A10MAIN

In-Line Assembly
Topic:
Comparison of C Language to
Assembly
Objective:
To construct a program with the
integration of Pascal and C language
programming.
Comparison of C Language To
Assembly Language
If..Else Construct
A conditional statement of C language in the form:

If <condition>
{
<statement-1>;
<statement-2>;
. . .
<statement-n>;
}
Else
{
<statement-1>;
<statement-2>;
. . .
<statement-n>;
}

Can be implemented by assembly language
of the form:

<cmp instruction>
<conditional jump instruction> <label-1>
<instruction-1>
<instruction-2>

<instruction-n>
<jump instruction> <label-2>
Comparison
C language Assembly language
If (ax == 0)
{
cx = cx ax;
ax = ax + 1;
}
Else
{
Cx = cx 8;
}
CMP AX,0000H
JNZ ACTION1
SUB CX,AX
INC AX
JMP NEXT

ACTION1: SUB CX,0008H

NEXT:
Do..While Construct
A do..while loop similar to C language:

do
{
<statement-1>;
<<statement-2>;

<statement-n>;
} while <condition>;



Can be implemented by assembly in the form:

<label>: <instruction-1>
<instruction-2>

<instruction-n>
<cmp instruction>
<conditional jump instruction> <label>

Comparison
C language Assembly language
ax = 5;
do
{
ax = ax 1;
bx = bx + 1;
} while (ax >= 0);
MOV AX,0005H
HERE: DEC AX
INC BX
CMP AX
JNZ HERE

While Construct
Similarly, C language while loop:

While <condition>
{
<statement-1>;
<statement-2>;

<statement-n>;
}
Is roughly equivalent to the assembly language
structure:
<label-1>: <cmp instruction>
<conditional jump instruction> <label>
<instruction-1>
<instruction-2>
. . .
<instruction-n>
jmp <label-1>
<label-2>: <instruction-1>
<instruction-2>
. . .
<instruction-n>
Comparison
C language Assembly language

while (ax <= 10)
{
ax = ax + bx;
bx = bx + 1;
}

START: CMP AX,000AH
JGE NEXT
ADD AX,BX
INC BX
JMP START

NEXT: ADD CX,AX


Final Case Study
Design a problem that will utilize
any of the constructs if-else, do-
while and while. Please dont
forget to check the complexity
and eventual completeness of
your output.
Objective:
To develop programs that apply the whole
concepts of assembly programming.

Final Examination
and
Case Presentation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy