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










Friday 14 October 2016

Simple mathematic operation and switching between register banks assembely language program 8051 microcontroller

8051 Assembly Language Program to add , subtract, multiply and divide two 8 bit numbers stored in register r0 and r1 of register bank 0 , store the results of various operations in different registers of register bank 1 (without loop) 

Assumption:
Values stored at register0 , register1 of register 0 are 23 h and 0A h respectively.
h=hexadecimal
Don't get panic with program it is very easy and small


Lets start
In our operation here PSW is used to select register bank only.

As it is assumed that r0 and r1 of register bank 0 have some values. So i had put it through immediate addressing.

To perform add operation one value should be stored in accumulator. And result of addition will be in accumulator(A)
As per requirement we want to store result in register bank 1
So first we have to select register bank1

why didn't we selected register bank 0 before?
Because on resetting the value of PSW register =00h means all bits are zero. Therefore automatically register bank zero get selected.




 As we had selected register bank 1 , now we could transfer the contents of accumulator(A) to register 0 of register bank 1. 
mov r0,a


To perform other arithmetic operation we need the value of register bank 0 therefore again we selected register bank 0. This is done by making 4th bit of PSW(PROGRAM STATUS WORD) again zero
Similary other mathematic operation will perform.



After first clr psw.4 rest of maths operation is performed as it is 

Subb a,r1 
Above instruction will minus contents of r1 from contents of accumulator

   Accumulator
-  value at register 1 of register bank 0
---------------------------------------------------
   result

Mul AB
 instruction for multiplication will multiply two 8 bit numbers stored in A(accumulator) and register 'B' and 16 bit result again get stored into A(lower byte of result) , B(higher byte of result)

Add  operation only

  

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...