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

Stack instruction in assembly 8051

Discussion in '8051/8951' started by Parth86, Oct 7, 2013.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    what exactly the use of stack instruction in assembly program I have read article tried to understand but I am bit confused I think stack is the hardware of RAM processor copies data move data and access bit from it how to use push and pop instruction
    I need some help here if anyone can help with program example in terms of sensors, led, and other IF you can its grate for me
     
    • Like Like x 1
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    The two stack commands PUSH and POP are for use when using structured programming..

    The stack is 0x30 to 0x7f of the general purpose registers...

    When you are doing anything say using R1 and R2 ( which I used in the delay ) and then you need to use these registers elsewhere... We can PUSH R1 and R2 onto the stack... Then once we return to what we were originally doing we can POP the R2 and R1 back so we can carry on....

    The order is imperative.. Watch this video...
     
    • Like Like x 1
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    ok I am trying
    DELAY:
    MOV R1, #5 ; load in R1
    MOV R2,#3 ; load in R2
    LOOP:
    PUSH R1 ; push R1
    PUSH R2 ; push R2
    DJNZ R1,LOOP ; decrease R1
    DJNZ R2,LOOP ; decrease R2
    POP R1 ; pop R1
    POP R2 ; pop R2
    RET
     
    • Like Like x 1
  4. dave

    Dave New Member

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


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    Two things.... You must POP the reverse of PUSH... But!!! this loop will NEVER end because you are pushing the decremented values and poping the non decremented values..

    R1 and R2 will just keep swapping values all day long....
     
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    if i increase R1 then
    DELAY:
    MOV R1, #5 ; load in R1
    MOV R2,#3 ; load in R2
    LOOP:
    PUSH R1 ; push R1
    PUSH R2 ; push R2
    INC R1,LOOP ; increase R1
    DJNZ R2,LOOP ; decrease R2
    POP R1 ; pop R1
    POP R2 ; pop R2
    RET
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Your code isn't a workable one.... Why are you using POP and PUSH here???
     
    • Like Like x 1
  8. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    actually I don't know how to use push and pop instruction in assembly program I am trying to understand with example
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,306
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Ok! Say you have this code..
    Code (asm):

    loop:
       jnz   P0.0, inc   ; test for up press
       jnz   P0.1, dec   ; test for down press
       sjmp  loop
    inc:
       setb  R2.0        ; Up flag
       ; do up stuff
       sjmp  loop

    dec:
       setb  R2.1        ; down flag
       ; do down stuff
      sjmp  loop
     
    Now you want to incorporate a delay function.. BUT!!! the delay function NEEDS R2... The problem is you will loose the two flags UP and DOWN if you corrupt R2 in the delay function

    Code (asm):

    loop:
       jnz   P0.0, inc   ; test for up press
       jnz   P0.1, dec   ; test for down press
      acall  delay
       sjmp  loop
    inc:
       setb  R2.0        ; Up flag
       ; do up stuff
       sjmp  loop

    dec:
       setb  R2.1        ; down flag
      ; do down stuff
       sjmp  loop

    delay:
       push R2
      ; do delay stuff.... If you destroy the data in R2 it will be refreshed on exit..
      pop  R2
      ret


     
     
  10. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    546
    Likes:
    5
    thanks this is very good for my understanding
     
  11. absf

    absf Active Member

    Joined:
    Jun 18, 2012
    Messages:
    206
    Likes:
    27
    Location:
    Malaysia
    I tried to come up with a simple program that would demonstrate the the use of PUSH and POP instruction. But nothing popped up on top of my head except on the ISR routines for saving the status flags etc.

    The 8051 has so many registers R0-R7 and duplicated (what's the word for 4x) in 4 banks. There are Acc and B registers so there is actually not much need to push or pop registers on subroutines. There's no POP or PUSH instructions on the 12F and 16F PIC mcu.

    Back in the 70s the motorola & mos technology mpu like 6800 and 6502 which have so little registers only A,B,X,Y so that's why PHA and PLA are necessary to make the life easier for programmers.

    Allen

    p/s this is just my personal ideas. but of course it is nice to have pop and push instructions..
     

Share This Page