<?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-64 linux assembly Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/x86-64-linux-assembly/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/x86-64-linux-assembly/</link>
	<description></description>
	<lastBuildDate>Sun, 15 Feb 2026 03:15:34 +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-64 linux assembly Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/x86-64-linux-assembly/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 15 Feb 2026 03:15:33 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[convert integer to float assembly]]></category>
		<category><![CDATA[cvtsi2sd]]></category>
		<category><![CDATA[double precision assembly]]></category>
		<category><![CDATA[ed jorgensen x86 book]]></category>
		<category><![CDATA[floating point registers]]></category>
		<category><![CDATA[movsd]]></category>
		<category><![CDATA[mulsd]]></category>
		<category><![CDATA[nasm yasm floating point]]></category>
		<category><![CDATA[stack alignment assembly]]></category>
		<category><![CDATA[system v abi xmm]]></category>
		<category><![CDATA[x86 assembly floating point]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=302</guid>

					<description><![CDATA[<p>Learn the basics of floating-point registers (XMM0–XMM15) in x86-64 assembly with YASM. Covers MOVSD, MULSD, CVTSI2SD, ABI rules, why floats return in XMM0 instead of RAX, saving/restoring around calls, and common stack alignment crashes when using printf.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/">x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)</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: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)" width="1380" height="776" src="https://www.youtube.com/embed/0Wcu_7LDw_w?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 but thorough introduction to floating-point registers in x86-64 assembly using YASM.</p>



<p>Learn why XMM0 is special, how to use MOVSD / MULSD / CVTSI2SD, why you must save floats around function calls, and how easy (or sneaky) stack alignment bugs can crash your program.</p>



<p>Live coding + real examples converting integers to doubles and multiplying them.</p>



<p>Great next step after basic integer assembly tutorials.</p>



<p>00:00 Introduction to Floating Point Registers<br>00:28 Why Floating Point Uses Special Registers<br>01:35 Floating Point Return Value in XMM0<br>02:17 XMM Registers Overview XMM0 to XMM15<br>02:48 ABI Rules No Callee-Saved XMM Registers<br>03:16 128-bit XMM Registers Purpose and Size<br>04:00 Ed Jorgensen x86-64 Textbook Reference<br>05:03 Locating XMM Documentation in Textbook<br>05:20 Earthquake &#8211; I am going to die<br>06:24 Chapter 18 Floating Point Instructions<br>07:34 MOVSS vs MOVSD Single vs Double Precision<br>09:11 Understanding SS and SD Instruction Suffixes<br>10:58 MOVSD Example Register to Register<br>11:03 Conversion Instructions CVT Family<br>13:02 Floating Point Arithmetic ADDSD MULSD SUBSD<br>25:48 Program Demo User Input Section<br>26:01 Converting Integer to Double CVTSI2SD<br>26:29 Multiplying by Constant Float MULSD<br>28:56 Saving Result Printing Modified Float<br>31:38 Multiplying User Integer by User Float<br>33:54 Final Result Display Program Summary<br>35:19 Stack Alignment Crash Demonstration<br>36:24 Conclusion Key Takeaways<br>36:52 Outro Subscribe and Thanks</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 we&#8217;re going to talk about pointers and dereferencing in a YASM x8664</p>



<p>assembly program, also as a hybrid program so that assembly and C++ can talk to each other</p>



<p>and send each other pointers and send each other data and things like that.</p>



<p>for what pointers are.</p>



<p>I&#8217;m going to write in C++ for a second.</p>



<p>Suppose you have a pointer for an integer.</p>



<p>We&#8217;ll call it P.</p>



<p>Suppose you have an integer by itself.</p>



<p>We&#8217;ll call it A.</p>



<p>Let&#8217;s say that the value of A is 5.</p>



<p>And if you wanted to say that P points to A,</p>



<p>you could say P equals the address of A.</p>



<p>I&#8217;ll put C++ at the top here.</p>



<p>And so now if I set A to 6</p>



<p>then I print P a dereference of P this is not like a full pointers tutorial</p>



<p>but basically by changing a I&#8217;m changing what P thinks it sees as a value</p>



<p>assuming ID reference it I could also let me do a print 6 here I could also</p>



<p>just change the value through P I could say dereference P and I could say equals</p>



<p>would actually print a seven right so you know you can have regular variables global variables</p>



<p>whatever kind of you know memory stuff on the stack and to get a pointer to it you really just</p>



<p>need to get its memory location in c++ it&#8217;s kind of easy syntactically you can see what&#8217;s happening</p>



<p>in assembly you really just need the memory location stored somewhere you could store that</p>



<p>variable that just simply stored the memory location of some other variable.</p>



<p>You could have a 64-bit register store the value of a variable.</p>



<p>Let&#8217;s say we have like a, I don&#8217;t know, my whatever, my number let&#8217;s say inside of assembly.</p>



<p>I&#8217;ll do ASM here and we say it&#8217;s a quad word and it starts off as this number or whatever.</p>



<p>So if you haven&#8217;t seen my previous videos, go see them for the basics of assembly and</p>



<p>of assembly and linking and make files and all that stuff but you know if you</p>



<p>have an assembly program and you have a data section and you define a global</p>



<p>variable like this what you&#8217;re basically saying is I want to take this giant</p>



<p>number and I want to write it into eight bytes that&#8217;s the DQ it says data quad</p>



<p>word I want to write that giant number across eight bytes and then I want to</p>



<p>get a pointer to it stored in the my number symbol so my number is not</p>



<p>actually the value it&#8217;s a pointer to the value so you know later if you want to</p>



<p>you know later if you want to move you know something into a register if you did this</p>



<p>that would move the pointer into rax but if you did this</p>



<p>with deref symbols after it or around it then you would move</p>



<p>maybe i&#8217;ll put that into rex you&#8217;d move that actual number that we specified into rex</p>



<p>into Rx. It&#8217;s important to understand also that pointers are integers even when we&#8217;re pointing to</p>



<p>doubles. So for example sometimes people make this mistake they&#8217;ll say you know my double</p>



<p>and they&#8217;ll say it&#8217;s a quad word meaning this is going to be a 64-bit double precision floating</p>



<p>point number and they&#8217;ll do like 44.55 or whatever. So that is a double and it is in memory</p>



<p>you know what is the symbol of my double remember it&#8217;s supposed to be just a</p>



<p>pointer right it can&#8217;t be an actual double because a memory location is not</p>



<p>a double a memory location is an integer so that means if you wanted to move a</p>



<p>pointer into a register you would only be able to move the pointer into a</p>



<p>regular general purpose register not a floating point register and you should</p>



<p>use the regular movement instructions for just regular general purpose</p>



<p>So keep that in mind if you see a signature like this like let&#8217;s say function F and we have</p>



<p>You know, let&#8217;s say long a and long B and actually let&#8217;s do pointers</p>



<p>Let&#8217;s say long pointer a and long pointer</p>



<p>B and double pointer C all three of those arguments are actually 64 bit integers</p>



<p>Because they&#8217;re all pointers even if one of the pointers points to adult a double</p>



<p>double why did I say dull pointers aren&#8217;t dull they&#8217;re exciting okay so I&#8217;m gonna open up some</p>



<p>code here real fast so usually I don&#8217;t explain my uh my driver I&#8217;m gonna explain it to you this time</p>



<p>because it&#8217;s kind of doing a little bit more than my other videos um again if you don&#8217;t have uh the</p>



<p>knowledge of how to make a make file see my other videos because that&#8217;s explained there for now I&#8217;m</p>



<p>what we really need to do is write a driver and an assembly module for a</p>



<p>hybrid program again hybrid programs covered in other videos so the driver is</p>



<p>pretty easy I&#8217;m just going to copy paste it honestly here and then just kind of</p>



<p>explain it to you the driver is pretty easy we&#8217;re going to do I O stream so we</p>



<p>can print stuff we&#8217;re going to mark an external function called point as extern</p>



<p>C so that just disables name mangling which means the C++ module will be able</p>



<p>will be able to call on this function called point and it won&#8217;t expect that</p>



<p>the point function has its name mangled like C++ does the reason being is that</p>



<p>point is actually going to be in a side it&#8217;s going to be inside assembly where</p>



<p>its name will not be mangled this disables the ability to overload but</p>



<p>that&#8217;s okay we don&#8217;t care it&#8217;s going to take two pointers a pointer to a character</p>



<p>and a pointer to a long since both of those are pointers they&#8217;re both</p>



<p>64-bit integers even the character pointer and then we have a function that is internal to this</p>



<p>module called hey driver print this remember we&#8217;re inside of the driver program right now</p>



<p>so if you look at the bottom it&#8217;s just a function that takes in some pointers</p>



<p>and then prints some stuff so it&#8217;s going to print like it&#8217;s going to print what the string is</p>



<p>it&#8217;s going to print what the long is my dog&#8217;s growling at me i&#8217;m going to ignore him because</p>



<p>i literally just let him pee and poop at this point now he&#8217;s harassing me for treats</p>



<p>now he&#8217;s harassing me for treats he always does this okay so uh the string the long the double</p>



<p>this function expects to receive three pointers to different data types it&#8217;s just going to print</p>



<p>all of them and the point get it the point of this function is we&#8217;re going to go inside of</p>



<p>the assembly module and then have the assembly module call on this function so that we can we</p>



<p>can prove that we can have stuff sent from assembly to c plus plus or c using pointers</p>



<p>using pointers we can have data sent over so anyway that&#8217;s why both of these</p>



<p>are in here the point needs to be marked as no name mangling because point is</p>



<p>inside of assembly which will not name mangle and then hey driver print this</p>



<p>that needs to have name mangling disabled also so that the assembly</p>



<p>module can call on this other than that we&#8217;re just basically inside of a main</p>



<p>saying hey this is the c string we&#8217;re making a c string inside of the main function notice how</p>



<p>this is a local variable so that c string is going to show up on the stack it&#8217;s going to show up in</p>



<p>the area that is owned by main for main stack area same thing for my long that&#8217;s a local variable on</p>



<p>the stack um and but then we can actually send pointers to those pieces of data to another</p>



<p>function in another module you don&#8217;t have to only transport globals or stuff on the heap</p>



<p>or stuff on the heap, you can transport pointers to local variables. Just make sure that by the</p>



<p>time this function finishes, then nowhere else is actually using that data because,</p>



<p>well, being on the stack, once main function or once any function finishes, then its portion of</p>



<p>the stack will be cleaned up and removed and it&#8217;ll be junk data. You&#8217;ll probably get a seg fault.</p>



<p>But for now, we&#8217;re not going to use anything on the stack. We&#8217;re not going to use these local</p>



<p>just going to use them quickly on this call to point and then we&#8217;re going to return to the</p>



<p>operating system and finish the program. So that&#8217;s the driver. Now the hard part. Let&#8217;s do this in</p>



<p>assembly. So for starters, I&#8217;m going to make a data section and just explain it to you very,</p>



<p>very quickly. Again, if you don&#8217;t understand the basics of YASM x86-64 assembly, did I mention</p>



<p>that that&#8217;s what this language is at the beginning of the video? I guess I should put that in the</p>



<p>put that in the description or record an announcement that I can tack on at the beginning</p>



<p>or something. Anyway, so if you don&#8217;t understand how to do this, see my other videos, but basically</p>



<p>we&#8217;re going to make a data section. We&#8217;re going to define some strings. Here&#8217;s like an announcement.</p>



<p>Oh, we&#8217;re inside of, you know, the module now, the assembly module. And now we&#8217;re going to print</p>



<p>the received string. And then we&#8217;re going to make a string that is owned by assembly, which we can</p>



<p>into C++ when we call the function inside of the driver.</p>



<p>So this string is owned by the assembly module.</p>



<p>Notice how these are null terminated strings.</p>



<p>I just have like a comma zero there,</p>



<p>which means I have some extra functions</p>



<p>I&#8217;m gonna paste in that we&#8217;re not really gonna talk about</p>



<p>because they&#8217;ve been discussed in other videos</p>



<p>just so that we can print null terminated strings.</p>



<p>Then I&#8217;ve got a new line here,</p>



<p>you know, carriage return line feed.</p>



<p>And then I&#8217;ve just got some numbers</p>



<p>that are owned by the assembly module.</p>



<p>Then I&#8217;ve got a system write call,</p>



<p>call code one for the system call writes and file descriptor standard output so I</p>



<p>can print just to the terminal again if you don&#8217;t understand this see my other</p>



<p>videos so now let&#8217;s start the actual text section so this is where our</p>



<p>instructions start so we got the text section here and we&#8217;re going to use some</p>



<p>external symbols don&#8217;t worry about these I&#8217;m just using my own little library to</p>



<p>and input integers if you have access to this library use it if you don&#8217;t if you&#8217;re watching</p>



<p>at home and you don&#8217;t have this library then that&#8217;s fine you can use you know printf or</p>



<p>scanf or something like that to get and print floats from and to the user</p>



<p>but yeah I&#8217;m just using that and then I&#8217;m marking an external function here called hey driver print</p>



<p>this if you recall the driver module has a function called hey driver print this so</p>



<p>just allows my assembly code to call on that external function. Okay now next</p>



<p>piece of code. This is going to be… actually I&#8217;m going to paste the print</p>



<p>null terminated string function and related code because it&#8217;s just like a</p>



<p>big giant mess and we&#8217;re mostly going to ignore it. So just to show you what I&#8217;m</p>



<p>doing here I have a function called print null terminated string so that I</p>



<p>can print these strings up here and then I have it rely on a function called</p>



<p>string length that I have implemented up here and all it does is just</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/">x86-64 Assembly: Floating Point Registers Basics with YASM (MOVSD, MULSD, CVTSI2SD)</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-floating-point-registers-basics-with-yasm-movsd-mulsd-cvtsi2sd/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)</title>
		<link>https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/</link>
					<comments>https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 08 Feb 2026 01:59:27 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi register preservation]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly length function]]></category>
		<category><![CDATA[assembly string handling]]></category>
		<category><![CDATA[assembly while loop]]></category>
		<category><![CDATA[c strings assembly]]></category>
		<category><![CDATA[callee saved registers]]></category>
		<category><![CDATA[learn assembly 2025]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[null terminated string]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[strlen assembly]]></category>
		<category><![CDATA[sys_write tutorial]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[write syscall assembly]]></category>
		<category><![CDATA[x86 assembly linux]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=298</guid>

					<description><![CDATA[<p>Learn to implement strlen from scratch in x86-64 assembly using YASM. We walk through building a null-terminated string length function with a while loop, proper register preservation, and ABI compliance, then use the length to print the string efficiently via sys_write.</p>
<p>The post <a href="https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/">Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)</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="Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)" width="1380" height="776" src="https://www.youtube.com/embed/KQaIEBP6Qp4?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>Learn how to write your own strlen function in x86-64 assembly (YASM) that finds the length of a null-terminated string using a simple while loop.</p>



<p>We preserve the proper registers, follow the ABI, compute the length safely, and then use that length to print the full string efficiently with a single sys_write call.</p>



<p>Great for anyone studying low-level programming, operating systems, or wanting to understand C strings at the assembly level.</p>



<p>00:00:00 Introduction to implementing string length in assembly<br>00:00:25 What are null-terminated strings and why they exist<br>00:01:59 Pre-computing length vs using null terminators<br>00:02:53 How the null byte (0) actually works in memory<br>00:04:14 Naive approach: printing one character at a time<br>00:05:20 Goal: efficient printing using computed length<br>00:06:00 Program structure overview – two main functions<br>00:06:32 Data section: defining null-terminated strings<br>00:08:19 Additional strings for output (prefix, CRLF)<br>00:09:15 Text section start and global looper function<br>00:10:44 Preserving callee-saved registers (ABI prologue)<br>00:11:28 Calling print_null_terminated_string<br>00:12:43 Simple crlf printing helper function<br>00:13:10 print_null_terminated_string function signature<br>00:14:31 Prologue for print_null_terminated_string<br>00:15:44 Saving arguments and calling strlen<br>00:17:12 Using sys_write with computed length<br>00:18:19 string_length (strlen) function begins<br>00:19:20 Prologue and fake return value testing<br>00:20:44 Planning the while loop in C-like pseudocode<br>00:21:33 While loop initialization (pointer and counter)<br>00:24:23 Loop top: check for null terminator<br>00:26:23 Loop body: increment pointer and counter<br>00:27:37 Done label and return length in RAX<br>00:28:29 First successful run – full string printed<br>00:29:30 Adding direct strlen call and length printing<br>00:31:02 Final run showing both string and its length (54)<br>00:31:53 Summary – benefits of computed length printing<br>00:32:59 Improving loop structure (better jump pattern)<br>00:34:07 Final improved loop verification<br>00:35:03 Closing thoughts and thanks<br>00:35:27 Outro, call to subscribe, website mention</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 show you how to implement the function string length.</p>



<p>So you can find the length of a null terminated string in a Yasm x86-64 assembly program.</p>



<p>Although if you&#8217;re using a different assembly language or different architecture, this video</p>



<p>will probably still be useful to you because the concepts are going to be the same.</p>



<p>So what am I talking about?</p>



<p>I&#8217;m not going to be around here with this.</p>



<p>So in a previous video, I discussed null terminated strings.</p>



<p>I should also point out that a lot of basic knowledge is going to be skipped in this video</p>



<p>because I&#8217;ve explained it in other videos.</p>



<p>For example, if you don&#8217;t know how to compile, link, assemble, write a basic assembly program,</p>



<p>write a make file and so forth, then you should see my other videos first.</p>



<p>I&#8217;ve also already published a video about null terminated strings, but I&#8217;ll just do</p>



<p>it again here since that&#8217;s in the title of the video.</p>



<p>of the video so imagine you have a string and it&#8217;s hello right so under the hood uh the string is</p>



<p>probably a collection of characters on some level so we&#8217;ll just say this is h e uh l</p>



<p>l o right um when you&#8217;re printing</p>



<p>it&#8217;s unlikely that your entire memory stick is just done like it just you&#8217;re at the very end of your memory by the time that O hits</p>



<p>So that means you need some way of understanding when the string ends because if the memory is not over at that point</p>



<p>There could probably be some junk data at the end of it</p>



<p>You know like a bunch of other random letters or you can even imagine these as just you know</p>



<p>One byte that&#8217;s not one byte one byte numbers that just go on and on and on forever for the entirety of your RAM stick</p>



<p>of your RAM stick and you have to know how do we actually stop at the O. One thing that you can do</p>



<p>is just pre-compute the length of the string so we do that in assembly a lot before we know how</p>



<p>to scan for null terminators. We&#8217;ll say all right well that string is just five long so I&#8217;ll tell</p>



<p>the system I want you to print five characters starting at that memory location wherever the H is</p>



<p>and then the system knows okay I&#8217;ll just you know print the H-E-L-L-O and just stop after that.</p>



<p>null terminated strings are a lot more convenient because you don&#8217;t have to pre-compute the strings.</p>



<p>I mean, maybe your user entered a string.</p>



<p>Maybe you have a lot of strings or they change quite often.</p>



<p>Maybe you have like a multinational program that has tons of translations,</p>



<p>or I think multilingual is probably the better word for that.</p>



<p>But it can be a pain in the butt to constantly compute the length of strings in advance.</p>



<p>So with a null terminated string, you basically just say,</p>



<p>that I want to print and I&#8217;m just going to stick actually the number zero at the end of the string.</p>



<p>I&#8217;ll leave the junk data there just to let you know that there is some stuff happening in memory.</p>



<p>Notice how this zero, it is not actually the character that looks like a zero to a human.</p>



<p>That&#8217;s actually a totally different code than just zero. So you can imagine just an actual zero here.</p>



<p>You know, each of these characters that a human would look at has a number underneath it.</p>



<p>You know, this H is not really an H.</p>



<p>It&#8217;s just some number between 0 and 255 if we&#8217;re talking about ASCII.</p>



<p>The E is a different number and so forth.</p>



<p>So if we just put the literal number 0 in our data,</p>



<p>or if you want to quote this inside of a single quote,</p>



<p>you can do, I think, slash 0 just to let the compiler know</p>



<p>that you intend to have the number 0 there</p>



<p>instead of something that looks like the number 0, you know, the character.</p>



<p>you know the character but anyways the point is we just have to stick a zero at the end</p>



<p>of the string we call it a null terminator because zero is also you know an alias for null</p>



<p>whenever you have a null pointer or you assign null to a memory location or a pointer or something</p>



<p>you know it&#8217;s zero basically under the hood so a zero will terminate it&#8217;ll be like a token to let</p>



<p>us know that the string is finished and so since zero is also considered null we&#8217;ll say it&#8217;s a</p>



<p>we&#8217;ll say it&#8217;s a null terminator.</p>



<p>It&#8217;s a basic idea for null terminators.</p>



<p>Now the question is, how do we actually know when to stop?</p>



<p>Well, the first thing that you could do if you&#8217;re trying to write a program that is highly inefficient,</p>



<p>which I&#8217;ve definitely done before, is you could just print one character at a time.</p>



<p>You use a for loop.</p>



<p>You start at the very beginning of your string, you know, a pointer,</p>



<p>whatever the user gave you as like this is the first character.</p>



<p>We&#8217;ll just print that letter, and then we&#8217;ll go on to the next letter.</p>



<p>the next letter and before we print it actually before we print the first letter even before we</p>



<p>print this letter we&#8217;ll uh we&#8217;ll say is this like a regular character or is this a null terminator</p>



<p>is this a zero if it&#8217;s not a zero we print that character if it is a zero we terminate the loop</p>



<p>and then we go through every character one by one just you know checking and printing checking and</p>



<p>printing checking and printing unfortunately that&#8217;s kind of inefficient because every time</p>



<p>you call a print you know you&#8217;re you&#8217;re calling on a function you&#8217;re asking the system to do some</p>



<p>for you and it would be a lot better if we could just flush the whole string at</p>



<p>the same time but but know how long the string was that would increase our</p>



<p>efficiency so the program that we&#8217;re going to write together is basically</p>



<p>going to use our knowledge of a while loop which I&#8217;ve explained in other</p>



<p>videos already so see those other videos if you don&#8217;t know how to do while loops</p>



<p>in Yasm we&#8217;re going to use our knowledge of a while loop to sort of scan the</p>



<p>string real fast just you know kind of scan it and figure out how far into the</p>



<p>far into the string until we see a null terminator and use that to determine what is the length of</p>



<p>the string. At that point, we can use a system call in YASM, in assembly, to just say, I want you to</p>



<p>print this sequence of characters and here&#8217;s the length and then let the system worry about</p>



<p>efficiency. So with that said, let&#8217;s look at some code. Okay, it&#8217;s just going to be a simple while</p>



<p>loop. What we&#8217;re going to need to do is break this up into two parts. The first part is going to be</p>



<p>the first part is going to be a function called string length which you&#8217;ve probably already seen</p>



<p>in c if you program in c or c plus plus the second function is going to be called print null terminated</p>



<p>string which will just ask string length what the length of the string is first and then actually</p>



<p>print it with the system call so let me uh i guess let me start off with my data section here</p>



<p>to print I&#8217;m gonna copy paste that for my solution again this is not a not an</p>



<p>assembly basics video so if you don&#8217;t understand what I&#8217;m doing you should</p>



<p>watch my other videos first I&#8217;m assuming you know how to make a data section by</p>



<p>now we&#8217;ll put some C strings I&#8217;m just gonna make one null terminated string</p>



<p>actually I guess I&#8217;m making two but the focus of this program is just the first</p>



<p>one I&#8217;m calling it null terminated string and in assembly it&#8217;s pretty easy</p>



<p>you just make it a you know a character array just like a sequence of bytes with</p>



<p>a sequence of bytes with this DB meaning data bytes.</p>



<p>And I can just put a quoted string like this.</p>



<p>No problem.</p>



<p>As many characters as I want.</p>



<p>I can start injecting specific ASCII values if I wanted to</p>



<p>or byte values if I wanted to just by putting a comma</p>



<p>and then a number.</p>



<p>So I could do something like this.</p>



<p>I could do like, you know, 47, you know, 49, you know, 50, whatever.</p>



<p>If I knew the ASCII codes for the characters,</p>



<p>fortunately, I don&#8217;t need to.</p>



<p>normally into the double quoted area but then i need to be able to put a null terminator at the</p>



<p>end of my string because it&#8217;s not going to happen automatically so then i am going to do comma zero</p>



<p>and you&#8217;ll end up with something like this like if i guess if we look at the previous example real</p>



<p>fast i&#8217;ll call this a hello string just so that you see some similarity from what we just looked</p>



<p>a notepad thing would just be typing the word hello and then putting comma zero.</p>



<p>So it is now a null terminated string and it looks just like this inside of system memory.</p>



<p>Well, not just like that.</p>



<p>There would be numbers where the letters are, but you know, that&#8217;s basically what we have created.</p>



<p>And then of course there&#8217;s junk data afterwards, but we don&#8217;t really care about that.</p>



<p>You know, we&#8217;re just going to ignore it with the null terminator.</p>



<p>So I&#8217;m going to erase that since we&#8217;re not just going to print the word hello.</p>



<p>We have a null terminated string here and then after we print the null terminated string</p>



<p>I&#8217;m just going to print out what was the length of the string.</p>



<p>So this is a prefix string where it&#8217;s just, you know, it&#8217;s a prettier program.</p>



<p>The program is going to say the null terminated string&#8217;s length was something.</p>



<p>And then we&#8217;re going to use the null terminated string printer to print that also.</p>



<p>Convenient, right?</p>



<p>And then I&#8217;m going to actually print the number.</p>



<p>Then we have this down here, crlf, which is just printing a new line in the terminal.</p>



<p>That&#8217;s character code 13 and then 10 and then a null terminator so that we can use the null terminated string printer again.</p>



<p>And then we&#8217;re going to use system call code 1 to print a standard output right here.</p>



<p>If you don&#8217;t understand that, then see my other videos.</p>



<p>But let&#8217;s move on to the text section where all our instructions will go.</p>



<p>Okay, so now the instructions begin in our text section right here.</p>



<p>section.text and I&#8217;m using an external symbol this video is not about this</p>



<p>library here but basically I have a library that will help me print integers</p>



<p>you don&#8217;t need to worry about that you could imagine well I guess in your</p>



<p>example when you&#8217;re practicing if you don&#8217;t have this library you could just</p>



<p>not print the length of the string and just use it only and it all should still</p>



<p>work or you could hard code the thing that you&#8217;re printing if you really</p>



<p>wanted to. Okay, so I&#8217;m just going to continue on here. Now let&#8217;s do our entry point. So again,</p>



<p>this is not a video about hybrid programs. Just assume that there is another module in my program.</p>



<p>It&#8217;s a C++ module. It&#8217;s got the main function, you know, for the entry point for a hybrid program,</p>



<p>and it&#8217;ll just call on my looper function. So that&#8217;s why I&#8217;m marking a looper as global.</p>



<p>So my other module can call it. And well, it is a function that needs to return. So I&#8217;m going to</p>



<p>to return so i&#8217;m going to put ret at the end of it and you can see here i left myself a note saying</p>



<p>i&#8217;m going to use r12 to remember the length of the string so that i can print it back to the user</p>



<p>so that means i have to preserve r12 for the caller because the abi or the application binary</p>



<p>interface says that r12 is a callie saved register and if you don&#8217;t respect the abi</p>



<p>the abi is not going to respect you your program is going to end up crashing eventually</p>



<p>So I&#8217;m just going to do a push pop pair to preserve R12.</p>



<p>Oops, prologue and call that epilogue.</p>



<p>Okay. So we got a push pop pair. We got a return statement.</p>



<p>This program should probably do nothing so far. So let&#8217;s run it and see,</p>



<p>just make sure that it at least compiles.</p>



<p>So I&#8217;m going to say clear and make run running the program.</p>



<p>Hello from the driver. You don&#8217;t know that the driver has that.</p>



<p>that the driver has that. This is not a driver video. And then the driver regains control because</p>



<p>nothing happened inside of the assembly module. We just basically looper got called and then we</p>



<p>preserved R12 and then restored it and then we did nothing. Okay, so now let&#8217;s make a call to</p>



<p>print null terminated string. We have to make another function for this, but right now this is</p>



<p>just the call. So the name of the function that we&#8217;re going to write is called print null terminated</p>



<p>it it will call on the string length function to figure out how long the string is then it will use</p>



<p>a simple system call to print the whole string giving the length to the system call it also takes</p>



<p>two arguments the first argument is a pointer to the null terminated string so that&#8217;s just that</p>



<p>symbol we defined up above remember when you define variables up in the data section then</p>



<p>these symbols tend to be pointers so that symbol is a pointer to the h basically or just the memory</p>



<p>that h is sitting in ram then the second argument that it wants is uh is where we&#8217;re going to print</p>



<p>it so we&#8217;re just going to print it to standard output um which is just file descriptor number one</p>



<p>so again if you don&#8217;t understand arguments or you know file descriptors or function calls</p>



<p>see my other videos because i&#8217;ve explained those already anyway so we&#8217;re going to call</p>



<p>print null terminated string then we&#8217;re going to call on crlf which will just print a new line</p>



<p>So now maybe we should implement, well, let&#8217;s copy paste crlf so that I can implement the</p>



<p>other function a little bit more slowly.</p>



<p>What does crlf do?</p>



<p>It literally just asks the print null terminated string function to just print a crlf for us.</p>



<p>So it&#8217;s very, very simple.</p>



<p>Here&#8217;s the signature.</p>



<p>Nothing much to it.</p>



<p>Okay.</p>



<p>Now, a little bit more complicated is the print null terminated string function.</p>



<p>So in our looper, we&#8217;re going to print the null terminated string.</p>



<p>We have to have a function that actually does that.</p>



<p>So that&#8217;s going to be this one right here.</p>



<p>Here&#8217;s the signature that I&#8217;ve chosen for my print null terminated string function.</p>



<p>Basically, I want to receive a character pointer to the first character in the string that we&#8217;re going to print.</p>



<p>And then a file handle designating where we&#8217;re going to print it.</p>



<p>The reason I want to receive the file handle is so I could print a standard output or standard error.</p>



<p>or standard error, or I could print to a file,</p>



<p>like whatever I want to do.</p>



<p>You don&#8217;t have to have that in there, but it&#8217;s nice.</p>



<p>Anyway, so we have this function set up.</p>



<p>Notice how my notes that I left for myself</p>



<p>is that I&#8217;m gonna use R12</p>



<p>to remember the incoming C string pointer argument,</p>



<p>and I&#8217;m gonna use R13 to remember the file handle.</p>



<p>Remember, it&#8217;s probably not a good idea</p>



<p>to just let the incoming arguments</p>



<p>stay in their original registers,</p>



<p>original registers because those registers tend to get overwritten as you do system calls or</p>



<p>calls to any other function. So I&#8217;m just going to grab them real fast into R12 and R13. And then R14</p>



<p>is the string&#8217;s length, which I&#8217;m going to compute with a call to the function called string length.</p>



<p>So just three things to remember. And that&#8217;s it. So that means I&#8217;m going to have to preserve those</p>



<p>Okay, so we&#8217;re going to do a prologue to preserve those registers.</p>



<p>And then at the very end, we&#8217;re going to do an epilogue where we restore those registers.</p>



<p>Oh, I think I already overwrote my return statement from the previous function.</p>



<p>I think I did that in the last video and I was a little confused as to what was wrong.</p>



<p>So make sure you don&#8217;t accidentally overwrite or push down your return instructions.</p>



<p>Let me just double check here.</p>



<p>Looper&#8217;s got return.</p>



<p>Print and alternated string has got a return.</p>



<p>string has got a return.</p>



<p>CRLF has a return.</p>



<p>What the heck did I do?</p>



<p>Oh, I think I copy pasted in a bizarre place.</p>



<p>That&#8217;s probably what happened because the epilog for for print null terminated</p>



<p>string is like down in CRLF already.</p>



<p>That&#8217;s not good.</p>



<p>Okay, that would have been a crashing program.</p>



<p>Although sometimes if you omit the return statements, execution will just fall</p>



<p>through down to the next label and maybe your program will survive accidentally.</p>



<p>accidentally but for now it&#8217;s just crlf is supposed to be very simple it doesn&#8217;t preserve</p>



<p>any registers so we&#8217;ve got a prologue and an epilogue here notice how the push and pops are</p>



<p>in reverse order you want to know more about that see my other videos but now that we are preserving</p>



<p>the appropriate registers we can actually grab our incoming arguments so first thing i&#8217;m going to do</p>



<p>is i&#8217;m going to say r12 is going to be the first argument that i received and then r13 is going to</p>



<p>okay no problem then let&#8217;s rely on the string length function to compute the actual length of</p>



<p>the string i didn&#8217;t feel like having print null terminated string compute the length of the</p>



<p>string it&#8217;s a good idea especially in assembly or any language when you have multiple distinct</p>



<p>jobs happening within the same function you probably want to break that function up into</p>



<p>multiple functions just to reduce you know strain on your brain right cognitive load</p>



<p>So I&#8217;m going to use this function strlen string length to compute the length of the string.</p>



<p>It&#8217;s only going to take one argument and it&#8217;s going to take the pointer to the null terminated</p>



<p>string which is now in R12. It&#8217;s going to take that as its first argument so that&#8217;s why I&#8217;m loading</p>



<p>that up into RDI. When string length returns it&#8217;s going to give me the length of the string in the</p>



<p>RAX register which is the usual return register for integer or pointer return types. So I&#8217;m just</p>



<p>So I&#8217;m just going to save that in R14.</p>



<p>And that&#8217;s the usage of all those registers R12, 13, and 14.</p>



<p>We still have to implement string length.</p>



<p>Don&#8217;t worry.</p>



<p>Although if you were linking a hybrid program, you could probably just call</p>



<p>STRLEN in the C libraries and be fine.</p>



<p>But this is an assembly video.</p>



<p>We want to do everything in assembly if we can, or at least more of it.</p>



<p>So then finally, when we know what the strings length is, we can just use a</p>



<p>system call to actually print the string we&#8217;re going to say load up call code one to say you</p>



<p>know mr. system I want you to print a string and then r13 is going to be the file handle so we&#8217;re</p>



<p>going to basically say wherever the caller of print null terminated string said to print which</p>



<p>is probably going to be standard output we&#8217;ll just tell the system we want to print to the same place</p>



<p>and then r12 is a pointer to the c string so we just give that to the system call as well</p>



<p>system call wants to know how long the string is that&#8217;s r14 now now that we have used strlen</p>



<p>to determine the length of the string so not really that complicated of a function we just</p>



<p>kind of like grab some arguments preserve those registers and we ask another function to compute</p>



<p>the length of the string and then we actually just print it once we have the length this is still not</p>



<p>getting to the point where we&#8217;re going to use our while loop knowledge to compute the length so i</p>



<p>That&#8217;s probably all I need right now.</p>



<p>And I think we&#8217;re ready to use or to start the string length function.</p>



<p>Okay, so now let&#8217;s make another function called string length.</p>



<p>Hopefully I&#8217;ll paste in the right spot this time.</p>



<p>You&#8217;re cringing at home.</p>



<p>That just tells me that you care.</p>



<p>So the string length function, at least the version that I&#8217;m making right now,</p>



<p>just is going to take one argument.</p>



<p>It&#8217;s going to be a character pointer to the string that you want to compute.</p>



<p>It will expect that the string has a null terminator at the end.</p>



<p>the end if you accidentally didn&#8217;t put a null terminator at the end of the string then this</p>



<p>function definitely won&#8217;t work it&#8217;ll probably give you some huge number because it&#8217;ll go through ram</p>



<p>until it accidentally finds a zero um and then it&#8217;s going to return to you as its return value</p>



<p>and uh assigned a 64-bit integer actually this should be unsigned but i&#8217;m just putting long for</p>



<p>now um to indicate the length of the string okay inside the notes we&#8217;re going to use r12 and r13</p>



<p>So that means I should probably preserve those registers first before I do anything else.</p>



<p>So in the prolog, we&#8217;re going to push R12 and R13 so that we don&#8217;t break this program</p>



<p>for others.</p>



<p>And then we&#8217;re going to do an epilog.</p>



<p>Whoops.</p>



<p>Then we&#8217;re going to do an epilog to restore the registers.</p>



<p>And this is a function.</p>



<p>So it&#8217;s got to return to the caller.</p>



<p>If I didn&#8217;t put a return statement here, then execution is going to just go all the way</p>



<p>down to CRLF.</p>



<p>And this will be an infinite loop.</p>



<p>and this will be an infinite loop because crlf will end up calling null terminated string,</p>



<p>which we&#8217;ll then call string length, which will then fall through to crlf,</p>



<p>so the whole program won&#8217;t even work if we don&#8217;t have return.</p>



<p>And, you know, you don&#8217;t want to omit return statements anyways,</p>



<p>because that&#8217;s always a bad idea.</p>



<p>So now string length will just not do anything right now.</p>



<p>Maybe we could return a fake value for a second before we start implementing the loop.</p>



<p>the number five into RAX so that string length will always trick the caller into thinking that</p>



<p>the length of the string is five let&#8217;s see if that actually works we should get a portion</p>



<p>of the null terminated string unless I screwed something up</p>



<p>hello from the main driver notice how it just says hello here that&#8217;s kind of confusing let&#8217;s</p>



<p>let&#8217;s hard code the five to like a nine we should see more of that null terminated string</p>



<p>I sound when I wake up sometimes hello okay so let&#8217;s finish the str len function so again you</p>



<p>should know how while loops work if you don&#8217;t see my other videos but we&#8217;re going to use a while</p>



<p>loop to count the length of the string so we&#8217;re going to start with a little portion up here</p>



<p>think the string is and a running pointer so rdi is already supposed to come in as a pointer to the</p>



<p>string that we&#8217;re measuring so i&#8217;m going to save um the pointer into r12 so that we can have a</p>



<p>pointer that points to a character we&#8217;re going to use this as a running pointer so it&#8217;s going to like</p>



<p>sweep through the whole entire string until it hits a null terminator and then r13 is going to</p>



<p>keep track of uh how big we think the string is so when we first start we&#8217;re just looking at the</p>



<p>first start we&#8217;re just looking at the first letter and then we think the string has zero length.</p>



<p>So that&#8217;s the initialization part which will not be repeated as we continue looping. Now we&#8217;re</p>



<p>going to implement the top of the loop. I don&#8217;t know should I should I write this out as c code</p>



<p>for you? I don&#8217;t know if I should maybe let me do it. I didn&#8217;t prepare this so if it&#8217;s slow sorry</p>



<p>Maybe this is like a long strln, something like that.</p>



<p>And then we&#8217;ll do if my code is wrong or doesn&#8217;t compile, I&#8217;m so sorry.</p>



<p>I did not, I did not prepare this.</p>



<p>We&#8217;ll say character pointer s and then we&#8217;ll say, uh, maybe we can actually just leave</p>



<p>s alone because it&#8217;s coming in as an argument and in C plus plus you can just continue to</p>



<p>use that symbol.</p>



<p>It&#8217;s not going to get destroyed.</p>



<p>So imagine we&#8217;ve saved it already into R 12 and then we just keep using it.</p>



<p>using it so we&#8217;ll say while a let&#8217;s say a dereferencing of s is not equal to zero meaning</p>



<p>if we look at the value that the pointer is currently pointing to if we assume it&#8217;s just</p>



<p>pointing to one byte is we&#8217;ll keep going as long as that value is not a zero so that means</p>



<p>if the user called this function and gave us a pointer that was already looking at a zero</p>



<p>we would just return whoops we would just return that the length was zero so</p>



<p>that means I should probably keep track of the length here size type actually</p>



<p>long just to just to match the return signature long we&#8217;ll put size equals zero</p>



<p>and then at the very end we&#8217;ll just return the size and so again if the user</p>



<p>gave us a pointer that pointed to a zero already nothing would happen inside the</p>



<p>while loop we&#8217;d break through it right away and we would just return the number</p>



<p>the number zero that makes sense so then as long as it is not pointing at a zero</p>



<p>we&#8217;ll just increase what we think the size is and then we will increase the</p>



<p>pointer we can use s plus plus in C++ that&#8217;s just pointer arithmetic that&#8217;s</p>



<p>just going to tell the pointer to advance you know one memory location</p>



<p>further or whatever the data type is but in this case the data type is a</p>



<p>character so it really is going to be one memory location one byte so we&#8217;re</p>



<p>going to sweep through the string until we see a zero and then we stop and every time we see a</p>



<p>character that&#8217;s not a zero we increase our our measured length of the string by one and then</p>



<p>advance the pointer. So I haven&#8217;t tested this I don&#8217;t know if there&#8217;s an error in it but I hope</p>



<p>you get the basic idea of what we&#8217;re going to do. So that means up here you know this is the</p>



<p>initialization part that we were just talking about so we just set the running pointer to look</p>



<p>okay so then after we do that we are going to make the top of the while loop</p>



<p>so at the top of the while loop where we evaluate you know like right here this</p>



<p>is the top of the while loop it has to have its own label just like we explained</p>



<p>in the other videos and it is basically where we decide if we&#8217;re going to keep</p>



<p>looping or not are we going to jump into the body the loop or are we going to do</p>



<p>a long jump after the body to say that we&#8217;re done so the top of the loop is a</p>



<p>label. We compare the value that R12 is currently pointing at. We say that we only want to look at</p>



<p>one byte. We dereference R12 because remember R12 is supposed to be a pointer. You put the</p>



<p>brackets around it, it&#8217;s going to go to the memory location and then check what the value is that</p>



<p>the pointer is pointing to. That&#8217;s what dereferencing is, right? So we&#8217;re just going to</p>



<p>compare the byte that we&#8217;re looking at with a zero and we&#8217;ll say if it is equal to a zero,</p>



<p>jump to the done this is actually kind of a poor design pattern on my part usually we should jump</p>



<p>if it&#8217;s not equal into the body meaning we&#8217;ll always take a short jump into the body and then</p>



<p>execution will fall through on the next line to a long jump which has the ability to jump further</p>



<p>out of the body i&#8217;ve said in other videos that the conditional branch instructions they can only jump</p>



<p>about 128 bytes so if your if your loop body is too big then they won&#8217;t work but it&#8217;ll work for</p>



<p>But it&#8217;ll work for this example.</p>



<p>I don&#8217;t know, maybe if I have the gumption, I will fix up the loop for you if you want</p>



<p>me to after I copy paste my existing solution.</p>



<p>So for now we&#8217;re going to say, all right, I&#8217;m not going to do it.</p>



<p>I&#8217;m not going to do that.</p>



<p>Maybe in another video, if somebody requested, I might post another video in like five years.</p>



<p>Anyway, so we&#8217;re going to jump if it is a null terminator to the done label.</p>



<p>Otherwise we will fall through to the loop&#8217;s body where we&#8217;re just literally going to increase the pointer and also increase our idea of how big the string is.</p>



<p>So remember R12 is the pointer.</p>



<p>Integer arithmetic doesn&#8217;t, sorry, pointer arithmetic doesn&#8217;t really work here, but it accidentally works here because we&#8217;re looking at a byte array.</p>



<p>So if we just increase by one memory location, it will literally just increase by one memory location and we&#8217;ll be fine.</p>



<p>Just keep in mind that if you were sweeping through an array of, you know, quad words or some larger data type,</p>



<p>then just a simple ink wouldn&#8217;t actually work.</p>



<p>You&#8217;d have to increase by the appropriate number of bytes.</p>



<p>But hey, the number of bytes in one item is just one byte, so it&#8217;s easy.</p>



<p>So we&#8217;re making the pointer go forward by one on line 134 and then in line 135.</p>



<p>line 135 we&#8217;re increasing our idea of how big the string is and then we will unconditionally jump</p>



<p>to the top of our loop and so if you just kind of look at this what did i do i pasted that twice</p>



<p>oh god okay sorry guess i lost track of what i was doing so then we will unconditionally jump</p>



<p>to the top of the loop so basically you can imagine this loop is gonna it&#8217;s just gonna</p>



<p>continue forever just moving the pointer and increasing the counter and moving the pointer</p>



<p>finally when it sees a zero a null terminator then it actually breaks to</p>



<p>the done label and the done label is just doesn&#8217;t really do much it&#8217;s just a</p>



<p>label to get us out of the loop so the top of the loop says if we are done then</p>



<p>just jump to the done area notice how that skips over the the top jump and then</p>



<p>of course under that is going to be the epilog and then we can we can take the</p>



<p>we can take the return value and set that up now because at this point R13 should contain</p>



<p>the actual length of the string. So if we move that into RAX respecting the ABI for return values,</p>



<p>then the caller should be able to get the string length just at that point by itself.</p>



<p>So let&#8217;s see, that might actually be the whole entire program already. Let me</p>



<p>double check here. All right, let&#8217;s run it and see if it actually works.</p>



<p>and then do a make run.</p>



<p>What&#8217;s up with those asterisks?</p>



<p>Did I put that in there?</p>



<p>Oh, I wonder.</p>



<p>Okay.</p>



<p>So the driver comes in,</p>



<p>it calls on our function,</p>



<p>and the whole null terminated string gets printed out.</p>



<p>It says, hello, this is an example</p>



<p>of our null terminated string.</p>



<p>Notice how it printed the full length of the string,</p>



<p>not any less,</p>



<p>and it also didn&#8217;t print more than the length of the string,</p>



<p>i.e. junk data,</p>



<p>because it knew exactly how long the string was.</p>



<p>was and this is way better than printing one character at a time in terms of efficiency we</p>



<p>just pre-compute the length and then print exactly that length and then we&#8217;re done i think there is</p>



<p>one more thing i wanted to do here let me see up at the top yeah okay let me go back up to the top</p>



<p>of the program here so in the looper function we called on print null terminated string and we</p>



<p>didn&#8217;t do anything else so what i would like to do is just make an explicit call to string length</p>



<p>explicit call to string length inside of the lubr function just to get the length of the</p>



<p>null terminated string so we can just print it to the caller or print it to the user</p>



<p>and then I&#8217;m going to use my special library function here actually just just for your</p>



<p>information notice how I&#8217;m calling string length just like the the print null terminated string</p>



<p>function did and I&#8217;m just giving it as an argument a pointer to that null terminated string so then</p>



<p>So now I can just print r12</p>



<p>Well not yet, I&#8217;m gonna print a prefix if you look at the prefix here, it&#8217;s just</p>



<p>The null terminated strings length was and then I&#8217;ll print a number after that</p>



<p>You do it this way, you know your program is more pretty it&#8217;s more</p>



<p>It&#8217;s more nice to the user and so forth so I&#8217;m going to do this</p>



<p>we&#8217;re printing a nice prefix, a hard-coded string to the user to let them know that I&#8217;m about to</p>



<p>show them the length of the string. And then I use my external function that just prints a number to</p>



<p>the user. Again, this video is not about this library. You can use some other library if you</p>



<p>want to print something, or you can omit that part if you don&#8217;t have one set up yet. But</p>



<p>so I&#8217;m going to tell, I&#8217;m going to do first argument is R12, which was the length of the</p>



<p>I&#8217;m going to call this function and say I would like you to print r12 which is the length of the string so</p>



<p>After that we&#8217;ll print a new line to make things a little bit tidier and then I think this program is actually finished</p>



<p>Run it again now it says here&#8217;s the null terminated string and then on the next line it just says</p>



<p>The null terminated strings length was that was the prefix and then when I called my library</p>



<p>the number it says 54. so i don&#8217;t know was it 54? let&#8217;s just double check to make sure that it</p>



<p>actually was 54. 54 should not include the null terminator so i&#8217;m going to go 1 2 3 4 5 6 7 8 9 10</p>



<p>1 2 3 4 5 6 7 9 20 1 2 3 5 6 7 9 30 1 2 3 5 6 7 9 9 30 1 2 3 5 6 7 9 9 50 51 52 53 54 was it 54? i can&#8217;t even remember anymore.</p>



<p>So we have basically proved that this works.</p>



<p>We have leveraged our knowledge of while loops to implement a string length function, which</p>



<p>will let us have a printing function that is very smart.</p>



<p>So we don&#8217;t have to hard code string lengths up at the top anymore.</p>



<p>As long as we&#8217;re working with null terminated strings, everything will just work out now</p>



<p>with less variables or less defines.</p>



<p>Okay.</p>



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



<p>I think that&#8217;s pretty much everything that I wanted to talk to you about.</p>



<p>I don&#8217;t know. Could I do,</p>



<p>could I do this easy, easily?</p>



<p>Loop top.</p>



<p>Okay. Yeah. I think I could probably do this reasonably.</p>



<p>So at this point,</p>



<p>you are satisfied that you understand how to implement this and you&#8217;re happy just cut the</p>



<p>video the rest of this video is going to be me sort of like improvising trying to figure out if</p>



<p>i can rearrange the logic in a fast enough time for a video uh just to show you that you know you</p>



<p>should you should probably write your loops a little bit better than i did so here we go but</p>



<p>this is this is just redundant stuff so we have our loop here and we have our initialization</p>



<p>The loop top, it should compare R12 to 0 and it should break the loop if it is a 0.</p>



<p>So that means I&#8217;m going to comment out this.</p>



<p>And I&#8217;m going to do jump not equal to 0 to the body.</p>



<p>And I just need to make a label for the body here.</p>



<p>So I&#8217;m going to say str lane loop bottom.</p>



<p>So there&#8217;s a label, which is the body.</p>



<p>Maybe I&#8217;ll do a comment here just to remind us that this is actually the body.</p>



<p>I guess I&#8217;ll do another comment right here.</p>



<p>So that&#8217;s the loop&#8217;s body.</p>



<p>So I&#8217;m going to say if R12 is not a null terminator, jump into the loop&#8217;s body.</p>



<p>Otherwise, we fall through to the next instruction,</p>



<p>and that will just be an unconditional jump to the done area.</p>



<p>Okay, and then when we&#8217;re inside the loop&#8217;s body, we&#8217;ll jump back up to the top.</p>



<p>I don&#8217;t know why I thought this was going to be hard.</p>



<p>Let me run this to make sure I didn&#8217;t break the program.</p>



<p>Yeah, it still works.</p>



<p>Okay.</p>



<p>I guess I overestimated the difficulty there.</p>



<p>The point being, the body is a lot closer to the top of the loop.</p>



<p>So that should be the thing that does a conditional branch.</p>



<p>You should conditionally branch to the body because it&#8217;s a shorter jump and therefore</p>



<p>much less likely to be out of bounds of that 128 conditional jump bite restriction.</p>



<p>And then when we fall through to the next line, because we did not do that jump,</p>



<p>because we did not do that jump then we&#8217;ll do an unconditional jump to the done area and you know</p>



<p>our loop is small so it didn&#8217;t really matter the first time we did this but um again imagine your</p>



<p>loop is huge that you definitely want an unconditional jump that goes to the done area</p>



<p>at that point and that&#8217;s also what we&#8217;re doing an unconditional jump to the top here when we get to</p>



<p>the end of the body so when you&#8217;re jumping large uh you know spans you want to use unconditional</p>



<p>Alright, so I guess that&#8217;s it.</p>



<p>I&#8217;m going to erase maybe this comment.</p>



<p>Well, I&#8217;ll leave that in there just for posterity.</p>



<p>And now I will officially say that I hope you had a good time watching this video.</p>



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



<p>I will see you in the next video.</p>



<p>I&#8217;m going to go play some video games.</p>



<p>Maybe I&#8217;m going to eat some soup first.</p>



<p>Hey everybody.</p>



<p>Hey everybody, thanks for watching this video again from the bottom of my heart. I really</p>



<p>appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p>a small little favor, could you please subscribe and follow this channel or these videos or</p>



<p>whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



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



<p>So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making</p>



<p>to keep making videos in general. So please do me a kindness and subscribe. You know, sometimes</p>



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



<p>or followed. It just wakes me up and I get filled with joy. That&#8217;s exactly what happens every single</p>



<p>time. So you could do it as a nice favor to me or you could troll me if you want to just wake me up</p>



<p>in the middle of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what</p>



<p>will happen. Also, if you look at the middle of the screen right now, you should see a QR code,</p>



<p>you should see a QR code which you can scan in order to go to the website which I think is also</p>



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



<p>just kind of like see all the videos I published and the services and tutorials and things that I</p>



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



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



<p>what&#8217;s going on? You know, just send me a comment, whatever. I also wake up for those in the middle</p>



<p>of the night. I get, I wake up in a cold sweat and I&#8217;m like, it would really, it would really mean</p>



<p>the world to me. I would really appreciate it. So again, thank you so much for watching this video</p>



<p>and enjoy the cool music as, as I fade into the darkness, which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/">Implement strlen for Null-Terminated Strings in x86-64 Assembly (YASM)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/implement-strlen-for-null-terminated-strings-in-x86-64-assembly-yasm/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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>
