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

  

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