0% found this document useful (0 votes)
88 views10 pages

Roll No.511767 8051 - 1.factorial

The document contains code snippets in assembly language for 8051 and 8086 microcontrollers. It includes programs to calculate factorial, add two 8-bit numbers, Fibonacci series, bubble sort, if-else conditional, counting positive numbers in an array, multiplying two 8-bit numbers, finding smallest number in an array, and transferring 1KB of data from one memory location to another. The code demonstrates various instructions like MOV, ADD, MUL, CMP, JMP, LOOP for movement and manipulation of data in memory locations and registers.

Uploaded by

Aarti Sartape
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views10 pages

Roll No.511767 8051 - 1.factorial

The document contains code snippets in assembly language for 8051 and 8086 microcontrollers. It includes programs to calculate factorial, add two 8-bit numbers, Fibonacci series, bubble sort, if-else conditional, counting positive numbers in an array, multiplying two 8-bit numbers, finding smallest number in an array, and transferring 1KB of data from one memory location to another. The code demonstrates various instructions like MOV, ADD, MUL, CMP, JMP, LOOP for movement and manipulation of data in memory locations and registers.

Uploaded by

Aarti Sartape
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Roll no.

511767

8051-------

*1.FACTORIAL

MOV A,#01H ; hexadecimal 01 is moved to register A

MOV R0,#45H ; R0 is pointing to 45H memory location

MOV B,@R0 ; value at R0 is moved to B

loop:MOV R1,B ; starting of loop, copy value at B in R1

MUL AB ; multiply A and B, higher bites are stored B and


lower in A

MOV B,R1 ; value at R1 is moved to B

DJNZ B,loop ; decrement B i.e. B= B-1 and jump to loop until B=0

END ; end

*2.ADDITION OF TWO 8 BIT NUMBER

MOV R0,#45H ; R0 is pointing to 45H memory location

MOV R1,#46H ; R1 is pointing to 46H memory location

MOV A,@R0 ; value at R0 is moved to A

MOV B,@R1 ; value at R1 is moved to B

ADD A,B ; addition of A and B, and store value in A

END ; end
*3.FIBONOCCI

MOV A,#00H ; 0 is copied to A

MOV B,#01H ; hexadecimal 01 is moved to B

MOV R0,#05H ; hexadecimal 05 is moved to R0

loop:ADD A,B ; starting of loop, addition of values in A and B and store


in A

MOV R1,A ; value in A is moved to R1

MOV A,B ; value in B is moved to B

MOV B,R1 ; value in R1 is moved to B

DJNZ R0,loop ; decrement R0i.eR0= R0-1and jump to loop until R0 is 0

END ; end

*4.SORTING – ASCENDING(bubble sort)


MOV R3,#05H ; hexadecimal 05 is copied to R3

Loop4:MOV R0,#45H ; 45H is copied to R0

MOV R2,#05H ; hexadecimal 05 is copied to R2

Loop3:MOV A,@R0 ; the value at R0 is copied to A

INC R0 ; increment in R0 i.e. R0=R0+1

MOV B,@R0 ; value at R0 is copied to B

CJNE A,B,Loop1 ; compare A and B(A-B,B-A,A=B) ,and jump to


loop1 if A and B are not -
equal

Loop1:JC Loop2 ; if carry is there then jump to loop2 (A-B<0 i.e


A>B)

MOV @R0,A ;value in A is copied at R0

DEC R0 ; decrement in R0 i.e. R0=R0-1

MOV@R0,B ; value in B is copied at R0

INC R0 ; increment in R0 i.e. R0=R0+1

Loop2:DJNZ R2,Loop3 ;decrement and jump to loop3 until R2 is zero

DJNZ R3,Loop4 ;decrement and jump to loop4 until R3 is zero

END ; end
*5-----

If(x>56)

Y=x*7;

Else

Y=x*59…….(for hexadecimal values)

MOV R0,#45H ;R0 is pointing to 45H

MOV A,@R0 ;value at R0 is moved to A

MOV R1,#46H ;R1 is pointing to 46H

MOV B,#56H ;hexadecimal 56 is moved to B

CJNE A,B,LOOP1 ;compare A and B (A>B,B>1,A=B),and jump if A is not


equal to B

LOOP1:JC LOOP2 ;if carry is there jump to loop2

MOV B,#59H ;hexadecimal 59 is moved to B

MUL AB ;multiply A and B and result is stored in A

MOV @R1,A ;lower bytes of result stored in A are moved to R1

MOV @R0,B ;higher bytes of results stored in B are moved to R0

SJMP LOOP3 ;short jump to loop3 so that loop 2 will not repeat

LOOP2: ; end of loop2

MOV B,#07H ; hexadecimal 07 is moved to B


MUL AB ; multiply A and B and result is stored in A

MOV @R1,A ;lower bytes of result stored in A are moved to R1

MOV @R0,B ; higher bytes of results stored in B are moved to R0

LOOP3: ; end of loop3

END ; end of program

8086----

*1.counting positive numbers in an array of 100 numbers containing positive,


negative and zeros

CODE SEGMENT ; starting of Logical code segment


ORG 100H ; Assembler directive, starts the memory
allotment for the code block from the address
declared here
START:
MOV AX,4000H ; Load AX with 4000H
MOV DS,AX ; Transfer the address in AX to DS (as 4000H can’t
be directly transferred to DS )
MOV CX,0064H ; Counter is initialized with 0064H
MOV SI,2000H ; Moves offset of first number in the array to SI
MOV BL,00H ; Initialize counting of positive numbers to 00H
loop: MOV AL,[SI] ; The value at [2000] is moved to AL
ADD AL,00H ; 00H is added to AL as the flags are only affected
after an operation. If AL=0, ZF=1; and if AL is
negative, it’s MSB will be 1 and 0 if it is positive.
JZ no_inc ; Jumps if ZF=1, as number is zero
RCL AL,01 ; Rotates AL 1 bit left with carry such that the MSB
of the number moves to the CF
JC no_inc ; Jumps if CF=1, as the number will then be -ve
INC BL ; Increments the count as the number is +ve
no_inc:INC SI ; Increments the offset address
LOOP loop ; LOOP executes a DEC CX JNZ block. It jumps to
the label loop
MOV AH,4CH ; Return to DOS
INT 21H ; DOS interrupt 21H
CODE ENDS ; Logical code segment ends here
END START ; Program ends here

*2.Multiplication of two 8 – bit numbers

ASSUME CS:CODE, DS:DATA


DATA SEGMENT ; Data segment begins
A db 12H ; Reserves a byte of memory location for variable
name A
B db 03H ; Reserves a byte of memory location for variable
name B
RESULT dw 00H ; Reserves a byte of memory location for variable
name RESULT, and initializes RESULT with 00H
DATA ENDS ; Logical data segment ends here
CODE SEGMENT ; starting of Logical code segment
ORG 100H ; Assembler directive, starts the memory
allotment for the code block from the address
declared here
START: MOV AX,DATA ; Initializes Data Segment
MOV AL,A ; value in A is moved to AL
MOV BL,B ; value in B is moved to BL
MUL BL ; multiply the values in AL and BL
MOV RESULT, AX ; Moves the product in the location reserved for
RESULT
CODE ENDS ; Logical Code segment ends here
END START ; End of Program

*3.Smallest number in an array of 100 numbers


CODE SEGMENT ;starting Logical Code segment
ORG 100H ; Assembler directive, starts the
memory allotment for the code block
from the address declared here
START: MOV AX,4000H ; Load AX with 4000H
MOV DS,AX ; Transfer the address in AX to DS (as
4000H can’t be directly transferred to
DS )
MOV SI,2000H ; Moves offset of first number in the
array to SI
MOV CX,0064H ; Counter is initialized with 0064H
MOV AL,FFH ; Initialize smallest number with FFH
which will be updated if a smaller
number is found
back: CMP AL,[SI] ; Compares AL with the value at [SI]
and updates the CF accordingly
JC skip ; Jump if CF=1 as AL is smaller than
the number pointed by SI
MOV AL,[SI] ; Move the value at SI to AL as CF=0,
so, [SI]<AL
skip: INC SI ; Increments the offset address
LOOP back ; LOOP executes a DEC CX JNZ block.
It jumps to the label loop
MOV AH,4CH ; Return to DOS
INT 21H ; DOS interrupt 21H
CODE ENDS ; Logical code segment ends
END START ; End of program

*4.Transferring 1kb data from location 2000H to 3000H

CODE SEGMENT ; starting of Logical code segment


ORG 100H ; Assembler directive, starts the memory
allotment for the code block from the
address declared here
START: MOV AX,2000H ; Load AX with 2000H
MOV DS,AX ; Transfer the address in AX to DS (as
2000H can’t be directly transferred to
DS )
MOV AX,3000H ; Load AX with 3000H
MOV ES,AX ; Transfer the address in AX to ES (as
3000H can’t be directly transferred to
DS )
MOV SI,5000H ; Moves offset of first number in the
source array to SI
MOV DI,6000H ; Moves offset of first number in the
destination array to SI
MOV CX,0400H ; Counter
CLD ; Clear Direction Flag. Here REP
MOVSB is used. If DF=0, SI and DI are
incremented and if DF=1, SI and DI
are decremented
REP MOVSB ; Move the complete string
MOV AH,4CH ; Return to DOS
INT 21H ; DOS interrupt 21H
CODE ENDS ; Logical code segment ends
END START ; End of program
ROL AH,04 ; Rotate AH 4-bits left through carry

MOV [2000],AH ; Move AH back to [2000]


MOV [2001],AL ; Move AL back to [2001]
END ; End of program

*5.Average of n natural numbers

CODE SEGMENT ; starting of Logical code segment


ORG 100H ; Assembler directive, starts the memory
allotment for the code block from the
address declared here

MOV SI, 500 ; move offset 500 to Starting Index(SI).

MOV DI, 600 ; move offset 600 to Destination Index(DI).

MOV AX, 0000 ; move data 0000 to AX.

MOV CL, [SI] ; move the contents of [SI] to BL.

MOV BL, CL ; copy contents of CL to BL.

INC SI ; increment in contents of SI by 1.

ADD AL, [SI] ; add contents of [SI] to AL.

ADC AH, 00 ;is used to 00 along with previous cy to AH.

INC SI ; increment in contents of SI by 1.

DEC CL ; decrement in contents of CL by 1.

JNZ 40E ; jump to offset 40E if value of ZF = 0.

DIV BL ; multiply contents of AX by BL.

MOV [DI], AX ; ; move the contents of AX to [DI].

HLT ; stops executing the program and halts any further


execution.

You might also like