Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: Broseph Radson on December 16, 2010, 10:31:34 pm

Title: F**king stacks! How do they work?
Post by: Broseph Radson 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
Title: Re: F**king stacks! How do they work?
Post by: AngelFish 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
Title: Re: F**king stacks! How do they work?
Post by: calc84maniac 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).
Title: Re: F**king stacks! How do they work?
Post by: Broseph Radson 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?
Title: Re: F**king stacks! How do they work?
Post by: calc84maniac 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.
Title: Re: F**king stacks! How do they work?
Post by: Broseph Radson 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?
Title: Re: F**king stacks! How do they work?
Post by: AngelFish 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.
Title: Re: F**king stacks! How do they work?
Post by: Broseph Radson 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
Title: Re: F**king stacks! How do they work?
Post by: jnesselr 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.
Title: Re: F**king stacks! How do they work?
Post by: AngelFish on December 16, 2010, 11:09:49 pm
Thank you Graph.
/me slaps forehead after realizing that his fancy register swapping was unnecessary and inefficient.
Title: Re: F**king stacks! How do they work?
Post by: Runer112 on December 16, 2010, 11:16:07 pm
You know ld h,d / ld l,e is faster than push de / pop hl, right?
Title: Re: F**king stacks! How do they work?
Post by: Broseph Radson 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 :)
Title: Re: F**king stacks! How do they work?
Post by: Hot_Dog 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."
Title: Re: F**king stacks! How do they work?
Post by: Quigibo 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)
Title: Re: F**king stacks! How do they work?
Post by: AngelFish on December 17, 2010, 12:20:40 am
Doesn't destroying IX mess with the OS?
Title: Re: F**king stacks! How do they work?
Post by: Hot_Dog on December 17, 2010, 12:21:18 am
Doesn't destroying IX mess with the OS?

No, but destroying IY does
Title: Re: F**king stacks! How do they work?
Post by: AngelFish on December 17, 2010, 12:22:39 am
Okay, I'll be sure not to touch that. What exactly does the OS use IY for, anyway?
Title: Re: F**king stacks! How do they work?
Post by: Hot_Dog on December 17, 2010, 12:24:44 am
Okay, I'll be sure not to touch that. What exactly does the OS use IY for, anyway?

The OS has what are called "customized flags" to keep track of events that have occured, modes that have been set, graphs that have been drawn, etc.  IY points to where these flags are located.
Title: Re: F**king stacks! How do they work?
Post by: ztrumpet on December 17, 2010, 07:46:22 pm
But if you mess with IY, you can do a ld IY, Flags, right? ???
Title: Re: F**king stacks! How do they work?
Post by: Hot_Dog on December 17, 2010, 07:48:28 pm
But if you mess with IY, you can do a ld IY, Flags, right? ???

That's right.  But one has to remember to do that.

The TI-83+ has some custom flags available for ASM programmers to use.  I make use of these flags in S.A.D., but sometimes I would forget to use ld IY, flags after drawing sprites.  Needless to say, I got some very wacky results, because IY pointed to the wrong spot.
Title: Re: F**king stacks! How do they work?
Post by: jnesselr on December 17, 2010, 07:49:03 pm
But if you mess with IY, you can do a ld IY, Flags, right? ???
I think so. I know there's an actual address. I'm sure you could do push iy \ pop hl bcall DispHL to find out.

EDIT: ninja'd