0% found this document useful (0 votes)
490 views4 pages

Emu8086 Factorial

The document describes the data and code segments of an assembly language program that calculates the factorial of a number entered by the user. It declares variables to store the number, factorial, and result in the data segment. The code segment gets the number from the user, calculates the factorial using a loop, converts the result to a string, and displays it. Key steps include getting input, initializing variables, multiplying in a loop, converting the output to a string, and displaying results.

Uploaded by

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

Emu8086 Factorial

The document describes the data and code segments of an assembly language program that calculates the factorial of a number entered by the user. It declares variables to store the number, factorial, and result in the data segment. The code segment gets the number from the user, calculates the factorial using a loop, converts the result to a string, and displays it. Key steps include getting input, initializing variables, multiplying in a loop, converting the output to a string, and displaying results.

Uploaded by

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

DATA SEGMENT (Data segment is a starting of DS in a program and data is a name

given to the segment and segment is keyword


for definig segment where we can declared our variable.

NUM DB ? (BLANK VALUE We are accpeting value from user from console as a decimal
number to find factorical of it .)

FACT DB 1H (we are initially fact to 1H(hexadeciaml value)

RES DB 10 DUP ('$')(this line is a decleration of array initially with $. which


work as new line character $is used in /n null character
in C program a number character is of a byte size hence we
have to use only DB(DEFINE BYTE)AS WE DON'T know the length
of digit in resultant decimal equvialent printable from
therefore we take it approx size 10 here 10DUP $ stand for
N. That's the size of array DUP stand for DUPLICATE VALUE ie
it will duplicate the value . In all the array value
present in bracket.

MSG1 DB "ENTER NUMBER : $" (This line is a declartion character array initialized
with "ENTER NUMBER :$"and $is used a /n null character
in C program . A character is of btye hence we have
only DB.

MSG2 DB 10,13,"RESULT : $" This line is decleartion of character of array


initially with result $10 , 13 work as new line
character if this is is not present all the msg will be
printed on the same line and $is used as /n null
character in C program .

DATA ENDS DATA ENDS is the end point of the data segment in a program. We can
write just ENDS but the differtiate the end of the which segment
it is of which we have to write the same given to the data segment . Now
selection of data type is DS data type the number which
we are addining will be intergers so DS is sufficient .

CODE SEGMENT is a starking of CS in a program and code is the name given to


segement and segment the keyword defined segment were we can
write the coding of the program.

ASSUME DS:DATA,CS:CODE (In this assembly language program there are different
register present diffrent purpose so we have to assume
data the name given to the data segment register and
codeis the name given to code segment register.

START:( it is the label used to show the staring point of the code which is
written in the code segment.It is used to define a label as in coampany

MOV AX,DATA(after assuming data and code segment still it is compuslory initially
datasegment to DS register.
MOV DS,AX (MOV is akeyword to move the 2nd element to the 1st element but we can
directly to DS dur to move command restrication hence we move data to AX
and then AX TO DX. AX is the 1st and the most important regsister in
the ALU unit . this part also called initially of data segment . It is important
the data segment are made excessable.
LEA DX,MSG1( The above three line code use to print string or MSG present in the
charcater array $ which touch the complier stop.LEA DX MSG1 in this LEA stand
load effective address and it laods the effective address of 2nd
element into the 1st element. This same code can be interchangeable wriiten as mov
DS of set MSG1A where of set means effective into 1st element.

MOV AH,9(above two line code use to print the string or MSG of the address present
in DX register.(STANDARD INPUT OR STANDARD OUTPUT RELEATED )interput are found
in 21H which also k/a DOS enterput. It work with the value of DS register.
If the value is 9 or 9H thats means print the MSG of the address in the DX register
INT 21H

MOV AH,1(The above two line the read character from console and save the value
enter the variables R in it ASCITfrom If the value is 9 or 9H thats means print the
MSG
of the address in the DX register.That's means read the character from console echo
it on screen save the value enter in AL register.

SUB AL,30H( It is used to convert the value enter the variables NUM from ASCII from
to its DCD from. It can done by subtractive 30H.The value coming from console is
bacially in ASCII from.
MOV NUM,AL

MOV AH,0(It is used to clear the unwanted value (grabbage value)In AH register is
removed by assiging zero to eight.

MOV AL,FACT(means move value in AU register from variable fact.

MOV CH,0(It is used to clear the unwanted value in AH register is removed by


assiging 0 to 8.

MOV CL,NUM(means move value in AL regiter from variable NUM .

LABEL1: MUL CL(LABEL is a label and the all the word ending in colon:)(MUL CL is
the used to multiple CL with AX register that is value present in AX register.

LOOP LABEL1(This is end of loop in assembly programming lanague we have a loop


instruction this works with two other helper which are label and counter.
The loop start with label and end with loop instruction with same
label name with it . The excuation of the loop depends on the value in CX register.

LEA SI,RES( The above two line code is used to intialize RES to SI regsiter .

CALL HEX2DEC(call procedure HEX2DEC which will covert AX register value as result
and print it
on user screen.)

LEA DX,MSG2is used to load effecitve address of RES variable to SI register.

MOV AH,9(The above two line code is used to PRINT the string or message o the
address present in DX register i.e. for MSG2
INT 21H
LEA DX,RES
MOV AH,9(The above three line code is used to print string or message present in
the character array till $symbol which tells the complier to stop .As we have
initialized all the values in an aaray to $you will think what will be
printed . The procedure is going to chnge the array to its resultant decimal
equivalent printable from i.e.ASCII from of a digit number.
INT 21H

MOV AH,4CH(The above two line code is used to exit to dos or exit to operating
system. Stnadrd input and standard output related interupts are found in INT 21H
which is also called a DOS interrupt . It works with the vlaue of AH
register , If the value is 4ch ,That means retrun to operating systems or DOS which

is te end of the progrms


INT 21H

CODE ENDS(is the end point of the code segment in a program . We can write just
code segment in a program . We can write just ENDS but to differentitate the end of
which segment it is of which we have to write the same name given to the
code segment .

HEX2DEC PROC NEAR(This line of code is used to start a procedure code and we can
make out the procedure by the keyword PROC which tells us the procedure us started.
In assembly language we have two type of prodecure one is NEAR
and other is FAR . NEAR is used to call the procedure within the program whereas
FAR
is used to call the procedure outside the program. HEX2DEC is
only the Namw given to the procedure code.

MOV CX,0(is used to move or assign value 0 decimal value to CX . The program which
we are wishing to write is to convert hexadecimal value to decimal vlaue ,in which
we will divide the number till the quotient is going to be zero . CX
register(CX is also called COUNTER).CX register will the number digit generated by
dividing the hexadecimal number by base value of decimal i.e. Ten.

MOV BX,10(in this base value 10 is moved to BX register , so that it is used to


divide hexa number by 10.

LOOP1: MOV DX,0(is a label and all the words ending in colon(:)are labels . MOV
DX,O is used to clear the unwanted value (garbage value)in DX register is removed
by assiging zero to it. Frist loop starts here.

DIV BX(DIV instruction only works with REG or MEMORY hence we cannot use DIV 10
where 10 is immediate, so we have to move 10 to BX register (we can take any
register
this we have already done above and Then DIV BX Now DIV BX will divide AX
register and result of division is presnt in AX register contains quotientand DX
register contains Remainder .Here we will not touch quotient AX as it will
be used for furture division , but DX remainder will be decimal digit and will
always be less than Ten so the vlaue will be in DL register only and to
make it printable on console we have to add 30H So that it will be come a ASCII
character and will be saved in character array and will be printed as
string .

PUSH DX(PUSH is a stack function . Stck is an area of memoery for keeping temporary
data. PUSH and POP are two stack opration which stores or gets 16 bits of data
PUSH DX stores 16bit data inside DX register into stack Area .INC is a
instrction for increment the present in register or memory .

INC CX(will increment the value present in CX register as a counter and counting
the number of digits in thier ASCII from which are puched into stack . So
that the same count will help to POP the value out of stack.
CMP AX,9(is used to comapre AX register with 9 and jump if AX is greater to yhe
respective LABEL LOOP1. THE result of comparision not stored anywhere but flags
set according to result .is short jump if frist operands in greater then
second operands (as set by CMP instruction )signed . SECOND is the lable
where the complier will JUMP . frist loop ends here . NOTE:-this loop is
without LOOP keyword and depends upon the number to be converted .
JG LOOP1

ADD AL,30H(The last remainder will be decimal digit in AX REGISTER only as the
number cannot be divided future and will always be less than ten so the value
will be in AL register only and to make it printable on console
(screen)we have to add 30H so that it will become a ASCII chartcter and will be
printed
as string later .

MOV [SI],ALMOV [SI],AL saving the characters in characters array(i.e.string)is done


by moving AL register to address of SI register which is represtend
in square brackets i.e.[SI].SI is assigned with the character array
i.e.RES.

LOOP2: POP AX(is a LABEL and all the words ending in colon (:)are labels. POP is
stack function. Stack is an area of memeory for keeping temporary data . PUSH
and POP are the two stack operation which stores or gets 16 bits of
data . POP AX gets 16bits to AX register from top of stack. INC CX will
increment the value present in CX register by one . hence we are
using CX register as a counter and counting the number of digits in their ASCII
from which are pushed into stack. SO that the same count will help
to pop the value out of stack and save it in AX register SECOND LOOP starts here .
INC SI
MOV [SI],AL(The value out of stack in AX REGISTER saved in sting in this loop. MOV
[SI],AL saving the character in chatacter Array (ie string)is done by moving
AL register to address of SI register which is rreprestend in square
brackets ie. [SI]. SI is assigned with the character array ie.RES

LOOP LOOP2(This end of loop in assembly programming langauage we have a LOOP


instruction. This work with two others helpers which are label and counter.The
loop start with label abd ends with loop instruction with same LABEL
name with it. The execution of the loop depends on the value in CX register (CX is
also called COUNTER).

RET(RET is a retrun instruction is used only if the control is been passed to the
code outside main liked to procedure this return the control to the place where
the procudure was called.

HEX2DEC ENDP(is the end point of the procedure in a program.This line code is used
to end proceure code and we can make out thr procedure by the keyword ENDP which
tells us the prodecure is ended. In assembly langauge we have two
type of prodecure ne is NEAR and other is FAR. NEAR is used to call the procedure
the program wheras FAR is used to call the procedure outside the
program . HEX2DEC is only the name given to the produre code.

END START(is the endup of the label used to show the ending point of the code which
is wriiten in the code segment.

You might also like