SlideShare a Scribd company logo
Assembly Language Programs
Program to find the larger of two
numbers.
DATA SEGMENT
A DB 34H
B DB 78H
S DB ?
DATA ENDS
CONTD..
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AL,A
CMP AL,B ; COMPARE FIRST NO. WITH SECOND.
JNC FIRST ; JUMP IF NO CARRY (1ST NO >2ND NO.)
MOV AL,B ;1ST NO. < 2ND NO. copy 2nd no. into AL.
FIRST: MOV S,AL
INT 3
CODE ENDS
END START
Program to check whether the given
number is odd or even
Logic :
If any number is odd,
the least significant bit is ‘1’
and if even,
the least significant bit is ‘0’.
Eg: 12H :-- 00010010 EVEN
59H :-- 01011001 ODD
SHR (Shift Logical Right)
Syntax :-- SHR destination, count
•This instruction shifts the destination bit by
bit to the right and insert zeroes in the newly
introduced most significant bits.
•The shift operation is through carry.
•The count can be either 1 or specified by CL
register.
•The destination can be a byte or a word in
register or a memory location, but not an
immediate data.
BL CF
0
0
1
BL CF
0 0 0 0 0 0 1 1
• Operation Performed :--
–0  MSB ------------------ LSB  CF
• Example :--
– If CF = 0, BL = 07H
– After SHR BL, 1 ; Shift the contents of BL
register by one towards right
SHR (Shift Logical Right)Cntd..
0 0 0 0 0 1 1 1
BL = 03H
Program for odd or even
DATA SEGMENT
N DB 34H
R DB ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS,AX
CONTD..
MOV AL, N
SHR AL,1
JNC ev
MOV R,’O’
JMP last
ev: MOV R, ‘E’
last: INT 3
CODE ENDS
END START
Implementing a loop in assembly
language programming
• To implement a loop using LOOP instruction:
MOV CX, count
next: :
instructions
(executed ‘count’ times)
:
LOOP next
Note: LOOP instruction automatically decrements
CX register and control is transferred to label, if
CX <> 0.
Write an assembly language
program to transfer 10 bytes data
from base of data segment to base
of extra segment. Data segment
base address is 2C000H. Extra
Segment base address is DE000H.
Algorithm:
Step1: Initialize Data segment base, DS
2C00H
Step2 Initialize Extra segment base, ES
DE00H
Step3: Clear SI and DI for the offset.
Step4: Clear direction flag.
Step5: initialize counter, CX <-- 000Ah
Step6: Use string byte transfer instruction to
copy the bytes from one block to another
block (MOVSB), along with REP instruction
prefix.
Note: REP is used to repeat the instruction
execution till CX =0.
Program:
CODE SEGMENT
ASSUME CS:CODE
START: MOV AX, 2C00H
MOV DS,AX
MOV AX,DE00H
MOV ES,AX
MOV SI,0000H
MOV DI,0000H
MOV CX,000AH
CLD ;CLEAR THE DIRECTION FLAG
REP MOVSB
CODE ENDS
END START
Note: REP is used to repeat the instruction
execution till CX =0.
Write an assembly language
program to count the number of
‘1’ s in a byte.
DATA SEGMENT
N DB 56H
C DB 0H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV AL, N
MOV CX, 08H
L1: SHR AL,1
JNC NEXT
INC C
NEXT: LOOP L1
INT 3
CODE ENDS
END START
Program to find the smallest of N (9)
numbers in the array
DATA SEGMENT
ARRAY DB 0A7H,87H,34H,83H,80H,78H,0CDH,0D4H,67H
RESULT DB ?
DATA ENDS
CONTD..
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV CX,0008H
LEA BX,ARRAY
MOV AH, [BX]
BACK: INC BX
CMP AH,[BX]
JC GO ; jump if below, CF=1 same as JC
MOV AH,[BX]
GO: LOOP BACK
INC BX
MOV [BX],AH
INT 3
CODE ENDS
END START
Program to find the largest of N (9)
numbers in the array
• Note: In the above program change JB to JA
Or
• Make use of JNC
Program to arrange numbers in array
in ascending/descending order.
DATA SEGMENT
ARRAY DB 87H,67H,22H,45H,83H,80H,78H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV CL,07H ;ITERATION COUNTER
LOOP1: LEA BX,ARRAY
MOV CH,06H ; COMPARISON COUNTER
LOOP2: MOV AL, [BX]
INC BX
CMP AL,[BX]
JNC DOWN ;If 1st no is smaller, skip exchange
MOV DL,[BX] ; Exchange 2 numbers
MOV [BX],AL
DEC BX
MOV [BX],DL
INC BX
DOWN: DEC CH
JNZ LOOP2 ; if comparison counter <>0, loop2
DEC CL
JNZ LOOP1 ; if iteration counter <>0, loop1
INT 3
CODE ENDS

More Related Content

DOCX
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086
PPT
arithmetic ins in 8051
PPTX
Push down automata
PPTX
Arithmetic instrctions
PPTX
PDA (pushdown automaton)
PDF
implementation of data instrucions in emu8086
PDF
Up 8086 q9
PPTX
CFG to CNF
IMPLEMENTING ARITHMETIC INSTRUCTIONS IN EMU 8086
arithmetic ins in 8051
Push down automata
Arithmetic instrctions
PDA (pushdown automaton)
implementation of data instrucions in emu8086
Up 8086 q9
CFG to CNF

What's hot (12)

PDF
Dataflow Analysis
ODT
Ecet 330 final exam new 2016
DOCX
Notes arithmetic instructions
PDF
Asymptotic
PDF
Write a C function that returns the length of a string. Function name: strinq...
PPTX
الترددات
PPTX
2.7 normal forms cnf & problems
POTX
Matematica CS-GOTHIC
PPTX
C++:Lab 2
PDF
Proving Properties of Security Protocols by Induction
PDF
IBM Cloud University: Java, Node.js and Swift
Dataflow Analysis
Ecet 330 final exam new 2016
Notes arithmetic instructions
Asymptotic
Write a C function that returns the length of a string. Function name: strinq...
الترددات
2.7 normal forms cnf & problems
Matematica CS-GOTHIC
C++:Lab 2
Proving Properties of Security Protocols by Induction
IBM Cloud University: Java, Node.js and Swift
Ad

Similar to Assembly language programs 2 (20)

PPTX
Assembly language programs
PDF
Exp 03
PDF
chapter 7 Logic, shift and rotate instructions
PPTX
Part III: Assembly Language
PDF
1) (a) Write a code fragment which adds the 32-bit contents of $8100.pdf
PPTX
Microprocessor and Microcontrollers LAB-PP.pptx
PPTX
Home works summary.pptx
PDF
PPT
Instruction set of 8086
PDF
Taller Ensambladores
PPTX
Loop instruction, controlling the flow of progam
PPT
Unit 2 8086 Instruction set.ppt notes good
PDF
15CSL48 MP&MC manual
PDF
Microprocessor 8086-lab-mannual
PPTX
PPT
Assembly Language Lecture 4
PDF
Introduction to ibm pc assembly language
PPTX
L12_ COA_COmputer architecurte and organization
PPTX
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
PPTX
Assembly Language Compiler Implementation
Assembly language programs
Exp 03
chapter 7 Logic, shift and rotate instructions
Part III: Assembly Language
1) (a) Write a code fragment which adds the 32-bit contents of $8100.pdf
Microprocessor and Microcontrollers LAB-PP.pptx
Home works summary.pptx
Instruction set of 8086
Taller Ensambladores
Loop instruction, controlling the flow of progam
Unit 2 8086 Instruction set.ppt notes good
15CSL48 MP&MC manual
Microprocessor 8086-lab-mannual
Assembly Language Lecture 4
Introduction to ibm pc assembly language
L12_ COA_COmputer architecurte and organization
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Compiler Implementation
Ad

More from HarshitParkar6677 (20)

PPTX
Wi fi hacking
PPT
D dos attack
DOCX
Notes chapter 6
DOC
Interface notes
PPTX
Chapter6 2
PPTX
PPT
8086 cpu 1
DOC
Chapter 6 notes
DOC
Chapter 5 notes
PPTX
Chap6 procedures &amp; macros
DOC
Chapter 5 notes new
DOCX
Notes all instructions
DOCX
Notes aaa aa
DOCX
Notes 8086 instruction format
PPTX
Copy of 8086inst logical
PPT
Copy of 8086inst logical
PPTX
Chapter3 program flow control instructions
PPTX
Chapter3 8086inst stringsl
PPTX
Chapter3 8086inst logical 2
Wi fi hacking
D dos attack
Notes chapter 6
Interface notes
Chapter6 2
8086 cpu 1
Chapter 6 notes
Chapter 5 notes
Chap6 procedures &amp; macros
Chapter 5 notes new
Notes all instructions
Notes aaa aa
Notes 8086 instruction format
Copy of 8086inst logical
Copy of 8086inst logical
Chapter3 program flow control instructions
Chapter3 8086inst stringsl
Chapter3 8086inst logical 2

Recently uploaded (20)

PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
composite construction of structures.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
DOCX
573137875-Attendance-Management-System-original
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Digital Logic Computer Design lecture notes
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
PPT on Performance Review to get promotions
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT
Mechanical Engineering MATERIALS Selection
bas. eng. economics group 4 presentation 1.pptx
UNIT 4 Total Quality Management .pptx
composite construction of structures.pdf
OOP with Java - Java Introduction (Basics)
Embodied AI: Ushering in the Next Era of Intelligent Systems
573137875-Attendance-Management-System-original
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Digital Logic Computer Design lecture notes
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CYBER-CRIMES AND SECURITY A guide to understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Construction Project Organization Group 2.pptx
PPT on Performance Review to get promotions
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Mechanical Engineering MATERIALS Selection

Assembly language programs 2

  • 2. Program to find the larger of two numbers. DATA SEGMENT A DB 34H B DB 78H S DB ? DATA ENDS CONTD..
  • 3. CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV AL,A CMP AL,B ; COMPARE FIRST NO. WITH SECOND. JNC FIRST ; JUMP IF NO CARRY (1ST NO >2ND NO.) MOV AL,B ;1ST NO. < 2ND NO. copy 2nd no. into AL. FIRST: MOV S,AL INT 3 CODE ENDS END START
  • 4. Program to check whether the given number is odd or even Logic : If any number is odd, the least significant bit is ‘1’ and if even, the least significant bit is ‘0’. Eg: 12H :-- 00010010 EVEN 59H :-- 01011001 ODD
  • 5. SHR (Shift Logical Right) Syntax :-- SHR destination, count •This instruction shifts the destination bit by bit to the right and insert zeroes in the newly introduced most significant bits. •The shift operation is through carry. •The count can be either 1 or specified by CL register. •The destination can be a byte or a word in register or a memory location, but not an immediate data.
  • 6. BL CF 0 0 1 BL CF 0 0 0 0 0 0 1 1 • Operation Performed :-- –0  MSB ------------------ LSB  CF • Example :-- – If CF = 0, BL = 07H – After SHR BL, 1 ; Shift the contents of BL register by one towards right SHR (Shift Logical Right)Cntd.. 0 0 0 0 0 1 1 1 BL = 03H
  • 7. Program for odd or even DATA SEGMENT N DB 34H R DB ? DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX, DATA MOV DS,AX CONTD..
  • 8. MOV AL, N SHR AL,1 JNC ev MOV R,’O’ JMP last ev: MOV R, ‘E’ last: INT 3 CODE ENDS END START
  • 9. Implementing a loop in assembly language programming • To implement a loop using LOOP instruction: MOV CX, count next: : instructions (executed ‘count’ times) : LOOP next Note: LOOP instruction automatically decrements CX register and control is transferred to label, if CX <> 0.
  • 10. Write an assembly language program to transfer 10 bytes data from base of data segment to base of extra segment. Data segment base address is 2C000H. Extra Segment base address is DE000H.
  • 11. Algorithm: Step1: Initialize Data segment base, DS 2C00H Step2 Initialize Extra segment base, ES DE00H Step3: Clear SI and DI for the offset. Step4: Clear direction flag. Step5: initialize counter, CX <-- 000Ah Step6: Use string byte transfer instruction to copy the bytes from one block to another block (MOVSB), along with REP instruction prefix. Note: REP is used to repeat the instruction execution till CX =0.
  • 12. Program: CODE SEGMENT ASSUME CS:CODE START: MOV AX, 2C00H MOV DS,AX MOV AX,DE00H MOV ES,AX MOV SI,0000H MOV DI,0000H MOV CX,000AH CLD ;CLEAR THE DIRECTION FLAG REP MOVSB CODE ENDS END START Note: REP is used to repeat the instruction execution till CX =0.
  • 13. Write an assembly language program to count the number of ‘1’ s in a byte.
  • 14. DATA SEGMENT N DB 56H C DB 0H DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV AL, N
  • 15. MOV CX, 08H L1: SHR AL,1 JNC NEXT INC C NEXT: LOOP L1 INT 3 CODE ENDS END START
  • 16. Program to find the smallest of N (9) numbers in the array DATA SEGMENT ARRAY DB 0A7H,87H,34H,83H,80H,78H,0CDH,0D4H,67H RESULT DB ? DATA ENDS CONTD..
  • 17. CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV CX,0008H LEA BX,ARRAY MOV AH, [BX] BACK: INC BX CMP AH,[BX] JC GO ; jump if below, CF=1 same as JC MOV AH,[BX] GO: LOOP BACK INC BX MOV [BX],AH INT 3 CODE ENDS END START
  • 18. Program to find the largest of N (9) numbers in the array • Note: In the above program change JB to JA Or • Make use of JNC
  • 19. Program to arrange numbers in array in ascending/descending order. DATA SEGMENT ARRAY DB 87H,67H,22H,45H,83H,80H,78H DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX
  • 20. MOV CL,07H ;ITERATION COUNTER LOOP1: LEA BX,ARRAY MOV CH,06H ; COMPARISON COUNTER LOOP2: MOV AL, [BX] INC BX CMP AL,[BX] JNC DOWN ;If 1st no is smaller, skip exchange MOV DL,[BX] ; Exchange 2 numbers MOV [BX],AL DEC BX MOV [BX],DL INC BX DOWN: DEC CH JNZ LOOP2 ; if comparison counter <>0, loop2 DEC CL JNZ LOOP1 ; if iteration counter <>0, loop1 INT 3 CODE ENDS