1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

How to make algorithm for assembly language

Discussion in '8051/8951' started by Parth86, Sep 29, 2013.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    I am new in assembly language I don't no where to start i have spent many time searching in google but I did not find excellent tutorial I am confused
    I want to link where i can learn step by step with sample code
    before going to program I want make algorithm please help me to make algorithm 8051


    LABLE start end main org, setb, seta , delay
    PORT port for input and output
    PIN set pin for logic 0 or logic 1
    INSTRUCTION SET mov add sub

    tell me what i do next
     
  2. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,884
    Likes:
    500
    Location:
    Cleveland, OH, USA
    There are several tutorials for the Microchip devices, including one by Nigel Goodwin, who is a supermoderator for this site. Here's a short list:
    http://winpicprog.co.uk/pic_tutorial.htm
    http://www.gooligum.com.au/tutorials.html

    Although those are directed for a different line of chips, you may find their approach useful.

    I just searched on Google for "8051 assembly language tutorial" and got dozens of hits. Maybe there is something not working in your version of Google.

    http://galia.fc.uaslp.mx/~cantocar/microprocesadores/EL_Z80_PDF_S/8051.PDF

    Not quite sure what you mean by "algorithm" in the context of your question. Aren't your last 4 lines an algorithm of sorts?

    John
     
    • Like Like x 1
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    Here on this forum.... Jon Wilder and myself have used the assembly on the 8051 extensively..

    If you look through this 8051 forum you'll find loads of examples that we have posted.. If you download MCU51IDE
    http://mcu8051ide.sourceforge.net/ We can get you started... Loads on this site to look at...
     
    • Like Like x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5

    hello IAN Rogers thanks for response I have read your old post (wish to learn microcontroller) your explanation was very good I am facing some problem can you help
    I want to learn how to write statement for input output port and statement how to set pin as high or low
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    All my 8051 stuff is at home... I'll post something tonight...
     
  7. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,810
    Likes:
    121
    Location:
    Buenos Aires - Argentina
    ONLINE
    Hola Vead,

    I do not intend to show this as difficult but be aware that not only you have to write code. Before that, you should set (configurate) the different options telling how the micro would work.

    It took me quite some time to learn that. Writing code then, was simple. Go as per the datasheet. You hardly could fail if so.

    No matter what micro, you should start with all peripherics disabled what usually, seems to be the natural condition of most of micros (check the datasheet for details).

    Good luck.
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    Code (asm):

        org     0000h        ; Reset vector.
        sjmp    start        ; Jump over vector space.
        org    030h  

    start:                ; This will be assembled at 0x30.
        setb    p2.1        ; Set Port 2 pin 2 to ON. (input)
        setb    p2.0        ; Set Port 2 pin 1 to ON. (input)

    while:
        setb    p3.6        ; Set Port 3 pin 7 to ON.
        acall    delay        ; Wait a while
        clr    p3.6        ; Set Port 3 pin 7 to OFF.
        acall    delay
        jb    p2.1,toggle    ; Jump if Port 2 pin 2 is high
        sjmp    while        ; Or go back to LABEL (while)
    toggle:
        mov    a,R2        ; Load acumulator with value in R2
        jnz    turnon        ; Has it overflowed.. Turn on
        inc    R2        ; NO!! increment R2
        setb    p3.7        ; Set Port 3 pin 8 to ON.
        sjmp    while        ; end while
    turnon:  
        dec    R2        ; Decrease R2  
        clr    p3.7        ; Set Port 3 pin 8 to OFF.
        sjmp    while        ; end while
    delay:
        mov    R1, #0dh        ; Small delay routine
    loop:    djnz    R1, loop
        ret
    end
     
    This program has a good number of commands.. I have commented as best I can..
     
  9. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    I have done some homework for my basic understanding
    I am using 8051 mcu and want to learn assembly
    org statement
    org 0000h start at 0 location
    statement for port
    MOV P0, #FFH ; PORT FOR INPUT
    MOV P1, #00H ; PORT FOR OUTPUT

    STATEMENT FOR PIN
    SETB P0, 11111111 ; ALL PINS ARE HIGH
    CLR P1, 00000000 ; ALL PINS ARE LOW

    if i am wrong please correct me
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    The 8051/2 input/output doesn't work like any other chips.... They haven't got tri-state logic..

    If you need to read ANY pin all you need to do is set it HIGH.. If you need to output, then no action is needed..

    The issue is on the data latch if you set the output pin LOW, forget, then read the pin then you have a ground potential and if you read a high on that pin !!!!sizzle!!!. It's good practice to keep them HIGH if possible when not bieng used as outputs...

    Your statements are correct..
     
    • Like Like x 1
  11. absf

    absf Active Member

    Joined:
    Jun 18, 2012
    Messages:
    206
    Likes:
    27
    Location:
    Malaysia
    I think you mean
    "SETB P0,#11111111b" which is wrong, setb is used only for setting a single bit like
    SETB P0.1 ;set port 0, bit 1
    SETB P1.2 ;set port 1, bit 2.

    If you want to set the whole port to %11111111 then you have to use
    MOV P0,#0FFH ;# means that the number that follows is a literal or numeric constant.

    Same goes for CLR P1,00000000 as CLR can be
    CLR A ;Acc=0
    CLR C ;Carry=0
    CLR bit ;as in CLR P1.2

    Allen
     
    • Like Like x 1
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    Whoops.... Thanks Allen.... Missed that...
     
  13. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    ok now I want to write statement for delay
    turn on LED
    wait 30 ms
    turn off LED
    how to write statement for DELAY ?
    i am trying

    ORG OOOOOO
    SETB P0.1 ;set port 1, bit 1 LED TURN ON
    ACALL DELAY ; WAIT FOR 30MS
    SETB P0.1 ;set port 0, bit 2. LED TURN OFF
     
    Last edited by a moderator: Oct 1, 2013
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    If you look at the code I posted the delay is for 13 iterations of 2uS (two commands) 26uS

    If you load R1 with 0 then you'll get 256 iterations or 512uS ( half a milli second )
    So using a second register R2 we can pre-set the register with 60 and get somewhere near...
     
  15. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    i am confused i don't know what are you trying to say. why we use resistor R1 R2 for delay
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK
    R1 and R2 are registers within the 8051 ... We load them with initial values then let them count down to generate the desired delay..
     
  17. absf

    absf Active Member

    Joined:
    Jun 18, 2012
    Messages:
    206
    Likes:
    27
    Location:
    Malaysia
    Code (asm):
    delay:
            mov    R1, #0dh            ; R1=13 so it delays 13*2=26uS
    loop:  djnz  R1, loop        ; 2uS per loop
            ret
    end

    delay512:
        mov    R1,#00
    loop1:    djnz    R1, loop1    ; 2uS per loop
        ret            ; 2uS*256 = 512uS delay
    end

    delay30mS:
        mov    R2, #60        ; 60*(256*2)=30mS
    loop2a:    mov    R1, #00        ; 256*2=512uS
    loop2b:    djnz    R1, loop2b    ; 2uS per loop
        djnz    R2, loop2a
        ret            ;
    end
     
    The first "Delay" is the orginal code posted by Ian. As #0dh is decimal 13, and each djnz loop would used up 2uS assuming the xtal frequency is 12MHz. So the total delay is approimately 26uS.

    In "Delay512" if you change R1 value to #00, then it would loop 256 times giving a total of 512 uS.

    In "delay30mS" another register R2 is being introduced. It holds a value of dec 60 and goes into the delay512 loop 60 times giving a total of 60*512uS = 30mS.

    Hope that helps.

    Allen
     
    Last edited: Oct 12, 2013
  18. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    510
    Likes:
    5
    i am heartly saying thanks for both Ian and Allen you have done grate help for me I want continue this thread if you don't mind I want to learn more about assembly language
    now I know how to write statement for port , pin and delay
    next topic I want to know arithmetic instruction ADD SUB MUL DIV I know add instruction used for addition for two bits or byte
    but i want to know how and when we need this instruction in project IF you can explain with example its grate for me
     
  19. absf

    absf Active Member

    Joined:
    Jun 18, 2012
    Messages:
    206
    Likes:
    27
    Location:
    Malaysia
    The best way to learn asm language is to practice it as much as possible and also study how other programmers wrote their programs. It's like doing Maths: practice make perfect. Instead of we here doing the examples for you, why not you try your best to write 2 simple programs that uses Add, Sub, Mult instructions as the assignments given below?

    1. Write an assembly program that would add the first 10 term of odd numbers from 1 to 20. i.e. 1+3+5+7+...+19, and store the sum in a variable called "SUM".

    2. Write a program in assembly that would multiply 12 by 12 without using the "MUL" instruction. Store the answer in a cariable called "product".

    Allen
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    907
    Location:
    Rochdale UK

Share This Page