<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>low level loops Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/low-level-loops/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/low-level-loops/</link>
	<description></description>
	<lastBuildDate>Sun, 25 Jan 2026 01:02:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>low level loops Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/low-level-loops/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 25 Jan 2026 01:02:57 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly control flow]]></category>
		<category><![CDATA[assembly language loops]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly while loop example]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional jump assembly]]></category>
		<category><![CDATA[learn assembly language]]></category>
		<category><![CDATA[low level loops]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[unconditional jump]]></category>
		<category><![CDATA[while loop assembly]]></category>
		<category><![CDATA[x86 assembly loops]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[x86-64 programming]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=291</guid>

					<description><![CDATA[<p>This video teaches how to implement a classic while loop structure in x86-64 assembly language using YASM. We cover the conceptual mapping from C-style while(condition) { body } to assembly labels (while_top, while_body, while_done), conditional jumps (jne, je, etc.), and the unconditional jump back to the top of the loop. A complete runnable example repeatedly reads integers from the user and echoes them until 99 is entered.</p>
<p>The post <a href="https://www.NeuralLantern.com/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 &#8211; From Concept to Working Program</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly While Loops Explained Step by Step in YASM - From Concept to Working Program" width="1380" height="776" src="https://www.youtube.com/embed/JnkyFMw09Us?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>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.</p>



<p>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.</p>



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



<ul class="wp-block-list">
<li>prints an intro message</li>



<li>repeatedly asks the user to enter numbers</li>



<li>echoes each number back</li>



<li>continues until the user enters 99</li>



<li>prints a goodbye message when finished</li>
</ul>



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



<p>Assumed knowledge: basic x86-64 assembly, how to use a makefile, simple system calls, and calling external functions. If you&#8217;re new to those topics, check my earlier videos first.</p>



<p>Hope this helps someone finally &#8220;get&#8221; while loops in assembly!<br>Thanks for watching &#8211; subscribe if these kinds of low-level explanations are useful to you.</p>



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



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



<p>Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p>Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to Blog: https://www.NeuralLantern.com</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p>All right, hello there. In this video, I&#8217;m going to teach you how to implement while loops in YASM</p>



<p>x86-64 assembly. Although you probably don&#8217;t need to be using YASM assembly to understand</p>



<p>this video or to benefit from it. So what am I talking about? First off, okay, so we&#8217;re going</p>



<p>to do while loops. There&#8217;s a lot of assumed knowledge in this video. If you don&#8217;t know how</p>



<p>assembly program, if you don&#8217;t know how to make a make file, if you don&#8217;t know a lot</p>



<p>of the basics that I&#8217;m just skimming over in this video, you&#8217;ll need to see my other</p>



<p>videos because I explain all of those things at length.</p>



<p>For this video, I&#8217;m just going to be talking about while loops only.</p>



<p>So for starters, let&#8217;s see here and open up a little notepad here and just type a while</p>



<p>loop and try to explain a little bit about it conceptually.</p>



<p>And then after that, we&#8217;re going to, we&#8217;re going to write a program in Yasm to show that</p>



<p>we can get this to work.</p>



<p>So what is a while loop in a higher level language?</p>



<p>We&#8217;ll just suppose C or C++.</p>



<p>You can imagine whatever language you want, but typically in a while loop,</p>



<p>we&#8217;re not talking about a do while loop, although that would be really easy</p>



<p>to implement after watching this video.</p>



<p>But in a regular while loop, you have the keyword while usually,</p>



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



<p>So maybe I&#8217;ll just say does a equal B?</p>



<p>Expression evaluates to true then the body of the while loop will execute and then when it&#8217;s done executing</p>



<p>Execution will go back up to look at the expression again and make sure that it&#8217;s still true</p>



<p>So the loop will run forever until that expression evaluates to false</p>



<p>Maybe that&#8217;s what you want. Maybe that&#8217;s not what you want, but that&#8217;s what it&#8217;ll do</p>



<p>And that means the body runs an endless number of times</p>



<p>Also, if the expression evaluates to false on the very first run</p>



<p>then the body of the while loop will just never run at all.</p>



<p>So that&#8217;s, you know, a basic idea of a while loop in a higher level language.</p>



<p>Hopefully you kind of already know that.</p>



<p>So let&#8217;s look at the parts right here.</p>



<p>If we imagine this as assembly,</p>



<p>then probably the first thing we should do is label the part</p>



<p>where we look at the expression and decide whether we&#8217;re going to continue or not, right?</p>



<p>So I&#8217;m going to put a little label here</p>



<p>and maybe we&#8217;ll call it a while top or something.</p>



<p>You know, whatever you want to do.</p>



<p>just to label the part of the code</p>



<p>So that we know when we write assembly</p>



<p>We&#8217;ll be able to put the appropriate labels on the different parts of the while loop and we&#8217;ll be able to imagine</p>



<p>The while loop that we&#8217;re implementing in assembly</p>



<p>So I&#8217;m gonna say this is the top or you can call it the eval part whatever you want</p>



<p>Then we&#8217;ll make another label down here. We&#8217;ll call it</p>



<p>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</p>



<p>expression, we just have to basically jump to that label called while top.</p>



<p>And if we want to jump to the body to actually execute the code,</p>



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



<p>very bottom after the whole while loop is over and we&#8217;ll call it, you know, while finished</p>



<p>or while, whoops, finished or while done or while over something like that. So I&#8217;m just going to</p>



<p>Now you can imagine that when we&#8217;re at the top of the while loop and we see that the</p>



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



<p>entire while loop.</p>



<p>Then of course we probably want to have some jump statements in here.</p>



<p>So for example, the expression part here, it&#8217;s probably going to jump either to the</p>



<p>body or either to the done area based on whether or not the expression evaluated to true or</p>



<p>false.</p>



<p>label while top you know which contains a conditional branch and then like an unconditional</p>



<p>jump so if a condition is true maybe we can jump into the body if the condition is false we&#8217;ll fall</p>



<p>through to the next statement and do an unconditional jump to the done label if you don&#8217;t</p>



<p>know conditional branching this is another one of the topics that i&#8217;ve covered in previous videos</p>



<p>as we implement the while loop.</p>



<p>So just keep in mind, there are other videos that I have</p>



<p>that you should watch first</p>



<p>if you don&#8217;t understand conditional branching or jumps.</p>



<p>The reason that I&#8217;m going to kind of design</p>



<p>the jumps like this where I&#8217;m going to say,</p>



<p>let&#8217;s do a conditional branch.</p>



<p>And basically if the loop evaluates to true,</p>



<p>we&#8217;ll jump into the body of the loop.</p>



<p>And if it doesn&#8217;t evaluate to true,</p>



<p>then the conditional branch will allow execution</p>



<p>to fall through down to the next statement,</p>



<p>which will simply contain a jump to the done portion.</p>



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



<p>by an unconditional jump which was fallen through to by the conditional branch that would only jump</p>



<p>into the body if uh expression was true i&#8217;m doing it this way because it&#8217;s usually a shorter jump</p>



<p>uh to jump to the true area you know the the body of the while loop and remember conditional</p>



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



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



<p>the logic to where you could say, if an expression is true, let&#8217;s jump to the done portion and say,</p>



<p>we&#8217;re going to break the loop. If the expression was false, we&#8217;ll jump into the body. You could</p>



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



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



<p>let&#8217;s see yeah if you have if you have too long of while loops you can&#8217;t jump more than 128</p>



<p>instructions away so that&#8217;s a limitation of contentional branching so I just I want to put</p>



<p>the biggest jump on an unconditional jump instruction because it doesn&#8217;t have a limitation</p>



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



<p>the reversal I guess I&#8217;m just going to ignore the reversal scenario and we&#8217;ll just do it the</p>



<p>and we&#8217;ll just do it the regular way.</p>



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



<p>And then at the very bottom, we want to have an unconditional jump to the top,</p>



<p>which basically means every time we get to the bottom of the while loop body,</p>



<p>we&#8217;re just going to jump up to the while top.</p>



<p>Actually, I&#8217;ll just say to while top.</p>



<p>Remember again, the regular jump instructions,</p>



<p>maybe I&#8217;ll take out the U here.</p>



<p>The regular jump instructions don&#8217;t have a limitation</p>



<p>on how far they can jump.</p>



<p>So that should be fine.</p>



<p>And that&#8217;s the basic idea of implementing a while loop.</p>



<p>Maybe I should draw just a quick diagram</p>



<p>before we start looking at the code,</p>



<p>just to make sure everybody of different learning styles</p>



<p>understands what I&#8217;m talking about.</p>



<p>So we&#8217;ll, let&#8217;s see, eval.</p>



<p>I&#8217;m still adjusting my pen and you know what?</p>



<p>You know what? It looked a lot better before I hit record.</p>



<p>Eval the expression.</p>



<p>So I&#8217;m going to first evaluate the expression.</p>



<p>If it evaluates to true, we&#8217;ll put a green arrow here and I&#8217;ll put like a T for true.</p>



<p>Then this will be the body of the while loop.</p>



<p>I&#8217;ll say like the body.</p>



<p>If it evaluates to false, let&#8217;s see.</p>



<p>I&#8217;ll just put that in red and I&#8217;ll put an F here then this is just going to be the done area</p>



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



<p>based on the results of comparing something whatever it is that your condition is then we</p>



<p>you know we we either branch to the done area or we branch to the to the body area</p>



<p>want to have a more complicated while loop and you don&#8217;t want to put a huge</p>



<p>amount of expressions or if you don&#8217;t want to logically concatenate a bunch of</p>



<p>different components into the expression you know that&#8217;s fair I usually write my</p>



<p>while loops in real life as just while true and then I break when certain</p>



<p>conditions are met you can do that too we&#8217;re not going to talk about that in</p>



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



<p>and there&#8217;s no branching that goes to while done but then throughout the loop</p>



<p>loop you can just sort of check to see if certain conditions are met and if they are then you&#8217;ll do</p>



<p>a branch to the done area and if they&#8217;re not then by the time you make it down to the bottom of the</p>



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



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



<p>whatever if it means we&#8217;re going to continue the loop then you probably just want to do a conditional</p>



<p>then you probably just want to do a conditional branch that jumps down a little bit</p>



<p>so that it can continue the body of the loop.</p>



<p>And then the part that it skipped over,</p>



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



<p>That way, it doesn&#8217;t matter how big your while loop body is,</p>



<p>you can always jump out of the loop.</p>



<p>But we&#8217;re not going to talk about that in this video.</p>



<p>Anyway, so we evaluate the expression.</p>



<p>If it&#8217;s true, we go to the body.</p>



<p>If it&#8217;s false, we go to done.</p>



<p>After the body is done executing,</p>



<p>then we just jump back up to the top where we evaluate the expression.</p>



<p>where we evaluate the expression.</p>



<p>I&#8217;ll put while here just to make it more clear that we&#8217;re talking about the basic</p>



<p>idea of a while loop.</p>



<p>Not too hard, you know.</p>



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



<p>evaluate the body at least once.</p>



<p>That&#8217;s really the only difference.</p>



<p>But that&#8217;s up to you.</p>



<p>This is just a regular while loop video.</p>



<p>Okay.</p>



<p>So we&#8217;ve kind of talked about it a little bit.</p>



<p>We&#8217;ve sort of, you know, drawn it out in a diagram and put some regular code in.</p>



<p>and put some regular code in let&#8217;s set up an actual assembly program that will do this so for starters</p>



<p>again i&#8217;m not going to show you my make file because i&#8217;ve already made other videos where i</p>



<p>explained how to make a make file from scratch same thing goes for hybrid programs i&#8217;m going</p>



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



<p>if you want to know how to make hybrid programs or you know drivers or you know whatever see my</p>



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



<p>looper. And I&#8217;m just going to copy paste the data section for my program real fast, just so you can</p>



<p>see it. Okay, so here&#8217;s the data section. Again, this is not a basics for assembly video. If you</p>



<p>don&#8217;t know how to do a data section in Yasm, see my other videos. But for now, I&#8217;m just going to say</p>



<p>like the actual array of bytes and then a length and all I&#8217;m doing is printing out messages you</p>



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



<p>that&#8217;s going to be printed to the user right away then every time the program wants a number from</p>



<p>the user it&#8217;ll just ask you know please enter a number or 99 to quit and then it&#8217;ll prefix</p>



<p>the echo back it&#8217;ll say you entered and then it will actually print the number and then when you</p>



<p>we&#8217;re done and i&#8217;m going to use an external uh function that i have available to just kind of</p>



<p>like input and output numbers this video is not about uh input and outputting and external libraries</p>



<p>if you&#8217;re interested you could probably just link a hybrid program and use printf and scan</p>



<p>f to very easily do input and output but that&#8217;s not what this video is about see my other videos</p>



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



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



<p>Then a file descriptor just to print a standard output again. That&#8217;s explained in other videos</p>



<p>So now I&#8217;m ready to start copy pasting the main portion of my program</p>



<p>So first off I&#8217;m going to start the text section, which is where the instructions go in the ASM</p>



<p>So there it is and then I&#8217;m going to copy paste</p>



<p>of external symbols. So I just have a little library that I&#8217;m using that makes it easier for</p>



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



<p>number in the globals area. If you don&#8217;t want to deal with input and output while you&#8217;re learning</p>



<p>how to do loops, that&#8217;s totally fine. But then the entry point is going to be a function called</p>



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



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



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



<p>module and that&#8217;s why i have to mark it as global and then it is a function so i&#8217;m going to call</p>



<p>return at the end of it to say we&#8217;re done and then within this function i&#8217;m just going to call two</p>



<p>other functions that i&#8217;m about to create one is called while test which is going to actually do</p>



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



<p>which just prints a new line for me.</p>



<p>I don&#8217;t know why I do it that way, but I want to.</p>



<p>Sorry, not sorry.</p>



<p>So this is the real meat of the function here.</p>



<p>Let&#8217;s start, well, the real meat of the code or the video.</p>



<p>Let&#8217;s start a function called while test.</p>



<p>You can see it&#8217;s got a void signature with no arguments,</p>



<p>so it doesn&#8217;t really, you know, take anything or return anything.</p>



<p>I&#8217;m going to use register R12 to hold the user&#8217;s input</p>



<p>so that I can, you know, print it out and stuff.</p>



<p>print it out and stuff so that means I have to do a preservation of R12 because it&#8217;s a</p>



<p>callee saved for the ABI which you should respect so I&#8217;m going to push it at the beginning</p>



<p>and then I&#8217;m going to pop it at the end and then this is a function so I have to return</p>



<p>at the end of the function let me just double check that there&#8217;s nothing else weird at the</p>



<p>bottom of that nope okay so we&#8217;ve got the prologue and epilogue the wild test function</p>



<p>maybe I should copy paste my crlf real fast my crlf function it&#8217;s just sad but</p>



<p>it&#8217;s also kind of cute right it does nothing except just print out a new line</p>



<p>that&#8217;s all it does this video is not about that so at this point I should</p>



<p>have a program that probably works let&#8217;s see if it does I&#8217;m gonna do clear and</p>



<p>make run again if you want to know how to use make files or compile or link or</p>



<p>anything like that see my other videos so I&#8217;m gonna run it and it just says</p>



<p>which is some code that we&#8217;re not looking at.</p>



<p>And then it says it&#8217;s regain control.</p>



<p>And this line in the middle, which is just an empty new line,</p>



<p>that&#8217;s definitely from the assembly program.</p>



<p>Because at the top here we have CRLF.</p>



<p>If I call it multiple times CRLF,</p>



<p>then you&#8217;ll see there are multiple blank lines.</p>



<p>All right, I&#8217;m gonna take that out.</p>



<p>Now let&#8217;s continue with while test.</p>



<p>So what should we do here?</p>



<p>The first thing that we should do is print an intro message</p>



<p>message just to let the user know that we&#8217;re about to you know begin our while</p>



<p>tests and if we run the program one more time we should see that message now</p>



<p>begin the while test your numbers will be printed back to you system calls and</p>



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



<p>of like to init all of my loops even if it&#8217;s a while loop and not just a for</p>



<p>loop or anything so I always have an extra label that I like to call init</p>



<p>or before or something like that.</p>



<p>So I have a label now called while test underscore init,</p>



<p>and it&#8217;s just where I&#8217;m gonna initialize</p>



<p>whatever it is that I think I need to initialize</p>



<p>so that the loop will actually work.</p>



<p>If you look here, all I&#8217;m really doing is setting R12 to zero</p>



<p>because what I&#8217;m gonna do is stop the loop.</p>



<p>I&#8217;m gonna break the loop whenever the user enters a 99.</p>



<p>So I don&#8217;t know what&#8217;s inside of R12</p>



<p>when we first start this function.</p>



<p>And I just wanna make sure that it&#8217;s not 99,</p>



<p>the stopping number at the very start so I&#8217;m just going to set it to zero. So</p>



<p>another note about my labels you don&#8217;t have to do it this way but I love to</p>



<p>write labels where the first part of the label is always the function that the</p>



<p>label is inside of so notice how the function is named while test and so my</p>



<p>label is always while test underscore something and I&#8217;m putting init here</p>



<p>just to say we&#8217;re initializing the loop but if you had a function that had a</p>



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



<p>into multiple functions but but assuming you didn&#8217;t uh you should probably do another underscore</p>



<p>and then like another component and then another underscore based on what giant chunk of your</p>



<p>function you&#8217;re inside of so if there was like an if part a while part an input part an output part</p>



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



<p>infused in assembly and this makes it easier.</p>



<p>So we&#8217;re going to initialize so that we can run our while loop and then the next thing</p>



<p>we&#8217;re going to do is implement the top of the while loop.</p>



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



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



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



<p>jump into the ending area, the done area. So the top, whoops, the top is always for that.</p>



<p>We&#8217;ll say first, I&#8217;m going to ask, are we done? So, you know, how do we know if we&#8217;re done?</p>



<p>In this particular while loop, we want to compare the user&#8217;s input, which is R12 to the number 99.</p>



<p>And if it&#8217;s equal, then we will quit, which means also if it&#8217;s not equal, we will jump into the</p>



<p>So by the way, you&#8217;re probably wondering how did R12 get the user&#8217;s input?</p>



<p>Well, we&#8217;re going to do that as the next step.</p>



<p>And of course, your design pattern may vary a little bit.</p>



<p>No, that&#8217;s okay.</p>



<p>I just like to implement it this way.</p>



<p>So first thing we&#8217;re going to do is compare R12 with 99.</p>



<p>And that&#8217;s why I&#8217;ve written this comment here, just like the blank while comparison part.</p>



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



<p>of feels like I&#8217;m leaving these other two instructions.</p>



<p>So I moved it down one.</p>



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



<p>R12 and 99 compare and conditional branching is covered in other videos but we compare those two</p>



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



<p>will be like a short jump which will be within the range of a conditional branch</p>



<p>So anyway, if the not equal branch didn&#8217;t happen, that means R12 is equal to 99.</p>



<p>At that point, execution falls through to line 84, and we&#8217;ll just unconditionally jump out of the while loop.</p>



<p>So basically we&#8217;re saying, if it&#8217;s true, we continue looping.</p>



<p>If it&#8217;s not true, we just jump outside of the loop.</p>



<p>We&#8217;re just totally done with the while loop.</p>



<p>Okay.</p>



<p>So then we need a body, because obviously we&#8217;re going to jump into the body here.</p>



<p>that means I&#8217;m going to just copy paste another little set of code here.</p>



<p>Right after that unconditional jump.</p>



<p>So now we got the body.</p>



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



<p>hey, this is the beginning of the actual while loop body,</p>



<p>just to make it a little bit more clear.</p>



<p>And what are we going to do inside of the body?</p>



<p>We&#8217;re just going to ask the user for some input.</p>



<p>So I&#8217;m printing a simple message here,</p>



<p>and then I&#8217;m calling on my helper function</p>



<p>to just actually input a number from the user.</p>



<p>I&#8217;m going to store that number into R12.</p>



<p>So that&#8217;s how R12 gets the numbers, gets the user&#8217;s input.</p>



<p>And the way I&#8217;ve written this, if the user enters a 99,</p>



<p>it&#8217;ll echo it back to the user and then it&#8217;ll break the loop afterwards.</p>



<p>So, you know, if you wanted to rearrange things like I talked about before,</p>



<p>where you input before you check to see if you&#8217;re going to keep going, you could do that.</p>



<p>But it would be a little harder to echo the user&#8217;s input back to them before you break.</p>



<p>I don&#8217;t know. It&#8217;s up to you.</p>



<p>I don&#8217;t know it&#8217;s up to you anyway so we grab input from the user and then we print another</p>



<p>message basically saying here&#8217;s the thing that you inputted no problem and then again we use</p>



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



<p>a very complicated body it&#8217;s just asking for a number and then printing the number back to them</p>



<p>and then after that I&#8217;m going to do another label and I&#8217;m going to call it the body bottom</p>



<p>we don&#8217;t really have to do this label but for clarity I think it&#8217;s probably a good idea</p>



<p>I think it&#8217;s probably a good idea.</p>



<p>So the very bottom of the while loop&#8217;s body is usually where you don&#8217;t do any more instructions</p>



<p>that are part of the work of the while loop&#8217;s body,</p>



<p>but just sort of the place where you jump back up to the top</p>



<p>so you can evaluate and decide to continue or not again.</p>



<p>So notice how I&#8217;m using an unconditional jump here.</p>



<p>That&#8217;s a good idea because again, if you have like a huge while loop body,</p>



<p>you might end up surpassing the threshold of 128 bytes</p>



<p>of 128 bytes and then you&#8217;ll get a assembler error that says</p>



<p>I can never remember this. It&#8217;s like a</p>



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



<p>because some people like to check to see</p>



<p>if they should continue the loop, they like to check for that at the bottom. I&#8217;ve done that before.</p>



<p>And then if true, then we&#8217;ll jump to the top of the loop. But if the loop is too big, that won&#8217;t work. So</p>



<p>I just like to take a long jump to the top of the loop and then decide if I&#8217;m going to keep going at the very top.</p>



<p>if I&#8217;m going to keep going at the very top. And then there&#8217;s a comment saying, hey, that&#8217;s the</p>



<p>end of the body. Okay, no problem. Now let&#8217;s do the done label, which is basically where we jump</p>



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



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



<p>And so I&#8217;m just going to put the while test done label right here. So we&#8217;re done. So the done is</p>



<p>So we&#8217;re done. So the done is not part of the loop. It comes after the loop. That&#8217;s this right</p>



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



<p>point, we can return to a caller, we can just do other stuff, we can, you know, do a different</p>



<p>loop or, you know, whatever, we&#8217;re just done with the original loop. For me, I&#8217;m just going to say</p>



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



<p>we&#8217;ll do the epilogue where we restore R12. And then we just return to the caller,</p>



<p>the driver worry about exiting the program okay so if we&#8217;ve done this correctly we should now have</p>



<p>a working program let&#8217;s see let&#8217;s see okay let&#8217;s enter a number let&#8217;s do 22 and it says you enter</p>



<p>22 and let&#8217;s do 55 and we just we can enter any numbers we want and as long as we&#8217;re not entering</p>



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



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



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



<p>immediately breaks you know it prints out what you entered but then it immediately breaks</p>



<p>and that&#8217;s it that&#8217;s uh the basics for how to write a while loop you just use basically</p>



<p>layered on top of an abstract concept of what you think a while loop is,</p>



<p>or what I guess the world thinks a while loop is.</p>



<p>Okay, thank you so much for watching this video.</p>



<p>I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p>I&#8217;ll see you in the next video.</p>



<p>Hey everybody.</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>if you could do me a please a small little favor could you please subscribe and follow this channel</p>



<p>or these videos or whatever it is you do on the current social media website that you&#8217;re looking</p>



<p>at right now it would really mean the world to me and it&#8217;ll help make more videos and grow this</p>



<p>community so we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able</p>



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



<p>I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p>It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time.</p>



<p>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.</p>



<p>Just subscribe and then I&#8217;ll just wake up.</p>



<p>I promise that&#8217;s what will happen.</p>



<p>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,</p>



<p>which I think is also named somewhere at the bottom of this video.</p>



<p>and it&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p>i published and the services and tutorials and things that i offer and all that good stuff and</p>



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



<p>see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know</p>



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



<p>it would really mean the world to me. I would really appreciate it.</p>



<p>So again, thank you so much for watching this video and</p>



<p>enjoy the cool music as I fade into</p>



<p>the darkness which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/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 &#8211; From Concept to Working Program</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
