Search This Blog

Friday, August 5, 2011

WAP in assembly language to toggle bits of port 1 with a delay which depends on the value of a number in R0




ORG 0H ; START PROGRAM FROM 0H
START:  MOV A, #O ;
MOV P1, A ;
MOV R0, #30H ;
ACALL DELAY ; CALL DELAY
CPL A ; COMPLEMENT A TO TOGGLE
MOV P1, A ; TOGGLE P1 BY TRANSFERING A
MOV R0, #OFFH ; CHANGE R0 VALUE TO CHANGE DELAY
ACALL DELAY ; CALL DELAY WITH DIFFERENT R0
SJMP START ; JUMP BACK TO START




DELAY: NOP ; DELAY SUBROUTINE
LOOP: DJNZ RO, LOOP ;
RET ;


END ;
; NOTE THE PROGRAM RUNS CONTINUOSLY TOGGLEING PORT 1 REPEATEDLY
; QUESTION ARISES WHERE ONE NEED TO USE CONTINUOUS TOGGLEING OF
; PORTS. WELL SIMPLE APPLICATION INCLUDES BLINKING LEDS ON/OFF
; WITH ON TIME LARGER THAN OFF TIME.

Thursday, August 4, 2011

Binary Coded Decimal (BCD) Number System


BCD stands for Binary Coded Decimal.
Use Of BCD:
BCD means digits starting from 0 to 9.
In our day to day life we are not using hex values, so sometimes it is quite difficult to work with hex. Hence to reduce the complexity and for the ease of users, 8051 is provided with BCD.
Now while working with Embedded Systems, we encounters two terms in BCD number system:
  1. Unpacked BCD:
    Those numbers (in hex) whose upper nibble is zero, are considered as unpacked BCD numbers.
    This means  that in any number if the upper 4 bits are zero, then it is said to be in unpacked format.
    In unpacked BCD format, a single byte has only one BCD number in it.
    Ex:      ”0000 1001″ and “0000 0110″ are unpacked BCD for  9 and 6 respectively.
  2. Packed BCD:
    Those numbers whose upper nibble is not equal to zero, but posses some value is termed as Packed BCD.
    In packed BCD, a single byte has two BCD numbers in it, one in the lower 4 bits and one in upper 4 bits.
    Ex:      ”0011 1001″ and “0001 0001″ are packed BCD for 39 and 11 respectively.
Ok, now we understand what is a BCD number system.
  • Now the Q. is how to convert a hex number into decimal number..??
We can do this by two methods:
  1. In order to convert a hex value in BCD value, we have to add 6 under the number which is higher than 9.
    Consider the following examples.
    • Convert 2Ah in BCD  :
    2Ah
    +06h
    =30h
    ~ So, 30h is the BCD value of 2Ah .
    • Convert A2h in BCD  :
      A2h
      + 60h
      = 102h
      ~So, 102h is the BCD value of A2h.
    • Convert BCh in BCD  :
      BCh
      + 66h
      = 122h
      ~So, 122h is the BCD value of BCh.
  2. Another method is, by the use of DA Instruction:
    The DA (Decimal Adjust for Addition) instruction is provided in the 8051 to perform the above procedure itself.
    By the use of DA instruction, the controller will automatically add 6 under the value greater than 9 and will show us the BCD value.
    As simple as that.. :)
    Use of DA Instruction:
    MOV B,#25h        ; Move 21 hex in Register B
    MOV A,#49h       ; Move 8A hex in Accumulator
    ADD A,B                ; Add Register B with A
    DA A                       ; Convert hex value into BCD value
    After the execution of program we will get 74h in accumulator.
    NOTE:
    The DA Instruction works only with Accumulator, you can not use it with any other 8bit or 16bit register.
    The DA instruction is only used after ADD instruction; it will not work after INC operation.

Monday, August 1, 2011

Difference Between RISC and CISC

To terms RISC(Reduced instruction set computer) and CISC(Complex instruction set Compter) were coined in the late 1970's. The processors at that time were of mainly 2 types i.e is RISC and CISC. Former being the cheap one and the latter being the costly one. But in the modern time the terms RISC and CISC have almost become meaningless as both RISC and CISC have undergone evolution and the distinction between the two has progressively become blurred with both being used in computer systems. So we can say our processor 8085 is a RISC and controller 8051 is a CISC.

RISC

Reduced instruction set Computer. It is a type of microprocessor that has been designed to carry out few instructions at the same time.  As instruction are few it can be executed in a less amount of time. Another advantage is the use of fewer transistor reducing its cost.
Features include:
  • Demand less decoding
  • Uniform instruction set
  • Identical general purpose register
  • Simple addressing modes
  • Fewer data types in hardware
CISC
Complex instruction set computer. Its actually a CPU designed to carry out many operation in a single in a single instruction. These can be loading from and to memory and performing mathematical operation etc.
Features Include:
  • Complex instruction
  • More number of addressing modes
  • Highly Pipelined
  • More data types in hardware

Monday, July 25, 2011

Absolute division using loop

;program to divide absolute value like 20/5 = 4 etc.
;or like 4/2 not 20.5/12 no decimal values will be divided
ORG 0H          ;
MOV A, #25      ;
MOV R0, #05     ;
MOV R1, #00H    ;
LOOP: SUBB A, R0;
      INC R1    ;
      JNZ LOOP  ;
END

Multiplying 2 numbers using MUL mnemonic

;program to multiply using MUL mnemonic
ORG 0H;
MOV A, #00H ;
MOV R0,#13H
MOV R1, #00H
MOV B, #03H ;
MUL AB 
END

Multiplying 2 numbers with 16 bit answer

; program to add 2 numbers whose answer is 16 bit
ORG 0H;
MOV A, #00H ;
MOV R0,#13H
MOV R1, #00H
AGAIN: ADD A, #12H;
       JC INCREMENT ; 
       DJNZ R0,AGAIN;

INCREMENT: INC R1 ;
           SJMP AGAIN;
END

Multiplying with 8 bit answer using loop in 8051

; program to multiply two numbers using loop answer is 8 bit 
ORG 0H;
MOV A, #00H ;
MOV R0,#13H

AGAIN: ADD A, #12H;
       DJNZ R0,AGAIN;
END

Saturday, July 23, 2011

Jump Instructions


JZ LABEL ; JUMP IF A = 0
JNZ LABEL ; JUMP IF A <> 0
DJNZ Rn, LABEL ; DECREMENT AND JUMP IF REGISTER <> 0
CJNE A, DATA ; JUMP IF A <> DATA
CJNE REG, #DATA ; JUMP IF BYTE <> #DATA
JC LABEL ; JUMP IF CY = 1
JNC LABEL ; JUMP IF CY = 0
JB BIT, LABEL ; JUMP IF BIT = 1
JNB BIT, LABEL ; JUMP IF BIT = 0
JBC BIT, LABEL ; JUMP IF BIT = 1 AND CLEAR BIT
SJMP LABEL ; SHORT JUMP
LJMP LABEL ; LONG JUMP

Assembling and Running 8051 Program


4 basic steps are needed to be performed to assemble and run an 8051 program


Step 1: To write the program using any editor and save it with extension .asm or .src depending on the assembler one is using.


Step 2: The source file is fed into assembler which converts it to machine code and gives us 2 files one with extension .lst i.e. the list file and other with extension .obj i.e. the object file.


Step 3: Old assemblers required a third step to link all object files to create one absolute file with extension .abs This file is used by 8051 trainers to monitor program.


Step 4: The abs file is fed into a program called "OH" (i.e Object to hex converter) which creates a file with extension .hex that is ready to be burned in the ROM of the controller.


The List file
This is a very important file for programmers as it shows all opcodes and memory address as they will be stored in the controller. This makes error detection of labels and code quite easy.

Structure of Assembly language


An assembly language program consits of lines of assembly language instruction. An assembly language instruction consists of 


mnemonics, optionally followed by one or two operand. Obviously the operands are the one being manipulated and mnemonics are 


telling CPU how to manipulate them.


An assembly language consists of 4 fields


label: mnemonic [operands] ; comments


the Syntax above is self explanatory. So no post will be on its description.