Author Topic: F**king stacks! How do they work?  (Read 6705 times)

0 Members and 1 Guest are viewing this topic.

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
F**king stacks! How do they work?
« on: December 16, 2010, 10:31:34 pm »
All memes aside, i need someone to describe to me what stacks are, how they work, and why and how they are used, in the simplest, easiest, 3rd grade laymans(sp) terms possible. I cant seem to find a plain english description of stacks anywhere (i cant search here since im on my phone). Stacks seem to be rather important, since i see their use in a lot of asm programs, and most of the DCSB graphics functions require you to use the stack. This is basically one of the many things that is making it difficult for me to learn assembly lol. Thanks guise :D

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: F**king stacks! How do they work?
« Reply #1 on: December 16, 2010, 10:37:59 pm »
All stacks are basically the same, so I'll describe the one native to z80 processors. Think of the stack like a Pez dispenser. You push data onto the stack and it is saved to a byte in RAM in front of everything else on the stack. When you pop it, it's removed from that byte in Last in, First out order.
Code: (Stack illustration) [Select]
***********
*Byte that data will be pushed into
*last filled byte of stack. Second byte to be popped
*Second to last byte
*....
*Second byte to stack. Second to last byte that will be popped.
*First byte of stack
***********

Let's say that the stack consists of several bytes, say

Code: [Select]
04
03
02
01
00

If you were to Push $05 onto the stack, then it would look like

Code: [Select]
05
04
03
02
01
00

If you were to Pop the original stack into H, then it'd place the value $04 in H and the stack would look like

Code: [Select]
03
02
01
00
« Last Edit: December 16, 2010, 10:43:51 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: F**king stacks! How do they work?
« Reply #2 on: December 16, 2010, 10:38:12 pm »
A stack works just like it sounds... a stack of stuff. You can put stuff on top (known as pushing), or take things off the top (known as popping). You can't push or pop data from the middle (though if you really wanted to in ASM, you could read data from the middle of the stack). It's usually used for temporarily saving data to be restored later (and whenever you CALL a routine, the stack is used to store the location to return to upon RET).
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
Re: F**king stacks! How do they work?
« Reply #3 on: December 16, 2010, 10:43:15 pm »
So as a visual example, with the gui functions of DCSB libs, when you create a window or text box, it gets pushed on, and you pop it off to remove it from the screen basically?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: F**king stacks! How do they work?
« Reply #4 on: December 16, 2010, 10:44:00 pm »
So as a visual example, with the gui functions of DCSB libs, when you create a window or text box, it gets pushed on, and you pop it off to remove it from the screen basically?
I haven't used DCSB, but that sounds quite possible.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
Re: F**king stacks! How do they work?
« Reply #5 on: December 16, 2010, 10:51:28 pm »
So its pretty much a place to store data if you need to store it LIFO and it isnt entirely necessary for all programs except for calling and returning from functions?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: F**king stacks! How do they work?
« Reply #6 on: December 16, 2010, 10:52:27 pm »
No, see my explanation above. You don't have to use it just like you don't have to use variables in Axe. If you're doing a lot of computations, the registers won't hold everything, so you'll probably have to use the stack.
« Last Edit: December 16, 2010, 10:53:50 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
Re: F**king stacks! How do they work?
« Reply #7 on: December 16, 2010, 10:56:33 pm »
Ah ok. so i should probably get better at z80 before using stacks too much since i wont be doing much complex math until i start making games and whatnot haha

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: F**king stacks! How do they work?
« Reply #8 on: December 16, 2010, 11:01:53 pm »
Ah ok. so i should probably get better at z80 before using stacks too much since i wont be doing much complex math until i start making games and whatnot haha
It's not just for complex math. If I want to use a for a port or something, but need to get the value later, I can just store it on the stack. It's the easiest way to get it.  It's also useful for transferring numbers that normally wouldn't be allowed. For example, assume you couldn't do ld hl,de.  With a stack, you could push de and then pop dl.  There are many uses for it, some are just more common than others.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: F**king stacks! How do they work?
« Reply #9 on: December 16, 2010, 11:09:49 pm »
Thank you Graph.
* Qwerty.55 slaps forehead after realizing that his fancy register swapping was unnecessary and inefficient.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: F**king stacks! How do they work?
« Reply #10 on: December 16, 2010, 11:16:07 pm »
You know ld h,d / ld l,e is faster than push de / pop hl, right?

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
Re: F**king stacks! How do they work?
« Reply #11 on: December 16, 2010, 11:16:56 pm »
I think im gonna have to learn assembly a bit better (im a total noob) before i start trying to implement this too much. Thanks for explaining it to me guise :)

Offline Hot_Dog

  • CoT Emeritus
  • LV12 Extreme Poster (Next: 5000)
  • *
  • Posts: 3006
  • Rating: +445/-10
    • View Profile
Re: F**king stacks! How do they work?
« Reply #12 on: December 17, 2010, 12:07:06 am »
You might be interested in this:

http://ourl.ca/4673/91286

Just look under "saving register values."

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: F**king stacks! How do they work?
« Reply #13 on: December 17, 2010, 12:09:28 am »
Be very careful with the stack though because the "push" and "pop" commands use the same stack as the "call" and "ret" instructions.  So if you call a routine, push something, and then return, you will crash the calculator since the ret is now returning to the address you pushed instead of the return address.  Same thing can happen with pop.  Make sure you match the number of pushes with the number of pops until you feel advanced enough to exploit some of those tricks.  This was my biggest source of bugs when I first started learning z80.

  ___Stack___ 
Main:
  call Routine
MainReturn:
  pop hl
  ret
Routine:
  push hl
  ret
  ___Stack___ 
  MainReturn

Main:
  call Routine
MainReturn:
  pop hl
  ret
Routine:
  push hl
  ret
  ___Stack___ 
  MainReturn 
  HL 

Main:
  call Routine
MainReturn:
  pop hl
  ret
Routine:
  push hl
  ret
  ___Stack___ 
  MainReturn 
  HL 


The error here is that when it gets to the return, its returning to the address HL instead of the place right after the call like it should.  So whatever random address HL holds will be jumped to and likely crash the calculator.  So you CANNOT write subroutines that push or pop arguments.  There actually is a way to do this though using a callback in case you're curious.  This one uses the ix register.

Code: [Select]
Main:
  call PushStuff
  ret

PushStuff:
  pop ix
;Do any pushing or popping you need here:
  push hl
  push bc
  push de
;Then use this instead of a return:
  jp (ix)
« Last Edit: December 17, 2010, 12:11:07 am by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: F**king stacks! How do they work?
« Reply #14 on: December 17, 2010, 12:20:40 am »
Doesn't destroying IX mess with the OS?
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ