You are on page 1of 86

Computer System Organization

Compilation of Lectures

Course Syllabus
Subject Code: CX323L1 Code: Descriptive Title: Computer System Organization Title: Credit Units: Lecture 3; Laboratory 1 Units: Pre-Requisite: Pre-Requisite: Logic Circuit and Switching Theory Course Description: This course covers: information in computers, covers: instruction sequencing and execution, addressing, instruction formats; formats; study of small computer systems and recent developments. developments.

Course Syllabus
General Objectives: At the end of the course, the students are expected to:  Understand how the computer system works  Provide a practical approach in assembly language programming  Appreciate the importance of the assembly language in manipulating the computer hardware  Gain basic knowledge in interfacing using assembly language

Course Syllabus

PRELIM 1 2 3 4 5 6 Orientation Number System: Conversion and Operations Computer System Structure Introduction to Assembly Programming Debug Instruction Prelim Examination

Course Syllabus
MIDTERM 7 8 9 10 11 12 FINALS 13 14 15 16 17 18 Logic Instruction Introduction to Parallel Port Interfacing Status Port Port Control Project Presentation Final Examination

Introduction to Turbo Assembler String Manipulation Stack Instruction Control Instruction Screen Handling Instruction Midterm Examination

Number System
Number System Base/Radix Symbols



Decim l


Hex decim l

-9; A-F

I. Integral Part
A. Decimal to Binary 1. 2. 3. 4. Divide the decimal number by the radix of the desired number system List down the remainder Repeat steps 1 and 2 until the quotient becomes zero (0) Generate the final answer from the list of remainders from bottom to top

EXAMPLES: Decimal to Binary 1. 4110 = 1010012 Quotient 41/2 20 20/2 10 10/2 5 5/2 2 2/2 1 1/2 0

Remainders 1 0 0 1 0 1

2. 5310 = 1101012 Quotient 53/2 26 26/2 13 13/2 6 6/2 3 3/2 1 1/2 0 Remainders 1 0 1 0 1 1

Decimal to Hexadecimal 1. 4110 = 2916 Quotient 41/16 2 2/16 0 2. 5310 = 3510 Quotient 53/16 3 3/16 0

Remainders 9 2

Remainders 5 3

II. Fractional Part
1. Multiply the fraction by the base of the desired number system 2. List down the integral part 3. Repeat steps 1 and 2 until the integral part becomes zero (0) 4. Convert the final answer from the list of the integral part from top to bottom


EXAMPLES: A. Decimal to Binary 1. 0.937510 = 0.11112 product .9375 *2 1.875 .875*2 1.75 .75*2 1.5 .5*2 1.0 2. 0.87510 = 0.1112 product .875*2 1.75 .75*2 1.5 .5*2 1.0

integral part 1 1 1 1

integral part 1 1 1


B. Any Number System to Decimal 1. Multiply each digit of the given number by its increasing/ decreasing positional value 2. Add the products to get the final answer
EXAMPLES: Binary to Decimal 1. 101111.1012 = 47.625 1*2-3= 0.125 1*2-1= 0.5 1*20=1 1*21=2 1*22=4 1*23=8 1*25=32 47.625


C. Binary To Hexadecimal 1. Partition the bits into groups of four from the binary point going from left to right 2. Assign the equivalent hex-digit for each group. hexEXAMPLE: Binary to Hexadecimal 1 B E . A 8 1. 1 1011 1110. 1010 12 = 1BE.A816 1 D E F . D C 2. 1 1101 1110 1111. 1101 112 = 1DEF.DC16


D. Hexadecimal to Binary 1.Convert each digit of the given hexadecimal to it's binary equivalent.
EXAMPLE: Hexadecimal to Binary 1100 0000 1110. 1110 1110 1.C O E .E E 16 = 110000001110.111011102 1101 1110 1010 1111 . 1011 1110 1110 2.D E A F . B E E 16 = 1101111010101111.1011111011102

Addition of Binary Numbers

0+0 = 0 0+1 = 1 1+0 = 1 1 + 1 = 0 carry 1 Example: 1011.101 + 11.11 1001.011


Subtraction of Binary Numbers

0-0 0-1 1-0 1-1 Example: 10111.111 - 1010.010 1101.101 = = = = 0 1 borrow 1 1 0


rs (radix complement) = 2's complement r-1s (diminished radix) = 1's complement 1's complement obtained by subtracting the bit from 1 (inverse) Example: 10101 = 01010 1's complement


2's complement obtained by taking the 1's complement then adding 1 to the least significant bit (LSB) of the complemented number Example: 10101 = 01010 1's complement + 1 01011 2's complement


Subtraction using the 1s Complements

1. Take the 1's complement of the subtrahend then add it to the minuend. 2. If there is an end carry in the sum, add it to the LSB of the sum, otherwise get the 1's complement and prefix a negative (-) sign. (Example: 10111.111 - 1101.101 =

10111.111 + 10010.010 01010.001 + 1 01010.010


Subtraction using the 2s Complements

1. Get the two's complement of the subtrahend then add it to the minuend 2. If there is an end carry in the sum, disregard, otherwise take the 2's complement of the sum then a prefix a negative sign (-) (Example: 10111.111 - 1101.101 =

10010.010 + 1 10010.011 10111.111 (1)01010.010


Microcomputer Architecture







General Purpose Registers

AX register accumulator; input and output; arithmetic operations Ex.: AX=8035h AH=80h AL=35h BX register base register; can be used as an index to extend addressing; memory pointer Ex.: BX=0C5h BH=0Ch Bl=05h CX register - counter register; performs operations register; repeatedly; repeatedly; contain a value to control the number of times a loop is repeated Ex.: CX=B1F2h CH=B1h CL=F2h DX register data register; hold data to any purpose Ex.: DX=0123 DH=01h DL=23h

Pointer Registers
BP Register This 16 bit base pointer register facilitates referencing parameters, which are the data and the addresses that a program passes through the stack. stack. IP Register This 16 bit instruction pointer register contains the offset address of the next instruction that is to execute. It is associated execute. with the CS register in that the IP indicates the currents instruction within the currently executing code segment. segment. SP Register This 16 bit stack pointer register provides an offset value with out when associated with the SS register, refers to the current word being processed in the stack. Both the BP and SP registers stack. are associated with the SS register and permit the system to access data in the stack segment. segment.

Segment Registers
memory known as the current segment
- register that provide for addressing an area of

CS Register Code segment register contains the starting address of a program segment. This segment plus an offset value segment. in the IP register indicates the address of an instruction to be fetched for execution. execution. DS Register Data segment register contains the starting address of a programs data segment. Instruction use this address segment. to locate data. data.

Segment Registers
ES Register Extra segment register used by some string operations to handle memory addressing. Associated addressing. with DI register. register. Stack Segment (SS) Register Permits the implementation of a stack in memory that a program uses for temporary storage of addresses and data. The system stores the starting data. address of a programs stack segment in the SS register. register.


Index Registers
SI Register - 16 bit source index register is required for some string operations associated with the DS register. DI Register - 16 bit destination index register is also required for some string operations and is associated with the CS register.


Flag Registers
Special registers that indicate the present status of the computer of the results of processing. Many processing. instructions involving comparison and arithmetic change. change. The status of the flags which some instructions may test to determine subsequent action. action. 1. Overflow Flag (OF) Indicates that the result of an operation is too long to be stored in the destination operand.
OV of there is an overflow NV no overflow Ex. mov a1, 04 add a1, 7F int 20

Flag Registers
2. DF (Direction) Used in string operations to determine the direction of data transfer.
UP upward DN downward

3. Interrupt (IF) Indicates that all external interrupts such as keyboard entry are to be processed or ignored.
EI enabled DI disabled Ex. mov ah, 02 mov dl, 41 int 21 int 20


Flag Registers
4. SF (Sign) Indicates that the result of an operation is positive/negative
PL plus
Ex. mov al, 03 sub al, 08 int 20

NG negative

5. Zero (ZF) Indicates whether an operation produced a zero result.

ZR zero
Ex. mov ax, 0008 sub ax, 0008 int 20

NZ non zero


Flag Registers
6. Auxiliary Carry (AF) Similar to carry flag except that it indicates the presence or absence of a carry or borrowed based on a 4 bit numeric representation in bits 0,1,2 and 3.
NA no auxiliary carry AC no auxiliary carry Ex. mov al, fl sub al, 01 int 20

7. Parity (PF) Indicates whether the result of an operation contains an even number or add number of 1s.
PE parity even PO parity odd

Flag Registers
Ex. mov al, 08 add al, 02 int 20

8. Carry Flag (CF) Indicates whether the instructions produced a value that can be too big or too small to be hold in the specified negative or memory location.
CY carry (N(Y) No no carry Ex. mov al, FF sub al, 01 int 20

Basic Assembly Instruction used in Debug

1. mov (move data) It copies and transfers data between two registers, or between an immediate data a register. Format: mov <register>, <register> mov <register>,<immediate data> Ex. mov ax, bx mov cx, 5083 mov cl, dl mov bl, 33

Basic Assembly Instruction used in Debug

2. add (Add data) It is used to get the sum of 2 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> Ex. add cx, bx ;cx = cx+bx add ax, 0308 ;ax = ax+0308 add al, bl ;al = al+bl add dh, 85 ;dh = dh+85

Basic Assembly Instruction used in Debug

3. SUB (subtract data) Used to get the difference of two register of an immediate data and stores the result to the left most register. Format: SUB<register>, <register> SUB<register>, <immediate data> Ex. sub cx, bx sub ax, 0308 sub al, bl sub cl, 85 ;cx = cx-bx cx;ax = ax-0308 ax;al = al - bl ;cl = cl 85

Basic Assembly Instruction used in Debug

4. MUL (multiply) It is use to get the product of a 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> ; cx:ax = cx*ax Ex. mul cx mul cl ;ax = cl*al 5. DIV (divide data) Use to divide the value of a given register and AX register and stores the quotient to AX and remainder to DX register. 36

Basic Assembly Instruction used in Debug

Format: DIV <register> Ex. DIV BX ;DX:AX=DX:AX/BX

6. INC (increment by 1) Used to increase the value of register by 1. Format: INC<register> Ex. INC AX INC CH

Basic Assembly Instruction used in Debug

7. DEC (decrement by 1) Decreases the value by 1 Format: DEC<register> Ex. DEC AX DEC CH


Basic Assembly Instruction used in Debug

8. Loop Controls the execution of program segment in a specified number of times the CX register should contain a count value before starting the loop. Format: LOOP<offset address> Ex. Loop 0108


Debug Commands
1. Q (Quit) Finishes the debug session and exits back to dos environment. Ex. Q 2. H (Hexarithmetic) Shows the sum and different of two 4-digit 4hexadecimal number. Coded as H<hex value><hex value> Ex. H 000C 0008 3. R (Register) Allows you to display all registers and their values. Ex. R -R CX


Debug Commands
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 contents 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 Ex. A 0100


Debug Commands
7. T (Trace) Runs the program in 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


Debug Commands
9. U (Unassemble) List all the instruction contained in the program beginning at the given address Ex. U 0100 -U 0100 0109 10. N (Name) Gives a name to a program the base name of the filename must be 8 characters long and the extension name is complete. Ex. N A 11. W (Write) Solves the program onto disk stage. Ex. W


Introduction to Assembly Language

Programming Language - a notation for expressing instructions to be carried out by the computer. CLASSIFICATION: HIGH LEVEL - an English like language that provides for more natural expression of algorithm. MID LEVEL - computer language which combines low level efficiency with high level language structure abstraction and portability. LOW LEVEL - considered to be machine oriented since it closely follows to the built in instruction set of under laying 44 computer.

Language Translator
Language Translator

a language processor. a software that accepts source program added in high / mid language as input and produces the equivalent machine language.


Types of Language Translator

1. Compiler - transform an entire source program. Composed of high level statements into an object program. Consisting of machine language executable code. COMPILER high level

COMPILER machine language

2. Interpreter - processes the source program on a statement by statement basis. It translate high level language statements and then executes it one statement at a time. INTERPRETER high level INTERPRETER machine language



LINKER -a computer program for high, mid, low levels of programming language that completes the process by connecting the object code into executable machine code.


Assembly Language
ASSEMBLY LANGUAGE -a low level language that provides a kind of short hand notation called mnemonic (memory aid) for actual machine language instruction.


Requisites for Coding Assembly Language Program

1. Comment -part of a program that is ignored by the assembler. -it can improve readability and clarity -optional -begins with a semicolon (;)
Examples: add ax,bx mov dx,8053h ; initializes the value of the 8053h

2. Reserved Words -words that have special meaning in the assembler


1. Instructions -statement that will be translated into machine language and executed by the computer. 2. Directives -statement that give information to the assembler not translated into machine language Example : .stack .model .data .code

3. Operators -use at assembly time to affect the value of an operand. Example: +, -, *, /

4. Predefined Symbols -symbols that return information to your program Example : data @model

5. Identifier -user defined name (variable) that you apply to an item in your program that you expect to references.

Types of Identifier
1. Name - refers to the address of data item 2. Label -refers to the address of an instruction or procedure.


Rules in Constructing a Valid Identifier

Must start with a letter.  Consists of letters, numbers, special character like (-) (?) ((@).  Must not use reserved words.  Must not exceed 31 Characters.

Example : num1 r2d2 num2 msg8


a statement may begin anywhere in the program each line can only contain 1 statement not case sensitive a statement can have 2 operands, a single operand or none at all Example: add ax,bx dec cx ret


1.Title - creates a title up to 60 characters of a source 1.Title listing Example: Title this program displays 'kumusta' Title Prog1.asm 2. Model - specifies and initializes the memory model before defining any segment Example: .model tiny .model small .model medium


Types of Memory Model

Memory Model Tiny Small Medium Compact Large Number of Data Segment 0 1 1 More than 1 More than 1 Number of Code Segment 0 1 More than 1 1 More than 1


Sample Program
.model small ; initializes the memory model .stack ; .stack directives that directs the assembler to use stack .code ; .code directives that directs the assembler to use code begin ; label that denotes the body of the program mov ah,02h ; function that lets you print a character on a screen mov dl,' T ' ; assigns character ' T ' to register dl int 21h ; makes a function call mov dl, ' I int 21h mov dl, ' P ' int 21h mov ah,4ch ; program termination int 21h end begin ; ends the body of the program


Turbo Assembler
Turbo Assembler must contain the following : 1. Tasm.exe to assemble the program to create an object module Tlink.exe to create an executable program Edit workspace to create the source program Rtm.exe

2. 3. 4.


How to run a program:

1. Assemble C:\ C:\Tasm > Tasm prog1.asm input output *.asm -> *.obj 2. Link C:\ C:\Tasm > tlink prog1.obj input output *.obj -> *.exe 3. Run C:\ C:\Tasm > prog1.exe

Function 09
function that displays a string of characters Example Program .model small .stack .data msg1 db TIP-Manila$ TIPmsg2 db1338 Arlegui St. Quiapo, Manila$ .code begin: mov ax, @data ; assigning data items in ax mov ds,ax ;data items should be in ds register

Function 09
mov ah,09h mov dx,offset msg1 ;loading the offset of ;the msg1 in the dx ;register int 21h mov ah,09h mov dx,offset msg2 int 21h mov ah,4ch int 21h end begin


Screen Location
Screen Location Upper Left Corner Upper Right Corner Center of the Screen Lower Left Corner Lower Right Corner Column Number 0 79 40 0 79 Row Number 0 0 12 24 24


Int 10h
use as a video function use to manipulate your output on the screen Example Program .model small .stack 0100h .code begin: mov ax,0600h ;ah=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


Int 10h
mov ah,02h mov bh,00h mov dh,0ch mov dl,28h int 10h mov ah,02h mov dl,'x' int 21h mov ax,4c00h int 21h end begin ;request to set the cursor position ;page number (0) ;row=12 in decimal ;column=40 in decimal ;call interrupt service


Character Input
mov ah, 01h int 21h mov ah, 07h int 21h ;request for character input ;without echo ;request for character input ;with echo


Sample Program
;this program accepts any character typed from the keyboard .model small .stack .code begin: mov ah, 02h mov dh, 12 mov dl, 40 int 10h mov ah, 01h int 21h mov ah, 4ch int 21h end begin


The CMP Instruction

- used to compare character data as well as numeric data fields. The main use of CMP is in decision making in conjunction with conditional jump instructions. Format: CMP <register>, <register> CMP <register>, <immediate data>

Example: CMP AL, 'Y' CMP DX, 0000h


Conditional Jump Instructions

- instructions that transfer control depending on the setting of CMP instruction whether a certain condition is met or not. Format: Example <conditional jump> <label> CMP AL, Y JE TAMA CMP AL, N JE MALI TAMA: mov AL,09h mov dx,offset msg1 int 21h


Conditional Jump Instructions

Instruction JE JNE JL JLE JG JGE JZ JZE Description Jump if equal Jump if not equal Jump if less than Jump if less than or equal to Jump if greater than Jump if greater than or equal to Jump if zero Jump if not zero

Unconditional Jump
- JMP is considered unconditional jump instruction because it does not depend on a condition being true or false.
Format: Example: JMP <label> CMP AL, Y JE TAMA CMP AL, N JE MALI JMP TAPOS

TAMA: mov ah, 09h mov dx,offset msg1 int 21h TAPOS: mov ax, 4ch int 21hs


DBDB-25 Pin Configuration

2 5

2 4

2 3

2 2


Types of Ports
Data Port output port (2-9) (2Status Port input port (10, 11, 12, 13, 15) Control Port I/O port (1, 14, 16, 17) (8-25 GND) (872

Port Addresses

Data Status Control

378h 379h (Base + 1) 37Ah (Base + 2)


Data Port
Bit 0 1 2 3 4 6 7 6 7 8 9

Pin 2 3 4

Four Fundamental Bit Operations

turn off certain bits (set to 0) turn on certain bits (set to 1) complement certain bits (0 to 1, 1 to 0) complement all bits


Turning off particular bits

Syntax: and operand1, operand2 al = 10010110b and al, 11110000b 10010110 11110000 al = 10010000


Turning on particular bits

Syntax: or operand1, operand2 al = 10101100b or al, 11110000b 10101100 11110000 al = 11111100


Complementing particular bits

Syntax: xor operand1, operand2 al = 00111100b xor al, 11110000b 00111100 11110000 al = 11001100


Basic Shift Instructions: SHL & SHR

Shift bits to the left and right respectively The bits that are shifted out are last The last bit to be shifted out is always kept in the carry flag cf; spaces are filled with 0s


Basic Shift Instructions: SHL & SHR

Name SHL SAL SHR SAR ROL ROR RCL RCR Shift Left Shift Arithmetic Left Shift Right Shift Arithmetic Right Rotate Left Rotate Right Rotate w/ Carry Left Rotate w/ Carry Right


Basic Shift Instructions: SHL & SHR

DL = 11001100

DL = 00011001 CF = 1

DL = 01100000 CF = 0

Basic Shift Instructions: SHL & SHR SAL = same as SHL SAR = duplicates the leftmost bit as you to the right Ex. SAR DL, 2 00001111 00000011 SAR BL, 2 11110001 11111100


Rotate Instructions
With the shift instruction, bits are shifted out are last the empty spaces are filled with 0s (1s) With rotate instruction, bits from continuous loop as bit is shifted into the opposite side


Rotate Instructions
ROR = moves bits to the right Ex. ROR AL, 1 00001111


CF = 1

CF = the last bit to be shifted out


Rotate Instructions
RCR RCL = same as ROR ROL, CF is considered as an extra bit that participates in the rotation RCR AL, 1 AL CF 0011 1100 1

AL CF 10011110 0

RCL AL, 3 CF AL 0 00111100 -

CF 1

AL 11100000

Status Port

11 0

10 1

12 1

13 1

15 1

x 1

x 1

x 1