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 Pointer working in the assembly code

Status
Not open for further replies.

Savy_tech

New Member
Can anyone tell me how stack pointer is working in the below code, since I am new to this field:

CSEG AT 0H
MOV 0x40, #5
MOV 0X41, #6
PUSH 0X41
PUSH 0X40
POP 0X41
POP 0X40
END
 
What do you need to know.

Location 0x40 holds number 5
Location 0x41 holds number 6

Put the contents of location 0x41 on the stack
Put the contents of location 0x40 on the stack

Retrieve top value from stack place in location 0x41
Retrieve top value from stack place in location 0x40

Location 0x40 holds number 6
Location 0x41 holds number 5


Basically swap those two location values...
 
Last edited:
Thank you Ian Rogers it was helpful, but still, I am confused regarding, that how does 0x40 hold 6 and 0x41 holds 5, since PUSH and POP work on the principle of last in first out if it is a sequential execution in assembly language
 
because the stack is a growing set of data locations... if you push three values say 12, 13 and 14in that order 14 will sit on the top.. one pop and 13 will sit on the top... There is only x levels in the stack

Yep sorry les... edidted
 
Last edited:
Stacks normally progress downwards so if the stack is empty and starts at address 0x1000. The stack pointer will point to 0x1000. So using Ian's example you take the value you want to save on the stack from location 0x41 (Which contains the value 6)
You push the value of 6 from location 0x41 onto the stack.
So location 0x1000 now contains 6 (The stack pointer is decremented so now points to location 0xFFF)
You now push the value of 5 from location 0x40 onto the stack
So location 0xFFF now contains 5 (The stack pointer is decremented so now points to location 0xFFE)

Now you POP the stack. The stack pointer is incremented to point to the last used location on the stack so it points to 0xFFF.
this contains the value 5 so you place 5 in location 0x41
Now you POP the stack again . The stack pointer is incremented to point to the last used location on the stack so it points to 0x1000.
this contains the value 6 so you place 6 in location 0x40
It is the last thing that is pushed onto the stack that is POPed off the stack. It is up to you where you decide to save it.

Les.
 
Les Jones
"You push the value of 6 from location 0x41 onto the stack.
So location 0x1000 now contains 6 (The stack pointer is decremented so now points to location 0xFFF)
You now push the value of 5 from location 0x40 onto the stack
So location 0xFFF now contains 5 (The stack pointer is decremented so now points to location 0xFFE) "
I tried either ways but the result is swapped numbers at those addresses, by the way, 8051 stack is up growing stack (i.e. each time you push the SP is incremented) if I am wrong please correct me.
 
It's called a stack because it's like a stack of paper or boxes. In a processor, it normally starts at a high address and decrements but just think of it like a stack of paper, you write a number on the paper and stack it. When you pop it, you just get the top piece of paper. Does that help?

Mike.
Edit, not familiar with 8051 and didn't know it's stack grew upwards but the principal is the same.
 
Pommie Yeah I am familiar with the concept just I am not able to figure out why the final answer is swapped numbers:
If I interchange the addresses the result is the same, so my concern is, does assembly language follow a sequential execution of code like in the C, or how is it?

CSEG AT 0H
MOV 0x40, #5
MOV 0X41, #6
PUSH 0X40
PUSH 0X41
POP 0X41
POP 0X40
END

Quote
 
It shouldn't be reversed. Push 0x41 followed by pop 0x41 should result in the same value in location 0x41. Try just those two operations.

Mike.
Edit, where (at what address) is the stack?
 
In 8051, when the controller is initialized Stack Pointer starts from 07H. In RAM, it starts from the 08 location. I don't know why but now it is working perfectly fine, maybe it is the fault of the compiler I guess, anyway thank you so much for your help Ian Rogers, Pommie, and Les Jones. Since I am a beginner to controllers so I was not able to figure out the solution.
 
Ian Rogers the code that I posted yesterday swaps the numbers and the second one which I posted today doesn't swap any number. But till today both the codes were swapping numbers, but when I tried with only one address location it worked fine and then I tried with both the address locations, now it is working as said. I don't know what has happened might be compiler issues since I am using Keil version 4.
 
You can search the assembly language books online to learn it, I also learned it for a while a long time ago and I remember the stack is in order from top to bottom.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top