Unit-4 Notes
Unit-4 Notes
Syllabus:
Solution:
The factorial of a number ‘n’ is n × ( n−1 ) × ( n−2 ) × ( n−3 ) …
i.e. factorial of 4 is 4*3*2*1 = 24
Assumptions –
Starting address of program: 0400
Input memory location: 0500
Output memory location: 0600 and 0601
MNEMONICS COMMENTS
MOV CX, [0500] Move the content of memory address 0500 in register CX
MOV AX, 0001 Move 1 in the register AX
MOV DX, 0000 Move 0 in the register DX
MUL CX Multiply the contents of CX & DX and store product in
DX:AX
LOOP 040A Go to address [040A] till CX->00
MOV [0600], AX Move the content of AX in memory address 0600
MOV [0601], DX Move the content of DX in memory address 0601
HLT Stop Execution
Note –
The result of multiplication is stored in AX for byte data type.
For the word data type, the result of multiplication is stored in AX-DX. i.e.
90C3×12DA=AA8FC0E;
After multiplication AX=FC0E & DX=AA8
2
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
Algorithm –
1. Assign value 500 in SI and 600 in DI
2. Move the contents of [SI] in BL and increment SI by 1
3. Move the contents of [SI] and [SI + 1] in AX
4. Use DIV instruction to divide AX by BL
5. Move the contents of AX in [DI].
6. Halt the program.
MNEMONICS COMMENT
Quotient Remainder
3
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
Problem – Write a program to add two 16-bit numbers where starting address is 2000H and the
numbers are at 3000H and 3002H memory address and store result into 3004H and 3006H
memory address
Mnemonics Comment
ADD AX, BX AX AX + BX
Add the contents of AX & BX and store the result in
AX
JNC L2 Jump to address 2010 if there is no carry
HLT Stop
Algorithm –
1. Load 0000H into CX register (for carry)
2. Load the data into AX(accumulator) from memory 3000
3. Load the data into BX register from memory 3002
4. Add BX with Accumulator AX
5. Jump if no carry
6. Increment CX by 1
7. Move data from AX(accumulator) to memory 3004
8. Move data from CX register to memory 3006
9. Stop
4
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
Problem – Write a program to subtract two 16-bit numbers where starting address is 2000 and
the numbers are at 3000 and 3002 memory address and store result into 3004 and 3006 memory
address
Mnemonics Comment
MOV CX, 0000 [CX] 0000
Assign 0 into the reg. CX
MOV AX, [3000] [AX] [3000]
Move the data of address [3000] to reg. AX
MOV BX, [3002] [BX] [3002]
Move the data of address [3002] to reg. BX
SUB AX, BX AX AX - BX
JNC 2010 Jump to memory address 2010 if there is
no borrow
INC CX CX <- CX + 1
MOV [3004], AX [3004] AX
MOV [3006], CX [3006]CX
HLT Stop
Algorithm –
1. Load 0000H into CX register (for borrow)
2. Load the data into AX(accumulator) from memory 3000
3. Load the data into BX register from memory 3002
4. Subtract BX with Accumulator AX
5. Jump if no borrow
6. Increment CX by 1
7. Move data from AX(accumulator) to memory 3004
8. Move data from CX register to memory 3006
9. Stop
5
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
After Five number iterations of CL, the content of BL=1. This process will continue until BL is
not equal to 2
6
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
Problem – Write a program to transfer a block of 4 bytes, starting address is 0500 and transfer
the block at address 0600 by using string instructions
Mnemonics Comments
MOV SI, 500 SI500 Move the offset address 500 of Data segment to SI
MOV DI, 600 DI600 Move offset address 600 of Extra segment to DI
MOV AX, 0000 AX0000
MOV DS, AX Move 0000H to DS register so that base address of data segment is
0000H
MOV ES, AX Move 0000H to ES register so that base address of Extra segment is
0000H
MOV CX, 0004 CX0004 since four data have to be moved
CLD Clear directional Flag
REP reduce CX and repeat till CX≠ 0
MOVSB Move the string byte from DS:[SI] to ES:[DI]
HLT End of the program
Problem – Write a program to convert a string of data to its two’s compliment form
7
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
Mnemonics Comment
MOV SI, 3000H SI [3000]
MOV DI, 4000H DI [4000]
MOV CX, 000AH CX 000A
LODSB Load the string byte at DS:[SI] into AL
NEG AL Negative of AL
STOSB Load the string byte from AL into ES:[DI]
LOOP NZ Loop through a sequence of instructions till CX>0
HLT Stop
Output data 0A
Input data 04 03 02 01
8 0600
Memory address 0500 0501 0502 0503 Memory address
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
Mnemonics Comments
HLT Stop
Mnemonics Comments
9
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
HLT Stop
10
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
11
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
12
Prepared by Dr. Ajit Kumar (Asst. Professor, Dept. of ECE, Presidency University, Bengaluru)
13