Skip to content
← Back to Community
Assembly Language Crash Course 4
Profile icon
JustAWalrus

Hey.

Part 4.

Okay, so lets get started!

Section 8: Basic Stack Manipulation.

If you have ever programmed before, the Stack is kind of like an array.

Except the array has a pointer to the top or working register.

Values can be pushed to and popped from the stack.

The stack is stored in RAM so it might be a teeny tiny bit slower than eax and such.

The pointer is stored in esp, don't mess with this unless you are very smart.

Every time you pop a value from the stack you decrease the stack pointer by 4 and store whatever was there in the register specified afterword.

Every time you push a value to the stack you increase it by 4 and then it does the operation of putting the value there.

To pop a value to the stack you do:
pop <value/register>

To push a value you do:
push <value/register>

This can be useful for saving registers like this:

_start: mov eax, 1 push eax mov eax, 4 pop eax

Section 9: Functions.

Yay!

So functions in Assembly are broken up into 3 parts.

  • Prologue
  • Story
  • Epilogue

Let's go in order and break it down.

The basic function prologue looks like this:

push ebp mov ebp, esp sub esp, *

ebp is the call pointer.

What is this?

It's the register that stores the location after your calls!

Like call <label>

It stores the next line number.

Then we push ebp

Then to save the value of esp we put esp into ebp

Then sub, that is a keyword for subtraction. There is also add.

Both of these store the result in the first variable defined

So the syntax is sub <a>, <b>

And both could be a value or a register.

Then it will try to store the result in a.

So we are subtracting the stack pointer by *

What is *?

Well, you want to replace * with the stack change times 4

A good way to put it is pretending that the pointer starts at 0 when you enter the function.

And every push and pop action increases or decreases the stack by 1.

Hope that makes sense.

The story

This is just your code

The Epilogue

This is your basic epilogue.

mov esp, ebp pop ebp ret

We include that ret

And undo everything.

Anyway Sorry for this one being short tomorrows will be 3 sections long.

The usual, upvote, feedback.

Voters
Profile icon
ComputerProgram
Profile icon
programmeruser
Profile icon
zararitz
Profile icon
rileyhead
Profile icon
LoganSpong
Profile icon
ret394
Profile icon
Viper2211
Profile icon
PaoloAmoroso
Profile icon
Coder100
Profile icon
TheForArkLD
Comments
hotnewtop
Profile icon
goalkenhighligh

This topic is very interesting and I am interested but do not know where to find, thankfully you create this topic, hope everyone will help me https://run3sonline.com

Profile icon
ambaloch

Stream India APK lets you watch TV channels and movies online for free. For many great reasons, you should download this app right away.
https://apkchew.com/stream-india/

Profile icon
LoganSpong

I love the way you style your tutorials. It's great! Keep it up!

Profile icon
Highwayman

I don’t mean to be pushy or anything and I don’t want to rush you, but when does the next one come out?

Profile icon
JustAWalrus

@Highwayman do the fans want it?

Profile icon
Highwayman

@Wuru I mean I want it, and seeing as your initial fan base is currently at least twenty ppl I’d say yeah a sizable amount of ppl want it. :) lol.

Profile icon
JustAWalrus

@Highwayman not really feeling motivated. If you really want it, you can take over the series.

Profile icon
Highwayman

@Wuru oof. Hm I would, but I need to entirely learn it first lol.

Profile icon
Highwayman

Every time you pop a value from the stack you decrease the stack pointer by 1.

This seems a bit misleading...

Profile icon
JustAWalrus

@Highwayman how so?

Profile icon
Highwayman

@Wuru well firstly, one what? The reader can assume that it’s one anything- a byte, a bit, a word a double word..... and secondly it makes it sound like all stack operations are completely uniform, pushing and piping only one certain amount of data.

Profile icon
JustAWalrus

@Highwayman in 32bit it decreases it by 4. Pretending it's 1 is much easier.

Profile icon
JustAWalrus

@Highwayman I'll edit it, though.

Profile icon
Highwayman

@Wuru ye, but the reader isn’t ready for pretending yet I think...

thanks. :) great tutorial as always XP

Profile icon
HahaYes

do penguins fly?

Profile icon
JustAWalrus

@HahaYes no, i don't think so XD

Profile icon
HahaYes

@Wuru how is this not cycle squeezing?

Profile icon
HahaYes

@Wuru why can't you just compile all of this into one? Also congrats on 100 cycles

Profile icon
JustAWalrus

@HahaYes im not saying im not squeezing lol

Profile icon
JustAWalrus

@HahaYes thanks dude, and i really don't know.

Profile icon
JustAWalrus

@HahaYes dude, can we also just kinda be chill?

Profile icon
HahaYes

@Wuru lol cycle squeezing is fun

Profile icon
HahaYes

@Wuru hmmmm yeah I'm chill, I'm just saying cycle squeezing is a great tactic

Profile icon
JustAWalrus

@HahaYes do u have discord?

Profile icon
HahaYes

@Wuru nah I don't I'll make one in August

Profile icon
JustAWalrus
Profile icon
firefish

@Wuru a n t i - s q u e e z e s o c i e t y

Profile icon
Highwayman

Hm idk this one seems super slim... maybe elaborate more on how the stack works n’ stuff? Like talk about indexing the rsp or rbp using [ rbp + 3 ] n’ stuff like that?

Profile icon
JustAWalrus

@Highwayman yes, imma edit it just had to get it out before the I lost my monopoly.

Profile icon
JustAWalrus
Profile icon
Highwayman

@Wuru awesome thanks! I learned some cool stuff today! (^ω^)

Profile icon
firefish
Profile icon
Highwayman

@johnstev111 (^ω^)はい

Profile icon
firefish
Profile icon
Highwayman

@johnstev111 bruh it looks like it’s winking XP