kaizen cso002 l1
DESCRIPTION
TRANSCRIPT
CX323L1
COMPUTER SYSTEM ORGANIZATION
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: 523410
103 = 1000 102 = 100 101 = 10 100 = 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: 1101012
positional powers of 2: 25 24 23 22 21 20
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: 1101012
positional powers of 2: 25 24 23 22 21 20
decimal positional value: 32 16 8 4 2 1
binary number: 1 1 0 1 0 1
32 + 16 + 4 + 1 = 5310
Ex: 1010112
positional powers of 2: 25 24 23 22 21 20
decimal positional value: binary number:
Ex: 1010112
positional powers of 2: 25 24 23 22 21 20
decimal positional value: 32 16 8 4 2 1binary number: 1 0 1 0 1 1
32 + 8 + 2 + 1 = 4310
The Division Method. Divide by 2 until you reach zero, and then collect the remainders in reverse. Ex 1: 5610 = 1110002
2 ) 56 Rem:2 ) 28 02 ) 14 02 ) 7 02 ) 3 12 ) 1 1 0 1
Ex 2: 3510 =
2 ) Rem:2 ) 2 ) 2 ) 2 ) 2 )
Answer: 3510 = 2
Character Representation
Ex: Find the binary ASCII and decimal ASCII values for the ‘&’ character.
Rightmost Leftmost Three BitsFour Bits 000 001 010 011 100 101 110 1110000 NUL DLE Space 0 @ P ` p0001 SOH DC1 ! 1 A Q a q0010 STX DC2 " 2 B R b r0011 ETX DC3 # 3 C S c s0100 EOT DC4 $ 4 D T d t0101 ENQ NAK % 5 E U e u0110 ACK SYN && 6 F V f v0111 BEL ETB ' 7 G W g w1000 BS CAN ( 8 H X h x1001 HT EM ) 9 I Y I y1010 LF SUB * : J Z j z1011 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:
01001102 = 32 + 4 + 2 = 3810
Therefore:
‘&’ = 38 (decimal ASCII value)
Octal Numbering systems
• Base: 8• Digits: 0, 1, 2, 3, 4, 5, 6, 7• Octal number: 12468
powers of : 84 83 82 81 80
decimal value: 4096 512 64 8 1Octal 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: 12468
positional powers of 8: 83 82 81 80
decimal positional value: 512 64 8 1Octal number: 1 2 4 6
512 + 128 + 32 + 6 = 67810
Ex: 103528
positional powers of 8: 84 83 82 81 80
decimal positional value: Octal number:
The Division Method. Divide by 8 until you reach zero, and then collect the remainders in reverse. Ex 1: 433010 = 103528
8 ) 4330 Rem:8 ) 541 28 ) 67 58 ) 8 38 ) 1 0
0 1
Ex 2: 81010 =
8 ) 810 Rem:8 ) 8 ) 8 )
Answer: 81010 = 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: 1F416
powers of : 164 163 162 161 160
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: 1F416
positional powers of 16: 163 162 161 160
decimal positional value: 4096 256 16 1Hexadecimal number: 1 F 4
256 + 240 + 4 = 50010
Ex: 7E16
positional powers of 16: 163 162 161 160
decimal positional value: Hexa number:
• The Division Method. Divide by 16 until you reach zero, and then collect the remainders in reverse.
Ex 1: 12610 = 7E16
16) 126 Rem:
16) 7 14=E
0 7
Ex 2: 81010 =
16 ) 810 Rem:16 ) 16 )
Answer: 81010 = 16
Binary to Octal Conversion
• Since the maximum value represented in 3 bit is equal to:
23 – 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 7428 = 2
7 = 1114 = 1002 = 010
7428 = 111 100 0102
Ex :Convert 101001102 = 8
110 = 6100 = 4010 = 2 ( pad empty digits with 0)
101001102 = 2468
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 3D916 = 2
3 = 0011 D = 1101 9 = 1001
3D916 = 0011 1101 10012
Ex :Convert 101001102 = 16
0110 = 61010 = A
101001102 = A616
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 E8A16 = 8
1110 1000 10102
111 010 001 010 (group by 3 bits)
7 2 1 2
E8A16 = 72178
Ex :Convert 7528 = 16
111 101 0102 (group by 4 bits)
0001 1110 1010
1 E A
7528 = 1EA16
Ex :Convert 7528 = 16
111 101 0102 (group by 4 bits)
0001 1110 1010
1 E A
7528 = 1EA16
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
Computer’s 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 OrganizationBit – 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 210 equals, 1,024, which happens to be the value K, for kilobyte.
• Megabyte (MB) – The number 220 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 AXINC 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 AXDEC 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 COMPILERMachine 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 INTERPRETERMachine language
Instruction
Assembly language
Source Program ASSEMBLERMachine 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.ASMDOSSEG. MODEL SMALL. STACK 0100h. DATA
X DB “Hello everyone! $”. CODE
MOV AX, @DATAMOV DS, AXMOV AH, 09hMOV DX, OFFSET XMOV AX, 4C00hINT 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 programDOSSEG. MODEL SMALL. STACK 0100h. DATA. CODE
MOV AX, @DATAMOV DS, AXMOV AH, 02h ; request display
characterMOV DL, ‘A’ ; character to
displayMOV AX, 4C00h ; endINT 21h
END
Displaying character (lowercase ‘b’) twice on screen.
TITLE MYPROG.ASM ; name of the programDOSSEG. MODEL SMALL. STACK 0100h. DATA. CODE
MOV AX, @DATAMOV DS, AXMOV AH, 02h ; request display characterMOV DL, ‘b’ ; character to displayINT 21h ; call interrupt serviceINT 21h ; call interrupt serviceMOV AX, 4C00h ; endINT 21h
END
• Using control characters (0A – Line Feed and 0D – Carriage return) in a screen display.
TITLE MYPROG.ASM ; name of the programDOSSEG. MODEL SMALL. STACK 0100h. DATA. CODE
MOV AX, @DATAMOV DS, AXMOV AH, 02h ; request display characterMOV DL, ‘T’ ; character to displayINT 21h ; call interrupt serviceMOV DL, 0Ah ; line feed characterINT 21h ; call interrupt serviceMOV DL, 0DH ; carriage return characterINT 21h ; call interrupt serviceMOV DL, ‘I’ ; character ‘I’INT 21h ; call interrupt serviceMOV DL, 0Ah ; line feed characterINT 21h ; call interrupt serviceMOV DL, 0Dh ;carriage return characterINT 21h ; call interrupt serviceMOV DL, ‘P’ ; character ‘P’INT 21h ; call interrupt serviceMOV AX, 4C00h ; endINT 21h
END
• Displaying different characters (‘A to ‘Z’) using LOOP on the same line.
TITLE MYPROG.ASMDOSSEG. MODEL SMALL. STACK 0100h. DATA. CODE
MOV AX,@DATAMOV DS,AXMOV CX,001Ah ; setting the number of loopsMOV AH,02h ; request display characterMOV DL,‘A’ ; character ‘A’
B: INT 21h ; call interrupt serviceINC DL ; add to 1 to the value of DLLOOP B ; go to label BMOV AX,4C00h ; endINT 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 Instructions1. DATA TRANSFER
To move data in the memory and in the CPU registers.
Format: Memory to register Register to registerRegister to stackStack to register
Syntax: Mov Destiny, SourceExample: 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 OPERATIONSSyntax: AND destiny,sourceExample: And ax,bx
AND: Logical ANDNOT: Logical NOTOR: Logical ORXOR: Exclusive XOR
5. INPUT/OUTPUTSyntax: OUT destiny,sourceExample: in ax,dx
IN: Input byte or wordINSn: Input stringOUT: Output byte or wordOUTSn: Output string
6. JUMP(conditional)Syntax: ja labelExample: ja start
JA: Jump if AboveJAE: Jump if above/equalJB: Jump if belowJBE: Jump if below/equalJE: Jump if EqualJG: Jump if GreaterJGE: Jump if Greater/EqualJL: Jump if LessJNE: 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 as”su-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 ModelMEMORY 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 person’s 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 “Mary’s 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 ProgramTITLE 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
. CODEMOV AX, @DATAMOV DS, AXMOV AH, 02h ; request display
characterMOV DL, ‘j’ ; character to displayINT 21h ; call interrupt serviceINT 21h ; call interrupt serviceMOV AX, 4C00h ; endINT 21h
END
Character InputInt 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 doesn’t 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
.CODEMOV AX,@DATAMOV DS, AXMOV AH,09HMOV DX,OFFSET MSG1INT 21H
MOV AH,01H ;REQUEST FOR CHARACTER INPUTINT 21HMOV X,AL
MOV AH,09HMOV DX,OFFSET MSG2INT 21H
MOV AH,02HMOV DL,XINT 21H
MOV AX,4C00HINT 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, “I’m glad to meet you ! $”
. CODEMOV AX, @DATAMOV DS, AXMOV AH, 09hMOV DX, OFFSET QMOV AH, 0Ah ; request for string inputMOV DX, OFFSET NEYM ; load addressINT 21hMOV AH, 09hMOV DX, OFFSET A1INT 21hMOV AH, 09hMOV DX, OFFSET NEYM+2INT 21hMOV DX, OFFSET A2INT 21hMOV AX, 4C00hINT 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
. DATANEYM DB 08h, ?, 08h DUP (“$”)Q DB “What is your name? $”A1 DB 0Ah, 0Dh, “What a nice name ! $”A2 db 0Ah, 0Dh, “I’m glad to meet you ! $”
. CODEMOV AX, @DATAMOV DS, AXMOV AH, 09hMOV DX, OFFSET QMOV AH, 0Ah ; request for string inputMOV DX, OFFSET NEYM ; load addressINT 21hMOV AH, 09hMOV DX, OFFSET A1INT 21hMOV AH, 09hMOV DX, OFFSET NEYM+2INT 21hMOV DX, OFFSET A2INT 21hMOV AX, 4C00hINT 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 / 02Used to set the cursor position.
AH = 02
MOV AH, 02h ; request to set the cursor positionMOV BH, 002h ; page number 0MOV DH, 0Ah ; Row = 10 in decimalMOV DL, 08h ; Column = 8 in decimalINT 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,@DATAMOV DS,AXMOV 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 ServiceMOV AH,02h ; request to set the cursor positionMOV BH,00h ; page number 0MOV DH,0Ch ; Row = 12 in decimalMOV DL,24h ; Column = 36 in decimalINT 10h ; Call interrupt service
MOV AH,09H ; request display string LEA DX,A ; load address of the string
MOV AX, 4C00hINT 21h
END
GraphicsTEXT AND BACKGROUND COLORS
BACKGROUND COLOR TEXT COLOR HEX CODE MEANINGBL 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 ColorBlinking
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 don’t 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 SubdirectoryThis 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 = 39HDS:DX = points to ASCIIZIf 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 SubdirectoryThis 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 = 3AHDS:DX = points to ASCIIZ stringIF 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 DirectoryThis 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 don’t want to overwrite an existing file.
INT 21H / 3CH
Create File
AH = 3CH
DS:DX = points to ASCIIZ string
CX = attribute
File AttributesBIT 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 don’t 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 PROCCALL P1CALL P2CALL QUITPROGRAM
MAIN ENDP
CALLING PROCEDURE
To call a procedure
Format:
CALL <procedure-name>
Example:.model small.codeorg 100h
start: jmp mainx db “assembly$”y db “language$”z db “program$”
main proc nearmov dx,offset xcall proc1call proc2mov dx,offset ycall proc 1call proc2mov dx,offset zcall proc1call proc2
main endp
proc2 proc nearmov ah,2mov dl,13int 21hmov dl,10int 21hret
proc2 endp
proc1 proc nearmov ah,9int 21hret
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.codeorg 100h
start: mov cx,5 x: mov ah,1
int 21hadd al,20hpush axloop x
mov ah,2mov dl,13int 21hmov dl,10int 21hmov cx, 5
y: pop dxint 21hloop yint 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
.codeorg 100hstart:mov cx,20
mov ah,2mov dl, ‘j’
z: int 21hloop 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 +39HTotal 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 72Clears 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 NUMBERSExample 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 ten’s 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 09AND AL, 0FH ; Convert CL to 05 0005 09MUL CL ; Multiply AL by CL 002D 09AAM ; 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 07AND AX,0FH0FH ; Convert to unpacked BCD 0208AAD ; Convert to binary 00ICDIV 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, exitXOR DX,DX ; Clear upper quotientDIV CX ; Divided by 10OR DL,30HMOV [SI],DL ; Store ASCII characterDEC SIJMP L20
L30:OR AL,30H ;Store last quotientMOV [SI], AL ; as ASCII character
C20: mov al,[SI] ;get a byteShr al,04 ;shift off right hex digitLea bx,xlatab ;set table addressXlat ; translate hexStosb ;store AL in DISPAREAMov al,[SI] ;get other half of byteAnd al,0fh ;clear left hex digitXlat ;translate hexStosb ;store AL in DISPAREAInc SI ; incr address of IOBUFFERLea bx,IOBUFFER+512 ;Cmp SI,BX ;end of IOBUFFER?Jb c20 ;if no, repeatLea bp,disparea ;if yes, displayMov cx,1024 ;hex charactersCall D10DISPLAYret
C10CONVRT endp
; display data:
; - - - - - - - - - ;uses AX, BX, DXD10DISPLAY 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 ConstructA 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>
ComparisonC 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 don’t forget to check the complexity and eventual completeness of your output.
Objective:
• To develop programs that apply the whole concepts of assembly programming.
Final Examinationand
Case Presentation
ENDEND