ch-2_
ch-2_
Programming
Compiled by :- Er. Nagendra Karn
Computer language :-
• Over the years, computer languages have been evolved from low level to high
level languages. In the earliest days of computer only binary language was used
to write programs. The computer languages are classified as follows:-
Computer language
Use 1’s & 0’s to create use mnemonics to create similar to human
Instructions (e.g. binary) instructions (Assembly) language ( C,C++, java)
Assembly language
• Each personal computer has a microprocessor that manages the
computer's arithmetical, logical, and control activities.
• Each family of processors has its own set of instructions for handling
various operations such as getting input from keyboard, displaying
information on screen and performing various other jobs. These set of
instructions are called 'machine language instructions'.
• A processor understands only machine language instructions, which are
strings of 1's and 0's. However, machine language is too obscure and
complex for using in software development.
• the low-level assembly language is designed for a specific family of
processors that represents various instructions in symbolic code and a
more understandable form.
Assembly language
• An assembly language is the most basic programming language
available for any processor. With assembly language, a programmer
works only with operations that are implemented directly on the
physical cpu.
• Assembly languages generally lack high level conveniences such as
variables and functions, and they are not portable between various
families of processor.
• They have same structure and set of command as machine language,
but allow programmer to use name instead of numbers.
• Assembly language is specific to a given processor. E.g. assembly
language of 8085 is different that of Motrola 6800 microprocessor.
Assembly language
• Microprocessor can’t understand program written in assembly language. A
program known as assembler is used to convert assembly language
program to machine language.
Assembly
Assembler Machine
language
Program language code
Program
Input: Output:
Memory location Data Memory location Data
2050H 65H 2055H 12H
2051H 53H
5. Program to find 1’s complement of a number.
• Input number from memory location 2013H and store result in memory location 2052H.
LDA 2013H //Load contents from memory location 2013H to accumulator
CMA //Complement contents of accumulator
STA 2052H //Store result in memory location 2052H
HLT //Terminate the program.
Input: Output:
Memory location Data Memory location Data
2013H 12H 2052H EDH
6. Program to find 2’s complement of a number.
Input number from memory location 2013H and store result in memory location 2052H.
LDA 2013H //Load contents from memory location 2013H to accumulator
CMA //Complement contents of accumulator
ADI 01H //Add 01H to the contents of accumulator
STA 2052H //Store result in memory location 2052H
HLT //Terminate the program.
Input: Output:
Memory location Data Memory location Data
2013H 12H 2052H EEH
7. Program to right shift 8 bit numbers.
Shift an eight-bit data four bits right. Assume data is in memory location 2051H. Store
result in memory location 2055H.
LDA 2051H //Load data from memory location 2051H to accumulator
RAR //Rotate accumulator 1-bit right
RAR
RAR
RAR
STA 2055H //Store result in memory location 2055H
HLT //Terminate the program.
8. Program to left shift 8 bit numbers.
Shift an eight-bit data four bits left. Assume data is in memory location 2051H. Store
result in memory location 2055H.
LDA 2051H //Load data from memory location 2051H to accumulator
RAL //Rotate accumulator 1-bit left
RAL
RAL
RAL
STA 2055H //Store result in memory location 2055H
HLT //Terminate the program.
• Program to add two 16-bit numbers.
• Add numbers 1124H & 2253H and store result in memory location 2055H &
2056H.
LXI H,1124H //Load 16-bit data 1124H to HL pair
LXI D,2253H //Load 16-bit data 2253H to DE pair
MOV A,L //Move contents of register L to Accumulator
ADD E //Add contents of Accumulator and E register
MOV L,A //Move contents of Accumulator to L register
MOV A,H //Move contents of register H to Accumulator
ADC D //Add contents of Accumulator and D register with carry
MOV H,A //Move contents of Accumulator to register H
SHLD 2055H //Store contents of HL pair in memory address 2055H & 2056H
HLT //Terminate the program.
Input: Output:
Register pair Data Memory location Data
HL 65H 2055H 77H
DE 53H 2056H 33H
10. Program to add two 16 bit numbers :-
Input first number from memory location 2050H & 2051H and second number from memory location 2052H &
2053H and store result in memory location 2055H & 2056H.
LHLD 2052H //Load 16-bit number from memory location 2052H & 2053H to HL pair
XCHG //Exchange contents of HL pair and DE pair
LHLD 2050H //Load 16-bit number from memory location 2050H & 2051H to HL pair
MOV A,L //Move contents of register L to Accumulator
ADD E //Add contents of Accumulator and E register
MOV L,A //Move contents of Accumulator to L register
MOV A,H //Move contents of register H to Accumulator
ADC D //Add contents of Accumulator and D register with carry
MOV H,A //Move contents of Accumulator to register H
SHLD 2055H //Store contents of HL pair in memory address 2055H & 2056H
HLT //Terminate the program.
Input: Output:
Register pair Data Memory location Data
2050H 33H 2055H 57H
2051H 45H 2056H 79H
2052H 24H
2053H 34H
11. Program to subtract two 16 bit numbers :-
• Subtract number 1234H from 4897H and store result in memory location 2055H &
2056H.
LXI H,4567H //Load 16-bit data 4897H to HL pair
LXI D,1234H //Load 16-bit data 1234H to DE pair
MOV A,L //Move contents of register L to Accumulator
SUB E //Subtract contents of Accumulator and E register
MOV L,A //Move contents of Accumulator to L register
MOV A,H //Move contents of register H to Accumulator
SBB D //Subtract contents of Accumulator and D register with borrow
MOV H,A //Move contents of Accumulator to register H
SHLD 2055H //Store contents of HL pair in memory address 2055H & 2056H
HLT //Terminate the program.
Input: Output:
Register pair Data Memory location Data
HL 4879H 2055H 63H
DE 1234H 2056H 36H
12. Program to subtract two 16 bit numbers :-
• Input first number from memory location 2050H & 2051H and second number from memory location
2052H & 2053H and store result in memory location 2055H & 2056H.
LHLD 2052H //Load 16-bit number from memory location 2052H & 2053H to HL pair
XCHG //Exchange contents of HL pair and DE pair
LHLD 2050H //Load 16-bit number from memory location 2050H & 2051H to HL pair
MOV A,L //Move contents of register L to Accumulator
SUB E //Subtract contents of Accumulator and E register
MOV L,A //Move contents of Accumulator to L register
MOV A,H //Move contents of register H to Accumulator
SBB D //Subtract contents of Accumulator and D register with carry
MOV H,A //Move contents of Accumulator to register H
SHLD 2055H //Store contents of HL pair in memory address 2055H & 2056H
HLT //Terminate the program.
Input: Output:
Register pair Data Memory location Data
2050H 78H 2055H 54H
2051H 45H 2056H 11H
2052H 24H
2053H 34H
13. Program to Multiply two 8 bit numbers :-
• Multiply 06 and 03 and store result in memory location 2055H.
MVI A,00H
MVI B,06H
MVI C,03H
X: ADD B
DCR C
JNZ X
STA 2055H
HLT
14. Program to divide to 8-bit numbers.
Divide 08H and 03H and store quotient in memory location 2055H and remainder in memory location 2056H.
MVI A,08H
MVI B,03H
MVI C,00H
X: CMP B
JC Y
SUB B
INR C
JMP X
Y: STA 2056H
MOV A,C
STA 2055H
HLT
• Program to find greatest among two 8 bit numbers.
• Input numbers from memory location 2050H & 2051H and store greatest number
in memory location 2055H.
LDA 2051H
MOV B,A
LDA 2050H
CMP B
JNC X
MOV A,B
X: STA 2055H
HLT
• Program to find smallest among two 8 bit numbers.
• Input numbers from memory location 2050H & 2051H and store smallest number
in memory location 2055H.
LDA 2051H
MOV B,A
LDA 2050H
CMP B
JC X
MOV A,B
X: STA 2055H
HLT
• Program to find whether the number is even or odd.
• Input number from memory location 2050H and store result in 2055H.
LDA 2050H
ANI 01H
JZ X
MVI A,0DH
JMP Y
X: MVI A,0EH
Y: STA 2055H
HLT
• Program to count number of 1’s in a given number.
• Input number from memory location 2050H and store result in 2055H.
LDA 2050H
MVI C,08H
MVI B,00H
X: RAR
JNC Y
INR B
Y: DCR C
JNZ X
MOV A,B
STA 2055H
HLT
• Find sum of numbers from 1 to 10.
MVI B,01H
MVI C,0AH
MVI A,00H
X: ADD B
INR B
DCR C
JNZ X
STA 2055H
HLT
• Display all odd numbers from 1 to 10.
LXI H,2050H
MVI B,01H
MVI C,0AH
X: MOV M,B
INX H
INR B
INR B
DCR C
DCR C
JNZ X
HLT
• Find the sum of 5 numbers in array.
LXI H,2050H
MVI C,O5H 02 03 01 02 04
MVI A,00H
X: MOV B,M
ADD B
INX H
DCR C
JNZ X
STA 2060H
HLT
• Find the smallest number in array.
LDA 2200H
MOV C,A
LXI H,2201H
MVI A,00H
X: CMP M
JC Y
MOV A,M
Y: INX H
DCR C
JNZ X
STA 2300H
HLT