Step-by-step guide to implementing while loops in x86-64 assembly using YASM. We go from high-level concept ? label structure ? conditional & unconditional jumps ? full working example that reads numbers until user enters 99.

Great for people learning assembly control flow, students taking computer organization/architecture courses, reverse engineers, or anyone who wants to understand how loops really work under the hood.

Previous videos cover conditional branching, system calls, makefiles, hybrid C++/asm projects, etc.

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

#x86Assembly #AssemblyLanguage #YASM #LowLevelProgramming #x64 #WhileLoop #SystemsProgramming #ComputerArchitecture

x86-64 assembly, YASM tutorial, while loop assembly, assembly language tutorial, x86 assembly loops, low level programming, x86-64 linux assembly, conditional jump assembly, unconditional jump, assembly control flow, learn assembly language, NASM vs YASM, x86-64 programming, assembly while loop example, low level loops, computer architecture, systems programming, assembly language loops

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

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
Transcript
00:00all right hello there in this video i'm going to teach you how to implement while loops in
00:06yasm x8664 assembly although you probably don't need to be using yasm assembly to understand this
00:12video or to benefit from it so what am i talking about first off okay so we're going to do while
00:22loops there's a lot of assumed knowledge in this video if you don't know how to make a basic yasm
00:27assembly program if you don't know how to make a make file if you don't know a lot of the basics
00:32that i'm just skimming over in this video you'll need to see my other videos because i explain all
00:36of those things at length for this video i'm just going to be talking about while loops only
00:41so for starters let's see here now open up a little notepad here and just type a while loop and try to
00:48explain a little bit about it conceptually and then after that we're going to we're going to write a
00:52program in yasm to show that we can get this to work so what is a while loop in a higher level
00:57language we'll just suppose c or c plus plus you can imagine whatever language you want but
01:02typically in a while loop we're not talking about a do while loop although that would be really easy
01:07to implement after watching this video but in a regular while loop you have the keyword while
01:13usually and then you have some sort of an expression like does a equal b or whatever so
01:18maybe i'll just say does a equal b um and as long as that expression evaluates to true then the body
01:27of the while loop will execute and then when it's done executing uh execution will go back up to look
01:33at the expression again and make sure that it's still true so the loop will run forever until that
01:38expression evaluates to false maybe that's what you want maybe that's not what you want but uh that's what
01:43it'll do and that means the body runs an endless number of times also if the expression evaluates
01:50to false on the very first run then the body of the while loop will just never run at all so that's
01:55you know a basic idea of a while loop in a higher level language hopefully you kind of already know
01:59that so let's look at the parts right here if we imagine this as assembly then probably the first thing
02:06we should do is label the part where we look at the expression and decide whether we're going to
02:11continue or not right so i'm going to put a little label here and maybe we'll call it a while top or
02:17something you know whatever you want to do just to label the part of the code so that we know when we
02:23write assembly we'll be able to put the appropriate labels on the different parts of the while loop and
02:28we'll be able to imagine the while loop that we're implementing in assembly so i'm going to say this is the
02:32top or you can call it the eval part whatever you want then we'll make another label down here we'll call it
02:38uh you know while body something like that and so basically if we want to jump up to the top of the
02:46while loop to evaluate the uh expression we just have to basically jump to that label called while
02:51top and if we want to jump to the body to actually execute the code then we just have to jump to that
02:57other label instead we should also have a label at the very bottom after the whole while loop is over
03:02and we'll call it uh you know while finished or while whoops finished or while done or while over
03:11something like that so i'm just going to put while done and then uh now you can imagine that when
03:17we're at the top of the while loop and we see that the expression is false then we can just jump down
03:21to the done label uh thereby you know exiting the entire while loop then of course we probably want
03:27to have some jump statements in here so for example uh the expression part here it's probably going to
03:34jump either to the body or either to the done area based on uh whether or not the expression evaluated to
03:40true or false so maybe i'll put uh label while top you know which contains a conditional branch and then
03:51like an unconditional jump so if a condition is true maybe we can jump into the body if the condition
03:57is false we'll fall through to the next statement and do an unconditional jump to the done label if you
04:05don't know conditional branching this is another one of the topics that i've covered in previous videos
04:09we're just going to skim over conditional branching as we implement the while loop so just keep in mind
04:14there are other videos that i have that you should watch first if you don't understand conditional
04:18branching or jumps um the reason that i'm going to kind of design the jumps like this where i'm going
04:24to say um let's let's do a conditional branch and basically if the loop evaluates to true we'll jump
04:30into the body of the loop and if it doesn't evaluate to true then the conditional branch will allow
04:35execution to fall through down to the next statement which will simply contain a jump to the done portion
04:41so basically as soon as the while loop is ready to break it gets uh implemented by an unconditional jump
04:47which was fallen through to by the conditional branch that would only jump into the body if
04:53expression was true i'm doing it this way because it's usually a shorter jump uh to jump to the true
04:59area you know the the body of the while loop and remember conditional branching instructions have a
05:05maximum you know jump reach of about 128 bytes so if you try to jump too far like for example if you wanted
05:11to you could reverse the logic to where you could say if an expression is true let's jump uh to the
05:17done portion and say we're going to break the loop if the expression was false we'll jump into the body
05:21you could do that but then if the body of your while loop was too long maybe it was uh so long that the
05:28last instruction uh was greater than 128 bytes away from the top then by the time you wanted to uh
05:38let's see yeah if you have if you have too long of while loops you can't jump more than 128 uh
05:46instructions away so that's a limitation of conditional branching so i just i want to put
05:50the biggest jump uh on an unconditional jump instruction uh because it doesn't have a limitation
05:55like that i was having a hard time imagining what i was about to say just now for the uh
06:00the reversal i guess i'm just going to ignore the reversal scenario and we'll just do it the regular
06:05way anyway so we want to have a conditional jump that either jumps into the body or to the done area
06:12and then at the very bottom we want to have an unconditional jump to the top which basically
06:22means every time we get to the bottom of the while loop body we're just going to jump up to the while
06:27top actually i'll just say to while top remember again the regular jump instructions maybe i'll take
06:34out the you here the regular jump instructions don't have a limitation on how far they can jump
06:39so that should be fine and that's the basic idea of implementing a while loop um maybe i should draw
06:45just a quick diagram before we start looking at the code just to make sure everybody of different
06:49learning styles understands what i'm talking about so we'll uh let's see eval i'm still adjusting my pen
06:59and you know what it looked a lot better before i hit record eval uh the expression
07:07so i'm going to first evaluate the expression if it evaluates to true we'll put a green arrow here
07:14and i'll put like a t for true then this will be the uh the body of the while loop i'll say like the
07:19body if it evaluates to false um let's see evaluates to false maybe i'll just put that in red
07:29and i'll put an f here then this is just going to be uh the done area
07:37right so you can imagine we evaluate an expression using the compare instruction in assembly
07:42and based on the results of comparing something whatever it is that your condition is
07:46then we uh you know we we either branch to the done area or we branch to the to the body area
07:54um and if you want to have a more complicated while loop and you don't want to put a huge amount of
07:59expressions or uh if you don't want to logically concatenate a bunch of different components into
08:04the expression you know that's fair i usually write my while loops in real life as just while true
08:10and then i break when certain conditions are met you can do that too we're not going to talk about
08:15that in this video but you could just have a while true so you always jump to the top
08:19and there's no branching that goes to while done but then uh throughout the loop you can just sort
08:25of check to see if certain conditions are met and if they are then you'll do a branch to the done
08:31area and if they're not then by the time you make it down to the bottom of the loop then it just
08:37automatically jumps to the top although you got to make sure with the the reach of conditional
08:41branching you probably want to test to see if your condition is true or false or whatever if it means
08:50we're going to continue the loop then you probably just want to do a conditional branch that jumps down
08:54a little bit so that it can continue the body of the loop and then the part that it skipped over just
09:00a little part should be an unconditional jump that jumps out of the loop that way it doesn't matter
09:04how big your while loop body is you can always jump out of the loop but we're not going to talk
09:09about that in this video anyway so we evaluate the expression if it's true we go to the body
09:14if it's false we go to done after the body is done executing then we just jump back up to the top where
09:20we evaluate the expression i'll put while here just uh just to make it more clear that we're talking
09:26about the basic idea of a while loop not too hard you know uh and then if you wanted to implement a
09:33do while loop just make sure that you always evaluate the body at least once that's really
09:37the only difference but that's up to you this is just a regular while loop video okay so we've kind
09:43of talked about it a little bit we've sort of you know drawn it out in a diagram and put some regular
09:48code in let's set up an actual assembly program that will do this so for starters again i'm not going
09:55to show you my make file because i've already made other videos where i explained how to make a make
09:59file from scratch same thing goes for hybrid programs i'm going to have a driver which is a c plus plus
10:05module that just sort of calls on this assembly module if you want to know how to make hybrid
10:09programs or you know drivers or you know whatever i'll see my other videos for now i'm just going to
10:16be talking about looping only okay so let me get my solution up here my source code is called looper
10:24and i'm just going to copy paste the data section for my program real fast just so you can see it
10:29okay so here's the data section again this is not a basics for assembly a video if you don't know how
10:35to do a data section in yasm see my other videos but for now i'm just going to say i'm making some c
10:40strings and each c string just has like the actual array of bytes and then a length and all i'm doing is
10:49printing out messages you know begin the wild test your numbers will be printed back to you until you
10:53decide to quit so that's going to be printed to the user right away then every time the program
10:58wants a number from the user it'll just ask you know please enter a number or 99 to quit
11:03and then it'll prefix the echo back it'll say you entered and then it will actually print the number
11:09and then when you eventually enter 99 it's just going to say we're done
11:13and i'm going to use an external uh function that i have available to just kind of like input
11:18and output numbers this video is not about uh input and outputting and external libraries
11:24if you're interested you could probably just link a hybrid program and use print f and scan
11:29f to very easily do input and output but that's not what this video is about see my other videos
11:36then i have crlf which is just a carriage return in line feed uh you know select the cursor goes to the
11:41next line and then i have like a system call code to just print and then a file descriptor just to print
11:47to standard output again that's explained in other videos so now i'm ready to start copy pasting the
11:53main portion of my program so first off i'm going to start the text section which is where the
11:58instructions go in the asm so there it is and then i'm going to copy paste my designation of external
12:08symbols so i just have a little library that i'm using that makes it easier for me to input and output
12:13integers so you can do this any way you want or you can hard code a number in the globals area if you
12:19don't want to deal with input and output while you're learning how to do loops that's totally fine
12:24but then the entry point is going to be a function called looper so this is my function and again this
12:31is a hybrid program so the main function or the underscore start function is not going to be present
12:36in my assembly module the driver is going to be a c plus plus module that just calls on a function
12:42named looper so that means this function is going to get called from another module and that's why
12:47i have to mark it as global and then it is a function so i'm going to call return at the end of
12:53it to say we're done and then within this function i'm just going to call two other functions that i'm
12:57about to create one is called while test which is going to actually do the while loop and the other
13:02is called crlf which just print which just prints a new line for me i don't know why i do it that way
13:06but i want to sorry not sorry so this is the real uh this is the real meat of the function here let's
13:14start uh well the real meat of the code or the video let's start a function called while test
13:21you can see it's got a void signature with no argument so it doesn't really you know take anything
13:25or return anything i'm gonna use register r12 to hold the user's input so that i can you know print
13:32it out and stuff so that means i have to do a preservation of r12 because it's uh callee saved
13:39for the abi which you should respect so i'm going to push it at the beginning and then i'm going to
13:44pop it at the end and then this is a function so i have to return at the end of the function let me just
13:50double check that there's nothing else weird at the bottom of that nope okay so we've got the prolog
13:56and epilog uh the wild test uh function is being called maybe i should copy paste my crlf real fast
14:03my crlf function it's just sad but it's also kind of cute right it does nothing except just print out
14:09a new line that's all it does this video is not about that so at this point i should have a program
14:14that probably works let's see if it does i'm going to do clear and make run again if you want to know how
14:20to use make files or compile or link or anything like that see my other videos
14:24so i'm going to run it and it just says hello from the driver which is some code that we're not
14:29looking at and then uh it says it's regain control and this line in the middle which is just an empty
14:34new line that's definitely from the assembly program because at the top here we have crlf
14:40if i call it multiple times crlf then you'll see there are multiple blank lines
14:47all right i'm going to take that out now let's continue with while tests so what should we do here
14:51um the first thing that we should do is print an intro message just to let the user know that
14:58we're about to uh you know begin our while test and if we run the program one more time we should
15:03see that message now begin the while test your numbers will be printed back to you
15:08um system calls and simple printing is covered in other videos so now uh you know for me i kind of
15:15like to init all of my loops even if it's a while loop and not just a for loop or anything
15:19so i always have an extra label that i like to call init or uh you know before or something like
15:26that so i have a label now called while test underscore init and it's just where i'm going
15:31to initialize whatever it is that i think i need to initialize so that the loop will actually work
15:37um if you look here all i'm really doing is setting r12 to zero because what i'm going to do is stop the
15:43loop i'm going to break the loop whenever the user enters a 99 so i don't know what's inside of r12 when we
15:48first start this function and i just want to make sure that it's not 99 the stopping number at the
15:53very start so i'm just going to set it to zero um so another note about my labels uh you don't have
16:00to do it this way but i love to write labels where the first part of the label is always the function
16:06that the label is inside of so notice how the function is named while test and so my label is
16:11always while test underscore something and um i'm putting a nit here just to say we're initializing
16:18the loop but if you had a function that had a lot of stuff going on in it for starters you should
16:22probably be breaking up that function into multiple functions but but assuming you didn't uh you should
16:27probably do another underscore and then like another component and then another underscore based on
16:33what giant chunk of your function you're inside of so if there was like an if part
16:37a while part an input part an output part you probably want to you know stick that into your
16:43labels your labels will get huge but for me personally it's really easy to get confused in
16:48assembly and this makes it easier so we're going to initialize uh so that we can run our while loop
16:54and then the next thing we're going to do is implement the top of the while loop so remember
16:59if we looked at this code up here maybe if i drag this over to the side
17:03and pin it to the top for a little while you can see that the top of the while loop is where we
17:08kind of evaluate the expression to see if we need to keep going or not and then we'll either jump into
17:14the body or or we will jump into the ending area the done area so the top whoops the top is always for
17:21that uh we'll say first i'm going to ask are we done so you know how do we know if we're done uh in
17:28this particular while loop we want to compare the user's input which is r12 to the number 99
17:33and if it's equal then we will uh quit which means also if it's not equal we will jump into the uh
17:40the loop's body you know if it's not equal then we continue looping so by the way you're probably
17:46wondering how did r12 get the user's input well we're going to do that as the next step
17:52and of course your design pattern may vary a little bit you know that's okay
17:55i just like to implement it this way so first thing we're going to do is compare r12 with 99
18:00and that's why i've written this comment here just like the blank uh while comparison part
18:06and i guess i could have put this r12 not equal to 99 up at the top but then
18:10it kind of feels like i'm leaving these other two instructions so i moved it down one
18:14so basically uh as long as you know we compare r12 and and 99 compare and conditional branching is
18:22covered in other videos but we compare those two values and then we say if r12 is not equal to 99
18:29then jump to the body and we expect that the body will be like a short jump which will be within the
18:34range of a conditional uh branch so anyway if uh the uh the not equal branch didn't happen that means r12
18:45is equal to 99 at that point execution falls through to line 84 and we'll just unconditionally
18:51jump out of the while loop so basically we're saying if it's true we continue looping if it's
18:58not true we uh just jump outside of the loop we're just totally done with the while loop okay so then
19:04we need a body because obviously we're going to jump into the body here um so that means i'm going to just
19:09copy paste another little set of code here um like right after that uh unconditional jump
19:17so now we got the body notice how i put a little comment here that has a brace just to indicate
19:22to you hey this is the beginning of the actual while loop body just to make it a little bit more clear
19:26and what are we going to do inside of the body we're just going to ask the user for some input so
19:31i'm printing a simple message here and then i'm calling on my helper function
19:36to just actually input a number from the user i'm going to store that number into r12 so that's
19:40how r12 gets the numbers uh gets the user's input and the way i've written this if the user enters a
19:4799 it'll echo it back to the user and then it'll break the the loop afterwards so you know if you
19:53wanted to rearrange things like i talked about before where you input before you check to see if you're
19:57going to keep going you could do that but it would be a little harder to uh echo the user's input
20:02back to them before you break i don't know it's up to you anyway so we grab input from the user
20:09and then we print another message basically saying here's the thing that you inputted
20:12no problem and then again we use one of my helper functions to actually spit the number back out at
20:17them so this is not a very complicated body it's just asking for a number and then printing the number
20:22back to them and then um after that i'm going to do another label and i'm going to call it the body
20:29bottom we don't really have to do this label but for clarity i think it's probably a good idea
20:34so the very bottom of the while loops body is usually where you don't do any more instructions
20:39that are part of the work of the while loops body but just sort of the the place where you jump back
20:45up to the top so you can evaluate and decide to continue or not again so notice how i'm using an
20:52unconditional jump here that's a good idea because again if you have like a huge while loop body you
20:57might end up surpassing the threshold of 128 bytes and then you'll get a assembler error that says
21:04i can never remember this it's it's like a jump out of range error or something like that basically
21:10the if you do a conditional branch to jump up at the top because some people like to check to see
21:15if they should continue the loop they'd like to check for that at the bottom i've done that before
21:19and 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
21:23i just like to take a long jump to the top of the loop and then decide if i'm going to keep going
21:28at the very top and then there's a comment saying hey that's the end of the body
21:34okay no problem now uh let's do the done label which is basically where we jump if the loop is
21:40actually finished so remember if uh if this expression right here evaluates to false
21:46then execution is going to fall through to line 84 where we jump to this while test done label
21:52and so i'm just going to put the while test done label right here so we're done so the done is not
21:57part of the loop it comes after the loop that's this right here on line nine of the little notepad
22:03and we can just kind of do whatever we want at that point we can return to a caller we can just do other
22:08stuff we can you know do a different loop or you know whatever we're just done with the original loop
22:13for me i'm just going to say goodbye with this little print uh to just sort of like you know
22:17print an exit message and then uh we'll do the epilogue where we restore r12 and then we just
22:22return to the caller let the driver worry about exiting the program okay so if we've done this
22:28correctly we should now have a working program let's see let's see okay let's enter a number let's do 22
22:37and it says you enter 22 and let's do 55 and we just we can enter any numbers we want
22:42and as long as we're not entering 99 the program will just continue forever so this is a while loop
22:47if i want to quit i do 99 and it says you entered 99 and then it breaks at the top
22:54it breaks by jumping down to the done area where we print our goodbye message which just is end while test
23:02and then if we run this again if i do 99 from the start then it just immediately breaks you know it
23:08prints out what you entered but then it immediately breaks and that's it that's uh the basics for how
23:15to write a while loop you just use basically conditional branching layered on top of an
23:21abstract concept of what you think a while loop is or what i guess the world thinks a while loop is
23:27okay thank you so much for watching this video i hope you learned a little bit of stuff and
23:30had a little bit of fun i'll see you in the next video
23:41hey everybody thanks for watching this video again from the bottom of my heart i really appreciate it
23:46i do hope you did learn something and have some fun if you could do me a please a small little favor
23:51could you please subscribe and follow this channel or these videos or whatever it is you do on the current
23:57social media website that you're looking at right now it would really mean the world to me and it'll
24:02help make more videos and grow this community so we'll be able to do more videos longer videos better
24:07videos or just i'll be able to keep making videos in general so please do do me a kindness and uh and
24:14subscribe you know sometimes i'm sleeping in the middle of the night and i just wake up because i know
24:19somebody subscribed or followed it just wakes me up and i get filled with joy that's exactly what happens
24:23every single time so you could do it as a nice favor to me or you could you control me if you
24:28want to just wake me up in the middle of the night just subscribe and then i'll i'll just wake up i
24:32promise that's what will happen also uh if you look at the middle of the screen right now you should
24:37see a qr code which you can scan in order to go to the website which i think is also named somewhere
24:42at the bottom of this video and it'll take you to my main website where you can just kind of like see
24:47all the videos i published and the services and tutorials and things that i offer and all that good stuff and
24:53uh if you have a suggestion for uh uh clarifications or errata or just future videos that you want to
25:01see please leave a comment or if you just want to say hey what's up what's going on you know just
25:06send me a comment whatever i also wake up for those in the middle of the night i get i wake up in a cold
25:11sweat and i'm like it would really it really mean the world to me i would really appreciate it so
25:16again thank you so much for watching this video and um enjoy the cool music as as i fade into the
25:24darkness which is coming for us all
25:39so
25:54so
26:09so
26:15so
26:24so
26:36so
26:44so
26:50so
26:56so
27:17so
27:19so
27:39so
27:41so
28:02so
28:04so
28:13so
28:25you
28:27you
28:29you
28:51you

Recommended