Guide to stack basics.
SneakyCodes Gaming Forum
Welcome to SneakyCodes. Contrary to what some may want you to think, SneakyWorld.forumotion is IN NO WAY affiliated with SneakyCodez. Anyone who tries to tell you different, is a lying piece of shit.

SneakyCodes is in the process of an entire site reboot, with no sections, topics, forums, and users. Feel free to sign up, and check back regularly for updates.

Thank you,
SwoRNLeaDejZ


SneakyCodes Gaming Forum

The Evolution Begins...
 
HomePortalRegisterLog inSneakyCodes RSS Feed
Everyone go to SneakyWorld! Link on the home page!
Sneaky Generators

Sneaky Image Editor
The Walking Dead Game

All Generators By SwoRNLeaDejZ


FTB3 Room Generator
FTB3 Name Generator
FTB2 Tag Generator
FTB2 Name Generator
FTB2 Room Generator


Donate to SneakyCodes Today!
Affiliates
Please Join and Visit the SneakyCodes Affiliates




We Love A-G!!!!

Join TheUnseenCoders.com Today!!


Free Forumotion Designs


kHaoZ is a frickin beast :)

Tech Support Guy
Latest topics
» Deepest regards to the Sneaky Community
Tue Oct 28, 2014 5:42 am by _-_EVIL-LOCO_-_

» damn sneaky, RiP. It was fun
Sat Oct 11, 2014 1:25 am by Sylen7Nato

» Just Another Blonde Joke xD FUNNY AS FAWK
Mon Mar 03, 2014 5:37 pm by Rossy Redness

» Hey it's Skinny107
Tue Dec 03, 2013 8:24 pm by o5Gz

» ps3 hacking!
Mon Jun 10, 2013 9:42 pm by Sylen7Nato

» PSP ISO List
Mon Jun 10, 2013 9:28 pm by Sylen7Nato

» tiger render
Fri May 10, 2013 3:11 pm by mk7

» SWORN.
Sun Dec 09, 2012 6:25 am by EverEffects

» homefront redeem code
Fri Nov 30, 2012 1:32 am by EverEffects

» Calling Sworn
Tue Nov 20, 2012 4:34 pm by EverEffects

» Thought Id Stop By And Say Hi
Thu Nov 15, 2012 3:40 am by HappySnacks69

» An Introduction
Tue Nov 13, 2012 2:33 am by Ace700

Views
Powered by web analytics software.
Statistics
We have 4719 registered users
The newest registered user is CraftPR

Our users have posted a total of 13085 messages in 2891 subjects
Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest

None

Most users ever online was 59 on Sat Dec 03, 2011 5:04 pm
Facebook Stream
Top posters
SwoRNLeaDejZ
 
blckhwksfan
 
-Ch33zy-
 
EverEffects
 
l7annylvlex
 
+Elegance
 
Whiteyy
 
JZydex
 
PumaSnIpejZ
 
kHaoZ
 






Share | 
 

 Guide to stack basics.

View previous topic View next topic Go down 
AuthorMessage
EverEffects
Administrator

Administrator
avatar

Posts : 805
1337ness : 25
Join date : 2010-10-31
Age : 24
Location : Oklahoma CIty

PostSubject: Guide to stack basics.   Wed Nov 10, 2010 4:27 am


Introduction
I am not totally sure how useful this is to most average PSP users, but if you are interested than this is something you should definitely learn. Do not allow this to strike fear in your eyes, fear not, the stack is relatively easy once you get the hang of operating it. So if you are even slightly interested I suggest you read on cowboy, there's a lot to learn but I will only cover the basics, as everyone knows you need to figure the rest out by yourself via playing with code. A little MIPS knowledge for commands is required.

Basic Info
What is the stack you ask? Well for those of you who do not know, it is an area in memory where many useful actions can be done, and generally if you understand a little bit about what your doing you can write code to your stack using pointers etc to get a desired effect. The stacks location in user mode(space)(not sure what it is for kernal?) is located at the address 0x0XXXXXX. As you can see it is located quite high in memory. EVERY stack must have its pointer, the stack pointer aka $sp. The stack pointer draws as you will to an area of RAM that is allocated by almost every bit of code in that mode and allocates all of its data, saved/preserved registers etc. The stack also follows the basis of MIPS, and that being its offset, so it counts 00, 04, 08, 0c and so on.

Keeping your stack clean
Some find it important, some coders do not. Personally I do. What I mean by "keeping your stack clean" is keeping it as organized as possible and when allocating your data that you do it in order, such as if you were going to use the t0-t1 and then a0 for pointers in vram you would do it like so.

Code:

#Clean code
sw ra, $0000(sp) !You MUST store your return if you have any type of 'jump' or carry over command i.e. jal, j, beq and so on.
sw t0, $0004(sp)
sw t1, $0008(sp)
sw a0, $000c(sp)

#Messy code
sw ra, $0000(sp)
sw t1, $0004(sp)
sw a0, $0008(sp)
sw t0, $000c(sp)

Writing to the stack
Once you have an idea of what you are doing, it would be in your best interest to make a blueprint of how your code is going to operate so that you can make it as functional as possible, also it is highly helpful to writing our first command, stack allocation. This is how many bytes of data we are going to barrow from the stack for use, this is how we would do so.

Code:

stack
addiu sp, sp, $fff0(-0x10)

This simple command will remove 10 from the stack pointer for our use. Next we must allocate the registers which is quite simple and also a good example why its crucial to plan out what you are going to do because you should plan out what registers you will need and for what, overdrawing registers(allocating ones you wont use) is not smart as it wastes space, so instead of being lazy and using everything, just take what you need, and only what you need.

Since I will be using a jump or similar command in this tutorial, remember from above, you MUST save the ra before anything.

Code:

stack
addiu sp, sp, $fff0(-0x10)
sw ra, $0000(sp)

The sw command allocates that data contained in that specified register for use. Where as $0000(sp) is the offset from the stack pointer so $sp +0000 is how I used to think of it when I started.

Next we are to allocate the rest our data so here are the arguments for that, please keep in mind the offset from the stack pointer adding the value of 4 as the offset for every register we allocate.

Code:

stack
addiu sp, sp, $fff0(-0x10) //subtracts 10 from the stack pointer
sw ra, $0000(sp) //needed if we use a jump, or other similar command
sw t0, $0004(sp) //allocates the data contained in temporary register 0, with the offset of 0x0004 from the stack pointer
sw t1, $0008(sp) //allocates the data contained in temporary register 2, with the offset of 0x0008 from the stack pointer
sw a0, $000c(sp) //allocates the data contained in argument register 0, with the offset of 0x000c from the stack pointer

Now this is where you implement your actual commands and code, this is what you are actually writing to the stack, well technically its basically you are taking the data from the stack and doing something with it, such as, modding or increment/decrement a float value, add, subtract, load, store etc. For the sake of time and simplicity I will simply load and store than use a random pointer(cant think of how I can use one in this tutorial without man handling DMA)

So if you know any basic MIPS(look at my previous tutorials) than this should be a breeze!

Code:

lui t0, $aaaa //loads first half of the data into t0

Now lets store and add some value

Code:

addiu t1, t1, $2012
sw t1, $bbbb(t0)  //loads the second half of data into t1, combining it with the data stored in t0 making it the value(0xaaaabbbb)

Now here is what we have so far:

Code:

stack
addiu sp, sp, $fff0(-0x10) //subtracts 10 from the stack pointer
sw ra, $0000(sp) //needed if we use a jump, or other similar command
sw t0, $0004(sp) //allocates the data contained in temporary register 0, with the offset of 0x0004 from the stack pointer
sw t1, $0008(sp) //allocates the data contained in temporary register 2, with the offset of 0x0008 from the stack pointer
sw a0, $000c(sp) //allocates the data contained in argument register 0, with the offset of 0x000c from the stack pointer
lui t0, $aaaa //loads first half of the data into t0
addiu t1, t1, $2012 //adds in a little mix of doom's day(no I dont believe in it)
sw t1, $bbbb(t0)  //loads the second half of data into t1, combining it with the data stored in t0 making it the value(0xaaaabbbb)

Now let us add some more storing data and of course a random pointer

Code:

sw a0, $0500(t0) //storing a little randomness, just to use a0, lulz

Now remember if you don't add the ra at the begging you are most likely going to crash, simple mistake can cause so much frustration? Links to your choice of a dead-zone.

Code:

jal $deadbeef

NOW SINCE YOU JAL'd you MUST ADD A nop before re adding the data back to the stack.

Code:

nop //bye

Now of course since we borrowed from the stack, we must complete this routine by being a good neighbor and returning what we borrowed to its respective owner. Same as above, except instead of sw we lw it, again keep it clean!

Code:

lw ra, $0000(sp) //needed if we use a jump, or other similar command
lw t0, $0004(sp) //adds the data contained in temporary register 0, with the offset of 0x0004 back onto the stack
lw t1, $0008(sp) //adds the data contained in temporary register 2, with the offset of 0x0008 back onto the stack
lw a0, $000c(sp) //adds the data contained in argument register 0, with the offset of 0x000c back onto the stack

Now of course we only now just gave it back, we must sign our name as a thank you for the return to complete. To do this in shortbread really all it is a simple return so, simply put...

Code:

jr ra //return: all done?

No we are not all done, one last thing to do, I bet you can guess it?

Code:

addiu sp, sp, $0010 //adds the value of 10 we borrowed back onto our beloved stack!

MiLk
Back to top Go down
 

Guide to stack basics.

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
SneakyCodes Gaming Forum :: PSP Section :: PSP Tutorials-
Jump to:  





SneakyCodes.tk | Sneaky-Media.com


Free GeoHot NOW!
I support George Hotz and
the FREEDOM OF INFORMATION

Free forum | © phpBB | Free forum support | Contact | Report an abuse | Free forum