Saturday, 22 October 2016

Assembly language program to convert hexadecimal number to BCD (for 8051 microcontroller)


Assumptions:

1. A hexadecimal number is at location 40h and we need to convert it to BCD
2. Value stored at r0 is 40h
3. At the address 40h value is FFh
4. result stored in unpacked BCD form as ones digit at r3, tens value at r2 , hundred value at r1

Before we start with actual coding lets revise about BCD

BCD is a binary representation of numbers from 0 to 9
Since there could be max 4 bit change from 0 to 9 therefor BCD could be represented by 4 bits.
9d=9h=1001 BCD

Lets back to the program,
Max 8 bit number could be FFh = 255d , So on converting any 8 bit number to bcd we have our answer upto three places ones, tens, hundred.
Answer will stored in unpacked BCD form

first three instructions are only mov instructions
first r0 is assigned a value 40h which is the address where our value (hex value to be converted into BCD) is stored 
we will get this value from memory location 40h and assign it to accumulator by instruction:
mov a, @ro 

This type of addressing is called indrect addressing where a register  stores   address of any memory location and value from that address is assigned to destination.

the above picture is showing the procedure
As our leftmost digit could at hundred place max therefore we will divide our hexvalaue by 100(decimal) = 64 (hex) instruction 4
quotient will be the leftmost value.
remainder of it is again divided by 10d=Ah
quotient will be the middle value,
remainder is again divided by 1

So we got three values at hundredth place, tens , ones would be stored at r1, r2, r3










No comments:

Post a Comment

Program to solve 4 variable equation(octave code)

function [x,y,z,alpha]=eq4variable( aaaa1,bbbb1,dddd1,eeee1,cccc1,aaaa2,bbbb2,dddd2,eeee2,cccc2,aaaa3,bbbb3,dddd3,eeee3,cccc3,aaaa4,bbbb4,d...