0% found this document useful (0 votes)
7 views

COAL Lecture Notes (Before Mid)

The document outlines the first week of a course on Computer Organization and Assembly Language, covering key topics such as CPU, ALU, microcomputers, number systems, and data conversions. It emphasizes the importance of assembly language for precise control over processor operations, while also noting its complexity in larger programs. Additionally, it includes exercises for converting between number systems and performing arithmetic operations.

Uploaded by

blazewaze09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

COAL Lecture Notes (Before Mid)

The document outlines the first week of a course on Computer Organization and Assembly Language, covering key topics such as CPU, ALU, microcomputers, number systems, and data conversions. It emphasizes the importance of assembly language for precise control over processor operations, while also noting its complexity in larger programs. Additionally, it includes exercises for converting between number systems and performing arithmetic operations.

Uploaded by

blazewaze09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Computer Organization & Assembly Language (CS-530)

Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 1) Lecture 1 & 2


Objectives: Learning objectives of this lecture are

 CPU & ALU


 Micro Computer
 Number System, Conversions, Operations
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

Introduction
Assembly language is a symbolic representation of a processor's native code. Using machine code
allows the programmer to control precisely what the processor does. It offers a great deal of power
to use all of the features of the processor. The resulting program is normally very fast and very
compact. In small programs timing is also very predictable.
Timings, for example, can be calculated very precisely and program flow is easily controlled. It is
often used for small, real-time applications. However, the programmer needs to have a good
understanding of the hardware being used. As programs become larger, assembly language gets
very cumbersome.
Maintenance of assembly language is notoriously difficult, especially if another programmer is
brought in to carry out modifications after the code has been written. Assembly language also has
no support of an operating system, nor does it have any complex instructions.

Micro Computer:
 CPU
 MEMORY UNIT
 I/O CIRCUIT

CPU
CPU is the abbreviation for central processing unit. Sometimes referred to simply as the central
processor, but more commonly called processor the CPU is the brains of the computer where most
calculations take place. In terms of computing power, the CPU is the most important element of
a computer system.
 Memory Unit (Bytes and words)
 Smallest unit is bit.
 bit groups of bits referred as byte.
 Each memory byte is identified by an identifier called address.
 The data stored in byte is called contents.
 Difference of address byte contents & address.

1
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Q: How many memory bytes can be accessed by a processor using ‘20’ bits address?
 Some pc treats consecutive word as a single unit, called a memory word.
 The lower byte address is used as a word address.

High byte low byte


15----------8(MSB) 7---------0(LSB)

1. Memory operations.
 Read/Write
 Ram /Rom

2. Buses
 Address bus CPU places the address of
 Data bus the content (memory
 Control bus location)

3. CPU
 Machine code usage.
 Instruction set defined.
 Instruction set of every CPU is unique.

2
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 CPU has two units


1. Execution unit (ALU)
2. Bus interface unit (LU)
 Instruction execution
How an instruction is executed?
Machine instruction has two parts.
 OP-Code
OP-code specifies the type of operation.
 Operand
Operand on which the operation is performed (mostly memory location).
 Execution of instruction
Fetch:
1. Fetch an instruction from memory.
2. Decode the instruction to determine the operation.
3. Fetch data from memory.
Execution:
1. Perform the operation on data.
2. Store the result in memory if needed.
Timing:
To ensure steps are carried out in timely manner.
1. A clock circuit controls the processor by generating a train of clock pulse.
2. The time interval between the pulses is called clock period.
3. Number of pulses per second is called the clock rate measures in MHZ.

3
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Number System:
 Binary Number System:
In the binary number system, the base is two and there are only two digits, 0 and 1.
For example, the binary string 11010 represents the number:
1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 = 26
The base two is represented in binary as 10.

 Hexadecimal Number System:


The hexadecimal (hex) system is a base sixteen system. The hex digits are 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, A, B, C, D, E, and F. The hex letters A through F denote numbers ten to fifteen,
respectively. After F comes the base sixteen, represented in hex by 10.

4
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

5
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Conversion:
 Converting Binary and Hex to Decimal:
Consider the hex number 82AD. It can be written as:
8A2Dh = 8 x 163 +A x 162 + 2 x 161 +D x160
= 8 x 163 +10 x 162 + 2 x 161 + 13 x 160 = 35373d

Similarly, the binary number 11101 may be written as:


11101 b = 1 x 24 + 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 29d

This gives one way to convert a binary or hex number to decimal, but an easier way
is to use nested multiplication. For example:
8A2D = 8 x 163 + A x 162 + 2 x 161 + D x160
= ((8 x 16 + A) x 16 + 2) x 16 + D
= ((8 x 16 + 10) x 16 + 2) x 16 + 13
= 35373d

 Converting Decimal to Binary and Hex:


Suppose we want to convert 11172 to hex. The answer 2BA4h may be obtained as follows.
First, divide 11172 by 16. We get a quotient of 698 and a remainder of 4. Thus:
11172 = 698 x 16 + Ah
The remainder 4 is the unit's digit in hex representation of 11172. Now divide 698 by 16. The
quotient is 43, and the remainder is 10 = Ah. Thus:

698 = 43 x 16 + Ah

The remainder Ah is the sixteen's digit in the hex representation of 11172. We just continue this
process, each time dividing the most recent quotient by 16, until we get a 0 quotient. The remainder
each time is a digit in the Hex representation of 11172. Here are the calculations:
11172 = 698 x 16 + 4
698 = 43 x16 + 10(Ah)
43 = 2 x 16 + 11(Bh)
2 = 0 x 16 + 2

Now just convert the remainders to hex and put them together in reverse order to get 2BA4h.
This same process may be used to convert decimal to binary. The only difference is that we
repeatedly divide by 2.

6
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Conversions between Hex and Binary:


To convert a hex number to binary, we need only express each hex digit in binary.

Operations:
 Addition:
Consider the following decimal addition:
2546 + 1872 = 4418

To get the unit's digit in the Sum, we just compute 6 + 2 = 8. To get the ten's digit, compute 4 + 7 = 11. We
write down 1 and carry 1 to the hundred's column. In that column we compute 5 + 8 + 1 = 14. We write
down 4 and carry 1 to the last column. In that column we compute 2 + 1 + 1 = 4 and write it down and the
sum is complete.

Hexadecimal Addition Table

Binary Addition Table

7
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

How Integers Are Represented in the Computer?


We’ll need to refer to two particular bits in a byte or word: the most significant bit, or MSB, is the
left most bit. In a word, the MSB is bit 15; in a byte, it is bit 7. Similarly, the least significant bit,
or LSB, is the rightmost bit; that is, bit 0.

Unsigned Integers:
An unsigned integer is an integer that represents a magnitude, so it is never negative. Unsigned
integers are appropriate for representing quantities that can never be negative, such as addresses
of memory locations, counters, and ASCII character codes (see later). Because unsigned integers
are by definition nonnegative, none of the bits are needed to represent the sign, and so all 8 bits in
a byte, or 16 bits in a word, are available to represent the number.

The largest unsigned integer that can be stored in a byte is 11111111 = FFh = 255. This is not a
very big number, so we usually store integers in words. The biggest unsigned integer a 16-bit it
word can hold is 1111111111111111 = FFFFh = 65535. This is big enough for most purposes.
If not, two or more words may be used. Note that if the least significant bit of an integer is 1, the
number is odd, and it's even if the LSB is 0.

Signed Integers:
A signed integer can be positive or negative. The most significant bit is reserved for the sign: 1
means negative and 0 means positive. Negative integers are stored in the computer in a special
way known as two's complement.

Complement:

 One's Complement:
The one's complement of an integer is obtained by complementing each bit; that is, replace each 0
by 1 and each 1 by 0. In the following, we assume numbers are 16 bits.

Find one’s compliment of 5.


Ans: 11111010b
 Two's Complement:
To get the two's complement of an integer, just add 1 to its one's complement.

Ans: 11111011b

8
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Exercise:

1. Convert 82ADh to decimal.


Ans: 35373d

2. Convert 11101b to decimal.


Ans: 29d

3. Convert 2BD4h to decimal.


Ans: 11220d

4. Convert 95 to binary.
Ans: 1011111b

5. Convert 2B3Ch to binary.


Ans: 0010101100111100b

6. Convert 1110101010b to hex.


Ans: 3AAh

7. Add 3546d and 1872d.


Ans: 5418d / 3CF61h

8. Add 5B39h and 7AF4h.


Ans: D62Dh

9. Add 100101111b and 110110.


Ans: 101100101b

10. Subtract 7283d from 9145d.


Ans: 1862d

11. Subtract BA94h from D26Fh.


Ans: 17DBh

12. Subtract 0111b from 1001.


Ans: 10b
13. Find one’s compliment of 5.
Ans: 11111010b

9
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

14. Find two’s compliment of 5.


Ans: 11111011b
15. Find two’s compliment of the two’s compliment of 5.
Ans: 101b
16. Suppose AX contains 5ABCh and BX contains 21FCh. Find the difference of AX minus
BX by using complementation and addition.
Ans: 10011100011000000b=38C0h
17. Suppose AX contains FE0Ch. Give the unsigned and signed decimal interpretations.
Ans: signed=500
Unsigned=65036

18. Show how the character string “RG 2z” is stored in memory, starting at address 0.
Ans:
Address Contents of Bytes
0 01010010
1 01000111
2 00100000
3 00110010
4 01111010

19. In many applications, it saves time to memorize the conversions among small binary,
decimal, and hex numbers. Fill in the blanks in the following table:

Binary Decimal Hex


1001b 9 9h
1010b 10 Ah
1101b 13 Dh
1100b 12 Ch
1110b 14 Eh
1011b 11 Bh

20. Convert the following binary and hex numbers to decimal:


a. 1110b=14d
b. 100101011101b=2397d
c. 46Ah=1130d
d. FAE2Ch=1027628d

10
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

21. Give the unsigned and signed decimal interpretations of each of the following 16-bit or 8 bit
numbers.
Signed unsigned
a. 7FFEh 32766d 32766d
b. 8543h 34115d -31421d
c. FEh -2d -254d
d. 7Fh 7d 7d

22. Show how the decimal integer -120 would be represented


a. In 16 bits=1111111110001000b
b. In 8 bits=10001000b

23. For each of the following decimal numbers, tell whether it could be stored (a) as a 16-bit
number (b) as an 8-bit number.
a. 32767=16 bit
b. -40000= out of range
c. 65536= out of range
d. 257= 16 bit
e. -128= 8 bit

24. For each of the following 16-bit signed numbers, tell whether it is positive or negative.
a. 1010010010010100b=negative
b. 78E3h=positive
c. CB33h=negative
d. 807Fh=negative
e. 9AC4h=negative

25. If the character string “$12.75” is being stored in memory starting at address 0, give the hex
contents of bytes 0-5.
Ans:
Address Contents of Bytes
0 00100100
1 00110001
2 00110010
3 00101110
4 00110111
5 00110101

11
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

26. Translate the following secret message, which has been encoded in ASCII as 41 74 74 61 63
6B 20 44 61 77 6E.
Ans: Attack Dawn.
27. Suppose that a byte contains the ASCII code of an uppercase letter. What hex number should
be added to it to convert it to lower case?
Ans: 20h

28. Suppose that a byte contains the ASCII code of a decimal digit; that is, “0” . . . “9”. What hex
number should be subtracted from the byte to convert it to the numerical form of the characters?
Ans: 30h

12
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 2) Lecture 3 & 4


Objectives: Learning objectives of this lecture are

 Introduction to Assembly Language


 Data Register
 Segment Register
 Pointer & Index Registers
 Program structure
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

Assembly language
Assembly language is a symbolic representation of a processor's native code. Using machine code
allows the programmer to control precisely what the processor does. It offers a great deal of power
to use all of the features of the processor. The resulting program is normally very fast and very
compact. In small programs timing is also very predictable.

Timings, for example, can be calculated very precisely and program flow is easily controlled. It is
often used for small, real-time applications. However, the programmer needs to have a good
understanding of the hardware being used. As programs become larger, assembly language gets
very cumbersome.

Maintenance of assembly language is notoriously difficult, especially if another programmer is


brought in to carry out modifications after the code has been written. Assembly language also has
no support of an operating system, nor does it have any complex instructions.

 Registers
The registers are classified according to the functions they perform In general data registers hold
data for an operation, address registers hold the address of an instruction or data and status registers
keeps the current status of the processor.

The 8086 has four general data registers the address registers are divided into segment, pointer and
index registers and the status register is called the FLAGS register.

13
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Data Registers:
Four register are available to the programmer for general data Manipulation. The high and low
bytes of the data registers can be accessed separately. The high byte of AX is called AH and the
low byte is AL. Similarly, the high and low bytes of BX, CX, and DX are BH and BL, CH and
CL, DH and DL respectively.

AX (Accumulator Register):
AX is the preferred register to use in arithmetic, logic, and transfer instructions. In multiplication
and division operations, one of the numbers involved must be in AX or AL. AX is 16 bit register.

BX (Base Register):
BX also serves as an address register; an example is a table look-up Instruction called XLAT
(translate).

CX (Count Register):
CX serves as a loop counter. Another example of using CX as counter is REP (repeat), which
controls a special class of instructions called string operation.

DX (Data Register):
DX Is used in multiplication and division. It is also used in I/0 Operations.

14
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Segment Registers:
In assembly 4 segments are:
CS (code segment)
DS (data segment)
SS (stack segment)
ES (extra segment)

The registers CS, DS, SS and ES. A typical machine language program consists of instructions
(code) and data. There is also a data structure called the stack used by the processor to implement
procedure calls. The program's code, data, and stack are loaded into different memory segments,
we call them the code segment, data segments, and stack segment.

The CS, DS, and SS registers contain the code, data, and stack Segment numbers, respectively. If
a program needs to access a second data segment, it can use the ES (extra segment) register.

15
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Pointer and Index Registers:


The registers SP, BP, SI, and DI normally point to (contain the offset addresses of) memory
locations.

SP (Stack Pointer):
The SP (stack pointer) register is used in conjunction with SS for accessing the stack segment.

BP (Base Pointer):
The BP (base pointer) register is used primarily to access data on the stack. However, unlike SP,
we can also use BP to access data in the other segments.

SI (Source index):
The SI (source index) register is used to point to memory locations in the data segment addressed
by DS. By incrementing the contents of SI, we can easily access Consecutive memory locations.

DI (Destination Index):
The DI (destination Index) register performs the same functions as SI. There is a class of
instructions, called string operations that use DI to access memory locations addressed by ES.

Program Structure:
Machine language programs consist of code, data, and stack. Each part occupies a memory
segment. The same organization is reflected in an assembly language program. This time,
the code, data, and stack are structured as program segments. Each program segment is
translated into a memory segment by the assembler.

Memory Models:
The size of code and data a program can have is determined by specifying a memory model using
the .MODEL directive. The syntax is:
.MODEL memory_model

16
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Data Segment:
A program's data segment contains all the variable definitions, Constant definitions are often made
here as well, but they may be placed elsewhere ln the program since no memory allocation is
involved. To declare a data segment, we use the directive .DATA, followed by variable and
constant declarations. For example:

17
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Stack Segment:
The purpose of the stack segment declaration is to set aside a block of memory (the stack area) to
store the stack: The stack area should be big enough to contain the stack at its maximum size. The
declaration syntax is:
.STACK size

where size is an optional number that specifies the stack area size In bytes. For example:

.STACK l00H

sets aside l00h bytes for the stack area (a reasonable size for most applications). If size is omitted,
1 KB is set aside for the stack area.

Code Segment:
The code segment contains a program's instructions. The declaration syntax is:

.CODE

INT 21H:
INT 21h may be used to Invoke a large number of DOS functions, a particular function is
requested by placing a function number in the AH register and invoking INT 2lh. Here we
are interested in the following functions:

INT 21h functions expect input values to be in certain registers and return output values in
other registers.

To invoke the routine, execute these instructions:

18
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

The processor will wait for the user to hit a key if necessary. If a character key is pressed,
AL gets its ASCII code; the character is also displayed on the screen. If any other key is
pressed, such as an arrow key, Fl-F10, and so on, AL will contain 0. The instructions
following the INT 21 h can examine AL and take appropriate action.

To display a character with this function, we put its ASCII code in DL. For example, the
following instructions cause a question mark to appear on the screen:

After the character is displayed, the cursor advances to the next position on the line.

19
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Example:

Terminating a Program:
The last two lines in the MAIN procedure require some explanation.
When a program terminates, it should return control to DOS. This can be accomplished by
executing INT 2lh, function 4Ch.

The "S" marks the end of the string and is not displayed. If the string contains the ASCII
code of a control character, the control function is performed.
To demonstrate this function, we will write· a program that prints
"HELLO!" on the screen. This message is defined in the data segment as:

20
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

The LEA Instruction


INT 2lh, function 9, expects the offset address of the character string
to be in DX. To get it there, we use a new instruction:

where destination is a general register and source is a memory location. LEA


stands for "Load Effective Address." It puts a copy of the source offset address
into the destination. For example:

puts the offset address of the variable MSG into DX.

Program Segment Prefix:


When a program is loaded in memory, DOS prefaces it with a 256-byte program segment
prefix (PSP). The PSP contains information about the program. So that programs may
access this area, DOS places its segment number in both DS and ES before executing the
program. The result is that DS does not contain the segment number of the data segment.
To correct this, a program containing a data segment begins with these two instructions:

@Data is the name of the data segment defined by .DATA. The assembler translates the
name @DATA into a segment number. Two instructions are needed because a number (the
data segment number) may not be moved directly into a segment register.
With DS initialized, we may print the "HELLO!" message by placing its address in DX
and executing INT 2lh:

21
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

22
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 3) Lecture 5 & 6


Objectives: Learning objectives of this lecture are

 Field
 Program Data
 Variables
 Basic Instructions
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

Fields:
 Name Field:
The name field is used for instruction labels, procedure names, and variable names. The
assembler translates names into memory addresses.
Names can be from 1 to 31 characters long. And may commits of letters, digits, and the special
character ? . @ _ $ %. Embedded blanks are not allowed. If a period is used, it must be the first
char. Names may not begin with a digit. The assembler does not differentiate between upper
and lower case in a name.

Examples of legal names


COUNTER1
@CHARACTER
SUM_OF_DIGITS
$1000
DONE?
.TEST

Examples of illegal names


TWO WORDS contain a blank
2ABC begin with a digit
A45.28 . not first character
YOU&ME contains an illegel character

23
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Operation Field:
The assembler translates a symbolic opcode into a machine language opcode. Opcode symbols
often describe the operation's function;

For example:
MOV, ADD, SUB.

Operand Field:
The operand field specifies the data that are to be acted on by the operation. An instruction
may have zero, one, or two operands. For example:

NOP no operands. Does nothing


INC AX one operand. Adds, 1 to the contents of AX
ADD WORDl,2 Two operands; adds 2 to the contents of memory word WORDl

In a two, operand instruction, the first operand is the destination operand. It is the register or
memory location where the result is stored (note: some instructions don't store the result). The
second operand is the source operand.

Comment Field:
A semicolon marks the beginning of this field, and the assembler ignores anything typed after
the semicolon. Comments are optional, but because assembly language is so low-level, it is
almost impossible to understand an assembly language program without comments.

MOV CX, 0 ; move 0 to CX

Instead, use comments to put the instruction into the context of the program:

MOV CX.0 ; CX counts terms, initially 0

It is also permissible to make an entire line a comment, and use them to create space in a
program:
;
; initialize registers
;
MOV AX, 0
MOV BX, 0

24
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Program Data:
In an assembly language program, we may express data as binary, decimal, or hex
numbers, and even as characters.

Numbers:
A binary number is written as a bit string followed by the letter "R" or "b"; for example: 1010B.
A decimal number is a string of decimal digits, ending with an optional "D" or "d".
A hex number must begin with a decimal digit and end with the letter "H" or "h"; for example,
0ABCH (the reason for this is that the assembler would be unable to tell whether a symbol such
as "ABCH" represents the variable name "ABCH" or the hex number ABC). Any of the
preceding numbers may have an optional sign.
Here are examples of legal and illegal numbers for MASM:
Number Type
11011 decimal
11011B binary
64223 decimal
-21843D decimal
1,234 illegal – contains a non-digit Character
1B4DH hex
1B4D illegal hex number–doesn’t end in H
FFFFH illegal hex number – doesn’t being with a decimal digit
0FFFFH hex

 Character:
Characters and character strings must be enclosed in single or double quotes: for example, "A" or
'hello'.

 Variables:
Byte Variables:
The assembler directive that defines a byte variable takes the following form:
name DB initial_value

Where the pseudo-op DB stands for "Define Byte".

For example:
ALPHA DB 4

25
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

This directive causes the assembler to associate a memory byte with the name ALPHA, and
initialize it to 4. A question mark (“?") used in place of an initial value sets aside an uninitialized
byte; for example:
BYT DB ?

Word Variables:
The assembler directive for defining a word variable has the following form:
name DW initial_value

The pseudo-op DW means "Define Word." For example:


WRD DW -2

The decimal range of Initial values that can be specified is -32768 to 32767 for a signed
interpretation, or 0 to 65535 for an unsigned interpretation.

 A Few Basic Instructions:


MOV and XCHG:
The MOV Instruction is used to transfer data between register, between a register and a memory
location, or to move a number directly into a register or memory location. The syntax is:
MOV destination, source
Here are some examples:
MOV AX, WORDl
MOV AX, ’A’

26
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

The XCHG (exchange) operation is used to exchange the contents of two registers, or a register
and a memory location. The syntax is:
XCHG destination, source
An example is:
XCHG AH, BL

Restrictions on MOV and XCHG:


For technical reasons, there may a few restrictions on the use of MOV and XCHG. Table shows
the allowable combinations. For example,

ILLEGAL: MOV WORD1, WORD2

But we can get around this restriction by using a register:


MOV AX, WORD2
MOV WORD1, AX

27
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

ADD and SUB:


The ADD and SUB instructions are used to add or subtract the contents of two registers, a register
and a memory location, or to add (subtract) a number to (from) a register or memory location. The
syntax is:

This instruction, "Add AX to WORD1," causes the contents of AX and memory word WORD1 to
be added, and the sum is stored in WORD1. AX is unchanged.

INC and DEC:


INC (increment) is used to add 1 to the contents of a register or memory location and DEC
(decrement) subtracts 1 from a register or memory location. The syntax is:

28
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Programming Practice
1. Write instructions (not a complete program) to do the following.
a. Read a character, and display it at the next position on the same line.

MOV AH, 1
INT 21H

MOV DL, AL
INT 21H

b. Read an uppercase letter (omit error checking), and display it at the next position on
the same line in lower case.

MOV AH, 1
INT 21H

MOV BL, AL
ADD BL, 32

MOV AH, 2
MOV DL, BL
INT 21H

29
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

2. Write a Program to (a) display a “?”, (b) read two decimal digits whose sum is less than
10, (c) display them and their sum on the next line, with an appropriate message.
Sample execution:
?27
THE SUM OF 2 AND 7 IS 9

.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "?$"
MSG2 DB 10,13,"THE SUM OF $"
MSG3 DB " AND $"
MSG4 DB " IS $"
.CODE
MAIN PROC

MOV AX, @DATA MOV AH, 9


MOV DS, AX LEA DX, MSG3
INT 21H
MOV AH, 9
LEA DX, MSG1 MOV AH, 2
INT 21H MOV DL, CL
INT 21H
MOV AH, 1
INT 21H MOV AH, 9
MOV BL, AL LEA DX, MSG4
INT 21H
MOV AH, 1
INT 21H ADD BL, CL
MOV CL, AL SUB BL, 48

MOV AH, 9 MOV AH, 2


LEA DX, MSG2 MOV DL, BL
INT 21H INT 21H

MOV AH, 2 MOV AH, 4CH


MOV DL, BL INT 21H
INT 21H
ENDP MAIN
END MAIN

30
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

3. Write a program to (a) prompt the user, (b) read first, middle, and last initials of a person’s
name, and (c) display them down the left margin.
Sample execution:
ENTER THREE INITIALS: JFK
J
F
K
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER THREE INITIALS: $"
MSG2 DB 10,13,"$"
.CODE
MAIN PROC

MOV AX, @DATA MOV AH, 2


MOV DS, AX MOV DL, BL
INT 21H
MOV AH, 9
LEA DX, MSG1 MOV AH, 9
INT 21H LEA DX, MSG2
INT 21H
MOV AH, 1
INT 21H MOV AH, 2
MOV BL, AL MOV DL, CL
INT 21H
MOV AH, 1
INT 21H MOV AH, 9
MOV CL, AL LEA DX, MSG2
INT 21H
MOV AH, 1
INT 21H MOV AH, 2
MOV CH, AL MOV DL, CH
INT 21H
MOV AH, 9
LEA DX, MSG2 MOV AH, 4CH
INT 21H INT 21H
ENDP MAIN
END MAIN

31
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

4. Write a program to read one of the hex digits A-F; and display it on the next line in decimal.
Sample execution:
ENTER A HEX DIGIT: C
IN DECIMAL IT IS 12

.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER A HEX DIGIT: $"
MSG2 DB 10,13,"IN DECIMAL IT IS 1$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AH, 9
LEA DX, MSG1
INT 21H

MOV AH, 1
INT 21H
MOV BL, AL
SUB BL, 17

MOV AH, 9
LEA DX, MSG2
INT 21H

MOV AH, 2
MOV DL, BL
INT 21H

MOV AH, 4CH


INT 21H
ENDP MAIN
END MAIN

32
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

5. Write a program to display at 10 x 10 solid box of asterisks.


Hint: declare a string in the data segment that specifies the box, and display it with INT
21h, function 9h.

.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "**********$"
MSG2 DB 10,13,"**********$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AH, 9
LEA DX, MSG1
INT 21H

LEA DX, MSG2


INT 21H
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H

MOV AH, 4CH


INT 21H
ENDP MAIN
END MAIN

33
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

6. Write a program to (a) display “?”, (b) read three initials, (c) display them in the middle of
an 11 x 11 box of asterisks, and (d) beep the computer.

.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB 10,13,"***********$"
MSG2 DB 10,13,"$"
MSG3 DB "?$"
MSG4 DB "****$"

.CODE
MAIN PROC

MOV AX, @DATA


MOV DS, AX MOV AH, 2
MOV DL, BL
MOV AH, 9 INT 21H
LEA DX, MSG3
INT 21H MOV DL, CL
INT 21H
MOV AH, 1
INT 21H MOV DL, CH
MOV BL, AL INT 21H

INT 21H MOV AH, 9


MOV CL, AL LEA DX, MSG4
INT 21H
INT 21H
MOV CH, AL LEA DX, MSG1
INT 21H
MOV AH, 9 INT 21H
LEA DX, MSG1 INT 21H
INT 21H INT 21H
INT 21H INT 21H
INT 21H
INT 21H MOV AH, 2
INT 21H MOV DL, 7
INT 21H
MOV AH, 9
LEA DX, MSG2 MOV AH, 4CH
INT 21H INT 21H
ENDP MAIN
LEA DX, MSG4 END MAIN
INT 21H

34
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

****** * *
****** ** ** *
****** *** ***
**
***
Print the followings patterns with and without using data segment.

PROGRAM NO 1
.MODEL SMALL MOV DL,10
.STACK 100H INT 21H
.DATA INT 21H
N DB "*"
.CODE MOV DL,13
MAIN PROC INT 21H
MOV AX,@DATA
MOV DS,AX MOV DL,'*'
INT 21H
MOV DL,'*' INT 21H
MOV AH,2 INT 21H
INT 21H INT 21H
INT 21H INT 21H
INT 21H MOV AH,4CH
INT 21H INT 21H
INT 21H MAIN ENDP
INT 21H END MAIN
INT 21H

MOV DL,10
INT 21H
INT 21H

MOV DL,13
INT 21H

MOV DL,'*'
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H

35
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

PROGRAM NO 2
.MODEL SMALL
.STACK 100H
.DATA
N DB "*"

.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX

MOV DL,'*'
MOV AH,2
INT 21H

MOV DL,10
INT 21H
INT 21H
MOV DL,13
INT 21H

MOV DL,'*'
INT 21H
INT 21H

MOV DL,10
INT 21H
INT 21H

MOV DL,13
INT 21H
MOV DL,10
INT 21H

MOV DL,'*'
INT 21H
INT 21H
INT 21H

MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

36
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

PROGRAM NO 3
.MODEL SMALL
.STACK 100H
.DATA
N DB "*"
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX

MOV AH,2
MOV DL,'*'
INT 21H

MOV DL,10
INT 21H
INT 21H

MOV DL,'*'
INT 21H
INT 21H

MOV AH,2
MOV DL,10
INT 21H
INT 21H

MOV DL,'*'
INT 21H
INT 21H
INT 21H
INT 21H
INT 21H

MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

37
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 4) Lecture 7 & 8


Objectives: Learning objectives of this lecture are

 The Flag Register


 Over Flow
 How instructions affect the flags
 The Debug Program
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

This lecture will cover the flag registers, Overflow and Debug Program. The FLAGS register is
the status register in Intel x86 microprocessors that contains the current state of the processor. The
DEBUG program provides an environment in which a program may be tested. The user can step
through a program and display and change the registers and memory. After status, parity, carry,
auxiliary, zero flag and overflow (signed, unsigned) we will proceed to some examples.

FLAGS REGISTER
The status flags are located in bits 0, 2, 4, 6, 7, and 11 and the control flags are located in bit 8, 9,
and 10. The other bits have no significance.

The Status Flags:


As stated earlier, the processor uses the status flags to reflect the result of an operation. For
example, If SUB AX, AX is executed, the zero flag becomes 1, thereby Indicating that a zero result
was produced. Now let's get to know the status flags.

Carry Flag (CF):


CF = 1 if there is a carry out from the most significant bit (MSB) on addition, or there is a
borrow into the MSB on subtraction; otherwise, it is 0. CF is also affected by shift and
rotates instructions.

38
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Parity Flag (PF):


PF = 1 If the low byte of a result has an even number of one bits (even parity). It Is 0 if the
low byte has, odd parity. For example, if the result of a word addition is FFFEh, then the
low byte contains 7 one bits, so PF = 0.

Auxiliary Carry Flag (AF):


AF = 1 if there is a carry out from bit 3 on addition, or a borrow into bit 3 on subtraction.
AF is used in binary-coded decimal (BCD) operations.

Zero Flag (ZF):


ZF = 1 for a zero result, and ZF = 0 for a nonzero result.

Sign Flag (SF):


SF = 1 if the MSB of a result is 1; it means the result is negative if you are giving a signed
interpretation. SF = 0 if the MSB is 0.

39
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Overflow Flag (OF):


OF = 1 if signed overflow occurred, otherwise it is 0. The meaning of overflow is discussed
next.

Examples of Overflow:
Signed and unsigned overflows are independent phenomena. When we perform an
arithmetic operation such as addition, there are four possible outcomes: (1) no overflow,
(2) signed overflow only, (3) unsigned overflow only, and (4) both signed and unsigned
overflows.

As an example of unsigned overflow but not signed overflow, suppose AX contains FFFFh,
BX contains 0001 h, and ADD AX, BX is executed.
The binary result is
1111 1111 1111 1111
+ 0000 0000 0000 0001
1 0000 0000 0000 0000

If we-are giving an unsigned interpretation, the correct answer is 1000h = 65536, but this
is out of range for a word operation. A 1 is carried out of the MSB and the answer stored
in AX, 0000h, is wrong, so unsigned overflow occurred. But the stored answer is correct
as a signed number, for FFFFh = -10001h = 1, and FFFFh + 0001h = -1 + 1 = 0, so signed
overflow did not occur.

Unsigned Overflow:
On addition, unsigned overflow occurs when there is a carry out of the MSB. This means
that the correct answer is larger than the biggest unsigned number that is, FFFFh for a
word and FFh for a byte. On subtraction, unsigned overflow occurs when there is a borrow
into the MSB. This means that the correct answer is smaller than 0.

Signed Overflow:
On addition of numbers with the same sign, signed overflow occurs when the sum has a
different sign. This happened in the preceding example when we were adding 7FFFh. and
7FFFh (two positive numbers), but got FFFEh. (a negative result)

Subtraction of numbers with different signs is like adding numbers of the same sign. For
example, A - ( -B) = A + B and -A -(+B) = -A + -B. Signed overflow occurs if the result
has a different sign than expected.

40
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 How Instructions Affect the Flags:


In general, each time the processor executes an instruction, the flags are altered to reflect the
result. However, some instructions don't affect any of the flags, affect only some of them, or
may leave them undefined. The flags affected as follows:

 The DEBUG Program:


The DEBUG program provides an environment in which a program may be tested. The user
can step through a program and display and change the registers and memory. It is also possible
to enter assembly code directly, which DEBUG converts to machine code and stores in
memory.

41
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Exercise:
1. For each of the following instructions, give the new destination contents and the
new setting of CF, SF, ZF, PF, and OF. Suppose that the flags are initially 0 in
each part of this question.

• ADD AX, BX Where AX contains 7FFFh and BX contains 0001h


0111 1111 1111 1111
+ 0000 0000 0000 0001
──────────────────────
1000 0000 0000 0000

AF=1,CF=0,PF=1,SF=1,ZF=0,OF=1

• SUB AL, BL Where AL contains 01h and BL contains FFh


0111 0111
- 0000 0001
─────────────
0111 0110
AF=0, PF=0,SF=0,ZF=0,CF=0,OF=0

• DEC AL Where AL contains 00h


00, +(-1)=>
TAKE 2`S COMPLIMENT:1111 1111
ADD:0000 0000 + 1111 1111
=1111 1111
AF=1,CF=0,ZF=0,SF=1,OF=0,PF=1

• NEG AL Where AL contains 7Fh


0111 1111
TAKE 2`S COMPLIMENT
1000 0000+0000 0001=>1000 0001
AF=1, PF=1, CF=1,SF=1,OF=0,ZF=0

• XCHG AX, BX Where AX contains 1ABCh and BX contains 712Ah


After xchange operation:
AX=712Ah, BX=1ABCh
AX=0111 0001 0010
AF=0,PF=0,ZF=0,SF=0,OF=0,CF=0
NO EFFECT ON STATUS FLAGS

42
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

• ADD AL, BL Where AL contains 80h and BL contains FFh


1000 0000
+1111 1111
─────────
10111 1111
AF=0,PF=0,ZF=0,SF=0,OF=1,CF=1

• SUB AX, BX Where AX contains 0000h and BX contains 8000h


AX+(-BX)
TAKING 2`S COMPLIMENT:
1000 0000 0000 0000:
0111 1111 1111 1111+0000 0000 0000 0001
=1000 0000 0000 0000
ADD it with 00h: 1000 0000 0000 0000
AF=0,CF=1,PF=1,SF=1,ZF=0,OF=1,

2.
a. Suppose that AX and BX both contains positive numbers; and ADD AX, BX is executed.
Show that there is a carry into the msb but no carry out of the msb if, and only if, signed
overflow occurs.

LET AX=0110,BX=0011
ADD:
0110
+0011
─────
1001
Here there is a carry in msb but no carry out of the msb.

b. Suppose AX and BX both contain negative numbers, and ADD AX,BX is executed. Show
that there is a carry out of the msb but no carry into the msb if, and only if, signed overflow
occurs.
LET AX=1011,BX=1000
ADD:
1011
+1000
─────
10011

Here there is NO carry in msb but Acarry out of the msb.


43
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

• Suppose ADD AX, BX is executed. In each of the following parts, the first number being
added is the contents of AX, and the second number is the contents of BX. Give the resulting
value of AX and tell whether signed or unsigned overflow occurred.

a. 512Ch + 4185h

0101 0001 0010 1100


+ 0100 0001 1000 0101
──────────────────
1001 0000 1011 0001

OF=1 because two positive numbers are adding to give a negative number here.

b. FE12h + 1ACBh

1111 1110 0001 0010


+0001 1010 1100 1011
───────────────────
1┘0001 1000 1101 1101

Unsigned Overflow Occurred


No Signed Overflow

c.E1E4h + DAB3h
1110 0001 1110 0101
+1101 1010 1011 0011
───────────────────────
1┘1011 1100 1001 1000

OF=0 because two negative numbers are added give a negative number.
No Signed Overflow
Unsigned Overflow Occured

d.7132h + 7000h
0111 0001 0011 0010
+0111 0000 0000 0000
──────────────────
1110 0001 0011 0010

OF=1 because two positive numbers add to give positive not a negative number.
44
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Signed Overflow Occured


No Unsigned Overflow

e.6389h + 1176h
0110 0011 1000 1001
+ 0001 0100 1111 1111
0111 0100 1111 1111
OF=0 because two positive numbers give positive number by addition.
No signed Overflow
No unsigned Overflow

4. Suppose SUB AX, BX is executed. In each of the following parts, the first number is the
initial contents of AX and the second number is the contents of BX. Give the resulting value of
AX and tell whether signed or unsigned overflow occurred.

• 2143h – 1986h

0010 0001 0100 0011


- 0001 1001 1000 0110
──────────────────
0000 1111 1011 1101

OF=0, because the greater number is positive and the answer has msb 0
No Signed Overflow
No Unsigned Overflow

• 81FEh – 1986h
1000 0001 1111 1110
-0001 1001 1000 0110
─────────────────
0111 1000 0111 1000

OF =1 because answer was supposed to be negative as –A+(-B)=-C,here its positive.


Signed Overflow Occurred
No Unsigned Overflow

c. 19BCh – 81FEh
19BCh+(-81FEh)
Take compliment of 81FEh and then add. Both numbers
81FEh=1000 0001 1111 1110
45
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Taking 2;s compliment :0111 1110 0000 0010


Now add:
0001 1001 1011 1100
+0111 1110 0000 0010
──────────────────
1001 0111 1011 1110
OF=1 because two positive numbers add to give positive number

Signed Overflow Occured


No Unsigned Overflow
d.0002h – FE0Fh
FE0Fh=1111 1110 0000 1111
Taking 2;s compliment:
0000 0001 1111 0001
Add:
0000 0000 0000 0010
+0000 0001 1111 0001
───────────────────
0000 0001 1111 0011
OF=0 because both are positive
No Unsigned Overflow
No Signed Overflow

e.8BCDh – 71Abh
1000 1011 1100 1101
-1111 0001 1010 1011
─────────────
0001 1010 0010 0010
OF=1 because both are negative and gives positive number
Signed Overflow
No Unsigned Overflow
5.
ADD AX, BX , where AX contains FFFFh, BX contains FFFFh.
(SF, PF, ZF, CF, OF = ?)
1111 1111 1111 1111
+1111 1111 1111 1111
────────────────────
1┘1111 1111 1111 1110

ZF=0,PF=0,CF=1,OF=0,SF=1
46
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

6.
ADD AL, BL, where AL contains 80h, BL contains 80h.
(SF, PF, ZF, CF, OF = ?)
1000 0000
+1000 0000
──────────
1┘0000 0000
Sf=0,PF=1,ZF=1,CF=1,OF=1
7.
SUB AX, BX, where AX contains 8000h and BX contains 0001h.
(SF, PF, ZF, CF, OF = ?)
1000 0000 0000 0000
-0000 0000 0000 0001
──────────────────
0111 1111 1111 1111
CF=0,ZF=0,SF=0,OF=1,PF=1
8.
INC Al, where AL contains FFh.
(SF, PF, ZF, CF, OF = ?)
1111 1111
+0000 0001
─────────
1┘0000 0000
CF=0(Carry is unaffected by INC),ZF=1,SF=0,PF=1,OF=0

9.
MOV AX, -5.
(SF, PF, ZF, CF, OF = ?)
Taking 2s compliment:
0101-> 1010+1=1011
CF=0,PF=0,SF=0,ZF=0,OF=0
10.
NEG AX, where AX contains 8000h.
(SF, PF, ZF, CF, OF = ?)
TAKING 2S COMPLIMENT:
1000 0000 0000 0000=> 0111 1111 1111 +1 = 1000 0000 0000 0000
IT’S THE SAME NUMBER so

OF=1,PF=1,CF=1,SF=1,ZF=0
47
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 5) Lecture 9 & 10


Objectives: Learning objectives of this lecture are

 Flow Control
 Conditional jumps
 The JMP Instruction
 LOOP Instruction
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

 Flow controls:
The jump and loop instructions transfer control to another part of the program. This transfer can
be unconditional or can depend on a particular combination of status flag settings.

 Conditional Jumps :
JNZ is an example of a conditional jump instruction.
The syntax is:

JNZ destination_label

 The JMP Instruction:


The JMP (jump) instruction causes an unconditional transfer of control. (Unconditional jump)
The syntax is:
JMP destination
JMP can be used to get around the range restriction of a conditional Jump. For example, suppose
we want to implement the following loop:

TOP:
; body of the loop
DEC CX ; decrement counter
JNZ TOP ; keep looping if CX > 0
MOV AX,BX

IF-THEN-ELSE:

48
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

49
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

The LOOP Instruction: A LOOP is a sequence of instructions that is repeated. The number of
times to repeat may be known in advance, or it may depend on conditions. Syntax is:
LOOP destination-label
;initialize CX to loop_count
Top:
;body of the LOOP
LOOP Top

50
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

EXAMPLES:
1. Read a character, and if it's an uppercase letter, display it.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC

MOV AH, 2
MOV DL, "?"
INT 21H

MOV AH, 1
INT 21H
MOV BL, AL

MOV AH,2
MOV DL,10
INT 21H
MOV DL,13
INT 21H

CMP BL,41H
JL ABC
CMP BL,5BH
JGE ABC

MOV AH,2
MOV DL,BL
INT 21H

ABC:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

51
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

2. Read a character. If it's "y" or "Y", display it; otherwise, terminate the program.
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,2
MOV DL,"?"
INT 21H

MOV AH,1
INT 21H
MOV BL,AL

MOV AH,2
MOV DL,10
INT 21H
MOV DL,13
INT 21H

CMP BL,79H
JE ABC

CMP BL,59H
JE ABC
JMP E

ABC:
MOV AH,2
MOV DL,BL
INT 21H
E:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN

52
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

3. Write a count-controlled loop to display a row of 80 stars.


.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV CX,80
MOV AH,2
ABC:
MOV DL,'*'
INT 21H
LOOP ABC

MOV AH,4CH
INT 21H
ENDP MAIN
END MAIN

4. Write some code to read characters until a blank is read.


.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH,1
WHILE_:
CMP AL,' '
JE END_WHILE

INT 21H
JMP WHILE_
END_WHILE:
MOV AH,4CH
INT 21H
ENDP MAIN
END MAIN

53
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

5. Write some code to count the number of characters in n input line.

.MODEL SMALL
.STACK 100H
.DATA
VAR DB 1
.CODE
MAIN PROC
MOV AX,0
MOV AH,1
INT 21H
WHILE_:
CMP AL,13
JE END_WHILE

INC VAR
INT 21H
JMP WHILE_

END_WHILE:
MOV AH,2
MOV DL,10
INT 21H
MOV DL,13
INT 21H

MOV BL,VAR
ADD BL,48
MOV DL,BL
INT 21H

MOV AH,4CH
INT 21H
ENDP MAIN
END MAIN

54
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

6. Write a program to display the extended ASCII characters (ASCII codes 80h to FFh).
Display 10 characters per line, separated by blanks. Stop after the extended characters have
been displayed once
.MODEL SMALL
.STACK 100H
.DATA
C10 DB 0D
.CODE
MAIN PROC
MOV AH,2
MOV BL,80H
MOV CL,10
JMP WH
AC:
MOV CL,10
MOV DL,10
INT 21H
MOV DL,13
INT 21H

WH:
MOV DL,BL
INT 21H
INC BL
DEC CL
CMP CL,0
JE AC

CMP BL,0FFH
JE END_WHILE
JMP WH

END_WHILE:
MOV AH,4CH
INT 21H
ENDP MAIN
END MAIN

55
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

7. Write a program that will prompt the user to enter a hex digit character ("0"· ... "9" or "A" ...
"F"), display it on the next line in decimal, and ask the user if he or she wants to do it again. If
the user types "y" or "Y", the program repeats; if the user types anything else, the program
terminates. If the user enters an illegal character, prompt the user to try again.
Sample execution:
ENTER A HEX DIGIT: 9
IN DECIMAL IS IT 9
DO YOU WANT TO DO IT AGAIN? Y
ENTER A HEX DIGIT: c
ILLEGAL CHARACTER - ENTER 0 - 9 OR A - F: C
IN DECIMAL IT IS 12 DO YOU WANT TO DO IT AGAIN? N

.MODEL SMALL
.STACK 100H
.DATA
MS1 DB "ENTER A HEX DIGIT:$"
MS2 DB 10,13,"IN DECIMAL IT IS: $"
MS5 DB 10,13,"IN DECIMAL IT IS:1 $"
MS3 DB 10,13,"DO YOU WANT TO DO IT AGAIN?: $"
MS4 DB 10,13,"ILLEGAL CHARACTER - ENTER 0 … 9 OR A ... F:$"
COT DB 0
.CODE
MAIN PROC

MOV AX,@DATA
MOV DS,AX AA:
MOV AH,9
ST: LEA DX,MS2
MOV AH,9 INT 21H
LEA DX,MS1
INT 21H MOV AH,2
MOV DL,BL
MOV AH,1 INT 21H
INT 21H JMP EXIT
MOV BL,AL
BB:
CMP BL,'9' CMP BL,'G'
JLE AA JGE CC

SUB BL,17
CMP BL,'A' MOV AH,9
JGE BB LEA DX,MS5
JMP CC INT 21H

56
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

MOV AH,2
MOV DL,BL
INT 21H
JMP EXIT

CC:
MOV AH,9
LEA DX, MS4
INT 21H
MOV DL, COT
CMP DL,3
JE END
INC DL
MOV COT,DL

EXIT:

MOV AH,9
LEA DX, MS3
INT 21H

MOV AH,1
INT 21H
MOV CL, AL

CMP CL,'N'
JE END
JMP ST

END:
MOV AH,4CH
INT 21H
ENDP MAIN
END MAIN

57
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 6) Lecture 11 & 12


Objectives: Learning objectives of this lecture are

 Logical Instructions (AND, OR and XOR)


 TEST Instruction
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

 Logical Instruction:
The truth tables for the logic operators AND, OR, XOR (exclusive OR), and NOT.

When a logic operation is applied to 8- or 16-bit operands, the result is obtained by applying
the logic operation at each bit position.

Example: Perform the following logic operations:


1. 10101010 AND 11110000
2. 10101010 OR 11110000
3. 10101010 XOR 11110000
4. NOT 101010.10

Solutions:
1. 10101010 AND 11110000 = 10100000
2. 10101010 OR 11110000 = 11111010
3. 10101010 XOR 11110000 =01011010
4. NOT 10101010 = 01010101

58
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 AND,OR, and XOR INSTRUCTION:


The AND, OR, and XOR instructions perform the named logic operations.
THE formats are:
AND destination, source
OR destination, source
XOR destination, source

The result of the operation-is stored in the destination, which must be a register or memory
location. However, memory-to-memory operations are not allowed.

Effect on Flags:
SF, ZF, PF reflect the result
AF is undefined
CF, OF= 0
If b represents a bit (0 or 1)
b AND 1 = b, b OR 0 = b, b XOR 0 = b
b AND 0 = 0, b OR 1 = 1, b XOR 1 = -b (complement of b)
From these, we may conclude that:

1. The AND instruction can be used to clear specific destination bits while preserving the others.
A 0 mask bit clears the corresponding destination bit; a 1 mask bit preserves the corresponding
destination bit.

Example:
Clear the sign bit of AL while leaving the other bits unchanged.
Solution:
Use the AND instruction with 01111111b = 7Fh as the mask.
Thus:
AND AL,7Fh

2. The OR instruction can be used to set specific destination bits while preserving the others. A 1
mask bit sets the corresponding destination bit; a 0 mask bit preserves the corresponding
destination bit.

Example:
Set the most significant and least significant bits of AL while preserving the other bits.
Solution:
Use the OR instruction with 10000001b = 81h as the mask. Thus:
OR AL, 81h

59
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

3. The XOR instruction· can be used to complement specific destination bits while preserving the
others. A 1 mask bit complements the corresponding destination bit; a 0-mask bit preserves
the corresponding destination bit.

Example:
Change the sign bit of DX.
Solution:
Use the XOR instruction with a mask of 8000h. Thus:
XOR DX, 8000h

 Test Instruction:
The TEST Instruction performs an AND operation of the destination with the source but does
not change the destination contents. The purpose of the TEST instruction is to set the status
flags. The format is TEST destination, source.
Effect on flags
SF, ZF, PF reflect the result
AF is undefined
CF, OF= 0

Examining Bits:
The TEST instruction can be used to examine individual bits in an operand. The mask should
contain 1's In the bit positions to be tested and 0's elsewhere. Because 1 AND b = b, 0 AND b
= 0, the result of TEST destination, mask will have l's in the tested bit positions if and only if
the destination has l's in these positions; it will have 0's elsewhere. If destination has 0's in all
the tested position, the result will be 0 and so ZF = 1.

Example:
Jump to label BELOW if AL contains an even number.
Solution:
Even numbers have a 0 in bit 0. Thus, the mask is 00000001b= 1.
TEST AL, 1 ; is AL even?
JZ BELOW ; yes, go to BELOW

60
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Positive Negative Even Odd

.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "PLEASE ENTER CHARACTER $"
MSG2 DB 10,13,"NUMBER IS EVEN POSITIVE $"
MSG3 DB 10,13,"NUMBER IS EVEN NEGATIVE $"
MSG4 DB 10,13,"NUMBER IS ODD POSTIVE $"
MSG5 DB 10,13,"NUMBER IS ODD NEGATIVE $"
.CODE
MAIN PROC

MOV AX, @DATA


MOV DS, AX JMP EXIT

MOV AH, 9 ODD:


LEA DX, MSG1 TEST BL, 1
INT 21H JZ ONEG
JMP OPOS
MOV AH, 1
INT 21H OPOS:
MOV BL, AL MOV AH, 9
LEA DX, MSG4
TEST BL, 1 INT 21H
JNZ ODD JMP EXIT
EVEN:
TEST BL, 80H ONEG:
JNZ ENEG MOV AH, 9
EPOS: LEA DX, MSG5
MOV AH, 9 INT 21H
LEA DX, MSG2 JMP EXIT
INT 21H
JMP EXIT EXIT:
MOV AH, 4CH
ENEG: INT 21H
MOV AH, 9 MAIN ENDP
LEA DX, MSG3 END MAIN
INT 21H

61
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 7) Lecture 13 & 14


Objectives: Learning objectives of this lecture are

 Shift Instructions
 Rotate Instructions
 Binary and Hex I/O
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut
 Shift Instructions:

 Shift Left (SHL):


The SHL (shift left) instruction shifts the bits
in the destination to the left. The format for a
single shift is SHL destination, 1

A 0 is shifted into the rightmost bit position


and the msb is shifted into CF.

Effect on Flags:
SF, PF, ZF reflect the result
AF is undefined
CF= last bit shifted out
OF= 1 if result changes sign on last shift

Example:
Suppose DH contains 8Ah and CL contains 3. What are the values of DH and of CF after the
instruction SHL DH, CL is executed?
Solution:
The binary value of DH Is 10001010. After 3 left shifts, CF will contain 0. The new contents
of DH may be obtained by erasing the leftmost three bits and adding three zero bits to the right
end, thus:
01010000b = 50h.

62
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Shift Arithmetic Left (SAL):


Same as SHL

Example:
Write some code to multiply the value of AX by 8. Assume that overflow will not occur.
Solution:
To multiply by 8, we need to do three left shifts.
MOV CL, 3 ;number of shifts to do
SAL AX, CL ;multiply by 8

 Shift Right (SHR):

The instruction SHR (shift right) performs right shifts on the destination operand. The format
for a single shift is
SHR destination, 1

A 0 Is shifted Into the msb position, and the


rightmost bit Is shifted into CF. See Figure.
If the shift count N is different from 1, the
instruction takes the form
SHR destination, CL

Where CL contains N. In this case N single right shifts are made. The effect on the flags is the
same as for SHL.

 Shift Arithmetic Right (SAR):


The SAR Instruction (shift arithmetic right)
operates like SHR, with one difference: the
msb retains its original value. See Figure.
The syntax is
SAR destination, 1
And SAR destination, CL

The effect on flags is the same as for SHR.

63
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Rotate Instructions:
 Rotate Left (ROL):
The instruction ROL (rotate left) shifts bits to the
left. The msb is shifted into the rightmost bit.
The CF also gets the bit shifted out of the msb.
You can think of the destination bits forming a
circle, with the least significant bit following the
msb in the circle. See Figure. The syntax is:
ROL destination, 1
And ROL destination, CL

 Rotate Right (ROR):


The instruction ROR (rotate right) works just like
ROL, except that the bits are rotated to the
right. The rightmost bit is shifted into the msb,
and also into the CF. See Figure.
The syntax is:
ROR destination, 1
And ROR destination, CL

 Rotate Carry Left (RCL):


The Instruction RCL (Rotate through Carry Left)
shifts the bits of the destination to the left. The msb
is shifted Into the CF, and the previous value of CF is
shifted into the rightmost bit. In other words, RCL
works like just like ROL, except that CF is part of the
circle of bits being rotated. See Figure. The syntax is:
RCL destination, 1
And RCL destination, CL

 Rotate Carry Right (RCR):


The instruction RCR (Rotate through Carry Right)
works just like RCL, except that the bits are rotated
to the right See Figure.
The syntax is:
RCR destination, 1
And RCR destination, CL

64
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Exercise:
Count 0 & 1 (Rotate)
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "PLEASE ENTER CHARACTER: $"
MSG2 DB 10,13,"THE NUMBER OF 1 ARE: $"
MSG3 DB 10,13,"THE NUMBER OF 0 ARE: $"
CONT DB 0
.CODE
MAIN PROC
MOV AX, @DATA INT 21H
MOV DS, AX
MOV DL, CONT
MOV AH, 9 ADD DL, 48
LEA DX, MSG1 MOV AH, 2
INT 21H INT 21H

MOV AH, 1 MOV AH, 9


INT 21H LEA DX, MSG3
MOV BL, AL INT 21H

MOV CX, 8 MOV CL, CONT


MOV BL, 8
CHECK: SUB BL, CL
ROL BL, 1 MOV DL, BL
JC ADDON ADD DL, 48
LOOP CHECK
JMP EXIT MOV AH, 2
INT 21H
ADDON:
INC CONT MOV AH, 4CH
LOOP CHECK INT 21H
EXIT:
MOV AH, 9 MAIN ENDP
LEA DX, MSG2 END MAIN

65
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Binary Input Binary Output


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.CODE .CODE
MAIN PROC MAIN PROC
XOR BX, BX MOV BX, 5
MOV AH, 1 MOV CX, 16
MOV AH, 2
INPUT:
INT 21H PRINT:
CMP AL, 13 ROL BX, 1
JE END_INPUT JC DISPONE

SHL BX, 1 MOV DL, '0'


SUB AL, 48 INT 21H
OR BL, AL
JMP INPUT LOOP PRINT
JMP EXIT

END_INPUT: DISPONE:
MOV AH, 4CH MOV DL, '1'
INT 21H INT 21H

MAIN ENDP LOOP PRINT


END MAIN EXIT:
MOV AH, 4CH
INT 21H

MAIN ENDP
END MAIN

66
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Hexa Input Hexa Output


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.CODE .CODE
MAIN PROC MAIN PROC
XOR BX, BX MOV BX, 9ABFh
MOV CL, 4 MOV CX, 4
MOV AH, 1 MOV AH, 2
INPUT:
INT 21H PRINT:
CMP AL,13 MOV DL, BH
SHR DL, 1
JE END_INPUT SHR DL, 1
SHL BX, CL SHR DL, 1
CMP AL, '9' SHR DL, 1

JLE NUMBER CMP DL, 9


SUB AL, 55 JLE DISPNUM
JMP INSERT ADD DL,55
JMP DISP
NUMBER:
SUB AL, 48 DISPNUM:
INSERT: ADD DL, 48
OR BL, AL
JMP INPUT DISP:
INT 21H
END_INPUT: ROL BX,1
MOV AH, 4CH ROL BX,1
INT 21H ROL BX,1
ROL BX,1
MAIN ENDP LOOP PRINT
END MAIN
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

67
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Hexa Input Binary Output


.MODEL SMALL
.STACK 100H END_INPUT:
.DATA
MSG DB 10,13,"IN BINARY IT IS: $" MOV AH, 9
.CODE LEA DX, MSG
MAIN PROC INT 21H
MOV AX, @DATA
MOV DS, AX MOV CX, 16

XOR BX, BX PRINT:


MOV CL, 4 ROL BX, 1
MOV AH, 1 JC DISPONE
MOV DL, '0'
INPUT: MOV AH, 2
INT 21H INT 21H
CMP AL, 13 LOOP PRINT
JE END_INPUT JMP EXIT

SHL BX, CL DISPONE:


CMP AL, '9' MOV DL, '1'
JLE NUMBER MOV AH, 2
SUB AL, 55 INT 21H
JMP INSERT LOOP PRINT

NUMBER: EXIT:
SUB AL, 48 MOV AH, 4CH
INT 21H
INSERT: MAIN ENDP
OR BL, AL END MAIN
JMP INPUT

68
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Binary Input Hexa Output


.MODEL SMALL MOV AH, 2
.STACK 100H
.DATA PRINT:
MSG DB 10,13,"IN HEX IT IS: $" MOV DL, BH
.CODE SHR DL, 1
MAIN PROC SHR DL, 1
MOV AX, @DATA SHR DL, 1
MOV DS, AX SHR DL, 1

XOR BX, BX CMP DL, 9


MOV AH, 1 JLE NUMBER
ADD DL, 55
INPUT: JMP DISPLAY
INT 21H
CMP AL, 13 NUMBER:
JE END_INPUT ADD DL, 48
SHL BX, 1
SUB AL, 48 DISPLAY:
OR BL, AL INT 21H
JMP INPUT ROL BX, 1
ROL BX, 1
END_INPUT: ROL BX, 1
ROL BX, 1
MOV AH, 9 LOOP PRINT
LEA DX, MSG
INT 21H MAIN ENDP
END MAIN
MOV CX, 4

69
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Hexa Input Hexa Output


.MODEL SMALL
.STACK 100H MOV CX, 4
.DATA
MSG DB 10,13,"YOU HAVE TYPED: $" MOV AH, 2
.CODE
MAIN PROC PRINT:
MOV AX, @DATA MOV DL, BH
MOV DS, AX SHR DL, 1
XOR BX, BX SHR DL, 1
MOV CL, 4 SHR DL, 1
MOV AH, 1 SHR DL, 1
CMP DL, 9
INPUT: JLE DISPNUMBER
INT 21H ADD DL, 55
CMP AL, 13 JMP DISP
JE END_INPUT
DISPNUMBER:
SHL BX, CL ADD DL, 48
CMP AL, '9'
JLE NUMBER DISP:
SUB AL, 55 INT 21H
JMP INSERT ROL BX, 1
ROL BX, 1
NUMBER: ROL BX, 1
SUB AL, 48 ROL BX, 1

INSERT: LOOP PRINT


OR BL, AL
JMP INPUT MOV AH, 4CH
INT 21H
END_INPUT:
MOV AH, 9 MAIN ENDP
LEA DX, MSG END MAIN
INT 21H

70
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Binary Input Binary Output


.MODEL SMALL MOV AH, 9
.STACK 100H LEA DX, MSG
.DATA INT 21H
MSG DB 10,13,"YOU HAVE ENTERED
$: " PRINT:
.CODE ROL BX, 1
MAIN PROC JC DISPONE
MOV AX, @DATA MOV DL, '0'
MOV DS, AX MOV AH, 2
XOR BX, BX INT 21H
MOV AH, 1
LOOP PRINT
INPUT: JMP EXIT
INT 21H DISPONE:
CMP AL, 13 MOV DL, '1'
JE END_INPUT MOV AH, 2
INT 21H
SHL BX, 1 LOOP PRINT
SUB AL, 48
OR BL, AL EXIT:
JMP INPUT MOV AH, 4CH
INT 21H
END_INPUT:
MAIN ENDP
MOV CX, 16 END MAIN

71
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

(Week 8) Lecture 15 & 16


Objectives: Learning objectives of this lecture are

 The Stack
 A Stack Application
 Procedures
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

In this lecture detailed explanation and implementation of Stack and Procedures with different
programming practices will be covered. A stack is one-dimensional data structure. Items are added
and removed from one end of the structure; that is, it is processed in a "last-in, first-out" manner.

Procedures or subroutines are very important in assembly language, as the assembly language
programs tend to be large in size. Procedures are identified by a name. Following this name, the
body of the procedure is described which performs a well-defined job. End of the procedure is
indicated by a return statement.

 Introduction of stack:
A stack is one-dimensional data structure. Items are added and removed from one end of the
structure; that is, it is processed in a "last-in, first-out" manner. The most recent addition to the
stack is called the top of the stack. A familiar example is a stack of dishes; the last dish to go
on the stack is the top one, and it's the only one that can be removed easily.

A program must set aside a block of memory to hold the stack. we have been doing this by
declaring a stack segment:
For example:

.STACK 100H

When the program is assembled and loaded in memory, SS will contain the segment number of
the stack segment. For the preceding Stack Declaration, SP, the stack pointer, is initialized to 100p.
This represents the empty stack position: When the stack is not empty, SP contains the offset
address of the top of the stack.

72
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

PUSH:
To add a new word to the stack we PUSH it on,
The syntax is:
PUSH Source
Where source is a 16-bit register or memory word.

For example:
PUSH AX after PUSH AX

POP:
To remove the top item from the stack, we POP It.
The syntax is:
POP destination
Where destination is a 16-bit register (except IP) or memory word.
For example:
POP BX
Before POP After POP CX After POP DX

73
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Stack (A String Reversal Application)


.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER A STRING: $"
MSG2 DB 10,13,"REVERSE STRING: $"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AH, 9
LEA DX, MSG1
INT 21H

MOV CX, 0 ;TO COUNT THE CHARACTER


MOV AH, 1
INPUT:
INT 21H

CMP AL, 13
JE END_INPUT

PUSH AX
INC CX
JMP INPUT

END_INPUT:
MOV AH, 9
LEA DX, MSG2
INT 21H

MOV AH, 2
PRINT:
POP DX
INT 21H
LOOP PRINT

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

74
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

 Procedures:
One of the procedures is the main procedure, and it contains the entry point to the program. To
carry out a task, the main procedure calls one of the other procedures. It is also possible for these
procedures to call each other, or for a procedure to call itself.

When one procedure calls another, control transfers to the called procedure and its instructions are
executed; the called procedure usually returns control to the caller at the next instruction after the
call statement.

Procedure Declaration:
The syntax of procedure declaration is the following:
Name PROC type
; Body of the procedure
RET
Name ENDP

Procedure Call and Return:

RET:
The RET (return) instruction causes control to transfer back to the calling procedure. Every
procedure (except the main procedure) should have a RET someplace; usually it's the last statement
in the procedure.

75
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587

Procedures Procedures (RETURN + ARGUMENT)


.MODEL SMALL
.STACK 100H .MODEL SMALL
.CODE .STACK 100H
MAIN PROC .CODE
CALL PRINTSTAR MAIN PROC
MOV CX, 10;FEEL LIKE ARGUMENT
MOV AH, 4CH PASS
INT 21H CALL PRINTSTAR

MAIN ENDP PRINT: ;FEEL LIKE RETURN VALUE


PRINTSTAR PROC INT 21H
MOV CX, 10 LOOP PRINT
MOV AH, 2
MOV DL, '*' MOV AH, 4CH
INT 21H
PRINT:
INT 21H MAIN ENDP
LOOP PRINT PRINTSTAR PROC
RET MOV AH, 2
MOV DL, '*'
PRINTSTAR ENDP RET
END MAIN
PRINTSTAR ENDP
END MAIN

Procedures (Get From File)

.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
CALL PRINTSTAR

MOV AH, 4CH


INT 21H

MAIN ENDP
INCLUDE D:\ PRINTSTAR.ASM
END MAIN

76

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