Learn to Code via Tutorials on Repl.it!

← Back to all posts
Assembly Language Crash Course 4
Wuru (585)

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.

Commentshotnewtop
goalkenhighligh (3)

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

Highwayman (1367)

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?

Highwayman (1367)

@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.

Wuru (585)

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

Highwayman (1367)

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

Highwayman (1367)

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

This seems a bit misleading...

Highwayman (1367)

@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.

Wuru (585)

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

Wuru (585)

@Highwayman I'll edit it, though.

Highwayman (1367)

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

thanks. :) great tutorial as always XP

Wuru (585)

@HahaYes no, i don't think so XD

HahaYes (1052)

@Wuru how is this not cycle squeezing?

HahaYes (1052)

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

Wuru (585)

@HahaYes im not saying im not squeezing lol

Wuru (585)

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

Wuru (585)

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

HahaYes (1052)

@Wuru lol cycle squeezing is fun

HahaYes (1052)

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

HahaYes (1052)

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

firefish (308)

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

Highwayman (1367)

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?

Wuru (585)

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

Highwayman (1367)

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

Highwayman (1367)

@johnstev111 bruh it looks like it’s winking XP