0% found this document useful (0 votes)
4 views62 pages

COAL Lecture Notes (After Mid)

The document outlines the generation of machine code from assembly instructions, detailing the formats of bytes and how to derive machine codes for various operations. It explains the significance of opcode, destination/source registers, and immediate addressing, providing examples for clarity. Additionally, it introduces multiplication instructions (MUL and IMUL) and their effects on status flags in the context of signed and unsigned numbers.

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)
4 views62 pages

COAL Lecture Notes (After Mid)

The document outlines the generation of machine code from assembly instructions, detailing the formats of bytes and how to derive machine codes for various operations. It explains the significance of opcode, destination/source registers, and immediate addressing, providing examples for clarity. Additionally, it introduces multiplication instructions (MUL and IMUL) and their effects on status flags in the context of signed and unsigned numbers.

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/ 62

Computer Organization & Assembly Language (CS-530)

Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 9) Lecture 17 & 18


Objectives: Learning objectives of this lecture are

 Machine code
 1 Byte, 2 Byte, 4 Byte
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

Introduction
In this lecture we are going to discuss that how machine codes are generated. When we
write and an instruction in assembly, then its equivalent machine code is generated by emu
8086. So, our concern is to know that how can we generate or find the machine code of an
assembly instruction manually.
So, let’s try to understand the whole procedure to find the machine code.

Machine code
Machine code of each instruction varies in number of bytes. Maximum size of a machine code or
an instruction can be 6 bytes. Each byte has its own format to consider when we are generating
machine code. Here keep in mind that the machine code of each instruction depends on the
considered memory or register through which we are generating machine code.

MOV BL, AL

For example, in above instruction, the machine code is 8AD8H if we consider BL register and
88C3H if we consider AL register. Here don’t worry about how the above codes are generated, the
main thing for you to pay the attention is that the machine code of each instruction depends on the
considered memory or register through which we are generating machine code.

So, now we understand the format of each byte starting from first byte.

1
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

First Byte Format


First byte contains following information:
1. What is opcode (i.e., operation code).
2. Whether the considered register or memory is destination or source.
3. Whether the considered register/memory is 8-bit or 16-bit.

Here,
• W = 1 if considered register is 16 bit and 0 otherwise.
• D = 1 if considered register is destination and 0 otherwise.
• Opcode depends on operation and you can find it on internet (i.e., MOV, ADD, SUB
etc.).

MOV AX, BX

Here we have to find the machine code of above instruction and keep in mind that we are going to
consider AX register to generate code.

Here we have to write values for each portion of first byte. So,
OPCODE = 100010 (Operation code for MOV instruction is 100010)
D = 1 (because AX is destination)
W= 1 (because AX is 16 bit register)

So machine code for first byte will be as follow:


OPCODE D W = 100010 1 1 = 1000 1011 = 8BH

2
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Second byte format


Second byte contains following information:
1. What is the second operand in instruction, that is, whether it is memory or register?
2. What is the name (binary) of first considered register.
3. What is the name (binary) of second operand (operand of step 1).
4.

Here MOD means what is the second operand in instruction, REG means what is the name
(binary) of first considered register, and R/M means what is the name (binary) of second operand
(operand of step 1).
Values for all these portions will be found using following tables:

Example regarding second byte


MOV AX, BX

Here we again consider the previous instruction as an example in which we have found machine
code for first byte.

3
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Here,
MOD = 11 (because second operand, which is BX, is register with no displacement)
REG = 000 (because considered register is AX and value for AX register in table is 000)
R/M = 011 (because second operand is BX and its value in table is 011)

So here machine code for second byte will be written just like first byte:
MOD REG R/M = 11 000 011 = 1100 0011 = C3

So, we will get complete code for instruction MOV AX, BX after combining code of both first
and second byte because it was 2 Byte instruction. i.e.,
Complete code = 8BC3h (2 byte instruction)
Example 1
MOV DL, BH
Here we are considering DL register.
For first byte:

OPCODE = 100010 (Operation code for MOV instruction is 100010)


D = 1 (because DL is destination)
W= 0 (because DL is 8 bit register)

First byte code = 100010 1 0 = 1000 1010 = 8A


For second byte:

MOD = 11 (because second operand, which is BH, is register with no displacement)


REG = 010 (because considered register is DL and value for DL register in table is 010)
R/M = 111 (because second operand is BH and its value in table is 111)

Second byte code = 11 010 111 = 1101 0111 = D7


Complete code = 8AD7h (2byte instruction)

4
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Example 2
MOV BL, AL

Here we will consider AL register.


For first byte:

OPCODE = 100010 (Operation code for MOV instruction is 100010)


D = 0 (because AL is source)
W= 0 (because AL is 8 bit register)

First byte code = 100010 0 0 = 1000 1000 = 88

For second byte:

MOD = 11 (because second operand, which is BL, is register with no displacement)


REG = 000 (because considered register is AL and value for AL register in table is 000)
R/M = 011 (because second operand is BL and its value in table is 011)

Second byte code = 11 000 011 = 1100 0011 = C3

Complete code = 88C3h ( 2byte instruction)

5
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Example 3
MOV AL, [DI]
Here we will consider AL register.
For first byte:

OPCODE = 100010 (Operation code for MOV instruction is 100010)


D = 1 (because AL is destination)
W= 0 (because AL is 8 bit register)

First byte code = 100010 1 0 = 1000 1010 = 8A


For second byte:

MOD = 00 (because second operand, which is [DI], is memory with no displacement and its value
is table is 00)
REG = 000 (because considered register is AL and value for AL register in table is 000)
R/M = 101 (because second operand is [DI] and its value in above table is 101)
Second byte code = 00 000 101 = 0000 0101 = 05
Complete code = 8A05h ( 2byte instruction)

6
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Complete 6 bytes format

Here is Byte 3 we will have to write Low displacement data and in Byte 4 we will have to write
High displacement data. For example in ADD [BX][DI]+1234H, AX instruction, we will have to
write 34 in Byte 3 and 12 in Byte 4.
Let’s understand this with an example.
Example 4
ADD [BX][DI]+1234H, AX
Here we are considering AX register.
For first byte:

OPCODE = 000000 (Operation code for ADD instruction is 000000)


D = 0 (because AX is source)
W= 1 (because AX is 16 bit register)

First byte code = 000000 0 1 = 0000 0001 = 01


For second byte:

7
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

MOD = 10 (because second operand, which is [BX][DI]+1234H, is memory with 16 bit


displacement and its value in table is 10)
REG = 000 (because considered register is AX and value for AX register in table is 000)
R/M = 001 (because second operand is [BX][DI]+1234H and its value in above table is 001)

Second byte code = 10 000 001 = 1000 0001 = 81

For 3rd and 4th byte:

Here Byte3 = 34 and Byte4= 12.


You can easily find which data should be in byte3 and byte4 by considering following structure of
a register.
If we write 1234 in BX then

So Low disp data should be 34 and high disp data should be 12.

Now we will get complete code by combining codes of all these bytes.

Complete code = 01 81 34 12 H ( 4 bytes instruction)

8
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Immediate Addressing
In previous topic, we were only using register or memories with different instructions. But now
we are going to use direct values or you can say immediate addressing. For example we were
dealing with instructions like MOV AX, BX but now we are going to discuss instructions like
MOV AX, 1. So here you can see that we are now moving direct value into AX which was not the
case in previous examples.

So, let us understand how machine codes are generated in immediate addressing.

Here you have to keep in mind that when you are dealing with immediate addressing, then the
format of bytes and operation code change depending on the operation being performed and you
can easily search about these formats and operation code on the internet. Because now you have
enough idea about how machine codes are generated, so I hope now you can generate machine
codes by just understanding the format of bytes.

For example the formats of bytes for MOV instruction are given below.

2 byte instruction format:

Here first byte is in orange color and second byte is in blue color
• 4 bits Opcode depends on operation and you can find it on internet (i.e., MOV, ADD,
SUB etc.)
• W = 1 if register is 16 bit and 0 otherwise.
• 3 bits Reg is binary of register which can be found using below table.
• Here in second byte data is the actual value which is being moved in the register.

9
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

3 byte instruction format:

Here first byte is in orange color and 2nd and 3rd bytes are in blue color
 Rest of the points are same as 2 byte format. Just difference is that if value consists of 4
characters then low byte data and high byte data will be placed in their respective bytes
just like as we did in previous example.
Here again I repeat that the above formats are just for MOV instruction/operation and these formats
may differ in case of ADD, SUB etc. operations.

Example 1

MOV AH, 1
Here we will consider register to generate code.

 Opcode = 1011 (operation code for MOV instruction is 1011 in case of immediate
addressing).
 W = 0 because AH is 8-bit register.
 Reg = 100 for AH register in above table.
 Data = 01 (it is the actual value which is being moved i.e, 1 in MOV AH, 1)

First byte code = 1011 0100 = B4


Second byte code = 01

Complete code = B401h (2 byte instruction)

10
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Example 2
MOV AX, 1234h
 Opcode = 1011 (operation code for MOV instruction is 1011 in case of immediate
addressing).
 W = 1 because AX is 16-bit register.
 Reg = 000 for AX register in above table.
 Low byte = 34 and High byte = 12 because:

First byte code = 1011 1000 = B8


Second byte code = 34
Third byte code = 12

Complete code = B8 34 12h (3 byte instruction)

11
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 10) Lecture 19 & 20


Objectives: Learning objectives of this lecture are

 MUL Instruction
 DIV Instruction
 Decimal Input and Output Procedures
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

 MUL(Signed Versus Unsigned Multiplication):


In binary multiplication signed and unsigned numbers must be treated differently. For example, suppose
we want to multiply the eight-bit numbers 10000000 and 11111111. Interpreted as unsigned numbers they
represent 128 and 255; respectively. The product is 32,640 =0111111110000000b. However, taken as
signed numbers, they represent-128 and-1, respectively; and the product is 128 = 0000000001000000b.
Because signed and unsigned multiplication lead to different results. There are two multiplication
Instructions: MUL (multiply) for unsigned multiplication and IMUL (integer multiply) for signed
multiplication.
The syntax of these instructions is:
MUL source

Byte form:
For byte multiplication, one number is contained in the source and the other is assumed to be in AL. The
16-bit product will be in AX. The source may be a byte register or memory byte, but not a constant.

Word Form:
For word multiplication, one number is contained in the source and the other is assumed to be in AX. The
most significant 16-bits of the double word product will be in DX and the least significant 16 bits will be
in AX (we sometimes write this as DX:AX). The source may be a 16-bit register or memory word, but not
a constant.

For multiplication of positive numbers (0 in the most significant bit), MUL and IMUL give the same result.

Effect of MUL on the status flags:


SF, ZF, PF, CF, OF: undefined

After MUL, CF/OF = 0 if the upper half of the result is


Zero = 1 otherwise.

12
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Example: Suppose AX contains 1 and BX contains FFFFh:

For MUL DX = 0, so CF/OF= 0.

Example: Suppose AX contains FFFh and BX contains FFFFh:

For MUL CF/OF = 1 because DX is not 0. This reflects the fact that the product FFFE000h is too big to fit
in AX.
Simple Applications of MUL:

.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV CX, 5
MOV AX, 1
FACT:
MUL CX
LOOP FACT

CALL DECOUT
MAIN ENDP
INCLUDE D:\DECOUT.ASM
END MAIN

13
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 DIVISION:
When division is performed, we obtain two results, the quotient and the remainder. As with multiplication,
there are separate Instructions for unsigned and signed division, DIV (divide) is used for unsigned division
and IDIV-(integer divide)-for signed division.
The syntax is:
DIV divisor
Byte Form:
In this form, the divisor is an 8-bit register or memory byte. The 16-bit dividend is assumed to be in AX.
After division, the 8-bit quotient is in AL and the 8-bit remainder is in AH. The divisor may not be a
constant.

Word Form:
Here the divisor is a 16-bit register or memory word. The 32-bit dividend Is assumed to be In DX : AX,
After division, the 16-bit quotient is in AX and the 16-bit remainder is In DX. The divisor may not be a
constant.

Divide Overflow:
It is possible that the quotient will be too big to fit in the specified destination (AL or AX). This can happen
if the divisor is much smaller than the dividend: When this happens, the program terminates and the system
displays 'the message "Divide Overflow"

Example: Suppose DX contains 000h, AX contains 0005h, and BX contains 0002h.

 Decimal Input and Output Procedures:


Even though the computer represents everything in binary, it's more convenient for the user to see input
and output expressed in decimal in this section, we write procedures for handling decimal I/O.

DECIMAL INPUT:
To do decimal input, we need to convert a string of ASCII digits to the binary representation of a decimal
integer. We will write a procedure INDEC to do this. In procedure OUTDEC, to output the contents of AX
in decimal we repeatedly divided AX by 10. For INDEC we need repeated multiplication by 10.
Decimal Input Algorithm
total = 0
read an ASCII digit
REPEAT
convert character to a binary value
total = 10 x total + value
read a character
UNTIL character is a carriage return

14
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

For example, an input of 123 is processed as follows:


total = 0
read '1'
convert '1'. to 1
total = 10 x 0 + = 1
read '2'
convert '2' to 2
total = 10 x 1 + 2 = 12
read '3'
convert '3' to 3.
total = 10 x 12 + 3 – 123
We will design INDEC so that it can handle signed decimal integers in the range -32768 to 32767. The
program prints a question mark, and lets the user enter an optional sign, followed by a string of digits,
followed by a carriage return. If the user enters a character outside the range "0" ... "9", the procedure goes
to a new line and starts over.
Procedure:
DECIN PROC PUSH AX
PUSH BX
PUSH CX MOV AX, 10
PUSH DX MUL BX
POP BX
BEGIN: ADD BX, AX
MOV AH, 2
MOV DL, '?' MOV AH, 1
INT 21H INT 21H
CMP AL, 0DH
XOR BX, BX JNE REPEAT2
XOR CX, CX
MOV AX, BX
MOV AH, 1 OR CX, CX
INT 21H JE EXIT
NEG AX
CMP AL, '-'
JE MINUS EXIT:
CMP AL, '+' POP DX
JE PLUS POP CX
JMP REPEAT2 POP BX
RET
MINUS:
MOV CX, 1 NOT_DIGIT:
PLUS: MOV AH, 2
INT 21H MOV DL, 0DH
REPEAT2: INT 21H
CMP AL, '0'
JNGE NOT_DIGIT MOV DL, 0AH
CMP AL, '9' INT 21H
JNLE NOT_DIGIT
JMP BEGIN
AND AX, 000FH DECIN ENDP

15
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Decimal Output:
We will write a procedure OUTDEC to print the contents of AX as a signed decimal integer. If
AX >= 0, OUTDEC will print the contents in decimal; if AX < 0, OUTDEC will print a minus
sign, replace AX by -AX (so that AX now contains a positive number), and print the contents in
decimal.
Algorithm for Decimal Output
1. IF AX < 0 /*AX holds output value */
2. THEN
3. Print a minus sign
4. Replace AX by its two's complement
5. END IF
6. Get the digits in AX’s decimal representation
7. Convert these digits to characters and print them

To see what line 6 entails, suppose the content of AX, expressed in decimal, is 24168. To get the
digits In the decimal representation, we cal)proceed as follows:
Divide 24618 t1y 10. Quotient= 2461, remainder= 8
Divide 2461 by 10. Quotient= 246, remainder = 1
Divide 246 by 10. Quotient= 24, remainder= 6
Divide 24 by 10. Quotient 2, remainder = 4
Divide 2 by 10. Quotient = 0, remainder = 2

DECOUT PROC WHILE_:


PUSH AX XOR DX, DX
PUSH BX DIV BX
PUSH CX PUSH DX
PUSH DX INC CX
CMP AX, 0
ADD AX, 655D JNE WHILE_

CMP AX, 0 MOV AH, 2


JG ALPHA PRINT:
PUSH AX POP DX
ADD DL, 48
MOV DL, '-' INT 21H
MOV AH, 2 LOOP PRINT
INT 21H
POP DX
POP AX POP CX
NEG AX POP BX
POP AX
ALPHA:
XOR CX, CX RET
MOV BX, 10D DECOUT ENDP

16
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

DECIMAL INPUT/OUTPUT
.MODEL SMALL JNGE NOT_DIGIT
.STACK 100H CMP AL, '9'
.CODE JNLE NOT_DIGIT
MAIN PROC
CALL DECIN AND AX, 000FH
PUSH AX
CALL DECOUT
MOV AH, 4CH MOV AX, 10
INT 21H MUL BX
MAIN ENDP POP BX
ADD BX, AX
DECIN PROC
PUSH BX MOV AH, 1
PUSH CX INT 21H
PUSH DX CMP AL, 0DH
JNE REPEAT2
BEGIN:
MOV AH, 2 MOV AX, BX
MOV DL, '?' OR CX, CX
INT 21H JE EXIT
NEG AX
XOR BX, BX
XOR CX, CX EXIT:
POP DX
MOV AH, 1 POP CX
INT 21H POP BX
RET
CMP AL, '-'
JE MINUS NOT_DIGIT:
CMP AL, '+' MOV AH, 2
JE PLUS MOV DL, 0DH
JMP REPEAT2 INT 21H

MINUS:
MOV CX, 1 MOV DL, 0AH
PLUS: INT 21H
INT 21H
JMP BEGIN
REPEAT2: DECIN ENDP
CMP AL, '0'

17
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

DECOUT PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX

CMP AX, 0
JG ALPHA
PUSH AX

MOV DL, '-'


MOV AH, 2
INT 21H

POP AX
NEG AX

ALPHA:
XOR CX, CX
MOV BX, 10D

WHILE_:
XOR DX, DX
DIV BX
PUSH DX
INC CX
CMP AX, 0
JNE WHILE_

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

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

POP DX
POP CX
POP BX
POP AX

RET
DECOUT ENDP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
END MAIN

18
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

ADD DECIMAL INPUT LCM


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.CODE .DATA
MAIN PROC NUM1 DW 30
CALL DECIN NUM2 DW 45
PUSH AX HCF DW ?
LCM DW ?
MOV AH,2 .CODE
MOV DL,10 MAIN PROC
INT 21H MOV AX, @DATA
MOV DL,13 MOV DS, AX
INT 21H
MOV AX, NUM1
POP AX MOV BX, NUM2
WHILEE_:
MOV CX,0 MOV DX, 0
MOV BX,10 MOV CX, BX

DIVI:
MOV DX,0 DIV BX
DIV BX MOV BX, DX
PUSH DX MOV AX, CX
INC CX CMP BX, 0
CMP AX,0 JNE WHILEE_
JNE DIVI
MOV HCF, AX
MOV AX,0 MOV CX, AX
ADDUP: MOV AX, NUM1
POP DX MOV BX, NUM2
ADD AX,DX
LOOP ADDUP MUL BX
DIV CX
CALL DECOUT MOV LCM, AX
MOV AH, 4CH CALL DECOUT
INT 21H MOV AH, 4CH
MAIN ENDP INT 21H
END MAIN MAIN ENDP
END MAIN

19
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 11) Lecture 21 & 22


Objectives: Learning objectives of this lecture are

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

This lecture will cover Arrays and Addressing Modes. To access an array in assembly language,
we use a pointer. A pointer is simply a register or variable that contains a memory address.
Most assembly language instructions require operands to be processed. When an instruction
requires two operands, the first operand is generally the destination, which contains data in a
register or memory location and the second operand is the source. Source contains either the
data to be delivered (immediate addressing) or the address (in register or memory) of the data.
Generally, the source data remains unaltered after the operation.

 One-Dimensional Arrays:
A one-dimensional array is an ordered list of elements, all of the same type. By “ordered,” we
mean that there is a first element, second element, third element, and so on. In mathematics, if A
is an array, the elements are usually denoted by A[1], A[2], A[3] and so on. Figure shows a one
dimensional array A with six elements.

Example:
MSG DB ‘abcde’

W DW 10, 20, 30, 40, 50, 60

20
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Or a word array of six integers, initialized to 10, 20, 30, 40, 50, 60 variable is called the base
address of the array. If the offset address assigned to W is 0200h, the array looks like this in
memory:

Offset address Symbolic address Decimal content


0200h W 10
0202h W+2h 20
0204h W+4h 30
0206h W+6h 40
0208h W+8h 50
020Ah W+Ah 60

The Dup Operator:


It is possible to define arrays whose elements share a common initial value by using the DUP
(duplicate) operator. It has this form:
repeat_count Dup (value)

This operator causes value to be repeated the number of times specified by repeat_count. For
example:
GAMMA DW 100 DUP (0)

Sets up an array of 100 words, with each entry initialized to 0. Similarly,

DELTA DB 212 DUP (?)

Creates an array of 212 uninitialized bytes. DUPs may be nested. For Example:
LINE DB 5,4, 3 DUP (2,3 DUP (0),1)

Which is equivalent to:


LINE DB 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1

Location of Array Elements:


The address of an array element may be specified by adding a constant to the base address. Suppose
A is an array and S denotes the number of bytes in an element (S=1 for a byte array, S=2 for a
word array). The position of the elements in array A can be determined as follows:

21
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Position Location
1 A
2 A=1XS
3 A=2XS
4 .
5 .
6 .
N A=(N-1)XS

Example: Exchange the 10th and 25th elements in a word array W.


MOV AX, W+18
XCHG W+48, AX
MOV W+18, AX

In many applications we need to perform some operation on each element of an array. For example,
suppose array is a 10-element array, and we want to add the elements. In a high-level language,
we could do it like this:

SUM=0
N=1
REPEAT
SUM=SUM+A[N]
N=N+1
UNTIL N>10

To code this in assembly language, we need a way to move from one array element to the next
one. In the next section, we’ll see how to accomplish this by direct addressing.

22
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 Addressing Modes:
The way and operand is known as its addressing mode. The addressing modes we have used so far
are:
1. Register mode: which means that an operand is a register.
2. Immediate mode: when an operand is a constant.
3. Direct mode: when an operand is a variable.
4. Register indirect Mode
In this mode, the offset address of the operand is contained in register. We say that the register acts
a pointer to the memory location.

The register BX, SI, DI, or BP. For BX, SI, or DI, the operand’s segment number is contained in
DS. For BP, SS has the segment numbers.

For example, suppose that SI contains 0100h, and the word at 0100h contains 1234h. To execute
MOV AX, SI

The CPU (1) examines SI and obtains the offset address 100h, (2) uses the address DS:0100h to
obtain the value 1234h, and (3) moves 1234h to AX. This is not the same as:
MOV AX, SI
Example: Suppose that
BX contains 1000h Offset 1000h contains 1BACh
SI contains 2000h Offset 2000h contains 20FEh
DI contains 3000h Offset 3000h contains 031Dh

Where the above offsets are in the data segment addressed by DS.

Tell which of the following instructions are legal. If legal, give the source offset address and the
result or number moved.
a. MOV BX, [BX]
b. MOV CX, [SI]
c. MOV BX, [AX]
d. ADD [SI], [DI]
e. INC [DI]

23
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Solution:
Source offset Result
a. 1000h 1BACh
b. 2000h 20FEh
c. Illegal source register (must be BX, SI or DI)
d. Illegal memory-memory Addition
e. 3000h 031Eh
Now let’s return to the problem of adding the elements of an array.

Example: Write some code to sum in AX the elements of the 10-elements array W defined by:
W DW 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
Solution: The idea is to set a pointer to the base of the array, and let it move up the array,
summing elements as it goes.
XOR AX, AX
LEA SI, W
ADDNOS:
ADD AX, [SI]
ADD SI, 2
LOOP ADDNOS

Example: Write a procedure REVERSE that will reverse an array of N words. This means that
the Nth word becomes the first, the (N-1) st word becomes the second, and so on, and the first
word becomes the Nth word. The procedure is entered with SI pointing to the array, and BX has
the number of words N.
Solution: The idea is to exchange the 1st and Nth words, the 2nd and (N-1) st word, and so on. The
number of exchanges will be N/2 (rounded down to the nearest integer if N is odd). Recall from
section 10.1 that the Nth element in a word array A has address A + 2 x (N - 1).
PUSH AX XCHG_LOOP:
PUSH BX MOV AX, [SI]
PUXH CX XCHG AX, [DI]
PUSH SI MOV [SI], AX
PUSH DI ADD SI, 2
; make DI point to nth word SUB DI, 2
MOV DI, SI LOOP XCHG_LOOP
MOV CX, BX POP DI
DEC BX POP SI
SHL BX, 1 POP CX
ADD DI, BX POP BX
SHR CX,1 POP AX
; swap elements REVERSE ENDP

24
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 Based and Indexed Addressing Modes:


In this modes, the operand’s offset address is obtained by adding a number called a
displacement to the contents of a register. Displacement may be any of the following:
 The offset address of a variable
 A constant (positive or negative)
 The offset address of a variable plus or minus a constant

If A is a variable, examples of displacements are:


A (offset address of a variable)
-2 (constant)
A+4 (offset address of a variable plus a constant)
Example:
Rework example by using based mode.
Solution:
The idea is to clear base register BX, then add 2 to it on each trip through the summing loop.
XOR AX, AX
XOR BX, BX
MOV CX, 10
ADDNOS:
ADD AW, W [BX]
ADD BX, 2
LOOP ADDNOS
Example:
Suppose that ALPHA is declared as
ALPHA DW 0123h, 0456h, 0789h, 0ABCDh
In the segment addressed by DS. Suppose also that
BX contains 2 Offset 0002 contains 1084h
SI contains 4 Offset 0004 contains 2BACh
DI contains 1

Tell which of the following instructions are legal. If legal, give the source offset address and
the number moved.
a. MOV AX, [ALPHA+BX]
b. MOV BX, [BX+2]
c. MOV CX, ALPAH[SI]
d. MOV AX, -2[SI]
e. MOV BX, [ALPHA+3+DI]
f. MOV AX, [BX]2
g. ADD BX, [ALPHA+AX]

25
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Solution:
Source offset Number moved
a. ALPHA+2 0456h
b. 2+2=4 2BACh
c. ALPHA+4 0789h
d. -2+4=2 1084h
e. ALPHA+3+1= ALPHA+4 0789h
f. Illegal form of source operand Illegal source register

Example:
Replace each lowercase letter in the following string by its upper-case equivalent. Use index
addressing mode.

Solution:
MOV CX, 17
XOR SI, SI
TOP:
CMP MSG[SI], ‘ ‘
JE NEXT
AND MSG[SI], 0DFh
NEXT:
INC SI
LOOP TOP

26
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

ARRAY (Addition) AVERAGE


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
ARR DW 100, 200, 300, 400, 500, 600, 700 ARR DW 100, 200, 300, 400, 500, 600, 700
.CODE .CODE
MAIN PROC MAIN PROC
MOV AX, @DATA MOV AX, @DATA
MOV DS, AX MOV DS, AX

LEA SI, ARR LEA SI, ARR


MOV CX, 7 MOV CX, 7
MOV AX, 0 MOV AX, 0

ADDUP: ADDUP:
ADD AX,[SI] ADD AX,[SI]
ADD SI, 2 ADD SI, 2
LOOP ADDUP LOOP ADDUP
CALL DECOUT
XOR DX, DX
MOV AH, 4CH MOV BX, 7
INT 21H DIV BX
MAIN ENDP
INCLUDE D:\DECOUT.ASM CALL DECOUT
END MAIN
MOV AH, 4CH
INT 21H

MAIN ENDP
INCLUDE D:\DECOUT.ASM
END MAIN

27
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

SEARCH NO NO OF OCCURRENCE
.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
MSG1 DB 10,13,"FOUND$" COUNT DW 0
MSG2 DB 10,13,"NOT FOUND$" X DW 100
X DW 100 ARR DW 800, 200, 100, 400, 500, 100, 700
ARR DW 800, 200, 100, 400, 500, 600, 700 .CODE
.CODE MAIN PROC
MAIN PROC MOV AX, @DATA
MOV AX, @DATA MOV DS, AX
MOV DS, AX
LEA SI, ARR
LEA SI, ARR MOV CX, 7
MOV CX, 7 MOV AX, X
MOV AX, X
ADDUP:
ADDUP: CMP AX, [SI]
CMP AX, [SI] JE PLUS
JE FOUND ADD SI, 2
ADD SI, 2 LOOP ADDUP
LOOP ADDUP JMP EXIT

MOV AH, 9 PLUS:


LEA DX, MSG2 INC COUNT
INT 21H ADD SI, 2
JMP EXIT JMP ADDUP
EXIT:
FOUND: MOV AX, COUNT
MOV AH, 9 CALL DECOUT
LEA DX, MSG1
INT 21H MOV AH, 4CH
INT 21H
EXIT:
MAIN ENDP MAIN ENDP
END MAIN INCLUDE D:\DECOUT.ASM
END MAIN

28
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

MIN NO MAX NO
.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
ARR DW 800, 200, 700, 400, 900, 600, 100 ARR DW 800, 200, 700, 400, 900, 600, 100
.CODE .CODE
MAIN PROC MAIN PROC
MOV AX, @DATA MOV AX, @DATA
MOV DS, AX MOV DS, AX

LEA SI, ARR LEA SI, ARR


MOV BX, [SI] MOV BX, [SI]
MOV CX, 7 MOV CX, 7

ADDUP: ADDUP:
CMP BX, [SI] CMP BX, [SI]
JG SWAP JL SWAP
ADD SI, 2 ADD SI, 2
LOOP ADDUP LOOP ADDUP

JMP EXIT JMP EXIT

SWAP: SWAP:
XCHG BX, [SI] XCHG BX, [SI]
ADD SI, 2 ADD SI, 2
LOOP ADDUP LOOP ADDUP

EXIT: EXIT:
MOV AX, BX MOV AX, BX
CALL DECOUT CALL DECOUT

MOV AH, 4CH MOV AH, 4CH


INT 21H INT 21H

MAIN ENDP MAIN ENDP


INCLUDE D:\DECOUT.ASM INCLUDE D:\DECOUT.ASM
END MAIN END MAIN

29
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 12) Lecture 23 & 24

Objectives: Learning objectives of this lecture are


Linked list

Insert, update, delete
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

Introduction
Basically, there are many data structures like arrays, linked lists, etc. Each sort of arrangement has its
strengths and weaknesses. For these reasons, it’s important to know the benefits and drawbacks of different
data structures when it comes to designing, optimizing, and scaling programs. In this lecture we are going
to discuss that what is linked list and how we can implement it in assembly.
So, let’s try to understand about linked list.

Linked List
A linked list is a linear data structure. It is a collection of nodes, and a node contains data and addresses the
next node.
Or in another words, A linked list is a sequence of data structures, which are connected together via links.
Linked List is a sequence of links which contains items. Each link contains a connection to another link.
Linked list is the second most-used data structure after array. It’s a set of structures ordered not by their
physical placement in memory (like an array) but by logical links that are stored as a part of the info within
the structure itself.
A linked list is another way to collect similar data. However, unlike an array, elements in a linked list aren’t
in consecutive memory locations. A linked list consists of nodes that are connected with one another using
pointers. The figure illustrates a linked list.

30
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Types Of Linked List:


 Singly Linked List: It is the simplest type of linked list in which every node contains some data
and a pointer to the next node of the same data type. The node contains a pointer to the next node
means that the node stores the address of the next node in the sequence. A single linked list allows
the traversal of data only in one way.
 Doubly or Two-Way Linked List: A doubly linked list or a two-way linked list is a more complex
type of linked list that contains a pointer to the next as well as the previous node in sequence,
Therefore, it contains three parts of data, a pointer to the next node, and a pointer to the previous
node. This would enable us to traverse the list in the backward direction as well.
 Circular Linked List: A circular linked list is one in which the last node contains the pointer to
the first node of the list. While traversing a circular linked list, one can begin at any node and
traverse the list in any direction forward and backward until reaching the same node where it
started. Thus, a circular linked list has no beginning and no end.
 Circular Doubly Linked List: A Doubly Circular linked list or a circular two-way linked list is a
more complex type of linked-list that contains a pointer to the next as well as the previous node in
the sequence. The difference between the doubly linked and circular doubly list is the same as that
between a singly linked list and a circular linked list. The circular doubly linked list does not contain
null in the previous field of the first node.

Advantages Of Linked List


 Dynamic data structure: A linked list is a dynamic arrangement so it can grow and shrink at
runtime by allocating and deallocating memory. So there is no need to give the initial size of the
linked list like an array.
 No memory wastage: In the Linked list, efficient memory utilization can be achieved since the
size of the linked list increase or decrease at run time so there is no memory wastage and there is
no need to pre-allocate the memory.
 Implementation: Linear data structures like stacks and queues are often easily implemented using
a linked list.
 Insertion and Deletion Operations: Insertion and deletion operations are quite easier in the linked
list. There is no need to shift elements (like an array) after the insertion or deletion of an element
only the address present in the next pointer needs to be updated.
 Flexible: This is because the elements in Linked List are not stored in contiguous memory locations
unlike the array.
 Efficient for large data: When working with large datasets linked lists play a crucial role as it can
grow and shrink dynamically.
 Scalability: Contains the ability to add or remove elements at any position.

31
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Disadvantages Of Linked List


 Memory usage: More memory is required in the linked list as compared to an array. Because in a
linked list, a pointer is also required to store the address of the next element and it requires extra
memory for itself.
 Traversal: In a Linked list traversal is more time-consuming as compared to an array. Direct access
to an element is not possible in a linked list as in an array by index. For example, for accessing a
node at position n, one has to traverse all the nodes before it.
 Reverse Traversing: In a singly linked list reverse traversing is not possible, but in the case of a
doubly-linked list, it can be possible as it contains a pointer to the previously connected nodes with
each node. For performing this extra memory is required for the back pointer hence, there is a
wastage of memory.
 Random Access: Random access is not possible in a linked list due to its dynamic memory
allocation.
 Lower efficiency at times: For certain operations, such as searching for an element or iterating
through the list, can be slower in a linked list.
 Complex implementation: The linked list implementation is more complex when compared to
array. It requires a complex programming understanding.
 Difficult to share data: This is because it is not possible to directly access the memory address of
an element in a linked list.
 Not suited for small dataset: Cannot provide any significant benefits on small dataset compare to
that of an array.

Applications of Linked List


Applications of linked list in computer science:
1. Implementation of stacks and queues
2. Implementation of graphs: Adjacency list representation of graphs is the most popular which uses
a linked list to store adjacent vertices.
3. Dynamic memory allocation: We use a linked list of free blocks.
4. Maintaining a directory of names
5. Performing arithmetic operations on long integers
6. Manipulation of polynomials by storing constants in the node of the linked list
7. representing sparse matrices
Applications of linked list in the real world:
1. Image viewer – Previous and next images are linked and can be accessed by the next and previous
buttons.
2. Previous and next page in a web browser – We can access the previous and next URL searched
in a web browser by pressing the back and next buttons since they are linked as a linked list.
3. Music Player – Songs in the music player are linked to the previous and next songs. So you can
play songs either from starting or ending of the list.

32
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Implementation in Assembly
Linked List
.MODEL SMALL
.STACK 100H
.DATA
ARR DB 10 DUP(?, -1) 0 1 2 3 4 5 6 7 8 9
P1 DW 0 ? -1 ? -1 ? -1 ? -1 ? -1
P2 DW 1
P3 DW 0
FD DW 0
M1 DB 10,13,"NUMBER NOT FOUND$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV CX, 1

MOV AX, 55 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 -1 ? -1 ? -1 ? -1 ? -1
CALL INSERT
55
CALL DISPLAY

CALL NEXT_LINE

MOV AX, 66 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 2 66 -1 ? -1 ? -1 ? -1
CALL INSERT
55 66
CALL DISPLAY

CALL NEXT_LINE

MOV AX, 77 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 2 66 4 77 -1 ? -1 ? -1
CALL INSERT 55 66 77
CALL DISPLAY

CALL NEXT_LINE

MOV AX, 88 0 1 2 3 4 5 6 7 8 9
;CALL DECINPUT 55 2 66 4 77 6 88 -1 ? -1
CALL INSERT 55 66 77 88
CALL DISPLAY

33
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

CALL NEXT_LINE

MOV AL, 77 0 1 2 3 4 5 6 7 8 9
CALL DELETE 55 2 66 6 77 6 88 -1 ? -1
CALL DISPLAY
55 66 88

CALL NEXT_LINE

MOV AX, 99
;CALL DECINPUT 0 1 2 3 4 5 6 7 8 9
CALL INSERT 55 2 66 6 77 6 88 8 99 -1
CALL DISPLAY 55 66 88 99

CALL NEXT_LINE

MOV AL, 66 0 1 2 3 4 5 6 7 8 9
CALL DELETE 55 6 66 6 77 6 88 8 99 -1
CALL DISPLAY 55 88 99

MOV AH, 4CH


INT 21H
MAIN ENDP

34
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

DELETE PROC INSERT PROC


MOV SI, FD INC CX
MOV BX, 0
CMP P1, 0
SEARCH: JNE SECOND
CMP ARR[SI], AL
JE FIND MOV SI, P1
MOV ARR[SI], AL
INC SI
MOV P3, SI INC P1
INC P1
MOV BL, ARR[SI] RET
MOV SI, BX SECOND:
LOOP SEARCH MOV SI, P2
MOV BX, P1
MOV AH, 9 MOV ARR[SI], BL
LEA DX, M1
INT 21H INC P2
INC P2
JMP TERM
FIND: MOV DI, P1
CMP P3, 0 MOV ARR[DI], AL
JE DELF
INC P1
MOV DI, P3 INC P1
INC SI RET
INSERT ENDP
MOV BL, ARR[SI]
MOV ARR[DI], BL

JMP TERM
DELF:
INC FD
INC FD
TERM:
MOV P3, 0
RET
DELETE ENDP

DISPLAY PROC
MOV DI, FD

35
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

PRINT:
XOR AX, AX
MOV SI, DI
MOV AL, ARR[DI]
;ADD DL, 48
;INT 21H
CALL DECOUTPUT

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

INC SI

CMP ARR[SI], -1
JE EXIT

MOV BL, ARR[SI]


MOV DI, BX
JMP PRINT
EXIT:
RET
DISPLAY ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
NEXT_LINE PROC
PUSH AX
PUSH DX

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

POP DX
POP AX
RET
NEXT_LINE ENDP

36
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

DECINPUT PROC MOV DL,10


PUSH BX INT 21H
PUSH CX MOV DL,13
PUSH DX INT 21H
BEGIN:
XOR BX,BX JMP Begin
XOR CX,CX RET
DECINPUT ENDP
MOV AH,1 DECOUTPUT PROC
INT 21H PUSH AX
PUSH BX
CMP AL,'-' PUSH CX
JE MINUS PUSH DX
CMP AL,'+'
JE PLUS CMP AX,0
JGE ALPHA1
JMP REPEAT2
MINUS: PUSH AX
MOV CX,1
PLUS: MOV DL,'-'
INT 21H MOV AH,2
REPEAT2: INT 21H
CMP AL,'0'
JL Not_Digit POP AX
CMP AL,'9'
JG Not_Digit NEG AX
ALPHA1:
AND AX,000FH XOR CX,CX
PUSH AX MOV BX,10
WHILE:
MOV AX,10 XOR DX,DX
MUL BX
DIV BX
POP BX PUSH DX
ADD BX,AX
INC CX
MOV AH,1
INT 21H CMP AX,0
JNE WHILE
CMP AL,13
JNE REPEAT2 MOV AH,2
PRINT1:
MOV AX,BX POP DX
ADD DL,48
CMP CX,0 INT 21H
JE EXIT2 LOOP PRINT1

NEG AX POP DX
EXIT2: POP CX
POP DX POP BX
POP CX POP AX
POP BX
RET RET
Not_Digit: DECOUTPUT ENDP
MOV AH,2 END MAIN

37
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 13) Lecture 25 & 26


Objectives: Learning objectives of this lecture are

 The Direction Flag


 String Instructions: MOVS, LODS, STOS, CMPS, SCAS
 Repetition Prefixes: REP, REPE, REPZ
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

In this lecture we will firstly cover the direction flag. One of the control flags in the direction flag
(DF). Its purpose is to determine the direction in which string operation will proceed. These
operations are implemented the two index registers will proceed. These operations are
implemented by the two index registers SI and DI. Then will proceed to load, store, compare and
scan string along with different programming examples.

 The Direction Flag


Previous, we saw that the FLAGS register contains six status flags and three control flags. We
know that the status flags reflect the result of an operation that the processor has done. The
control flags are used to control the processor’s operations.
One of the control flags in the direction flag (DF). Its purpose is to determine the direction in
which string operation will proceed. These operations are implemented the two index registers
will proceed. These operations are implemented by the two index registers SI and DI. Suppose,
for example, that the following string has been declared:
STRING DB ‘ABCDE’
And this string is stored in memory starting at offset 0200h:
Offset address Content ASCII character
0200h 041h A
0201h 042h B
0202h 043h C
0203h 044h D
0204h 045h E

If DF=0, SI and DI proceed in the creation of increasing memory addresses from left to right
across the string conversely, if DF=1, SI and DI proceed in the direction of decreasing memory
addresses: from right to left.
In the DEBUG display DI=0 is symbolized by UP, and DF=1 by DN.

38
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

CLD and STD:


To make DF=0, use the CLD instruction:
CLD; Clear direction flag

To make DF=1, use the STD instruction:


STD; set direction flag

CLD and STD have no effect on the other flags

 Moving a String:
Suppose we have defined two strings as follows:
.DATA
STRING1 DB ‘HELLO’
STRING2 DB 5 DUP (?)

And we would like to move the contents of STRING 1 (the source string) into STRING2 (the
destination string). This operation is needed for many string operations, such a) duplicating a
string or concatenating strings (attaching one string to the end of another string).

The MOVSB instruction:

MOVSB ; move string byte

Copies the contents of the byte addressed by DS:SI, to the byte addressed by ES: DI. The contents
of the source byte are unchanged. After the byte has been moved, both SI and DI are automatically
incremented. If DF=0, or decremented If DF= I. For example, to move the first two bytes of
STRING1 to STRING2, we execute the following Instructions:

MOV AX, @DATA


MOV DS, AX
MOV ES, AX
LEA SI, STRING1
LEA DI, STRING2
CLD
MOVSB
MOVSB

MOVSB is the first instruction we have seen that permits a memory- memory operation. It is also
the first instruction that involves the ES register.

39
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 The REP Prefix:


MOVSB moves only a single byte from the source string to the destination string. To move
the entire string, first initialize CX to the number N of bytes ln the source string and execute.

The REP prefix causes MOVSB to be executed N times. After each MOVSB, CX is
decremented until it becomes 0. For example, to copy STRING1 of the preceding section into
STRING2, we execute:
CLD
LEA SI, STRING1
LEA DI, STRING2
MOV CX, 5 ; no. of chars in STRING1
REP MOVSB

Example: Write instructions to copy STRING 1 of the preceding section into STRING2 in
reverse order.

Solution: The idea is to get SI pointing to the end of STRING 1, DI to the beginning of
STRING2, then move characters as SI travels to the left across STRING1.

LEA SI, STRING1+4


LEA DI, STRING2
STD
MOV CX, 5
MOVE:
MOVSB
ADD DI, 2
LOOP MOVE

Here it is necessary to add 2 to DI after each MOVSB. Because we do this when


OF = l, MOVSB automatically decrements both SI and DI, and we want to
increment DI.
MOVSW
There is a word form of MOVSB. It is

MOVSW ; move string word

MOVSW moves a word from the source string to the destination string. Like MOVSB, it expects
DS:SI to point to a source string word, and ES:DI to point to a destination string word. After a
string word has been moved, both SI and DI are increased by 2 if DF = 0, or are decreased by 2 if
OF = 1. MOVSB and MOVSW have no effect on the flags.

40
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Example: For the following array,


ARR DW 1o, 20, 40, 50, 60?
Write instructions to insert 30 between 20 and 40. (Assume DS and ES have been initialized
to the data segment.)
Solution: The idea is to move 40, 50, and 60 forward one position in the array, then insert 30.

STD
LEA SI, ARR+8h
LEA DI, ARR+8h
MOV CX, 3
PTR MOVSW
MOV WORD PTR [DI], 30

 Store String:
The STOSB Instruction moves the contents of the AL register to the byte addressed b ES:DI. DI
is incremented if DF=0 or decremented if DF=1. Similarly, the STOSW instruction moves the
contents of AX to the word at address ES:DI and updates DI by 2, according to the direction flag
setting.
STOSB and STOSW have no effect on the flags.

As an example of STOSB, the following instructions will store two “A”s in STRING1:

MOV AX, @DATA


MOV ES, AX
LEA DI, STRING1
CLD
MOV AL, ‘A’
STOSB
STOSB

Reading and Storing a Character String:


INT 21h, function 1 reads a character from the keyboard into AL. By repeatedly executing this
interrupt with STOSB, we can read and store characters in a string, until a carriage return is typed.
The procedure is entered with the string offset address in DI. It returns the string offset in DI and
number of characters entered in BX. If the user makes a typing mistake and hits the backspace
key, the previous character is removed from the string.

41
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 Load String:
The LODSB instruction moves the byte addressed by DS:SI into AL.SI is then incremented if
DF=O or decremented if DF=1. The word form is
LODSW. It moves the word addressed by DS:SI into AX;SI is incremented if By 2 if DF=O
or decreased by 2 if DF=1.
LODSB can be used to examine to characters of a string, as shown later.
LODSB and LODSW have no effect on the flags.

 Scan String:
SCASB can be used to examine a string for a target byte. The target byte is contained in Al.
SCASW in this case; the target word is in AX.

 Compare String:
The CMPSB Instruction CMPSB subtracts the byte with address ES:DI from the byte with
address DS:SI, and sets the flags. CMPSW it subtracts the word with address ES:DI from the
word whose address is DS:SI, and sets the flags.

42
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

String Input/output MAIN ENDP


.MODEL SMALL END MAIN
.STACK 100H
.DATA String Reverse
STR DB 20 DUP(?)
.MODEL SMALL
.CODE
.STACK 100H
MAIN PROC
.DATA
MOV AX, @DATA
STR1 DB "HELLOWORLD"
MOV DS, AX
STR2 DB 10 DUP(?)
MOV ES, AX
.CODE
MAIN PROC
LEA DI, STR
MOV AX,@DATA
MOV CX, 0
MOV DS, AX
MOV AH, 1
MOV ES, AX
CLD
INPUT:
LEA SI, STR1
INT 21H
LEA DI, STR2
CMP AL, 13
ADD DI, 9
JE END_INP
CLD
INC CX
STOSB
MOV CX, 10
JMP INPUT
SWAP:
END_INP:
MOVSB
MOV DL, 10
SUB DI, 2
MOV AH, 2
LOOP SWAP
INT 21H
MOV DL, 13
LEA SI, STR2
MOV AH, 2
CLD
INT 21H
MOV AH, 2
MOV CX, 10
LEA SI, STR
PRINT:
CLD
LODSB
MOV AH, 2
MOV DL, AL
PRINT:
INT 21H
LODSB
LOOP PRINT
MOV DL, AL
INT 21H
MOV AH, 4CH
LOOP PRINT
INT 21H
MOV AH, 4CH
MAIN ENDP
INT 21H
END MAIN

43
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

String Reverse 2 COPY IN SAME STRING


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
STR1 DB "HELLOWORLD" STR1 DB "HELLO"
STR2 DB 10 DUP(?) .CODE
.CODE MAIN PROC
MAIN PROC MOV AX,@DATA
MOV AX,@DATA MOV DS, AX
MOV DS, AX MOV ES, AX
MOV ES, AX
LEA SI, STR1
LEA SI, STR1 LEA DI, STR1
LEA DI, STR2 ADD DI, 5
ADD SI, 9 CLD
CLD
MOV CX, 5
MOV CX, 10 REP MOVSB

SWAP: LEA SI, STR1


MOVSB CLD
SUB SI, 2 MOV AH, 2
LOOP SWAP MOV CX, 10

LEA SI, STR2 PRINT:


CLD LODSB
MOV AH, 2 MOV DL, AL
MOV CX, 10 INT 21H
PRINT: LOOP PRINT
LODSB
MOV DL, AL MOV AH, 4CH
INT 21H INT 21H
LOOP PRINT MAIN ENDP
END MAIN
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN

44
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

REVERSE IN SAME STRING STRING APPEND


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
STR1 DB "HELLO" STR1 DB "FGHIJ"
.CODE STR2 DB "ABCDE"
MAIN PROC .CODE
MOV AX,@DATA MAIN PROC
MOV DS, AX MOV AX,@DATA
MOV ES, AX MOV DS, AX
MOV ES, AX
LEA SI, STR1
LEA DI, STR1 LEA SI, STR1
ADD DI, 9 LEA DI, STR2
CLD ADD DI, 5
CLD
MOV CX, 5
SWAP: MOV CX, 10
MOVSB SWAP:
SUB DI, 2 MOVSB
LOOP SWAP LOOP SWAP

LEA SI, STR1 LEA SI, STR2


CLD CLD
MOV AH, 2 MOV AH, 2
MOV CX, 10 MOV CX, 10
PRINT:
PRINT: LODSB
LODSB MOV DL, AL
MOV DL, AL INT 21H
INT 21H LOOP PRINT
LOOP PRINT
MOV AH, 4CH MOV AH, 4CH
INT 21H INT 21H
MAIN ENDP MAIN ENDP
END MAIN END MAIN

45
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

FIND STRING LENGTH REMOVE SPACES IN A STRING


.MODEL SMALL .MODEL SMALL
.STACK 100H .STACK 100H
.DATA .DATA
STR1 DB "THIS IS AN ASCIIZ STRING",0 STR1 DB "T H I S I S A T E S T"
.CODE STR2 DB 11 DUP(?)
MAIN PROC .CODE
MOV AX,@DATA MAIN PROC
MOV DS, AX MOV AX,@DATA
MOV DS, AX
LEA SI, STR1 MOV ES, AX
LEA DI, STR1
CLD LEA SI, STR1
LEA DI, STR2
MOV DL, 0 CLD
WILE_:
LODSB MOV CX, 11
INC DL SWAP:
CMP AL, 0 MOVSB
JNE WILE_ ADD SI, 1
LOOP SWAP
DEC DL
MOV AX, DX LEA SI, STR2
CALL DECOUT CLD
MOV AH, 2
MOV AH, 4CH MOV CX, 11
INT 21H PRINT:
MAIN ENDP LODSB
INCLUDE D:\DECOUT.ASM MOV DL, AL
END MAIN INT 21H
LOOP PRINT

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

46
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

FIND SUBSTRING
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB "ENTER sUB STRING",10,13,"$"
MSG2 DB 10,13,"ENTER MAIN STRING ",10,13,"$"
MAINST DB 80 DUP (0)
SUBST DB 80 DUP (0)
STOP DW ? ;LAST PLACE TO BEGIN SEARCH
START DW ? ;PLACE TO RESUME SEARCH
SUB_LEN DW ?
YESMSG DB 10,13,"SUBSTRING IS IN MAIN STRING $"
NOMSG DB 10,13,"SUBSTRING IS NOT IN MAIN STRING $"
.CODE
MAIN PROC

MOV AX, @DATA SUB STOP, CX


MOV DS, AX
MOV ES, AX MOV START, DI
REPEAT:
MOV AH,9 ;MOV CLEN
LEA DX, MSG1 MOV DI, START
INT 21H LEA SI, SUBST
REPE CMPSB
LEA DI, SUBST JE YES
CALL READSTR
MOV SUB_LEN, BX INC START

LEA DX, MSG2 MOV AX, START


INT 21H CMP AX, STOP
JNLE NO
LEA DI, MAINST JMP REPEAT
CALL READSTR YES:
LEA DX, YESMSG
OR BX, BX JMP DISPLAY
JE NO NO:
CMP SUB_LEN, BX LEA DX, NOMSG
JG NO DISPLAY:
LEA SI, SUBST MOV AH,9
LEA DI, MAINST INT 21H
CLD
MOV STOP, DI MOV AH,4CH
ADD STOP, BX INT 21H
MOV CX, SUB_LEN MAIN ENDP

47
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

READSTR PROC
PUSH AX
PUSH DI

CLD
XOR BX,BX
MOV AH,1
INT 21H

WHILE:
CMP AL,13
JE END_WHILE

CMP AL,8
JNE ELSE

DEC DI
DEC BX
JMP READ

ELSE:
STOSB
INC BX

READ:
INT 21H
JMP WHILE

END_WHILE:
POP DI
POP AX
RET

READSTR ENDP
END MAIN

48
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

NO OF CONSONENTS AND VOWELS


.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'Enter a string : $'
PROMPT_2 DB 0DH,0AH,'No. of Vowels = $'
PROMPT_3 DB 0DH,0AH,'No. of Consonants = $'
STRING DB 50 DUP (?)
C_VOWELS DB 'AEIOU'
S_VOWELS DB 'aeiou'
C_CONSONANTS DB 'BCDFGHJKLMNPQRSTVWXYZ'
S_CONSONANTS DB 'bcdfghjklmnpqrstvwxyz'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS and ES
MOV DS, AX
MOV ES, AX

LEA DX, PROMPT_1 ; load and display the string PROMPT_1


MOV AH, 9
INT 21H

LEA DI, STRING ; set DI=offset address of variable STRING


CALL READ_STR ; call the procedure READ_STR

XOR DX, DX ; clear DX


LEA SI, STRING ; set SI=offset address of variable STRING

OR BX, BX ; check BX for 0


JE @EXIT ; jump to label @EXIT if BX=0

@COUNT: ; jump label


LODSB ; set AL=DS:SI

LEA DI, C_VOWELS


MOV CX, 5 ; set CX=5

REPNE SCASB ; check AL is capital vowel or not


JE @INCREMENT_VOWELS

LEA DI, S_VOWELS


MOV CX, 5 ; set CX=5

REPNE SCASB ; check AL is small vowel or not


JE @INCREMENT_VOWELS

49
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

LEA DI, C_CONSONANTS ; set DI=offset address of variable


MOV CX, 21 ; set CX=21

REPNE SCASB ; check AL is capital consonant or not


JE @INCREMENT_CONSONANTS

LEA DI, S_CONSONANTS


MOV CX, 21 ; set CX=21

REPNE SCASB ; check AL is small consonant or not


JE @INCREMENT_CONSONANTS
JMP @NEXT ; otherwise, jump to label @NEXT

@INCREMENT_VOWELS: ; jump label


INC DL ; increment DL
JMP @NEXT ; jump to label @NEXT

@INCREMENT_CONSONANTS: ; jump label


INC DH ; increment DH

@NEXT: ; jump label


DEC BX ; decrement BX
JNE @COUNT ; jump to label @COUNT while BX!=0

@EXIT: ; jump label


MOV CX, DX ; set CX=DX

LEA DX, PROMPT_2 ; load and display the string PROMPT_2


MOV AH, 9
INT 21H

XOR AX, AX ; clear AX


MOV AL, CL ; set AL=CL
CALL DECOUT ; call the procedure OUTDEC

LEA DX, PROMPT_3 ; load and display the string PROMPT_3


MOV AH, 9
INT 21H

XOR AX, AX ; clear AX


MOV AL, CH ; set AL=CH

CALL DECOUT ; call the procedure OUTDEC


MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
INCLUDE D:\READ_STR.ASM
INCLUDE D:\DECOUT.ASM
END MAIN

50
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 14) Lecture 27 & 28


Objectives: Learning objectives of this lecture are

 Macros
 Macro Definition and Invocation
 Label Conflict
Text Book & Resources: Assembly Language Programming and Organization of the IBM-PC, by
Ytha Yu and Charles Marut

This lecture will cover Macros and label conflict along with different examples. A macro is a
block of text that has been given a name when MASM encounters the name during assembly, it
inserts the block into the program. The text may consist of instructions, pseudo-ops, comments, or
references to other macros.

 Macro Definition and Invocation


A macro is a block of text that has been given a name. when MASM encounters the
name during assembly, it inserts the block into the program. The text may consist of
instructions, pseudo-ops, comments, or references to other macros.

The syntax of macro definition is:


Macro_name MACRO d1, d2, . . . dn
Statements
ENDM

Example:
Define a macro to move a word into a word.
Solution:
MOVW WORD1, WORD2
PUSH WORD2
POP WORD1
ENDM

To use a macro in a program, we invoke it. The syntax is


Macro_name a1, a2, … an

51
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Where a1, a2, … an is a list of actual arguments. When MASM encounters the macro name,
it expands the macro; that is, it copies the macro statements into the program at the position
of invocation, just as if the user had typed them in. as it copies the statements, MASM
replaces each dummy argument di by the corresponding actual argument ai and creates the
machine code for any instructions.

Example:
Invoke the macro MOVW to move B to A, where A and B are word variables.
Solution:
MOV A, B
To expand this macro, MASM would copy the macro statements into the program at the
position of the call, replacing each occurrence of WORD1 by A, and WORD2 by B.
The result is:
PUSH B
POP A
In expanding a macro, the assembler simply substitutes the character strings defining the
actual arguments for the corresponding dummy ones. For example, the following calls
to the MOVW macro:
MOVW A, DX and MOVW A+2, B
Cause the assembler to insert this code into the program:
PUSH DX and PUSH B
POP A POP A+2

 Label conflict:
A macro with a loop or decision structure contains one or more labels. If such a macro is Invoked
more than once In a program, a duplicate label appears, resulting in an assembly error: This
problem can be avoided by using local labels In the macro. To declare them, we use the LOCAL
pseudo-op, whose syntax is:
LOCAL list_of _ labels

Where list_of_labels is a list of labels, separated by commas. Every time the macro is expanded,
MASM assigns different symbols to the labels in the list.
The LOCAL directive must appear on the next line after the MACRO statement.

52
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

 Macros and Procedures:


1. Procedures are called while Macros are expanded.
2. Procedure call generates a jump statement. (Branch with saving return address),
while there is no jump in Macros.
3. In Macros, program size may increase while Procedure this is not the case.
4. It is recommended that small codes should be handled by Macro while large code may by
handled by Procedures.

MACROS MACROS 1
.MODEL SMALL .MODEL SMALL
PRINT MACRO A,B PRINT MACRO A,B
MOV CX, A MOV CX, A
MOV DL, B MOV DL, B
MOV AH, 2 MOV AH, 2

DISP: DISP:
INT 21H INT 21H
LOOP DISP LOOP DISP
ENDM ENDM
.STACK 100H .STACK 100H
.CODE .CODE
MAIN PROC MAIN PROC
MOV BX, 10 PRINT 10, '*'
MOV AH, '*'
PRINT BX, AH MOV AH, 4CH
MOV AH, 4CH INT 21H
INT 21H
MAIN ENDP MAIN ENDP
END MAIN END MAIN

53
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

LABEL CONFLICT (MACROS)


.MODEL SMALL
MAX MACRO WORD1, WORD2
LOCAL EXIT
MOV AX, WORD1
CMP AX, WORD2
JG EXIT

MOV AX, WORD2


EXIT:
ENDM

.STACK 100H
.DATA

MSG DB "OK$"
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV BX, 10
MOV AH, '*'
MAX 30, 25
JMP EXIT

EXIT:
LEA DX, MSG
MOV AH, 9
INT 21H

MOV AH, 4CH


INT 21H
MAIN ENDP
END MAIN

54
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 15) Lecture 29 & 30


Objectives: Learning objectives of this lecture are

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

The inline assembler is a feature of some compilers that allows very low-level code written
in assembly to be embedded in a high-level language. In this lecture inline assembly along with
different programming examples will be covered.

Factorial
#include "stdafx.h" system("pause");
#include <windows.h> return 0;
#include <iostream> }
using namespace std;
int fact( int num)
int fact(int num); {
int x;
int main() __asm
{ {
int n; mov eax, 1
char c='y'; mov ecx,num
do{ z:
system("cls"); mul ecx
cout<<"Enter Number for factorial : "; loop z
cin>>n;
cout<<endl<<fact(n)<<endl; mov x,eax
cout<<"Enter Again (y/n) ? : "; }
cin>>c; return x;
} }
while(c=='y' || c=='Y');

55
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Power of a number
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

int power(int az,int bz);

int main()
{
int a,b;
char c='y';
do{
system("cls");
cout<<"Number = ";
cin>>a;
cout<<"power = ";
cin>>b;
cout<<a<<"^"<<b<<" = "<<power(a,b)<<endl;
cout<<"\n\nEnter Again (y/n) ? : ";
cin>>c;
}
while(c=='y' || c=='Y');
system("pause");
return 0;
}

int power(int az,int bz)


{
int x;
__asm
{
mov eax, az
mov ebx,az
mov ecx,bz
dec ecx
z:
mul ebx;
loop z
mov x,eax
}
return x;
}

56
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Arithmetic Operations (Inline Assembly)


#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int addition(int a,int b);
int subtraction(int a,int b);
int multiplication(int a,int b);
int division(int a,int b);
int main( )
{
int choice, fn,sn;
char a='y';
do {
system("cls");
cout<<"Enter 1 for addition\nEnter 2 for Subtraction\nEnter 3 for Multiplication\nEnter 4 for Division\n";
cout<<"Enter your choice : ";
cin>>choice;

cout<<"\n\nEnter 1st number : ";


cin>>fn;
cout<<"Enter 2nd number : ";
cin>>sn;
switch(choice){
case 1:
cout<<"Addition of "<<fn<<" and "<<sn<<" is = "<<addition(fn,sn);
break;
case 2:
cout<<"Subtr of "<<fn<<" and "<<sn<<" is = "<<subtraction(fn,sn);
break;
case 3:
cout<<"Mul of "<<fn<<" and "<<sn<<" is = "<<multiplication(fn,sn);
break;
case 4:
cout<<"Division of "<<fn<<" and "<<sn<<" is = "<<division(fn,sn);
break;
default:
cout<<"You enter wrong Operator";
break;
}
cout<<"\n\nDo you want to try again (Y/N) ? : ";
cin>>a;
}
while(a=='y' || a=='Y');
return 0;
}

57
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

int addition(int a,int b)


{
int x;
__asm
{
mov eax,a
add eax,b
mov x,eax

}
return x;
}
int subtraction(int a,int b)
{
int x;
__asm {
mov eax,a
sub eax,b
mov x,eax

}
return x;
}
int multiplication(int a,int b){
int x;
__asm {
mov eax,a
mov ebx,b
mul ebx
mov x,eax

}
return x;
}
int division(int a,int b){
int x;
__asm {
xor edx, edx
mov eax, a
div b
mov x,eax

}
return x;
}

58
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

(Week 16) Lecture 31 & 32


Objectives: Learning objectives of this lecture are

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

In this lecture recursion will be covered. A recursive procedure is one that calls itself. There are two kind
of recursion: direct and indirect. In direct recursion, the procedure calls itself and in indirect recursion, the
first procedure calls a second procedure, which in turn calls the first procedure.

A recursive procedure is a procedure that calls itself. Recursive procedures are important in high-level
languages, but the way the system uses the stack to implement these procedures is hidden from the
programmer. In - assembly language, the programmer must actually program the stack operations. So this
provides, an opportunity to see how recursion really works.

Addition of positive natural numbers


 1+2+3+...+n
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
;CALL DECIN

MOV AX, 3
PUSH AX

CALL ADDITION
CALL DECOUT

MOV AH, 4CH


INT 21H
MAIN ENDP

ADDITION PROC
PUSH BP
MOV BP, SP

CMP WORD PTR[BP + 4], 1


JG END_IF
MOV AX, 1
JMP RETURN

59
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

END_IF:
MOV CX, [BP + 4]

DEC CX
PUSH CX

CALL ADDITION

ADD AX, WORD PTR[BP + 4]

RETURN:
POP BP
RET 2
ADDITION ENDP
INCLUDE D:\DECIN.ASM
INCLUDE D:\DECOUT.ASM
MAIN ENDP

60
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Factorial
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AX, 5
PUSH AX

CALL FACTORIAL
CALL DECOUT

MOV AH, 4CH


INT 21H
MAIN ENDP

FACTORIAL PROC
PUSH BP
MOV BP, SP

CMP WORD PTR[BP + 4], 1


JG END_IF

MOV AX, 1
JMP RETURN

END_IF:
MOV CX, [BP + 4]

DEC CX
PUSH CX

CALL FACTORIAL

ADD AX, WORD PTR[BP + 4]

;MUL WORD PTR[BP + 4]

RETURN:
POP BP
RET 2
FACTORIAL ENDP
INCLUDE D:\DECIN.ASM
INCLUDE D:\DECOUT.ASM
MAIN ENDP

61
Computer Organization & Assembly Language (CS-530)
Mr. Muhammad Azeem email id: [email protected], WhatsApp# 0345-5106587

Searching
.MODEL SAMLL
.STACK 100H
.DATA
Max DW 3, 5, 2, 4
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX

MOV AX, 4
PUSH AX

CALL Find_Max
CALL DECOUT

MOV AH, 4CH


INT 21H
MAIN ENDP

Find_Max PROC
PUSH BP
MOV BP, SP

CMP WORD PTR[BP+4], 1


JG ELSE_

MOV AX, Max


JMP END_IF
ELSE_:
MOV CX, [BP+4]
DEC CX
PUSH CX

CALL Find_Max

MOV BX, [BP+4]


SHL BX, 1
SUB BX, 2

CMP Max[BX], AX
JGE END_IF

MOV AX, Max[BX]


END_IF:
POP BP
RET 2
Find_Max ENDP
INCLUDE D:\DECOUT.ASM
MAIN ENDP

62

You might also like