x86-64 Assembly While Loops Explained Step by Step in YASM – From Concept to Working Program

x86-64 Assembly While Loops Explained Step by Step in YASM – From Concept to Working Program

In this detailed tutorial I walk you through exactly how to implement while loops in x86-64 assembly language using YASM. We start with the basic concept of a while loop as it exists in higher-level languages like C/C++, then break it down into labels, conditional jumps, and unconditional jumps so you can see exactly how the control flow works at the assembly level.

I explain why we usually put the condition check at the top, how to handle the jump distances (especially the ~128-byte limit of conditional branches), why unconditional jumps are preferred for the loop-back, and how to structure init / top / body / done sections clearly.

We then build a complete, runnable hybrid program (assembly + tiny C++ driver) that:

  • prints an intro message
  • repeatedly asks the user to enter numbers
  • echoes each number back
  • continues until the user enters 99
  • prints a goodbye message when finished

Lots of practical tips about label naming conventions, register preservation (R12 in this case), and debugging flow are included along the way.

Assumed knowledge: basic x86-64 assembly, how to use a makefile, simple system calls, and calling external functions. If you’re new to those topics, check my earlier videos first.

Hope this helps someone finally “get” while loops in assembly!
Thanks for watching – subscribe if these kinds of low-level explanations are useful to you.

Introduction to While Loops in x86-64 YASM 00:00:00
While Loop Concept in High-Level Languages 00:00:56
Breaking Down While Loop Structure 00:02:00
Labeling Key Sections Top Body Done 00:02:40
Conditional and Unconditional Jumps Explained 00:03:26
Why Prefer Shorter Conditional Jumps 00:04:40
Diagram of While Loop Flow 00:06:52
Alternative While True with Internal Break 00:08:08
Do-While vs Regular While Difference 00:09:32
Program Setup and Data Section Overview 00:09:53
Hybrid Program Structure and External Functions 00:10:56
Main Function and While Test Call 00:12:56
While Test Function Prologue 00:13:21
CRLF Helper Function 00:14:00
Intro Message and Loop Initialization 00:14:50
While Top Comparison with 99 00:16:57
Entering the Loop Body 00:19:08
User Input and Echo Output 00:19:38
Jump Back to While Top 00:20:29
Exit to While Done Section 00:21:39
Goodbye Message and Function Epilogue 00:22:14
Live Demo Running the Program 00:22:30
Summary and Closing Remarks 00:23:12
Call to Subscribe and Website Mention 00:23:46

=-=-=-=-=-=-=-=-=

Thanks for watching!

Find us on other social media here:

  • https://www.NeuralLantern.com/social
  • Twitter / X: https://x.com/NeuralLantern
  • Rumble: https://rumble.com/c/c-3696939
  • BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt
  • Daily Motion: https://www.dailymotion.com/neurallantern
  • Minds: https://www.minds.com/neurallantern/
  • Odysee: https://odysee.com/@NeuralLantern:5

Please show your support!

  • Buy me a coffee: https://ko-fi.com/neurallantern
  • Subscribe + Sharing on Social Media
  • Leave a comment or suggestion
  • Subscribe to Blog: https://www.NeuralLantern.com
  • Watching the main “pinned” video of this channel for offers and extras

All right, hello there. In this video, I’m going to teach you how to implement while loops in YASM

x86-64 assembly. Although you probably don’t need to be using YASM assembly to understand

this video or to benefit from it. So what am I talking about? First off, okay, so we’re going

to do while loops. There’s a lot of assumed knowledge in this video. If you don’t know how

assembly program, if you don’t know how to make a make file, if you don’t know a lot

of the basics that I’m just skimming over in this video, you’ll need to see my other

videos because I explain all of those things at length.

For this video, I’m just going to be talking about while loops only.

So for starters, let’s see here and open up a little notepad here and just type a while

loop and try to explain a little bit about it conceptually.

And then after that, we’re going to, we’re going to write a program in Yasm to show that

we can get this to work.

So what is a while loop in a higher level language?

We’ll just suppose C or C++.

You can imagine whatever language you want, but typically in a while loop,

we’re not talking about a do while loop, although that would be really easy

to implement after watching this video.

But in a regular while loop, you have the keyword while usually,

and then you have some sort of an expression like does a equal B or whatever.

So maybe I’ll just say does a equal B?

Expression evaluates to true then the body of the while loop will execute and then when it’s done executing

Execution will go back up to look at the expression again and make sure that it’s still true

So the loop will run forever until that expression evaluates to false

Maybe that’s what you want. Maybe that’s not what you want, but that’s what it’ll do

And that means the body runs an endless number of times

Also, if the expression evaluates to false on the very first run

then the body of the while loop will just never run at all.

So that’s, you know, a basic idea of a while loop in a higher level language.

Hopefully you kind of already know that.

So let’s look at the parts right here.

If we imagine this as assembly,

then probably the first thing we should do is label the part

where we look at the expression and decide whether we’re going to continue or not, right?

So I’m going to put a little label here

and maybe we’ll call it a while top or something.

You know, whatever you want to do.

just to label the part of the code

So that we know when we write assembly

We’ll be able to put the appropriate labels on the different parts of the while loop and we’ll be able to imagine

The while loop that we’re implementing in assembly

So I’m gonna say this is the top or you can call it the eval part whatever you want

Then we’ll make another label down here. We’ll call it

You know while body something like that and so basically if we want to jump up to the top of the while loop to evaluate the

expression, we just have to basically jump to that label called while top.

And if we want to jump to the body to actually execute the code,

then we just have to jump to that other label instead. We should also have a label at the

very bottom after the whole while loop is over and we’ll call it, you know, while finished

or while, whoops, finished or while done or while over something like that. So I’m just going to

Now you can imagine that when we’re at the top of the while loop and we see that the

expression is false, then we can just jump down to the done label, thereby exiting the

entire while loop.

Then of course we probably want to have some jump statements in here.

So for example, the expression part here, it’s probably going to jump either to the

body or either to the done area based on whether or not the expression evaluated to true or

false.

label while top you know which contains a conditional branch and then like an unconditional

jump so if a condition is true maybe we can jump into the body if the condition is false we’ll fall

through to the next statement and do an unconditional jump to the done label if you don’t

know conditional branching this is another one of the topics that i’ve covered in previous videos

as we implement the while loop.

So just keep in mind, there are other videos that I have

that you should watch first

if you don’t understand conditional branching or jumps.

The reason that I’m going to kind of design

the jumps like this where I’m going to say,

let’s do a conditional branch.

And basically if the loop evaluates to true,

we’ll jump into the body of the loop.

And if it doesn’t evaluate to true,

then the conditional branch will allow execution

to fall through down to the next statement,

which will simply contain a jump to the done portion.

to the done portion so basically as soon as the while loop is ready to break it gets uh implemented

by an unconditional jump which was fallen through to by the conditional branch that would only jump

into the body if uh expression was true i’m doing it this way because it’s usually a shorter jump

uh to jump to the true area you know the the body of the while loop and remember conditional

branching instructions have a maximum you know jump reach of about 128 bytes so if you try to

128 bytes. So if you try to jump too far, like for example, if you wanted to, you could reverse

the logic to where you could say, if an expression is true, let’s jump to the done portion and say,

we’re going to break the loop. If the expression was false, we’ll jump into the body. You could

do that. But then if the body of your while loop was too long, maybe it was so long that the last

instruction was greater than 128 bytes away from the top. Then by the time you wanted to

let’s see yeah if you have if you have too long of while loops you can’t jump more than 128

instructions away so that’s a limitation of contentional branching so I just I want to put

the biggest jump on an unconditional jump instruction because it doesn’t have a limitation

like that I was having a hard time imagining what I was about to say just now for the

the reversal I guess I’m just going to ignore the reversal scenario and we’ll just do it the

and we’ll just do it the regular way.

Anyway, so we want to have a conditional jump that either jumps into the body or to the done area.

And then at the very bottom, we want to have an unconditional jump to the top,

which basically means every time we get to the bottom of the while loop body,

we’re just going to jump up to the while top.

Actually, I’ll just say to while top.

Remember again, the regular jump instructions,

maybe I’ll take out the U here.

The regular jump instructions don’t have a limitation

on how far they can jump.

So that should be fine.

And that’s the basic idea of implementing a while loop.

Maybe I should draw just a quick diagram

before we start looking at the code,

just to make sure everybody of different learning styles

understands what I’m talking about.

So we’ll, let’s see, eval.

I’m still adjusting my pen and you know what?

You know what? It looked a lot better before I hit record.

Eval the expression.

So I’m going to first evaluate the expression.

If it evaluates to true, we’ll put a green arrow here and I’ll put like a T for true.

Then this will be the body of the while loop.

I’ll say like the body.

If it evaluates to false, let’s see.

I’ll just put that in red and I’ll put an F here then this is just going to be the done area

right so you can imagine we evaluate an expression using the compare instruction in assembly and

based on the results of comparing something whatever it is that your condition is then we

you know we we either branch to the done area or we branch to the to the body area

want to have a more complicated while loop and you don’t want to put a huge

amount of expressions or if you don’t want to logically concatenate a bunch of

different components into the expression you know that’s fair I usually write my

while loops in real life as just while true and then I break when certain

conditions are met you can do that too we’re not going to talk about that in

this video but you could just have a while true so you always jump to the top

and there’s no branching that goes to while done but then throughout the loop

loop you can just sort of check to see if certain conditions are met and if they are then you’ll do

a branch to the done area and if they’re not then by the time you make it down to the bottom of the

loop then it just automatically jumps to the top although you got to make sure with the the reach

of conditional branching you probably want to test to see if your condition is true or false or

whatever if it means we’re going to continue the loop then you probably just want to do a conditional

then you probably just want to do a conditional branch that jumps down a little bit

so that it can continue the body of the loop.

And then the part that it skipped over,

just a little part should be an unconditional jump that jumps out of the loop.

That way, it doesn’t matter how big your while loop body is,

you can always jump out of the loop.

But we’re not going to talk about that in this video.

Anyway, so we evaluate the expression.

If it’s true, we go to the body.

If it’s false, we go to done.

After the body is done executing,

then we just jump back up to the top where we evaluate the expression.

where we evaluate the expression.

I’ll put while here just to make it more clear that we’re talking about the basic

idea of a while loop.

Not too hard, you know.

And then if you wanted to implement a do while loop, just make sure that you always

evaluate the body at least once.

That’s really the only difference.

But that’s up to you.

This is just a regular while loop video.

Okay.

So we’ve kind of talked about it a little bit.

We’ve sort of, you know, drawn it out in a diagram and put some regular code in.

and put some regular code in let’s set up an actual assembly program that will do this so for starters

again i’m not going to show you my make file because i’ve already made other videos where i

explained how to make a make file from scratch same thing goes for hybrid programs i’m going

to have a driver which is a c plus plus module that just sort of calls on this assembly module

if you want to know how to make hybrid programs or you know drivers or you know whatever see my

to be talking about looping only. Okay, so let me get my solution up here. My source code is called

looper. And I’m just going to copy paste the data section for my program real fast, just so you can

see it. Okay, so here’s the data section. Again, this is not a basics for assembly video. If you

don’t know how to do a data section in Yasm, see my other videos. But for now, I’m just going to say

like the actual array of bytes and then a length and all I’m doing is printing out messages you

know begin the wild test your numbers will be printed back to you until you decide to quit so

that’s going to be printed to the user right away then every time the program wants a number from

the user it’ll just ask you know please enter a number or 99 to quit and then it’ll prefix

the echo back it’ll say you entered and then it will actually print the number and then when you

we’re done and i’m going to use an external uh function that i have available to just kind of

like input and output numbers this video is not about uh input and outputting and external libraries

if you’re interested you could probably just link a hybrid program and use printf and scan

f to very easily do input and output but that’s not what this video is about see my other videos

then i have crlf which is just a carriage return in line feed uh you know select the cursor goes

You know select the cursor goes to the next line and then I have like a system call code to just print and

Then a file descriptor just to print a standard output again. That’s explained in other videos

So now I’m ready to start copy pasting the main portion of my program

So first off I’m going to start the text section, which is where the instructions go in the ASM

So there it is and then I’m going to copy paste

of external symbols. So I just have a little library that I’m using that makes it easier for

me to input and output integers. So you can do this any way you want, or you can hard code a

number in the globals area. If you don’t want to deal with input and output while you’re learning

how to do loops, that’s totally fine. But then the entry point is going to be a function called

looper. So this is my function. And again, this is a hybrid program. So the main function or the

be present in my assembly module the driver is going to be a c plus plus module that just calls

on a function named looper so that means this function is going to get called from another

module and that’s why i have to mark it as global and then it is a function so i’m going to call

return at the end of it to say we’re done and then within this function i’m just going to call two

other functions that i’m about to create one is called while test which is going to actually do

the while loop and the other is called crlf which just print which just prints a new line for me i

which just prints a new line for me.

I don’t know why I do it that way, but I want to.

Sorry, not sorry.

So this is the real meat of the function here.

Let’s start, well, the real meat of the code or the video.

Let’s start a function called while test.

You can see it’s got a void signature with no arguments,

so it doesn’t really, you know, take anything or return anything.

I’m going to use register R12 to hold the user’s input

so that I can, you know, print it out and stuff.

print it out and stuff so that means I have to do a preservation of R12 because it’s a

callee saved for the ABI which you should respect so I’m going to push it at the beginning

and then I’m going to pop it at the end and then this is a function so I have to return

at the end of the function let me just double check that there’s nothing else weird at the

bottom of that nope okay so we’ve got the prologue and epilogue the wild test function

maybe I should copy paste my crlf real fast my crlf function it’s just sad but

it’s also kind of cute right it does nothing except just print out a new line

that’s all it does this video is not about that so at this point I should

have a program that probably works let’s see if it does I’m gonna do clear and

make run again if you want to know how to use make files or compile or link or

anything like that see my other videos so I’m gonna run it and it just says

which is some code that we’re not looking at.

And then it says it’s regain control.

And this line in the middle, which is just an empty new line,

that’s definitely from the assembly program.

Because at the top here we have CRLF.

If I call it multiple times CRLF,

then you’ll see there are multiple blank lines.

All right, I’m gonna take that out.

Now let’s continue with while test.

So what should we do here?

The first thing that we should do is print an intro message

message just to let the user know that we’re about to you know begin our while

tests and if we run the program one more time we should see that message now

begin the while test your numbers will be printed back to you system calls and

simple printing is covered in other videos so now uh you know for me I kind

of like to init all of my loops even if it’s a while loop and not just a for

loop or anything so I always have an extra label that I like to call init

or before or something like that.

So I have a label now called while test underscore init,

and it’s just where I’m gonna initialize

whatever it is that I think I need to initialize

so that the loop will actually work.

If you look here, all I’m really doing is setting R12 to zero

because what I’m gonna do is stop the loop.

I’m gonna break the loop whenever the user enters a 99.

So I don’t know what’s inside of R12

when we first start this function.

And I just wanna make sure that it’s not 99,

the stopping number at the very start so I’m just going to set it to zero. So

another note about my labels you don’t have to do it this way but I love to

write labels where the first part of the label is always the function that the

label is inside of so notice how the function is named while test and so my

label is always while test underscore something and I’m putting init here

just to say we’re initializing the loop but if you had a function that had a

had a lot of stuff going on in it for starters you should probably be breaking up that function

into multiple functions but but assuming you didn’t uh you should probably do another underscore

and then like another component and then another underscore based on what giant chunk of your

function you’re inside of so if there was like an if part a while part an input part an output part

you probably want to you know stick that into your labels your labels will get huge but for me

infused in assembly and this makes it easier.

So we’re going to initialize so that we can run our while loop and then the next thing

we’re going to do is implement the top of the while loop.

So remember if we looked at this code up here, maybe if I drag this over to the side and

pin it to the top for a little while, you can see that the top of the while loop is

where we kind of evaluate the expression to see if we need to keep going or not.

jump into the ending area, the done area. So the top, whoops, the top is always for that.

We’ll say first, I’m going to ask, are we done? So, you know, how do we know if we’re done?

In this particular while loop, we want to compare the user’s input, which is R12 to the number 99.

And if it’s equal, then we will quit, which means also if it’s not equal, we will jump into the

So by the way, you’re probably wondering how did R12 get the user’s input?

Well, we’re going to do that as the next step.

And of course, your design pattern may vary a little bit.

No, that’s okay.

I just like to implement it this way.

So first thing we’re going to do is compare R12 with 99.

And that’s why I’ve written this comment here, just like the blank while comparison part.

And I guess I could have put this R12 not equal to 99 up at the top, but then it kind

of feels like I’m leaving these other two instructions.

So I moved it down one.

these other two instructions so I moved it down one so basically as long as you know we compare

R12 and 99 compare and conditional branching is covered in other videos but we compare those two

values and then we say if R12 is not equal to 99 then jump to the body and we expect that the body

will be like a short jump which will be within the range of a conditional branch

So anyway, if the not equal branch didn’t happen, that means R12 is equal to 99.

At that point, execution falls through to line 84, and we’ll just unconditionally jump out of the while loop.

So basically we’re saying, if it’s true, we continue looping.

If it’s not true, we just jump outside of the loop.

We’re just totally done with the while loop.

Okay.

So then we need a body, because obviously we’re going to jump into the body here.

that means I’m going to just copy paste another little set of code here.

Right after that unconditional jump.

So now we got the body.

Notice how I put a little comment here that has a brace just to indicate to you,

hey, this is the beginning of the actual while loop body,

just to make it a little bit more clear.

And what are we going to do inside of the body?

We’re just going to ask the user for some input.

So I’m printing a simple message here,

and then I’m calling on my helper function

to just actually input a number from the user.

I’m going to store that number into R12.

So that’s how R12 gets the numbers, gets the user’s input.

And the way I’ve written this, if the user enters a 99,

it’ll echo it back to the user and then it’ll break the loop afterwards.

So, you know, if you wanted to rearrange things like I talked about before,

where you input before you check to see if you’re going to keep going, you could do that.

But it would be a little harder to echo the user’s input back to them before you break.

I don’t know. It’s up to you.

I don’t know it’s up to you anyway so we grab input from the user and then we print another

message basically saying here’s the thing that you inputted no problem and then again we use

one of my helper functions to actually spit the number back out at them so this is not

a very complicated body it’s just asking for a number and then printing the number back to them

and then after that I’m going to do another label and I’m going to call it the body bottom

we don’t really have to do this label but for clarity I think it’s probably a good idea

I think it’s probably a good idea.

So the very bottom of the while loop’s body is usually where you don’t do any more instructions

that are part of the work of the while loop’s body,

but just sort of the place where you jump back up to the top

so you can evaluate and decide to continue or not again.

So notice how I’m using an unconditional jump here.

That’s a good idea because again, if you have like a huge while loop body,

you might end up surpassing the threshold of 128 bytes

of 128 bytes and then you’ll get a assembler error that says

I can never remember this. It’s like a

jump out of range error or something like that. Basically, if you do a conditional branch to jump up the top

because some people like to check to see

if they should continue the loop, they like to check for that at the bottom. I’ve done that before.

And then if true, then we’ll jump to the top of the loop. But if the loop is too big, that won’t work. So

I just like to take a long jump to the top of the loop and then decide if I’m going to keep going at the very top.

if I’m going to keep going at the very top. And then there’s a comment saying, hey, that’s the

end of the body. Okay, no problem. Now let’s do the done label, which is basically where we jump

if the loop is actually finished. So remember, if this expression right here evaluates to false,

then execution is going to fall through to line 84, where we jump to this while test done label.

And so I’m just going to put the while test done label right here. So we’re done. So the done is

So we’re done. So the done is not part of the loop. It comes after the loop. That’s this right

here on line nine of the little notepad. And we can just kind of do whatever we want. At that

point, we can return to a caller, we can just do other stuff, we can, you know, do a different

loop or, you know, whatever, we’re just done with the original loop. For me, I’m just going to say

goodbye with this little print to just sort of like, you know, print an exit message. And then

we’ll do the epilogue where we restore R12. And then we just return to the caller,

the driver worry about exiting the program okay so if we’ve done this correctly we should now have

a working program let’s see let’s see okay let’s enter a number let’s do 22 and it says you enter

22 and let’s do 55 and we just we can enter any numbers we want and as long as we’re not entering

99 the program will just continue forever so this is a while loop if i want to quit i do 99

breaks at the top it breaks by jumping down to the done area where we print our goodbye message

which just is end while test and then if we run this again if i do 99 from the start then it just

immediately breaks you know it prints out what you entered but then it immediately breaks

and that’s it that’s uh the basics for how to write a while loop you just use basically

layered on top of an abstract concept of what you think a while loop is,

or what I guess the world thinks a while loop is.

Okay, thank you so much for watching this video.

I hope you learned a little bit of stuff and had a little bit of fun.

I’ll see you in the next video.

Hey everybody.

Thanks for watching this video again from the bottom of my heart.

I really appreciate it.

I do hope you did learn something and have some fun.

if you could do me a please a small little favor could you please subscribe and follow this channel

or these videos or whatever it is you do on the current social media website that you’re looking

at right now it would really mean the world to me and it’ll help make more videos and grow this

community so we’ll be able to do more videos longer videos better videos or just i’ll be able

to keep making videos in general so please do do me a kindness and uh and subscribe you know

I’m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.

It just wakes me up and I get filled with joy.

That’s exactly what happens every single time.

So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.

Just subscribe and then I’ll just wake up.

I promise that’s what will happen.

Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website,

which I think is also named somewhere at the bottom of this video.

and it’ll take you to my main website where you can just kind of like see all the videos

i published and the services and tutorials and things that i offer and all that good stuff and

if you have a suggestion for uh uh clarifications or errata or just future videos that you want to

see please leave a comment or if you just want to say hey what’s up what’s going on you know

just send me a comment whatever i also wake up for those in the middle of the night i get

it would really mean the world to me. I would really appreciate it.

So again, thank you so much for watching this video and

enjoy the cool music as I fade into

the darkness which is coming for us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply