Continue to Site

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.

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

Stack instruction in assembly 8051

Status
Not open for further replies.

Parth86

Member
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
 
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...
 
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...
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
 
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....
 
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....
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
 
Ok! Say you have this code..
Code:
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:
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
 
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..
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top