COAL Lecture Notes (Before Mid)
COAL Lecture Notes (Before Mid)
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.
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
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.
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
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
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
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.
7
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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.
Ans: 11111011b
8
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
Exercise:
4. Convert 95 to binary.
Ans: 1011111b
9
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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:
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
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
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.
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
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.
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
@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
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.
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:
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.
Instead, use comments to put the instruction into the context of the program:
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
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 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.
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
27
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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.
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
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
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
32
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
.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
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
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
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.
38
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
39
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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
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.
AF=1,CF=0,PF=1,SF=1,ZF=0,OF=1
42
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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
• 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
OF=1 because two positive numbers are adding to give a negative number here.
b. FE12h + 1ACBh
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
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
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
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
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
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
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
MOV AH,4CH
INT 21H
ENDP MAIN
END MAIN
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
.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
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.
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
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
.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
61
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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:
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
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
The instruction SHR (shift right) performs right shifts on the destination operand. The format
for a single shift is
SHR destination, 1
Where CL contains N. In this case N single right shifts are made. The effect on the flags is the
same as for SHL.
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
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
65
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
END_INPUT: DISPONE:
MOV AH, 4CH MOV DL, '1'
INT 21H 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
67
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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
69
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
70
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
71
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: azeem@biit.edu.pk, WhatsApp# 0345-5106587
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
MOV AH, 9
LEA DX, MSG1
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
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
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
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
CALL PRINTSTAR
MAIN ENDP
INCLUDE D:\ PRINTSTAR.ASM
END MAIN
76