Pop and Push

Go To Last Post
48 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi I'm stuck in this assignment we've been asked.

We're given this code:

LDI R25,0b01010101;
lDI R26,0b10101010;
PUSH R25;
PUSH R26;
OR R25,R26;
POP R25;
POP R26;

The question is, what R25 contains after the code.
The answer is 0b10101010

But I can't seem to understand why? can someone please explain it to me? i'm very new to this :P

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This would be a very good time to use the AVRstudio simulator, if available, and step one instruction at a time. Each step you inspect register and stack contents.

While doing that, have the "AVR Instruction Set" document handy to look up the description of each op code.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Tue. Jun 11, 2013 - 07:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you understand the instructions listed? What do you think the answer should be, and why?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

R25 and R26 got swapped (and I do not see the use of OR):
line 3 puts R25 into the stack, line 4 puts R26;
the stack is emptied by line 6 into R25: R25<- old_R26
and emptied again by line 7 into R26 (which becomes old value of R25)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

..or just pretend you are the AVR and use a pen and paper to track the registers and stack contents after each opcode runs.

It should then be fairly easy to see why the answer is what it is...

EDIT ..or you could just let db show off and do your homework for you :(

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you guys! I understand all the instructions, just having trouble really understanding what pop and push do.. but now I have a better understanding

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you understand LIFO stacks? The data popped comes back in reverse order from the order it was pushed (LIFO means last in, first out). In your case, the last in push was R26, and the first out pop went into R25.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But what the OR instruction do? (the other instructions were used to swap bytes 30 years ago, on the 6809 -and might work with many (every) other processors-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:
But what the OR instruction do? (the other instructions were used to swap bytes 30 years ago, on the 6809 -and might work with many (every) other processors-)

I think it was just there to confuse students on the test question. I bet one of the possible answers was 0b11111111.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

+1 with kk6gm

4 possible answers
0b10101010
0b00000000
0b01010101
0b11111111

ok fifth answer
e) something else (that was present in my time at school)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, one should remove this line (adds cycle(s) and confusion)...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:
Well, one should remove this line (adds cycle(s) and confusion)...

My guess is that is what that line was exactly supposed to do. Keep in mind that this is a school project and probably tied to the lesson on how push and pop instructions work.....

It must have been an exercise that a student was supposed to think by himself on writing down what is happening at what stage and knowing what the different instructions do.
In my school days we had many exercises like this were we were given pieces of code and needed to ad the comments to it or tell what certain outcomes where on certain operations.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is cruel: and there are enough unoptimized, confusing softwares in the world: is it necessary to show them to students, who will therefore learn how to write ... confusing, wasting cycles softwares...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:
That is cruel: and there are enough unoptimized, confusing softwares in the world: is it necessary to show them to students, who will therefore learn how to write ... confusing, wasting cycles softwares...
You're missing the point.

There are, in fact, lots of examples of bad code out in the wild. Most of it is maintained by someone who didn't write it. Knowing how to spot bad code, how it works, and not be fooled by it is an important skill.

In any case, that wasn't the point either. The stated problem is akin to:

Quote:
There are 8 Apples on the table, you take 3. How many do you have?
Some of the information is relevant to the problem:
Quote:
you take 3
... some is not:
Quote:
There are 8 Apples on the table
Seeing though irrelevancies is how you design, build, and maintain good software.

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I agree that what I claimed is like training physicians by only showing them healthy people (they are trained at hospitals, today; but are physicians such efficient?) ... but were you trained in English (or any other language) by reading absurd texts, full of typos?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:
but were you trained in English (or any other language) by reading absurd texts, full of typos?
As a matter of fact, I was. French as well. Grammar was still taught in schools when I attended them. Many a lesson began with "What's wrong with this sentence?"

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, that was an error: there are enough typos/grammatical absurdities in the world and they do not need to be shown at school (and people may have enough nervious cells to detect something which is not as it has been taught to them, without stuffing neurons -are they that many- with errors)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I understand all the instructions, just having trouble really understanding what pop and push do..

The way I was taught it (35 odd years ago) was the idea of a restaurant that had a plate dispenser that looked something like this:

As the number of plates available in that reduced every now and again a waitress would come along and push maybe another 10 or 20 plates onto the stack. The whole pile would go down then the customers would come along, one at a time and "pop" a plate off the top of the pile and the big spring inside there would push the plates up a bit so the next one came to the top.

I thought if I did a Google image search for "push pop stack plates cafeteria" that I'd find some good sites explaining this analogy (which is, supposedly, where the terms "push" and "pop" actually comes from) but surprisingly there are very few sites that show this. About the best image I could find was:

That shows items 1, 2 then 3 first being push'd down in to the pile of plates and then pop'd off the top of the pile as it bounces back up.

You cannot access '1' while '2' and '3' are still on top. You have to "pop" 3 to be able to access 2 and then you have to pop 2 to be able to access 1.

If you think of each PUSH in your code as adding something to the pile of plates and each POP as taking the "one off the top" it might help you to visualise what's going on.

(did for me 35 years ago!).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MartinM57 wrote:
..or just pretend you are the AVR and use a pen and paper to track the registers and stack contents after each opcode runs.

It should then be fairly easy to see why the answer is what it is...

Martin has given the best advice by far.

Personally, I prefer a pencil.

When you learned how to add up at school, you practised with paper. As you grow older, you may well perform calculations in your head. But think about it. Would you recommend a schoolchild to start off with 'mental arithmetic' ?

Bear in mind that your teacher has given you one trick question. She will probably give you some more before you finish this class !

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

I understand all the instructions, just having trouble really understanding what pop and push do..

The way I was taught it (35 odd years ago) was the idea of a restaurant that had a plate dispenser that looked something like this:

As the number of plates available in that reduced every now and again a waitress would come along and push maybe another 10 or 20 plates onto the stack. The whole pile would go down then the customers would come along, one at a time and "pop" a plate off the top of the pile and the big spring inside there would push the plates up a bit so the next one came to the top.

I thought if I did a Google image search for "push pop stack plates cafeteria" that I'd find some good sites explaining this analogy (which is, supposedly, where the terms "push" and "pop" actually comes from) but surprisingly there are very few sites that show this. About the best image I could find was:

That shows items 1, 2 then 3 first being push'd down in to the pile of plates and then pop'd off the top of the pile as it bounces back up.

You cannot access '1' while '2' and '3' are still on top. You have to "pop" 3 to be able to access 2 and then you have to pop 2 to be able to access 1.

If you think of each PUSH in your code as adding something to the pile of plates and each POP as taking the "one off the top" it might help you to visualise what's going on.

(did for me 35 years ago!).

Yeah, but the stack on an AVR doesn't have castors.

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Yeah, but the stack on an AVR doesn't have castors.

I thought that is what the bumps on the BGA package are for...

I was also taught with the cafeteria-plate analogy in 1970. The kids here will need to comment on whether it is still done.

There are further analogies. For example, how many plates are visible? That can be thought of as the "stack frame" where one can peek below the top-of-stack.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Posting that did make me wonder how these plate dispensers actually work. What IS the cleverness that makes sure only 2 or 3 plates are actually visible at the top of the pile all the time? Are those three metal "prongs" sticking out of the top really "holding" the top plate in place? I don't think so because it's too easy to just lift the top plate off.

Enquiring minds need to know these things!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another things worries me: one uses two bytes on the stack (and there is little RAM in the avrs world). Stacking one register, copying into this register the value of the other -if it is possible-, and getting the top of the stack into the other register could save one stack byte, and might be not such cycles greedy...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have I lost the plot ?

alibrie came here with her school problem.

I don't think that RAM usage or efficiency come into the picture.

There are occasions when PUSH and POP can be handy to exchange data when no other instructions quite fit the bill. The Z80 comes to mind.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The Z80 comes to mind.

Ah yes:

EX HL,(SP)

What a wonderful opcode that was - one of the Z80's more powerful ones.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

What IS the cleverness that makes sure only 2 or 3 plates are actually visible at the top of the pile all the time?

Simple mechanical engineering, methinks: A spring.

http://en.wikipedia.org/wiki/Spr...(device)

Next time you're around one of those, just try to press the top plate down, release, down... in an oscillating pattern.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes but there's more to it than just a spring. If you take a spring that takes 10g to compress and put 100g on it then it still compresses by the same amount. Whereas if you have a spring that takes 100g to compress and put 10g on it then it will barely compress. Yet this device keeps the top plate level with the hole. So it's like the spring is somehow compensating for the weight of plates on top of it and is kind of "pushing harder" for more plates in order to keep the point of compression at the same level. Just seems kind of clever.

A bit like the SP mechanism in an AVR (he said trying to drag things back on topic!).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Yes but there's more to it than just a spring.

Ummm--say again? How do simple household scales work? Compress a spring; dial moves in proportion; roughly linear.

http://www.toolplanet.com/produc...
http://compare.ebay.com/like/250...

So, you size the spring so that the weight of one plate causes it to compress the thickness of one plate.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I suppose assembly is meant to spare as much RAM and cycles as possible (else, it is very cruel and somewhat absurd to teach assembly) :

Instead of:
PUSH R25;
PUSH R26;
POP R25;
POP R26;
Would:
PUSH R25; The old value of R25 hides into the stack
LDI R25, 0 ; in any base
OR R25, R26; R25 gets a copy of (R26 OR 0) == R26
POP 26 ; R26 gets the original value of R25; the stack is restored

work to swap R25 and R26?
(and PUSH and POP eat 2 cycles LDI and OR eat only one according to http://www.atmel.com/Images/doc1...)....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the manual of one of these stack thingies:
http://www.apwwyott.com/home/50/...

Quote:
SPRING ADJUSTMENT:
Adjustubes are factory set to an average rate. however, you may need to adjust the springs to obtain the best dispensing rate for your dinnerware.

1. Lift the tube out of the shield.
2. Place approximately 4 inches of dinnerware into the tube, and note the
height of the dinnerware above the tube.
3. Load the tube until it is approximately 3/4 full, and again note the height of the dinnerware above the tube. if the height is the same as in step 1, the rate is correct.

If the height is higher than in step 1, unhook one spring from he bottom of the spring hanger, and repeat steps 2 and 3. Continue disconnecting springs from the bottom hanger until you find the proper spring combination for your dinnerware.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PUSH R25; The old value of R25 hides into the stack
LDI R25, 0 ; in any base
OR R25, R26; R25 gets a copy of (R26 OR 0) == R26
POP 26 ; R26 gets the original value of R25; the stack is restored

Sorry but why the LDI and OR? Why not MOV?

PUSH R25; The old value of R25 hides into the stack
MOV R25, R26; R25 gets a copy of R26
POP 26 ; R26 gets the original value of R25; the stack is restored

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because I did not thought MOV existed and this combination ate one cycle less.... (I used only the instructions I could see in OP, as I am avr-assembly - clueless)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

dbrion0606 wrote:
Well, I suppose assembly is meant to spare as much RAM and cycles as possible
Then one would use this:

eor r25,r26
eor r26,r25
eor r25,r26

That swaps r25 and r26 without any RAM and in 3 cycles.

Why you can't simply accept that the example has only one single purpose: to teach how the stack works. Perhaps other topics (like efficiency) are coming later, and perhaps even with the same code, like
"Do you remember the code from the stack lesson? Now let's see how we can do the same more efficient. ..."

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, because I am not sure OP is a student (if it had been explicitly stated, I would not have given kind of a solution); (s)he can be someone curious and finding optimal solutions is interesting (your solution seems optimal -no RAM -it is optimal- , 3 cycles-, though I cannot prove it w/r cycles)...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Well, because I am not sure OP is a student
I thought that it was quite clear:
Quote:
Hi I'm stuck in this assignment we've been asked.

We're given this code:

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Would any non-student ask this sort of question?

At least it is a sensible assignment (compared to many school ASM assignments)

1. Learn how to search the internet for AVR instruction set.
2. Trace the behaviour by hand.
3. Answer the question.
4. Go to the top of the class.
===============================
20 minutes.

The idle student spends hours trying to get her homework completed by someone else. Makes no effort to study by themselves.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, I wouldn't jump on OP too hard on this one. The question/answer were apparently puzzling, and the experts here were consulted. ;)

Now, if there are further inquiries that indicate that the advice given (to consult AVR Instruction Set document and similar), then ...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
Quote:

Yes but there's more to it than just a spring.

Ummm--say again? How do simple household scales work? Compress a spring; dial moves in proportion; roughly linear.

http://www.toolplanet.com/produc...
http://compare.ebay.com/like/250...

So, you size the spring so that the weight of one plate causes it to compress the thickness of one plate.


Hooke's Law, if I remember correctly(from 45 or so years ago).
[Nostalgia alert]
The whole push/pop thing was originally explained to me by analogy to a deck of cards, but I have to say that the plate storage device is a better one.
Also, being raised on a diet of 6502/6800 the "pop" operation was called "pull".
As for the Z80 being able to "ex hl,sp", the awesome 6809 could use either stack pointer as an index, as in "lda -4,s". [/Nostalgia alert]

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
David, I wouldn't jump on OP too hard on this one. The question/answer were apparently puzzling, and the experts here were consulted. ;)

Now, if there are further inquiries that indicate that the advice given (to consult AVR Instruction Set document and similar), then ...


Translation:
"OK, David, I'm the resident curmudgeon around these parts, get back to your milk(of human kindness)."

Four legs good, two legs bad, three legs stable.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The whole push/pop thing was originally explained to me by analogy to a deck of cards, but I have to say that the plate storage device is a better one.

Agreed--for LIFO, plus the concept that only the TOS is "visible".

For FIFO, the deck of cards held in hand might be better.

I recall that there were names for different constructions and one of them was "deque" for a type of queue. Aaah--Wikipedia helps:

Quote:
In computer science, a double-ended queue (dequeue, often abbreviated to deque, pronounced deck) is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail).[1] It is also often called a head-tail linked list, though properly this refers to a specific data structure implementation

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I like the deck of cards analogy better than the plate stacking machine thingy. With the plate machine, you get the impression that when you add something to the stack, the stack moves rather than the stack pointer moving. Also it hides how much is currently on the stack. With the deck of cards you see the stack increasing upwards and you can easily see how big the stack currently is. Also, each card has a value whereas each plate is the same.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
... until you find the proper spring combination for your dinnerware.
It's official:
Anyone seen the OP lately? ;)
John_A_Brown wrote:
[Nostalgia alert] Also, being raised on a diet of 6502/6800 the "pop" operation was called "pull".
As for the Z80 being able to "ex hl,sp", the awesome 6809 could use either stack pointer as an index, as in "lda -4,s". [/Nostalgia alert]
Brings a tear to my eye... learned assembler on the 6809...

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Jun 12, 2013 - 05:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

the awesome 6809 could use either stack pointer as an index, as in "lda -4,s".

Yup, love the 6809. I did Forth on it once. The twin stack pointers and those addressing modes made the Forth NEXT almost as "tight" as the PDP11 I was also implementing it on - only 2 or 3 opcodes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I recall that there were names for different constructions and one of them was "deque" for a type of queue.

A term coined by Earl Schweppe as a graduate student. He went on to be the first chair of the newly formed Department of Computer Science at the University of Kansas (about 1968 or 1969). He should have quit while he was ahead.

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:
Quote:
...the awesome 6809 could use either stack pointer as an index, as in "lda -4,s". [/Nostalgia alert]
Brings a tear to my eye... learned assembler on the 6809...

I want a 50MHz, 1-3 cycle/instruction 6809 micro family, and I want it now!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:
I want a 50MHz, 1-3 cycle/instruction 6809 micro family, and I want it now!
You can get close. From Wikipedia:
Quote:
6809 cores are available in VHDL and can be programmed into FPGA and used as an embedded processor with speed ratings up to 40 MHz.
Here's one:I remember seeing a teardown of 6809, layer by layer, to reverse engineer it. Helped to identify how it worked, a few sublte design defects, and the true consequences of running undefined opcodes. Facinating stuff.

Can't find it now. I believe it was a video and a PPS slide presentation.

JJ

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Yet this device keeps the top plate level with the hole.

Read the Wikipedia article on different characteristics fos springs. They can be "linear", "exponential", "logarithmic" etc...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

on the derailed part:
there are springs that are non linear, so adding more weight gives less delta compression and linear springs.
In cars and motor bikes the springs are not linear to give better road handling. Note that this non linearity is probably only very small. They do this by changing the distance between the different windings(?)
By chance there was last monday a TV program that had a special on how to set-up/adjust the suspension on a motor and there they showed that.

regards