<?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>x86 assembly loops Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/x86-assembly-loops/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/x86-assembly-loops/</link>
	<description></description>
	<lastBuildDate>Sun, 01 Feb 2026 02:49:30 +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>x86 assembly loops Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/x86-assembly-loops/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Feb 2026 02:49:29 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[asm tutorial 2025]]></category>
		<category><![CDATA[assembly for loop]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly while loop]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional jump assembly]]></category>
		<category><![CDATA[implementing for loop in assembly]]></category>
		<category><![CDATA[inc instruction assembly]]></category>
		<category><![CDATA[learning assembly language]]></category>
		<category><![CDATA[loop in assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[reverse engineering basics]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86 assembly loops]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<category><![CDATA[yasm x86-64]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=295</guid>

					<description><![CDATA[<p>This tutorial explains how to implement a C-style for loop in x86-64 assembly using YASM by converting it to while-loop logic with initialization, conditional jump (jl), increment (inc), and unconditional jump back to the loop top. Demonstrates register usage (r12/r13), loop structure, and printing numbers inside the body.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/">x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</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 For Loops Explained - YASM Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/pftzv5lIPso?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>Quick practical guide showing how to implement a classic for loop (i=0; i&lt;n; i++) in x86-64 assembly with YASM. We break the for loop into init/check/update parts, convert it to while-style logic, then build it with compares, conditional jumps, inc, and unconditional jumps back to the top. Includes real working code, user input, and printed output.</p>



<p>Great for students learning assembly after C/C++, OSdev hobbyists, or reverse engineering beginners.</p>



<p>Introduction to For Loops in Assembly 00:00:00<br>For Loop Structure in High-Level Languages 00:00:28<br>Breaking Down For Loop Parts: Init, Check, Update 00:01:04<br>Converting For Loop to While Loop 00:01:52<br>Why While Loop Style Helps in Assembly 00:03:16<br>Program Overview and Setup 00:04:12<br>Data Section &#8211; Strings and Messages 00:04:40<br>External Functions and Hybrid Program 00:06:11<br>Entry Point &#8211; Looper Function 00:07:12<br>For Test Function and Register Usage 00:07:42<br>Preserving Callee-Saved Registers 00:08:05<br>Printing Welcome Message 00:09:00<br>Prompting User for Number 00:09:48<br>Getting User Input 00:10:25<br>For Loop Structure in Assembly &#8211; Comments 00:11:12<br>Initialization &#8211; Setting Counter to Zero 00:12:50<br>Loop Top Label and Condition Check 00:14:28<br>Conditional Jump Setup (jl) 00:15:07<br>Handling Jump Distances 00:15:34<br>Loop Body &#8211; Printing Current Number 00:16:58<br>Update Part &#8211; Increment Counter 00:18:40<br>Unconditional Jump Back to Loop Top 00:19:14<br>Loop Done Label and Exit 00:19:55<br>Printing Goodbye Message 00:20:23<br>Testing the Program 00:22:08<br>Final Results and Demo 00:22:24<br>Outro and Call to Action 00:23:25<br>Thanks and Subscribe Request 00:23:45<br>Website and QR Code Mention 00:24:38<br>Closing Thanks 00:25:16</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>Hey there! In this video I&#8217;m going to teach you how to implement for loops in YASM x86-64 assembly.</p>



<p>Although if you&#8217;re using a different type of machine or a different assembler,</p>



<p>this video should still be useful to you because it&#8217;s mostly about the concepts involved in implementing a for loop.</p>



<p>Okay, so for starters here, what am I even talking about? Let&#8217;s do…</p>



<p>are you even talking about dude imagine in a higher level language we have a for loop here</p>



<p>and so we have like four size type i equals zero i is less than 99 i plus plus right probably most</p>



<p>programmers looking up assembly uh on the internet probably are interested in uh or probably already</p>



<p>know how to implement for loops in c plus plus so let&#8217;s just pretend that you do and so inside of</p>



<p>your for loop we&#8217;ll do stuff and what happens here is uh well you know this for loop is going to run</p>



<p>Well, you know, this for loop is going to run 99 times.</p>



<p>It&#8217;s going to start at zero.</p>



<p>And, you know, so we have like an initialization part right here.</p>



<p>Maybe I should copy paste this and kind of break down the parts.</p>



<p>We have an init part here for the for loop.</p>



<p>We&#8217;ll say init part.</p>



<p>And then here we have a check part.</p>



<p>We&#8217;ll continue to loop as long as this expression is true.</p>



<p>You can make a big complicated expression if you want to,</p>



<p>but we&#8217;ll just keep it simple for this video.</p>



<p>So I&#8217;ll call this the check part.</p>



<p>And then we have sort of an update or a maintenance part.</p>



<p>an update or a maintenance part. So I&#8217;m going to call this the update part where we increment our</p>



<p>counter variable, move pointers, even call functions, do whatever it is you think you need</p>



<p>to do to update the loop so that it runs it, you know, so that the loops don&#8217;t actually have to be</p>



<p>integers. But in this case, I think it&#8217;s better, it&#8217;s easier to understand what the loop is doing</p>



<p>if we just kind of break down these parts. Anyway, so what is really a while loop? Sorry,</p>



<p>but a while loop that breaks and has break has like a nit logic and breaking logic and checking logic just in specific places.</p>



<p>Imagine this. What if I said before the loop starts, I&#8217;ll let&#8217;s let&#8217;s say we&#8217;re going to do a while loop before the loop starts.</p>



<p>I&#8217;m going to initialize a variable size type I equals zero, right?</p>



<p>That&#8217;s the initialization part that&#8217;ll only happen one time.</p>



<p>And then when you have a while loop and it checks to see if it should keep going, that&#8217;s</p>



<p>the check part of the for loop.</p>



<p>So I&#8217;ll just say while i is less than 99.</p>



<p>And then for the update part, we just have to stick that inside of the while loop somewhere.</p>



<p>Depending on your logic, you might want to put it at the top or the middle or the bottom.</p>



<p>I&#8217;m going to put it at the bottom because I&#8217;m going to expect that it happens right</p>



<p>before we do the next check.</p>



<p>So I&#8217;m going to maybe do a comment here and call it the update part and I&#8217;m just going</p>



<p>So maybe if I update this code to print something, it&#8217;ll seem a little bit more clear.</p>



<p>Value of I is, and then I&#8217;ll just say I end L, and then I&#8217;ll copy paste that to here.</p>



<p>And then I can also put this inside of the while loop.</p>



<p>And now we have transformed our for loop into a while loop.</p>



<p>transform your for loop into a while loop it&#8217;s pretty easy to implement in assembly if you</p>



<p>already know how to implement while loops so i should again say that there are lots of parts</p>



<p>i don&#8217;t know if i said that during this video but there are lots of things in this video that i&#8217;m</p>



<p>not going to that i&#8217;m not going to explain specifically because i&#8217;ve already explained</p>



<p>them in other videos so for example implementing a while loop is in another video so if you don&#8217;t</p>



<p>already know how to implement while loops if you don&#8217;t know how to use conditional branching or</p>



<p>If you don&#8217;t know the basics of assembly, hybrid programs, make files, all the basic stuff that I&#8217;m going to skim over in this video,</p>



<p>then you should probably check out my other videos first.</p>



<p>But for now, I&#8217;m just going to assume that you know how to implement a while loop.</p>



<p>And really the lesson is, hey, just take your for loop and convert it into a while loop.</p>



<p>And then you can implement the while loop pretty easily.</p>



<p>That&#8217;s the secret as far as I&#8217;m concerned.</p>



<p>So imagine this now. Let&#8217;s do some assembly code.</p>



<p>I have a source code file here called looper.asm for assembly.</p>



<p>And you can imagine that I have a hybrid program running under the hood.</p>



<p>I&#8217;m not going to show you all the code involved.</p>



<p>I&#8217;m not going to show you the make file or the C++ driver that calls on this module.</p>



<p>We&#8217;re just going to write the assembly here.</p>



<p>So, you know, if you&#8217;re writing pure assembly at home to practice, that&#8217;s fine.</p>



<p>Just keep in mind, there&#8217;s like a few things under the hood that I&#8217;m not showing in this specific video.</p>



<p>So first off, I&#8217;m going to copy paste my data section.</p>



<p>paste my data section of my assembly program. The first thing to note is we have a bunch of</p>



<p>C strings here. We&#8217;re basically going to be telling the user, hey, we&#8217;re going to begin</p>



<p>the four tests. Maybe I should have capitalized for, I&#8217;ll leave it. And then we&#8217;re going to prompt</p>



<p>the user for a number. We&#8217;re going to say, please enter a number. The loop will print from zero to</p>



<p>the number minus one, which is like the typical four loop that you usually write in the most</p>



<p>and then we&#8217;re going to have a little prefix we&#8217;re going to say you know like an arrow and then we&#8217;re</p>



<p>going to print the number back to the user that we&#8217;re looping through so if the user enters a</p>



<p>10 it&#8217;s going to print the arrow 0 and then arrow 1 arrow 2 all the way up to arrow 9</p>



<p>and then when we&#8217;re done we&#8217;re going to print an ending message so this is nothing new if you know</p>



<p>assembly already just c strings that will print with system calls see my other videos if you don&#8217;t</p>



<p>And then a CRLF string, just basically doing a new line, a carriage return new line feed</p>



<p>on the system.</p>



<p>And then we&#8217;re going to output using system call code one.</p>



<p>And we&#8217;re going to use file descriptor one so we can print to standard output.</p>



<p>Okay.</p>



<p>So now the real fun begins.</p>



<p>Let&#8217;s start our text section, which is where the instructions of our assembly program go.</p>



<p>So I&#8217;m going to do text section, section text right there.</p>



<p>And I&#8217;m going to use two external functions to just help me input and output numbers to the user.</p>



<p>If you wanted to, you could use a system call that just inputted a character.</p>



<p>And then you could just kind of like loop printing various characters.</p>



<p>And like, let&#8217;s say if the user typed A, maybe you could imagine doing a loop that increases the character that they typed all the way until it hits Z.</p>



<p>Or if they hit, you know, F, it&#8217;ll just print F all the way to Z.</p>



<p>to z you could do that without using an external library for printing integers this video is not</p>



<p>about this library right here so i&#8217;m not really going to go over it but uh you know you could</p>



<p>hard code the start and end points when you&#8217;re practicing um you could you could use a different</p>



<p>library or a different function call to get the inputs you could use a system call just to input</p>



<p>one character um or you could you know use a hybrid program to to utilize printf and scanf</p>



<p>f. Either way I&#8217;m just going to be using these two functions just to like get input and output.</p>



<p>It&#8217;s not really part of the idea of looping. So now let&#8217;s start our entry point.</p>



<p>Our function is called looper. Since this is a hybrid program you can imagine there&#8217;s a C++</p>



<p>module elsewhere calling on the looper function and so that&#8217;s why I mark it as global so it can</p>



<p>be called upon. And then I have another function called for test. I don&#8217;t really know why I chose</p>



<p>it this way but i wanted to make another function that was called upon by our entry point here</p>



<p>so the looper function really doesn&#8217;t do anything except recall the for test function</p>



<p>so now let&#8217;s start the for test function actually maybe this is where the fun begins</p>



<p>so i&#8217;m gonna put it down here and so you can see the signature it doesn&#8217;t take any arguments it</p>



<p>doesn&#8217;t return anything it just does stuff and then i have a note to myself this is how we&#8217;re</p>



<p>going to use the registers we&#8217;re going to use r12 for the user&#8217;s number and then we&#8217;re going to use</p>



<p>And then we&#8217;re going to use R13 to keep track of where we&#8217;re going.</p>



<p>So let&#8217;s see.</p>



<p>I&#8217;m going to start by saying let&#8217;s preserve R12 and R13</p>



<p>because you have to respect the application binary interface, the ABI,</p>



<p>and that designates R12 and R13 as Kali saved registers.</p>



<p>If I don&#8217;t preserve those and my program is even a little bit complicated,</p>



<p>I&#8217;m probably going to be debugging forever</p>



<p>debugging forever or I&#8217;m going to just crash my program for no reason. So I&#8217;m going to just</p>



<p>do a push pop pair. Notice how the pops are in reverse order. This is not a push pop video,</p>



<p>but just so you know, I guess while I&#8217;m here, I&#8217;m going to copy paste my crlf function,</p>



<p>which really does nothing. It just prints out the crlf string with a system call. That&#8217;s all</p>



<p>Okay, so do I even need CRLF in this program?</p>



<p>I think I just modified this.</p>



<p>Maybe I don&#8217;t even need it anymore.</p>



<p>No, I guess I do.</p>



<p>The first thing we&#8217;ll do is we&#8217;ll print an introduction message to the user.</p>



<p>So inside of the for test, we&#8217;ll just use a system call to print out, you know, a welcome</p>



<p>message to the user.</p>



<p>And then I&#8217;m going to call CRLF, which will just give us a new line.</p>



<p>And yeah, you can hard code, you know, the 13, 10 at the end of these strings, but I</p>



<p>don&#8217;t really like doing that.</p>



<p>doing that. Okay, so we should have a working program at this point. Let me see if it actually</p>



<p>does work. Clear and make run. Okay, so the driver prints a little welcome message. You don&#8217;t see the</p>



<p>driver code, but that&#8217;s what it&#8217;s doing. And then the for test prints the welcome message that we</p>



<p>just added. And then the driver lets us know it&#8217;s retained control. And then it&#8217;s responsible for</p>



<p>returning to the operating system for us. Okay, so then the next thing we&#8217;re going to do is we&#8217;re</p>



<p>we&#8217;re going to ask the user for a number so that we know how many times to loop.</p>



<p>And again, you could hard code this number if you don&#8217;t want to do IO right now.</p>



<p>You could even print a character a certain number of times</p>



<p>if you don&#8217;t want to even deal with printing an integer.</p>



<p>But I&#8217;m going to ask the user for a number.</p>



<p>That&#8217;s going to be the prompt string.</p>



<p>And if we run it again, now you should see it asks the user for a number.</p>



<p>So it&#8217;s going to say, please enter a number.</p>



<p>The loop will print from zero to N minus one.</p>



<p>print from 0 to n minus 1. It doesn&#8217;t actually ask for the number though. It doesn&#8217;t actually</p>



<p>I guess take the number so that&#8217;s going to be my external library that this video is not about</p>



<p>where I just call a function called input assigned 64 integer and I&#8217;m going to receive that back in</p>



<p>rax and I&#8217;m just going to save rax into r12. So r12 is now going to be the number that the user</p>



<p>inputted and that&#8217;s why up here I have it designated as the user&#8217;s number. So really not a big deal but</p>



<p>a big deal but uh you know if we run it again it&#8217;ll ask for a number and then it won&#8217;t do</p>



<p>anything else it&#8217;ll just kind of quit okay so now we can implement our for loop this is going to be</p>



<p>a little tricky so we&#8217;re going to start off with the initialization part remember we had several</p>



<p>parts here if i just kind of drag this off to the side maybe pin it up to the top so we can see it</p>



<p>aren&#8217;t we? If I move it a little bit to the side. So remember that for loop, it&#8217;s got an init part</p>



<p>and a check part and an update part. And I&#8217;ve kind of added that as a comment just to remind myself</p>



<p>of the way my mind is supposed to be wrapped around this concept. And so I made another comment</p>



<p>here with four and then empty parentheses just to denote that some comments that come below</p>



<p>I guess the top of the for loop</p>



<p>So now I&#8217;m going to make a label here and what I like to do with my labels is I like to</p>



<p>prefix them with the name of the function that I&#8217;m currently in and then an underscore and that kind of helps me keep track of</p>



<p>My symbols a little bit more easily, especially if I have a large module the symbols are less likely to overlap if I prefix them with the</p>



<p>The function names</p>



<p>to be Fortest underscore something and since I&#8217;m only doing one thing inside of the Fortest function</p>



<p>I&#8217;m just going to have a suffix only for the most part but you can imagine if you had more</p>



<p>parts inside of your function and it started getting a little cluttered you might want to have</p>



<p>you know another label you know that just sort of another I guess like part to your label that names</p>



<p>the part of your function you&#8217;re in although this is assembly it gets really hard really fast so</p>



<p>really hard really fast so if your function is even a little bit complicated you should probably</p>



<p>consider breaking it up into multiple functions if you can. We can&#8217;t really do that at this point</p>



<p>because it&#8217;s just a for loop but keep that in mind. Okay so we&#8217;re going to initialize. Remember</p>



<p>the first thing we had to do to initialize was you know setting i to zero or you know whatever</p>



<p>it is that we&#8217;re going to set up in the init part so I&#8217;m just going to do that here. I&#8217;m going to say</p>



<p>That&#8217;s the first part, the update part where we set size type i equals zero.</p>



<p>In fact, maybe I could do another copy paste of this.</p>



<p>Where instead of using a size type, we&#8217;ll just say that the register r13 equals zero.</p>



<p>I know that&#8217;s not going to make sense in a higher level language right now,</p>



<p>but just so that the for loop looks a little bit more like assembly.</p>



<p>We&#8217;ll keep going as long as r13 is less than r12.</p>



<p>and then we increase R13.</p>



<p>So I&#8217;m just going to put ink R13</p>



<p>so it looks more assembly-like,</p>



<p>even though this completely and totally</p>



<p>would not compile in C++.</p>



<p>I hope that this helps your understanding a little bit.</p>



<p>So yeah, we can do everything</p>



<p>except for just the increase part at the very top.</p>



<p>I&#8217;m going to choose to increase it at the bottom.</p>



<p>If you wanted to, I guess you could start off R13</p>



<p>as a negative number and then increase it at the top.</p>



<p>But I personally don&#8217;t feel that&#8217;s like very clean.</p>



<p>feel that&#8217;s like very clean it also forces you to use signed integers maybe you wanted to use an</p>



<p>unsigned integer so you could get like a gigantic um maximum number that you looped up to i don&#8217;t</p>



<p>know so we&#8217;ll just initialize here and we&#8217;ll say r13 is equal to zero the init part should not be</p>



<p>part of the actual like looping like every time you loop up to the top of the for loop you should</p>



<p>not repeat that part again it should only happen once so the next label that i have is called loop</p>



<p>have is called loop top and that&#8217;s just going to be the top of the loop that I</p>



<p>continue to go back up to every time I want to see if we&#8217;re supposed to</p>



<p>continue looping and then go into the loops body so maybe I should say that&#8217;s</p>



<p>why this is named to underscore loop top and I don&#8217;t know you don&#8217;t have to do</p>



<p>camel casing and your labels you know you could just do loop in it or whatever</p>



<p>but I&#8217;m just choosing to do it this way so at the top of our loop we&#8217;ll be</p>



<p>Remember that was the check part, right?</p>



<p>So we&#8217;re going to check to see that R13 is still less than R12.</p>



<p>If it is, we&#8217;ll continue with the for loop.</p>



<p>If it&#8217;s not, then we jump out of the for loop.</p>



<p>So that means probably the true case where R13 is indeed less than R12,</p>



<p>that&#8217;s going to be a short jump just into the loop&#8217;s body.</p>



<p>And the false case where R13 is not less than R12,</p>



<p>that should probably end up being a much longer jump.</p>



<p>on how big your your loop is maybe that jump is too long too far away for a conditional branching</p>



<p>instruction to reach if you&#8217;ve watched my previous videos you should know already that</p>



<p>the conditional branching instructions like jl like jump less than they can only reach about 128</p>



<p>bytes away if you try to go further than that the assembler will actually stop and block you from</p>



<p>finishing your compilation it&#8217;ll say i think it&#8217;s like jump out of range or something like that</p>



<p>So you want to try to keep the short jump points with your conditional branches.</p>



<p>And then in the false case, where the conditional branch doesn&#8217;t actually do anything,</p>



<p>then it falls through to the next instruction where you will have an unconditional jump.</p>



<p>And remember, the unconditional jumps, they don&#8217;t have a limitation of 128 bytes.</p>



<p>They can jump like all over the place.</p>



<p>Like they can jump anywhere, basically.</p>



<p>that means if you think about it, we come in to the loop top right here.</p>



<p>We immediately do a compare instruction and a conditional branch.</p>



<p>So if R13 is less than R12, meaning we should continue to for loop,</p>



<p>then we&#8217;ll just do a short jump into the loop&#8217;s body and actually execute its body.</p>



<p>And if not, we will end up falling through to line 92,</p>



<p>where there&#8217;s just an unconditional jump instruction that just says,</p>



<p>all right, let&#8217;s jump all the way down to being done.</p>



<p>Let&#8217;s jump all the way down to being done, which could be very far away for all we know.</p>



<p>Okay, so we&#8217;ve done that.</p>



<p>Now let&#8217;s implement the loops body because the first thing that we did is we wanted to</p>



<p>jump into the loop body to actually execute our instructions, which in this case, we&#8217;ll</p>



<p>just be, you know, printing a number every time we loop and then increasing that number.</p>



<p>So I&#8217;m going to do a little like new line there.</p>



<p>I&#8217;m going to paste the loop body.</p>



<p>little comments to help remind myself that this is actually the loop&#8217;s body. So for test loop body</p>



<p>that takes care of the branching instruction, hitting on that and actually going into the body.</p>



<p>And then later we&#8217;re going to have to implement a label for the loop being done. But for now,</p>



<p>we&#8217;ll just say the body. What does it do? It just prints a little message to the user.</p>



<p>If you look at the message for current number string, if I go up real fast,</p>



<p>message for current number, it&#8217;s just an arrow. So the user&#8217;s going to see an arrow and then</p>



<p>So the user is going to see an arrow and then their current number, I guess of the loop&#8217;s</p>



<p>current number.</p>



<p>And then every time it loops, it&#8217;s just going to continue printing that arrow with a number</p>



<p>on it.</p>



<p>So we&#8217;re doing that.</p>



<p>And then we&#8217;re going to say R13, which is the current counter variable, which started</p>



<p>at zero is going to get loaded into RDI, which if you watched my other videos, it&#8217;s just</p>



<p>the first integer argument for a function call.</p>



<p>So I&#8217;m just going to call this other function here, which is not part of the video to just</p>



<p>which is not part of the video to just say hey please print this number for me so the first</p>



<p>time this iterates it&#8217;s going to print zero because it&#8217;s going to print r13 and then it&#8217;s</p>



<p>going to print a new line so that the cursor goes to the next you know line of the terminal</p>



<p>so that&#8217;s all the body does it just kind of like prints the current number with an arrow and does</p>



<p>a new line and then at the very bottom of the loop body we just sort of maintain the for loop this is</p>



<p>for loop this is going to be the update part so like let&#8217;s see maybe scooch this up a little bit</p>



<p>the update part where we have let&#8217;s see on the very right side of the for loop which is like i</p>



<p>plus plus or in more assembly speak increasing the counter variable so i&#8217;m just going to increase r13</p>



<p>and then that way the loop can progress you know we&#8217;re always looking at r13</p>



<p>to decide if we need to stop or not you know we&#8217;re comparing r13 to r12</p>



<p>So we&#8217;re just increasing at the very bottom of the loop and then we unconditionally jump back up to the top.</p>



<p>Probably a better idea to unconditionally jump back up to the top because maybe the jump to the top is very very far and if it&#8217;s greater than 128 bytes it won&#8217;t work.</p>



<p>So the regular jump instruction doesn&#8217;t have that limitation. So now we&#8217;re jumping up to the loop top.</p>



<p>So you can imagine now that you know we&#8217;re not going to the initialization part. That would be bad.</p>



<p>We&#8217;re just jumping up to the top here where we immediately ask you know are we done?</p>



<p>If we&#8217;re not done, we jump into the loop body and print another number.</p>



<p>And then at the very bottom of the body, we say, all right, increase the counter and then jump back up to the top.</p>



<p>So this is a simple for loop, but you can see what it&#8217;s doing, right?</p>



<p>It&#8217;s just going to be printing a number over and over again as the number increases.</p>



<p>And then eventually it&#8217;ll stop when it hits the correct number.</p>



<p>The last thing we need is the loop done label.</p>



<p>If you look back up at the top here on line 92, if R13 was not less than R12,</p>



<p>less than r12 then execution would fall through to line 92 and there is our unconditional jump</p>



<p>instruction basically saying if r13 is not less than r12 then we&#8217;ll jump to the loop done label</p>



<p>which means we&#8217;re just totally finished with this loop so we have to make that real fast</p>



<p>and all it&#8217;s going to do is just basically say goodbye it&#8217;s just going to print a message to</p>



<p>to the restoration functions.</p>



<p>Oh, did I ruin my return somewhere?</p>



<p>Uh-oh, what did I do wrong?</p>



<p>I lost my return statement.</p>



<p>Did I accidentally delete that somehow?</p>



<p>That&#8217;s a bad program.</p>



<p>Or did I not even…</p>



<p>Hmm, I wonder if it like…</p>



<p>I wonder if I didn&#8217;t have my return statement</p>



<p>and it fell through into the CRLF function</p>



<p>and then the CRLF function returned to the caller</p>



<p>to the caller on behalf of the for test function I don&#8217;t even know only all only</p>



<p>the spirits know at this point I don&#8217;t know leave a comment or something if</p>



<p>you know what happened but every function has to have its own return for</p>



<p>sure so you know the loop done label is usually where you want to jump to when</p>



<p>you know the for loop is finished or the while loop is finished when it comes to</p>



<p>saying goodbye it probably would be a little bit more clear of me to add an</p>



<p>you know for test you know say goodbye just so that I that I remember that this</p>



<p>is this is the place where we&#8217;re done and this other place this is just</p>



<p>something else that&#8217;s happening maybe I&#8217;m doing more instructions or more</p>



<p>operations or calling a function or whatever so obviously if I did it this</p>



<p>way then the loop done label would just end up falling through to the goodbye</p>



<p>label and it would be fine but it&#8217;s just more visually clear I&#8217;m gonna take it</p>



<p>Anyway, so at this point we might have a program that actually works.</p>



<p>Let&#8217;s see if it does.</p>



<p>What else do I need to add?</p>



<p>No, I think we&#8217;re done with that.</p>



<p>Okay, let&#8217;s try it.</p>



<p>So we&#8217;ll do a make run and we&#8217;ll enter the number five and let&#8217;s see if it works or it</p>



<p>crashes.</p>



<p>It worked on the first try.</p>



<p>So I had a solution though.</p>



<p>Not fair, but I could have typoed.</p>



<p>Anyway, so it says we entered a five, the loop will print from zero to N minus one.</p>



<p>print from zero to n minus one so we should see from zero to four and so then every iteration of</p>



<p>the loop it&#8217;s just that little message we&#8217;re just printing a zero printing one printing a two you</p>



<p>know the number increases because we did did that little inc instruction the increase instruction</p>



<p>we unconditionally jump to the top of the loop where we decide if we&#8217;re supposed to uh finish</p>



<p>you know be done with the loop by jumping to the done label eventually after we uh let&#8217;s see</p>



<p>it prints this four here it&#8217;ll do the increase instruction at the bottom of the loop&#8217;s body</p>



<p>and then it&#8217;ll jump to the loop&#8217;s top then the loop&#8217;s top will see that it&#8217;s a five because we</p>



<p>just increased the four it will see that five is definitely not less than five so that&#8217;s a false</p>



<p>which means execution will you know fall through where the heck is that it&#8217;ll fall through to the</p>



<p>line 92 jump instruction which is just the loop being done so that&#8217;s here where it says goodbye</p>



<p>says goodbye and then we have successfully implemented a basic for loop</p>



<p>all right so thank you so much for watching this video I hope you learned</p>



<p>a little bit and had a little bit of fun I will see you in the next video happy</p>



<p>coding and happy studying hey everybody thanks for watching this video again</p>



<p>from the bottom of my heart I really appreciate it I do hope you did learn</p>



<p>hope you did learn something and have some fun. If you could do me a please, a small little favor,</p>



<p>could you please subscribe and follow this channel or these videos or whatever it is you do on the</p>



<p>current social media website that you&#8217;re looking at right now. It would really mean the world to</p>



<p>me and it&#8217;ll help make more videos and grow this community. So we&#8217;ll be able to do more videos,</p>



<p>longer videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please</p>



<p>and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and I just wake up because</p>



<p>I know somebody subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s</p>



<p>exactly what happens every single time. So you could do it as a nice favor to me or you could</p>



<p>you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p>and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also if you look at the middle of the</p>



<p>screen right now you should see a QR code which you can scan in order to go to the website which</p>



<p>at the bottom of this video and it&#8217;ll take you to my main website where you can just kind of like</p>



<p>see all the videos I published and the services and tutorials and things that I offer and all</p>



<p>that good stuff and if you have a suggestion for clarifications or errata or just future videos</p>



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



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



<p>I wake up in a cold sweat and I&#8217;m like,</p>



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



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



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



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



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/">x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</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-for-loops-explained-yasm-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
