Experiment
No 03
Title:
Write X86/64 ALP to convert 4-digit
Hex number into its equivalent BCD number and 5-digit BCD number into its
equivalent HEX number. Make your program user friendly to accept the choice
from user for:
(a) HEX
to BCD b) BCD to HEX (c) EXIT.
Display
proper strings to prompt the user while accepting the input and displaying the
result. (Wherever necessary, use 64-bit registers)
Objectives:
Basics of the Assembly language
programming
Knowledge of the instruction set.
Knowledge of nasm
Environment:
1.
OS:
ubuntu 12.10
2.
Assembler:
NASM
3.
Linker:
ld
Theory:
Segment directives
These directives indicate to assembler the order in which to load
segment. When it encounter one of these directives, it interprets all
subsequent instructor as belonging to the indicated segment (until the ne xt
directives is encounter).
.stack
<size>; specified .code .data
Data type declaration
As has been previously discu ssed, data can be of several diff erent
lengths and assembler
must be
able to decide what length a specific constant (or variable) is. This can be
down using data type declaration in conjunction with a constant declaration or
variable assignment .this is akin to strong typing of variable in high level
language .the data types are:
Byte (8
bit quantity)—synonyms are byte and db
Word
(16) -- synonyms are word dw
Dword
(32bit) -- synonyms are dword and dd
Qword
(64bit) -- synonyms with dq
Tword
(128bit) -- synonyms with dt
An
example of their use is:
MOV AX,
word VAR ; moves a 16-bit
Variable VAR into AX
Instruction Used:
1.
PUSH:-Push word onto stack.
2.
POP:-Pop word off stack.
3.
DIV:-Divide byte/word
4. XOR:
- Exclusive or byte/word
5.
JA/JNBE:-Jump if above/not below or equal
6.
JB/JNAE:-Jump if below /not above or equal
7.
JG/JNLE:-Jump if /not less nor equal
8.
JL/JNGE:-Jump if less /not greater nor equal
9.
INT:-It allows ISR to be activated by programmer & external H\W device
Algorithm:
HEX to BCD
1.
Define variable on data segment.
2.
Display message on screen ENTER 4-DIGIT HEX NO.
3. Accept
BCD NO from user.
4.
Transfer 0AH as a divisor in one of the register.
5.
Divide the no by 0AH
6. PUSH
reminder in one of the register
7.
Increment Count _1.
8.
Repeat Till BCD NO is not zero go to step 5.
9. Pop
the content of Reminder.
10.
Display result by calling display procedure.
11.
Decrement Count _1, till Count is not zero repeat step 9 else go to step 12.
12.
Stop
BCD to HEX
1.
Define variables in data segment
2.
Display message on screen ENTER 5-DIGIT BCD NO.
3.
Accept single digit from user
4.
Transfer 10000 to multiplier
5.
Multiply accepted BCD digit by multiplier & add it to RESULT variable.
6.
Accept single digit from user
7.
Transfer 1000 to multiplier
8.
Multiply accepted BCD di git by multiplier & add it to RES ULT variable.
9. Accept
single digit from u ser
10.
Transfer 100 to multiplier
11.
Multiply accepted BCD di git by multiplier & add it to RES ULT variable.
12.
Accept single digit from u ser
13.
Transfer 10 to multiplier
14.
Multiply accepted BCD digit by multiplier & add it to RESULT variable.
15.
Accept single digit from user
16.
Transfer 1 to multiplier
17.
Multiply accepted BCD digit by multiplier & add it to RESULT variable.
18.
Display result by calling display procedure
19.
Stop.
Procedure for accept numbers: (ASCII to HEX)
1. Read
a single character/digit from keyboard using function 0AH of INT 21H
2.
Convert ASCII to HEX as per following:
a.
Compare its ASCII with 30H if No is less than 0 (i.e case of -ve no given) then
go to
step f else go to step c.
b. Compare its ASCII with 39H if No is greater than 9 (i.e case of
character A – F given)
then go to step f else go to step c .
c. Store the resultant bit in NUM Variable.
d. Check whether four digits (16-bit number) or two digits (8-bit number)
are read; if yes
then go to display procedure else go to step 1 for next bit
e. Till counter is zero go to accept procedure.
f. Display massage “I/P is invalid BCD number” & go to step 17.
3. End
of accept procedure.
Procedure for display
Result: (HEX to ASCII)
1.
Compare 4 bits (one digit) of number with 9
2. If
it is <= 9 then go to step 4 else go to step 3
3. Add
07 to that number
4. Add
30 to it
5.
Display character on screen using function 02 of INT 21H
6.
Return to main routine
7. End
of display procedure.