<?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>assembly language tutorial Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/assembly-language-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/assembly-language-tutorial/</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=7.1.3</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>assembly language tutorial Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/assembly-language-tutorial/</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 class="wp-block-paragraph">Quick but thorough introduction to floating-point registers in x86-64 assembly using YASM.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Live coding + real examples converting integers to doubles and multiplying them.</p>



<p class="wp-block-paragraph">Great next step after basic integer assembly tutorials.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hey there! In this video we&#8217;re going to talk about pointers and dereferencing in a YASM x8664</p>



<p class="wp-block-paragraph">assembly program, also as a hybrid program so that assembly and C++ can talk to each other</p>



<p class="wp-block-paragraph">and send each other pointers and send each other data and things like that.</p>



<p class="wp-block-paragraph">for what pointers are.</p>



<p class="wp-block-paragraph">I&#8217;m going to write in C++ for a second.</p>



<p class="wp-block-paragraph">Suppose you have a pointer for an integer.</p>



<p class="wp-block-paragraph">We&#8217;ll call it P.</p>



<p class="wp-block-paragraph">Suppose you have an integer by itself.</p>



<p class="wp-block-paragraph">We&#8217;ll call it A.</p>



<p class="wp-block-paragraph">Let&#8217;s say that the value of A is 5.</p>



<p class="wp-block-paragraph">And if you wanted to say that P points to A,</p>



<p class="wp-block-paragraph">you could say P equals the address of A.</p>



<p class="wp-block-paragraph">I&#8217;ll put C++ at the top here.</p>



<p class="wp-block-paragraph">And so now if I set A to 6</p>



<p class="wp-block-paragraph">then I print P a dereference of P this is not like a full pointers tutorial</p>



<p class="wp-block-paragraph">but basically by changing a I&#8217;m changing what P thinks it sees as a value</p>



<p class="wp-block-paragraph">assuming ID reference it I could also let me do a print 6 here I could also</p>



<p class="wp-block-paragraph">just change the value through P I could say dereference P and I could say equals</p>



<p class="wp-block-paragraph">would actually print a seven right so you know you can have regular variables global variables</p>



<p class="wp-block-paragraph">whatever kind of you know memory stuff on the stack and to get a pointer to it you really just</p>



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



<p class="wp-block-paragraph">in assembly you really just need the memory location stored somewhere you could store that</p>



<p class="wp-block-paragraph">variable that just simply stored the memory location of some other variable.</p>



<p class="wp-block-paragraph">You could have a 64-bit register store the value of a variable.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">So if you haven&#8217;t seen my previous videos, go see them for the basics of assembly and</p>



<p class="wp-block-paragraph">of assembly and linking and make files and all that stuff but you know if you</p>



<p class="wp-block-paragraph">have an assembly program and you have a data section and you define a global</p>



<p class="wp-block-paragraph">variable like this what you&#8217;re basically saying is I want to take this giant</p>



<p class="wp-block-paragraph">number and I want to write it into eight bytes that&#8217;s the DQ it says data quad</p>



<p class="wp-block-paragraph">word I want to write that giant number across eight bytes and then I want to</p>



<p class="wp-block-paragraph">get a pointer to it stored in the my number symbol so my number is not</p>



<p class="wp-block-paragraph">actually the value it&#8217;s a pointer to the value so you know later if you want to</p>



<p class="wp-block-paragraph">you know later if you want to move you know something into a register if you did this</p>



<p class="wp-block-paragraph">that would move the pointer into rax but if you did this</p>



<p class="wp-block-paragraph">with deref symbols after it or around it then you would move</p>



<p class="wp-block-paragraph">maybe i&#8217;ll put that into rex you&#8217;d move that actual number that we specified into rex</p>



<p class="wp-block-paragraph">into Rx. It&#8217;s important to understand also that pointers are integers even when we&#8217;re pointing to</p>



<p class="wp-block-paragraph">doubles. So for example sometimes people make this mistake they&#8217;ll say you know my double</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">point number and they&#8217;ll do like 44.55 or whatever. So that is a double and it is in memory</p>



<p class="wp-block-paragraph">you know what is the symbol of my double remember it&#8217;s supposed to be just a</p>



<p class="wp-block-paragraph">pointer right it can&#8217;t be an actual double because a memory location is not</p>



<p class="wp-block-paragraph">a double a memory location is an integer so that means if you wanted to move a</p>



<p class="wp-block-paragraph">pointer into a register you would only be able to move the pointer into a</p>



<p class="wp-block-paragraph">regular general purpose register not a floating point register and you should</p>



<p class="wp-block-paragraph">use the regular movement instructions for just regular general purpose</p>



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



<p class="wp-block-paragraph">You know, let&#8217;s say long a and long B and actually let&#8217;s do pointers</p>



<p class="wp-block-paragraph">Let&#8217;s say long pointer a and long pointer</p>



<p class="wp-block-paragraph">B and double pointer C all three of those arguments are actually 64 bit integers</p>



<p class="wp-block-paragraph">Because they&#8217;re all pointers even if one of the pointers points to adult a double</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">what we really need to do is write a driver and an assembly module for a</p>



<p class="wp-block-paragraph">hybrid program again hybrid programs covered in other videos so the driver is</p>



<p class="wp-block-paragraph">pretty easy I&#8217;m just going to copy paste it honestly here and then just kind of</p>



<p class="wp-block-paragraph">explain it to you the driver is pretty easy we&#8217;re going to do I O stream so we</p>



<p class="wp-block-paragraph">can print stuff we&#8217;re going to mark an external function called point as extern</p>



<p class="wp-block-paragraph">C so that just disables name mangling which means the C++ module will be able</p>



<p class="wp-block-paragraph">will be able to call on this function called point and it won&#8217;t expect that</p>



<p class="wp-block-paragraph">the point function has its name mangled like C++ does the reason being is that</p>



<p class="wp-block-paragraph">point is actually going to be in a side it&#8217;s going to be inside assembly where</p>



<p class="wp-block-paragraph">its name will not be mangled this disables the ability to overload but</p>



<p class="wp-block-paragraph">that&#8217;s okay we don&#8217;t care it&#8217;s going to take two pointers a pointer to a character</p>



<p class="wp-block-paragraph">and a pointer to a long since both of those are pointers they&#8217;re both</p>



<p class="wp-block-paragraph">64-bit integers even the character pointer and then we have a function that is internal to this</p>



<p class="wp-block-paragraph">module called hey driver print this remember we&#8217;re inside of the driver program right now</p>



<p class="wp-block-paragraph">so if you look at the bottom it&#8217;s just a function that takes in some pointers</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">i literally just let him pee and poop at this point now he&#8217;s harassing me for treats</p>



<p class="wp-block-paragraph">now he&#8217;s harassing me for treats he always does this okay so uh the string the long the double</p>



<p class="wp-block-paragraph">this function expects to receive three pointers to different data types it&#8217;s just going to print</p>



<p class="wp-block-paragraph">all of them and the point get it the point of this function is we&#8217;re going to go inside of</p>



<p class="wp-block-paragraph">the assembly module and then have the assembly module call on this function so that we can we</p>



<p class="wp-block-paragraph">can prove that we can have stuff sent from assembly to c plus plus or c using pointers</p>



<p class="wp-block-paragraph">using pointers we can have data sent over so anyway that&#8217;s why both of these</p>



<p class="wp-block-paragraph">are in here the point needs to be marked as no name mangling because point is</p>



<p class="wp-block-paragraph">inside of assembly which will not name mangle and then hey driver print this</p>



<p class="wp-block-paragraph">that needs to have name mangling disabled also so that the assembly</p>



<p class="wp-block-paragraph">module can call on this other than that we&#8217;re just basically inside of a main</p>



<p class="wp-block-paragraph">saying hey this is the c string we&#8217;re making a c string inside of the main function notice how</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">the stack um and but then we can actually send pointers to those pieces of data to another</p>



<p class="wp-block-paragraph">function in another module you don&#8217;t have to only transport globals or stuff on the heap</p>



<p class="wp-block-paragraph">or stuff on the heap, you can transport pointers to local variables. Just make sure that by the</p>



<p class="wp-block-paragraph">time this function finishes, then nowhere else is actually using that data because,</p>



<p class="wp-block-paragraph">well, being on the stack, once main function or once any function finishes, then its portion of</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">just going to use them quickly on this call to point and then we&#8217;re going to return to the</p>



<p class="wp-block-paragraph">operating system and finish the program. So that&#8217;s the driver. Now the hard part. Let&#8217;s do this in</p>



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



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



<p class="wp-block-paragraph">that that&#8217;s what this language is at the beginning of the video? I guess I should put that in the</p>



<p class="wp-block-paragraph">put that in the description or record an announcement that I can tack on at the beginning</p>



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



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Oh, we&#8217;re inside of, you know, the module now, the assembly module. And now we&#8217;re going to print</p>



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



<p class="wp-block-paragraph">into C++ when we call the function inside of the driver.</p>



<p class="wp-block-paragraph">So this string is owned by the assembly module.</p>



<p class="wp-block-paragraph">Notice how these are null terminated strings.</p>



<p class="wp-block-paragraph">I just have like a comma zero there,</p>



<p class="wp-block-paragraph">which means I have some extra functions</p>



<p class="wp-block-paragraph">I&#8217;m gonna paste in that we&#8217;re not really gonna talk about</p>



<p class="wp-block-paragraph">because they&#8217;ve been discussed in other videos</p>



<p class="wp-block-paragraph">just so that we can print null terminated strings.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a new line here,</p>



<p class="wp-block-paragraph">you know, carriage return line feed.</p>



<p class="wp-block-paragraph">And then I&#8217;ve just got some numbers</p>



<p class="wp-block-paragraph">that are owned by the assembly module.</p>



<p class="wp-block-paragraph">Then I&#8217;ve got a system write call,</p>



<p class="wp-block-paragraph">call code one for the system call writes and file descriptor standard output so I</p>



<p class="wp-block-paragraph">can print just to the terminal again if you don&#8217;t understand this see my other</p>



<p class="wp-block-paragraph">videos so now let&#8217;s start the actual text section so this is where our</p>



<p class="wp-block-paragraph">instructions start so we got the text section here and we&#8217;re going to use some</p>



<p class="wp-block-paragraph">external symbols don&#8217;t worry about these I&#8217;m just using my own little library to</p>



<p class="wp-block-paragraph">and input integers if you have access to this library use it if you don&#8217;t if you&#8217;re watching</p>



<p class="wp-block-paragraph">at home and you don&#8217;t have this library then that&#8217;s fine you can use you know printf or</p>



<p class="wp-block-paragraph">scanf or something like that to get and print floats from and to the user</p>



<p class="wp-block-paragraph">but yeah I&#8217;m just using that and then I&#8217;m marking an external function here called hey driver print</p>



<p class="wp-block-paragraph">this if you recall the driver module has a function called hey driver print this so</p>



<p class="wp-block-paragraph">just allows my assembly code to call on that external function. Okay now next</p>



<p class="wp-block-paragraph">piece of code. This is going to be… actually I&#8217;m going to paste the print</p>



<p class="wp-block-paragraph">null terminated string function and related code because it&#8217;s just like a</p>



<p class="wp-block-paragraph">big giant mess and we&#8217;re mostly going to ignore it. So just to show you what I&#8217;m</p>



<p class="wp-block-paragraph">doing here I have a function called print null terminated string so that I</p>



<p class="wp-block-paragraph">can print these strings up here and then I have it rely on a function called</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Great for anyone studying low-level programming, operating systems, or wanting to understand C strings at the assembly level.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hey there, in this video, I&#8217;m going to show you how to implement the function string length.</p>



<p class="wp-block-paragraph">So you can find the length of a null terminated string in a Yasm x86-64 assembly program.</p>



<p class="wp-block-paragraph">Although if you&#8217;re using a different assembly language or different architecture, this video</p>



<p class="wp-block-paragraph">will probably still be useful to you because the concepts are going to be the same.</p>



<p class="wp-block-paragraph">So what am I talking about?</p>



<p class="wp-block-paragraph">I&#8217;m not going to be around here with this.</p>



<p class="wp-block-paragraph">So in a previous video, I discussed null terminated strings.</p>



<p class="wp-block-paragraph">I should also point out that a lot of basic knowledge is going to be skipped in this video</p>



<p class="wp-block-paragraph">because I&#8217;ve explained it in other videos.</p>



<p class="wp-block-paragraph">For example, if you don&#8217;t know how to compile, link, assemble, write a basic assembly program,</p>



<p class="wp-block-paragraph">write a make file and so forth, then you should see my other videos first.</p>



<p class="wp-block-paragraph">I&#8217;ve also already published a video about null terminated strings, but I&#8217;ll just do</p>



<p class="wp-block-paragraph">it again here since that&#8217;s in the title of the video.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">probably a collection of characters on some level so we&#8217;ll just say this is h e uh l</p>



<p class="wp-block-paragraph">l o right um when you&#8217;re printing</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">There could probably be some junk data at the end of it</p>



<p class="wp-block-paragraph">You know like a bunch of other random letters or you can even imagine these as just you know</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">is just pre-compute the length of the string so we do that in assembly a lot before we know how</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">the system I want you to print five characters starting at that memory location wherever the H is</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">null terminated strings are a lot more convenient because you don&#8217;t have to pre-compute the strings.</p>



<p class="wp-block-paragraph">I mean, maybe your user entered a string.</p>



<p class="wp-block-paragraph">Maybe you have a lot of strings or they change quite often.</p>



<p class="wp-block-paragraph">Maybe you have like a multinational program that has tons of translations,</p>



<p class="wp-block-paragraph">or I think multilingual is probably the better word for that.</p>



<p class="wp-block-paragraph">But it can be a pain in the butt to constantly compute the length of strings in advance.</p>



<p class="wp-block-paragraph">So with a null terminated string, you basically just say,</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">I&#8217;ll leave the junk data there just to let you know that there is some stuff happening in memory.</p>



<p class="wp-block-paragraph">Notice how this zero, it is not actually the character that looks like a zero to a human.</p>



<p class="wp-block-paragraph">That&#8217;s actually a totally different code than just zero. So you can imagine just an actual zero here.</p>



<p class="wp-block-paragraph">You know, each of these characters that a human would look at has a number underneath it.</p>



<p class="wp-block-paragraph">You know, this H is not really an H.</p>



<p class="wp-block-paragraph">It&#8217;s just some number between 0 and 255 if we&#8217;re talking about ASCII.</p>



<p class="wp-block-paragraph">The E is a different number and so forth.</p>



<p class="wp-block-paragraph">So if we just put the literal number 0 in our data,</p>



<p class="wp-block-paragraph">or if you want to quote this inside of a single quote,</p>



<p class="wp-block-paragraph">you can do, I think, slash 0 just to let the compiler know</p>



<p class="wp-block-paragraph">that you intend to have the number 0 there</p>



<p class="wp-block-paragraph">instead of something that looks like the number 0, you know, the character.</p>



<p class="wp-block-paragraph">you know the character but anyways the point is we just have to stick a zero at the end</p>



<p class="wp-block-paragraph">of the string we call it a null terminator because zero is also you know an alias for null</p>



<p class="wp-block-paragraph">whenever you have a null pointer or you assign null to a memory location or a pointer or something</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">we&#8217;ll say it&#8217;s a null terminator.</p>



<p class="wp-block-paragraph">It&#8217;s a basic idea for null terminators.</p>



<p class="wp-block-paragraph">Now the question is, how do we actually know when to stop?</p>



<p class="wp-block-paragraph">Well, the first thing that you could do if you&#8217;re trying to write a program that is highly inefficient,</p>



<p class="wp-block-paragraph">which I&#8217;ve definitely done before, is you could just print one character at a time.</p>



<p class="wp-block-paragraph">You use a for loop.</p>



<p class="wp-block-paragraph">You start at the very beginning of your string, you know, a pointer,</p>



<p class="wp-block-paragraph">whatever the user gave you as like this is the first character.</p>



<p class="wp-block-paragraph">We&#8217;ll just print that letter, and then we&#8217;ll go on to the next letter.</p>



<p class="wp-block-paragraph">the next letter and before we print it actually before we print the first letter even before we</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">and then we go through every character one by one just you know checking and printing checking and</p>



<p class="wp-block-paragraph">printing checking and printing unfortunately that&#8217;s kind of inefficient because every time</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">for you and it would be a lot better if we could just flush the whole string at</p>



<p class="wp-block-paragraph">the same time but but know how long the string was that would increase our</p>



<p class="wp-block-paragraph">efficiency so the program that we&#8217;re going to write together is basically</p>



<p class="wp-block-paragraph">going to use our knowledge of a while loop which I&#8217;ve explained in other</p>



<p class="wp-block-paragraph">videos already so see those other videos if you don&#8217;t know how to do while loops</p>



<p class="wp-block-paragraph">in Yasm we&#8217;re going to use our knowledge of a while loop to sort of scan the</p>



<p class="wp-block-paragraph">string real fast just you know kind of scan it and figure out how far into the</p>



<p class="wp-block-paragraph">far into the string until we see a null terminator and use that to determine what is the length of</p>



<p class="wp-block-paragraph">the string. At that point, we can use a system call in YASM, in assembly, to just say, I want you to</p>



<p class="wp-block-paragraph">print this sequence of characters and here&#8217;s the length and then let the system worry about</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">the first part is going to be a function called string length which you&#8217;ve probably already seen</p>



<p class="wp-block-paragraph">in c if you program in c or c plus plus the second function is going to be called print null terminated</p>



<p class="wp-block-paragraph">string which will just ask string length what the length of the string is first and then actually</p>



<p class="wp-block-paragraph">print it with the system call so let me uh i guess let me start off with my data section here</p>



<p class="wp-block-paragraph">to print I&#8217;m gonna copy paste that for my solution again this is not a not an</p>



<p class="wp-block-paragraph">assembly basics video so if you don&#8217;t understand what I&#8217;m doing you should</p>



<p class="wp-block-paragraph">watch my other videos first I&#8217;m assuming you know how to make a data section by</p>



<p class="wp-block-paragraph">now we&#8217;ll put some C strings I&#8217;m just gonna make one null terminated string</p>



<p class="wp-block-paragraph">actually I guess I&#8217;m making two but the focus of this program is just the first</p>



<p class="wp-block-paragraph">one I&#8217;m calling it null terminated string and in assembly it&#8217;s pretty easy</p>



<p class="wp-block-paragraph">you just make it a you know a character array just like a sequence of bytes with</p>



<p class="wp-block-paragraph">a sequence of bytes with this DB meaning data bytes.</p>



<p class="wp-block-paragraph">And I can just put a quoted string like this.</p>



<p class="wp-block-paragraph">No problem.</p>



<p class="wp-block-paragraph">As many characters as I want.</p>



<p class="wp-block-paragraph">I can start injecting specific ASCII values if I wanted to</p>



<p class="wp-block-paragraph">or byte values if I wanted to just by putting a comma</p>



<p class="wp-block-paragraph">and then a number.</p>



<p class="wp-block-paragraph">So I could do something like this.</p>



<p class="wp-block-paragraph">I could do like, you know, 47, you know, 49, you know, 50, whatever.</p>



<p class="wp-block-paragraph">If I knew the ASCII codes for the characters,</p>



<p class="wp-block-paragraph">fortunately, I don&#8217;t need to.</p>



<p class="wp-block-paragraph">normally into the double quoted area but then i need to be able to put a null terminator at the</p>



<p class="wp-block-paragraph">end of my string because it&#8217;s not going to happen automatically so then i am going to do comma zero</p>



<p class="wp-block-paragraph">and you&#8217;ll end up with something like this like if i guess if we look at the previous example real</p>



<p class="wp-block-paragraph">fast i&#8217;ll call this a hello string just so that you see some similarity from what we just looked</p>



<p class="wp-block-paragraph">a notepad thing would just be typing the word hello and then putting comma zero.</p>



<p class="wp-block-paragraph">So it is now a null terminated string and it looks just like this inside of system memory.</p>



<p class="wp-block-paragraph">Well, not just like that.</p>



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



<p class="wp-block-paragraph">And then of course there&#8217;s junk data afterwards, but we don&#8217;t really care about that.</p>



<p class="wp-block-paragraph">You know, we&#8217;re just going to ignore it with the null terminator.</p>



<p class="wp-block-paragraph">So I&#8217;m going to erase that since we&#8217;re not just going to print the word hello.</p>



<p class="wp-block-paragraph">We have a null terminated string here and then after we print the null terminated string</p>



<p class="wp-block-paragraph">I&#8217;m just going to print out what was the length of the string.</p>



<p class="wp-block-paragraph">So this is a prefix string where it&#8217;s just, you know, it&#8217;s a prettier program.</p>



<p class="wp-block-paragraph">The program is going to say the null terminated string&#8217;s length was something.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to use the null terminated string printer to print that also.</p>



<p class="wp-block-paragraph">Convenient, right?</p>



<p class="wp-block-paragraph">And then I&#8217;m going to actually print the number.</p>



<p class="wp-block-paragraph">Then we have this down here, crlf, which is just printing a new line in the terminal.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">And then we&#8217;re going to use system call code 1 to print a standard output right here.</p>



<p class="wp-block-paragraph">If you don&#8217;t understand that, then see my other videos.</p>



<p class="wp-block-paragraph">But let&#8217;s move on to the text section where all our instructions will go.</p>



<p class="wp-block-paragraph">Okay, so now the instructions begin in our text section right here.</p>



<p class="wp-block-paragraph">section.text and I&#8217;m using an external symbol this video is not about this</p>



<p class="wp-block-paragraph">library here but basically I have a library that will help me print integers</p>



<p class="wp-block-paragraph">you don&#8217;t need to worry about that you could imagine well I guess in your</p>



<p class="wp-block-paragraph">example when you&#8217;re practicing if you don&#8217;t have this library you could just</p>



<p class="wp-block-paragraph">not print the length of the string and just use it only and it all should still</p>



<p class="wp-block-paragraph">work or you could hard code the thing that you&#8217;re printing if you really</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">this is not a video about hybrid programs. Just assume that there is another module in my program.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">so that means i have to preserve r12 for the caller because the abi or the application binary</p>



<p class="wp-block-paragraph">interface says that r12 is a callie saved register and if you don&#8217;t respect the abi</p>



<p class="wp-block-paragraph">the abi is not going to respect you your program is going to end up crashing eventually</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do a push pop pair to preserve R12.</p>



<p class="wp-block-paragraph">Oops, prologue and call that epilogue.</p>



<p class="wp-block-paragraph">Okay. So we got a push pop pair. We got a return statement.</p>



<p class="wp-block-paragraph">This program should probably do nothing so far. So let&#8217;s run it and see,</p>



<p class="wp-block-paragraph">just make sure that it at least compiles.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say clear and make run running the program.</p>



<p class="wp-block-paragraph">Hello from the driver. You don&#8217;t know that the driver has that.</p>



<p class="wp-block-paragraph">that the driver has that. This is not a driver video. And then the driver regains control because</p>



<p class="wp-block-paragraph">nothing happened inside of the assembly module. We just basically looper got called and then we</p>



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



<p class="wp-block-paragraph">print null terminated string. We have to make another function for this, but right now this is</p>



<p class="wp-block-paragraph">just the call. So the name of the function that we&#8217;re going to write is called print null terminated</p>



<p class="wp-block-paragraph">it it will call on the string length function to figure out how long the string is then it will use</p>



<p class="wp-block-paragraph">a simple system call to print the whole string giving the length to the system call it also takes</p>



<p class="wp-block-paragraph">two arguments the first argument is a pointer to the null terminated string so that&#8217;s just that</p>



<p class="wp-block-paragraph">symbol we defined up above remember when you define variables up in the data section then</p>



<p class="wp-block-paragraph">these symbols tend to be pointers so that symbol is a pointer to the h basically or just the memory</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">it so we&#8217;re just going to print it to standard output um which is just file descriptor number one</p>



<p class="wp-block-paragraph">so again if you don&#8217;t understand arguments or you know file descriptors or function calls</p>



<p class="wp-block-paragraph">see my other videos because i&#8217;ve explained those already anyway so we&#8217;re going to call</p>



<p class="wp-block-paragraph">print null terminated string then we&#8217;re going to call on crlf which will just print a new line</p>



<p class="wp-block-paragraph">So now maybe we should implement, well, let&#8217;s copy paste crlf so that I can implement the</p>



<p class="wp-block-paragraph">other function a little bit more slowly.</p>



<p class="wp-block-paragraph">What does crlf do?</p>



<p class="wp-block-paragraph">It literally just asks the print null terminated string function to just print a crlf for us.</p>



<p class="wp-block-paragraph">So it&#8217;s very, very simple.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature.</p>



<p class="wp-block-paragraph">Nothing much to it.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Now, a little bit more complicated is the print null terminated string function.</p>



<p class="wp-block-paragraph">So in our looper, we&#8217;re going to print the null terminated string.</p>



<p class="wp-block-paragraph">We have to have a function that actually does that.</p>



<p class="wp-block-paragraph">So that&#8217;s going to be this one right here.</p>



<p class="wp-block-paragraph">Here&#8217;s the signature that I&#8217;ve chosen for my print null terminated string function.</p>



<p class="wp-block-paragraph">Basically, I want to receive a character pointer to the first character in the string that we&#8217;re going to print.</p>



<p class="wp-block-paragraph">And then a file handle designating where we&#8217;re going to print it.</p>



<p class="wp-block-paragraph">The reason I want to receive the file handle is so I could print a standard output or standard error.</p>



<p class="wp-block-paragraph">or standard error, or I could print to a file,</p>



<p class="wp-block-paragraph">like whatever I want to do.</p>



<p class="wp-block-paragraph">You don&#8217;t have to have that in there, but it&#8217;s nice.</p>



<p class="wp-block-paragraph">Anyway, so we have this function set up.</p>



<p class="wp-block-paragraph">Notice how my notes that I left for myself</p>



<p class="wp-block-paragraph">is that I&#8217;m gonna use R12</p>



<p class="wp-block-paragraph">to remember the incoming C string pointer argument,</p>



<p class="wp-block-paragraph">and I&#8217;m gonna use R13 to remember the file handle.</p>



<p class="wp-block-paragraph">Remember, it&#8217;s probably not a good idea</p>



<p class="wp-block-paragraph">to just let the incoming arguments</p>



<p class="wp-block-paragraph">stay in their original registers,</p>



<p class="wp-block-paragraph">original registers because those registers tend to get overwritten as you do system calls or</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">is the string&#8217;s length, which I&#8217;m going to compute with a call to the function called string length.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Okay, so we&#8217;re going to do a prologue to preserve those registers.</p>



<p class="wp-block-paragraph">And then at the very end, we&#8217;re going to do an epilogue where we restore those registers.</p>



<p class="wp-block-paragraph">Oh, I think I already overwrote my return statement from the previous function.</p>



<p class="wp-block-paragraph">I think I did that in the last video and I was a little confused as to what was wrong.</p>



<p class="wp-block-paragraph">So make sure you don&#8217;t accidentally overwrite or push down your return instructions.</p>



<p class="wp-block-paragraph">Let me just double check here.</p>



<p class="wp-block-paragraph">Looper&#8217;s got return.</p>



<p class="wp-block-paragraph">Print and alternated string has got a return.</p>



<p class="wp-block-paragraph">string has got a return.</p>



<p class="wp-block-paragraph">CRLF has a return.</p>



<p class="wp-block-paragraph">What the heck did I do?</p>



<p class="wp-block-paragraph">Oh, I think I copy pasted in a bizarre place.</p>



<p class="wp-block-paragraph">That&#8217;s probably what happened because the epilog for for print null terminated</p>



<p class="wp-block-paragraph">string is like down in CRLF already.</p>



<p class="wp-block-paragraph">That&#8217;s not good.</p>



<p class="wp-block-paragraph">Okay, that would have been a crashing program.</p>



<p class="wp-block-paragraph">Although sometimes if you omit the return statements, execution will just fall</p>



<p class="wp-block-paragraph">through down to the next label and maybe your program will survive accidentally.</p>



<p class="wp-block-paragraph">accidentally but for now it&#8217;s just crlf is supposed to be very simple it doesn&#8217;t preserve</p>



<p class="wp-block-paragraph">any registers so we&#8217;ve got a prologue and an epilogue here notice how the push and pops are</p>



<p class="wp-block-paragraph">in reverse order you want to know more about that see my other videos but now that we are preserving</p>



<p class="wp-block-paragraph">the appropriate registers we can actually grab our incoming arguments so first thing i&#8217;m going to do</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">okay no problem then let&#8217;s rely on the string length function to compute the actual length of</p>



<p class="wp-block-paragraph">the string i didn&#8217;t feel like having print null terminated string compute the length of the</p>



<p class="wp-block-paragraph">string it&#8217;s a good idea especially in assembly or any language when you have multiple distinct</p>



<p class="wp-block-paragraph">jobs happening within the same function you probably want to break that function up into</p>



<p class="wp-block-paragraph">multiple functions just to reduce you know strain on your brain right cognitive load</p>



<p class="wp-block-paragraph">So I&#8217;m going to use this function strlen string length to compute the length of the string.</p>



<p class="wp-block-paragraph">It&#8217;s only going to take one argument and it&#8217;s going to take the pointer to the null terminated</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">that up into RDI. When string length returns it&#8217;s going to give me the length of the string in the</p>



<p class="wp-block-paragraph">RAX register which is the usual return register for integer or pointer return types. So I&#8217;m just</p>



<p class="wp-block-paragraph">So I&#8217;m just going to save that in R14.</p>



<p class="wp-block-paragraph">And that&#8217;s the usage of all those registers R12, 13, and 14.</p>



<p class="wp-block-paragraph">We still have to implement string length.</p>



<p class="wp-block-paragraph">Don&#8217;t worry.</p>



<p class="wp-block-paragraph">Although if you were linking a hybrid program, you could probably just call</p>



<p class="wp-block-paragraph">STRLEN in the C libraries and be fine.</p>



<p class="wp-block-paragraph">But this is an assembly video.</p>



<p class="wp-block-paragraph">We want to do everything in assembly if we can, or at least more of it.</p>



<p class="wp-block-paragraph">So then finally, when we know what the strings length is, we can just use a</p>



<p class="wp-block-paragraph">system call to actually print the string we&#8217;re going to say load up call code one to say you</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">going to basically say wherever the caller of print null terminated string said to print which</p>



<p class="wp-block-paragraph">is probably going to be standard output we&#8217;ll just tell the system we want to print to the same place</p>



<p class="wp-block-paragraph">and then r12 is a pointer to the c string so we just give that to the system call as well</p>



<p class="wp-block-paragraph">system call wants to know how long the string is that&#8217;s r14 now now that we have used strlen</p>



<p class="wp-block-paragraph">to determine the length of the string so not really that complicated of a function we just</p>



<p class="wp-block-paragraph">kind of like grab some arguments preserve those registers and we ask another function to compute</p>



<p class="wp-block-paragraph">the length of the string and then we actually just print it once we have the length this is still not</p>



<p class="wp-block-paragraph">getting to the point where we&#8217;re going to use our while loop knowledge to compute the length so i</p>



<p class="wp-block-paragraph">That&#8217;s probably all I need right now.</p>



<p class="wp-block-paragraph">And I think we&#8217;re ready to use or to start the string length function.</p>



<p class="wp-block-paragraph">Okay, so now let&#8217;s make another function called string length.</p>



<p class="wp-block-paragraph">Hopefully I&#8217;ll paste in the right spot this time.</p>



<p class="wp-block-paragraph">You&#8217;re cringing at home.</p>



<p class="wp-block-paragraph">That just tells me that you care.</p>



<p class="wp-block-paragraph">So the string length function, at least the version that I&#8217;m making right now,</p>



<p class="wp-block-paragraph">just is going to take one argument.</p>



<p class="wp-block-paragraph">It&#8217;s going to be a character pointer to the string that you want to compute.</p>



<p class="wp-block-paragraph">It will expect that the string has a null terminator at the end.</p>



<p class="wp-block-paragraph">the end if you accidentally didn&#8217;t put a null terminator at the end of the string then this</p>



<p class="wp-block-paragraph">function definitely won&#8217;t work it&#8217;ll probably give you some huge number because it&#8217;ll go through ram</p>



<p class="wp-block-paragraph">until it accidentally finds a zero um and then it&#8217;s going to return to you as its return value</p>



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



<p class="wp-block-paragraph">now um to indicate the length of the string okay inside the notes we&#8217;re going to use r12 and r13</p>



<p class="wp-block-paragraph">So that means I should probably preserve those registers first before I do anything else.</p>



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



<p class="wp-block-paragraph">for others.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to do an epilog.</p>



<p class="wp-block-paragraph">Whoops.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do an epilog to restore the registers.</p>



<p class="wp-block-paragraph">And this is a function.</p>



<p class="wp-block-paragraph">So it&#8217;s got to return to the caller.</p>



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



<p class="wp-block-paragraph">down to CRLF.</p>



<p class="wp-block-paragraph">And this will be an infinite loop.</p>



<p class="wp-block-paragraph">and this will be an infinite loop because crlf will end up calling null terminated string,</p>



<p class="wp-block-paragraph">which we&#8217;ll then call string length, which will then fall through to crlf,</p>



<p class="wp-block-paragraph">so the whole program won&#8217;t even work if we don&#8217;t have return.</p>



<p class="wp-block-paragraph">And, you know, you don&#8217;t want to omit return statements anyways,</p>



<p class="wp-block-paragraph">because that&#8217;s always a bad idea.</p>



<p class="wp-block-paragraph">So now string length will just not do anything right now.</p>



<p class="wp-block-paragraph">Maybe we could return a fake value for a second before we start implementing the loop.</p>



<p class="wp-block-paragraph">the number five into RAX so that string length will always trick the caller into thinking that</p>



<p class="wp-block-paragraph">the length of the string is five let&#8217;s see if that actually works we should get a portion</p>



<p class="wp-block-paragraph">of the null terminated string unless I screwed something up</p>



<p class="wp-block-paragraph">hello from the main driver notice how it just says hello here that&#8217;s kind of confusing let&#8217;s</p>



<p class="wp-block-paragraph">let&#8217;s hard code the five to like a nine we should see more of that null terminated string</p>



<p class="wp-block-paragraph">I sound when I wake up sometimes hello okay so let&#8217;s finish the str len function so again you</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">loop to count the length of the string so we&#8217;re going to start with a little portion up here</p>



<p class="wp-block-paragraph">think the string is and a running pointer so rdi is already supposed to come in as a pointer to the</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">sweep through the whole entire string until it hits a null terminator and then r13 is going to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">first start we&#8217;re just looking at the first letter and then we think the string has zero length.</p>



<p class="wp-block-paragraph">So that&#8217;s the initialization part which will not be repeated as we continue looping. Now we&#8217;re</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Maybe this is like a long strln, something like that.</p>



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



<p class="wp-block-paragraph">I did not, I did not prepare this.</p>



<p class="wp-block-paragraph">We&#8217;ll say character pointer s and then we&#8217;ll say, uh, maybe we can actually just leave</p>



<p class="wp-block-paragraph">s alone because it&#8217;s coming in as an argument and in C plus plus you can just continue to</p>



<p class="wp-block-paragraph">use that symbol.</p>



<p class="wp-block-paragraph">It&#8217;s not going to get destroyed.</p>



<p class="wp-block-paragraph">So imagine we&#8217;ve saved it already into R 12 and then we just keep using it.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">if we look at the value that the pointer is currently pointing to if we assume it&#8217;s just</p>



<p class="wp-block-paragraph">pointing to one byte is we&#8217;ll keep going as long as that value is not a zero so that means</p>



<p class="wp-block-paragraph">if the user called this function and gave us a pointer that was already looking at a zero</p>



<p class="wp-block-paragraph">we would just return whoops we would just return that the length was zero so</p>



<p class="wp-block-paragraph">that means I should probably keep track of the length here size type actually</p>



<p class="wp-block-paragraph">long just to just to match the return signature long we&#8217;ll put size equals zero</p>



<p class="wp-block-paragraph">and then at the very end we&#8217;ll just return the size and so again if the user</p>



<p class="wp-block-paragraph">gave us a pointer that pointed to a zero already nothing would happen inside the</p>



<p class="wp-block-paragraph">while loop we&#8217;d break through it right away and we would just return the number</p>



<p class="wp-block-paragraph">the number zero that makes sense so then as long as it is not pointing at a zero</p>



<p class="wp-block-paragraph">we&#8217;ll just increase what we think the size is and then we will increase the</p>



<p class="wp-block-paragraph">pointer we can use s plus plus in C++ that&#8217;s just pointer arithmetic that&#8217;s</p>



<p class="wp-block-paragraph">just going to tell the pointer to advance you know one memory location</p>



<p class="wp-block-paragraph">further or whatever the data type is but in this case the data type is a</p>



<p class="wp-block-paragraph">character so it really is going to be one memory location one byte so we&#8217;re</p>



<p class="wp-block-paragraph">going to sweep through the string until we see a zero and then we stop and every time we see a</p>



<p class="wp-block-paragraph">character that&#8217;s not a zero we increase our our measured length of the string by one and then</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">initialization part that we were just talking about so we just set the running pointer to look</p>



<p class="wp-block-paragraph">okay so then after we do that we are going to make the top of the while loop</p>



<p class="wp-block-paragraph">so at the top of the while loop where we evaluate you know like right here this</p>



<p class="wp-block-paragraph">is the top of the while loop it has to have its own label just like we explained</p>



<p class="wp-block-paragraph">in the other videos and it is basically where we decide if we&#8217;re going to keep</p>



<p class="wp-block-paragraph">looping or not are we going to jump into the body the loop or are we going to do</p>



<p class="wp-block-paragraph">a long jump after the body to say that we&#8217;re done so the top of the loop is a</p>



<p class="wp-block-paragraph">label. We compare the value that R12 is currently pointing at. We say that we only want to look at</p>



<p class="wp-block-paragraph">one byte. We dereference R12 because remember R12 is supposed to be a pointer. You put the</p>



<p class="wp-block-paragraph">brackets around it, it&#8217;s going to go to the memory location and then check what the value is that</p>



<p class="wp-block-paragraph">the pointer is pointing to. That&#8217;s what dereferencing is, right? So we&#8217;re just going to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">jump to the done this is actually kind of a poor design pattern on my part usually we should jump</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">execution will fall through on the next line to a long jump which has the ability to jump further</p>



<p class="wp-block-paragraph">out of the body i&#8217;ve said in other videos that the conditional branch instructions they can only jump</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">But it&#8217;ll work for this example.</p>



<p class="wp-block-paragraph">I don&#8217;t know, maybe if I have the gumption, I will fix up the loop for you if you want</p>



<p class="wp-block-paragraph">me to after I copy paste my existing solution.</p>



<p class="wp-block-paragraph">So for now we&#8217;re going to say, all right, I&#8217;m not going to do it.</p>



<p class="wp-block-paragraph">I&#8217;m not going to do that.</p>



<p class="wp-block-paragraph">Maybe in another video, if somebody requested, I might post another video in like five years.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;re going to jump if it is a null terminator to the done label.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So remember R12 is the pointer.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Just keep in mind that if you were sweeping through an array of, you know, quad words or some larger data type,</p>



<p class="wp-block-paragraph">then just a simple ink wouldn&#8217;t actually work.</p>



<p class="wp-block-paragraph">You&#8217;d have to increase by the appropriate number of bytes.</p>



<p class="wp-block-paragraph">But hey, the number of bytes in one item is just one byte, so it&#8217;s easy.</p>



<p class="wp-block-paragraph">So we&#8217;re making the pointer go forward by one on line 134 and then in line 135.</p>



<p class="wp-block-paragraph">line 135 we&#8217;re increasing our idea of how big the string is and then we will unconditionally jump</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">oh god okay sorry guess i lost track of what i was doing so then we will unconditionally jump</p>



<p class="wp-block-paragraph">to the top of the loop so basically you can imagine this loop is gonna it&#8217;s just gonna</p>



<p class="wp-block-paragraph">continue forever just moving the pointer and increasing the counter and moving the pointer</p>



<p class="wp-block-paragraph">finally when it sees a zero a null terminator then it actually breaks to</p>



<p class="wp-block-paragraph">the done label and the done label is just doesn&#8217;t really do much it&#8217;s just a</p>



<p class="wp-block-paragraph">label to get us out of the loop so the top of the loop says if we are done then</p>



<p class="wp-block-paragraph">just jump to the done area notice how that skips over the the top jump and then</p>



<p class="wp-block-paragraph">of course under that is going to be the epilog and then we can we can take the</p>



<p class="wp-block-paragraph">we can take the return value and set that up now because at this point R13 should contain</p>



<p class="wp-block-paragraph">the actual length of the string. So if we move that into RAX respecting the ABI for return values,</p>



<p class="wp-block-paragraph">then the caller should be able to get the string length just at that point by itself.</p>



<p class="wp-block-paragraph">So let&#8217;s see, that might actually be the whole entire program already. Let me</p>



<p class="wp-block-paragraph">double check here. All right, let&#8217;s run it and see if it actually works.</p>



<p class="wp-block-paragraph">and then do a make run.</p>



<p class="wp-block-paragraph">What&#8217;s up with those asterisks?</p>



<p class="wp-block-paragraph">Did I put that in there?</p>



<p class="wp-block-paragraph">Oh, I wonder.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So the driver comes in,</p>



<p class="wp-block-paragraph">it calls on our function,</p>



<p class="wp-block-paragraph">and the whole null terminated string gets printed out.</p>



<p class="wp-block-paragraph">It says, hello, this is an example</p>



<p class="wp-block-paragraph">of our null terminated string.</p>



<p class="wp-block-paragraph">Notice how it printed the full length of the string,</p>



<p class="wp-block-paragraph">not any less,</p>



<p class="wp-block-paragraph">and it also didn&#8217;t print more than the length of the string,</p>



<p class="wp-block-paragraph">i.e. junk data,</p>



<p class="wp-block-paragraph">because it knew exactly how long the string was.</p>



<p class="wp-block-paragraph">was and this is way better than printing one character at a time in terms of efficiency we</p>



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



<p class="wp-block-paragraph">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 class="wp-block-paragraph">of the program here so in the looper function we called on print null terminated string and we</p>



<p class="wp-block-paragraph">didn&#8217;t do anything else so what i would like to do is just make an explicit call to string length</p>



<p class="wp-block-paragraph">explicit call to string length inside of the lubr function just to get the length of the</p>



<p class="wp-block-paragraph">null terminated string so we can just print it to the caller or print it to the user</p>



<p class="wp-block-paragraph">and then I&#8217;m going to use my special library function here actually just just for your</p>



<p class="wp-block-paragraph">information notice how I&#8217;m calling string length just like the the print null terminated string</p>



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



<p class="wp-block-paragraph">So now I can just print r12</p>



<p class="wp-block-paragraph">Well not yet, I&#8217;m gonna print a prefix if you look at the prefix here, it&#8217;s just</p>



<p class="wp-block-paragraph">The null terminated strings length was and then I&#8217;ll print a number after that</p>



<p class="wp-block-paragraph">You do it this way, you know your program is more pretty it&#8217;s more</p>



<p class="wp-block-paragraph">It&#8217;s more nice to the user and so forth so I&#8217;m going to do this</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">show them the length of the string. And then I use my external function that just prints a number to</p>



<p class="wp-block-paragraph">the user. Again, this video is not about this library. You can use some other library if you</p>



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



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Run it again now it says here&#8217;s the null terminated string and then on the next line it just says</p>



<p class="wp-block-paragraph">The null terminated strings length was that was the prefix and then when I called my library</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">So we have basically proved that this works.</p>



<p class="wp-block-paragraph">We have leveraged our knowledge of while loops to implement a string length function, which</p>



<p class="wp-block-paragraph">will let us have a printing function that is very smart.</p>



<p class="wp-block-paragraph">So we don&#8217;t have to hard code string lengths up at the top anymore.</p>



<p class="wp-block-paragraph">As long as we&#8217;re working with null terminated strings, everything will just work out now</p>



<p class="wp-block-paragraph">with less variables or less defines.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">I think that&#8217;s pretty much everything that I wanted to talk to you about.</p>



<p class="wp-block-paragraph">I don&#8217;t know. Could I do,</p>



<p class="wp-block-paragraph">could I do this easy, easily?</p>



<p class="wp-block-paragraph">Loop top.</p>



<p class="wp-block-paragraph">Okay. Yeah. I think I could probably do this reasonably.</p>



<p class="wp-block-paragraph">So at this point,</p>



<p class="wp-block-paragraph">you are satisfied that you understand how to implement this and you&#8217;re happy just cut the</p>



<p class="wp-block-paragraph">video the rest of this video is going to be me sort of like improvising trying to figure out if</p>



<p class="wp-block-paragraph">i can rearrange the logic in a fast enough time for a video uh just to show you that you know you</p>



<p class="wp-block-paragraph">should you should probably write your loops a little bit better than i did so here we go but</p>



<p class="wp-block-paragraph">this is this is just redundant stuff so we have our loop here and we have our initialization</p>



<p class="wp-block-paragraph">The loop top, it should compare R12 to 0 and it should break the loop if it is a 0.</p>



<p class="wp-block-paragraph">So that means I&#8217;m going to comment out this.</p>



<p class="wp-block-paragraph">And I&#8217;m going to do jump not equal to 0 to the body.</p>



<p class="wp-block-paragraph">And I just need to make a label for the body here.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say str lane loop bottom.</p>



<p class="wp-block-paragraph">So there&#8217;s a label, which is the body.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do a comment here just to remind us that this is actually the body.</p>



<p class="wp-block-paragraph">I guess I&#8217;ll do another comment right here.</p>



<p class="wp-block-paragraph">So that&#8217;s the loop&#8217;s body.</p>



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



<p class="wp-block-paragraph">Otherwise, we fall through to the next instruction,</p>



<p class="wp-block-paragraph">and that will just be an unconditional jump to the done area.</p>



<p class="wp-block-paragraph">Okay, and then when we&#8217;re inside the loop&#8217;s body, we&#8217;ll jump back up to the top.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I thought this was going to be hard.</p>



<p class="wp-block-paragraph">Let me run this to make sure I didn&#8217;t break the program.</p>



<p class="wp-block-paragraph">Yeah, it still works.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">I guess I overestimated the difficulty there.</p>



<p class="wp-block-paragraph">The point being, the body is a lot closer to the top of the loop.</p>



<p class="wp-block-paragraph">So that should be the thing that does a conditional branch.</p>



<p class="wp-block-paragraph">You should conditionally branch to the body because it&#8217;s a shorter jump and therefore</p>



<p class="wp-block-paragraph">much less likely to be out of bounds of that 128 conditional jump bite restriction.</p>



<p class="wp-block-paragraph">And then when we fall through to the next line, because we did not do that jump,</p>



<p class="wp-block-paragraph">because we did not do that jump then we&#8217;ll do an unconditional jump to the done area and you know</p>



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



<p class="wp-block-paragraph">loop is huge that you definitely want an unconditional jump that goes to the done area</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">the end of the body so when you&#8217;re jumping large uh you know spans you want to use unconditional</p>



<p class="wp-block-paragraph">Alright, so I guess that&#8217;s it.</p>



<p class="wp-block-paragraph">I&#8217;m going to erase maybe this comment.</p>



<p class="wp-block-paragraph">Well, I&#8217;ll leave that in there just for posterity.</p>



<p class="wp-block-paragraph">And now I will officially say that I hope you had a good time watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and I hope you had a little bit of fun.</p>



<p class="wp-block-paragraph">I will see you in the next video.</p>



<p class="wp-block-paragraph">I&#8217;m going to go play some video games.</p>



<p class="wp-block-paragraph">Maybe I&#8217;m going to eat some soup first.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p class="wp-block-paragraph">a small little favor, could you please subscribe and follow this channel or these videos or</p>



<p class="wp-block-paragraph">whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">to keep making videos in general. So please do me a kindness and subscribe. You know, sometimes</p>



<p class="wp-block-paragraph">I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed</p>



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



<p class="wp-block-paragraph">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 class="wp-block-paragraph">in the middle of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what</p>



<p class="wp-block-paragraph">will happen. Also, if you look at the middle of the screen right now, you should see a QR code,</p>



<p class="wp-block-paragraph">you should see a QR code which you can scan in order to go to the website which I think is also</p>



<p class="wp-block-paragraph">named somewhere at the bottom of this video and it&#8217;ll take you to my main website where you can</p>



<p class="wp-block-paragraph">just kind of like see all the videos I published and the services and tutorials and things that I</p>



<p class="wp-block-paragraph">offer and all that good stuff and if you have a suggestion for clarifications or errata or just</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">what&#8217;s going on? You know, just send me a comment, whatever. I also wake up for those in the middle</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">the world to me. I would really appreciate it. So again, thank you so much for watching this video</p>



<p class="wp-block-paragraph">and enjoy the cool music as, as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Great for students learning assembly after C/C++, OSdev hobbyists, or reverse engineering beginners.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hey there! In this video I&#8217;m going to teach you how to implement for loops in YASM x86-64 assembly.</p>



<p class="wp-block-paragraph">Although if you&#8217;re using a different type of machine or a different assembler,</p>



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



<p class="wp-block-paragraph">Okay, so for starters here, what am I even talking about? Let&#8217;s do…</p>



<p class="wp-block-paragraph">are you even talking about dude imagine in a higher level language we have a for loop here</p>



<p class="wp-block-paragraph">and so we have like four size type i equals zero i is less than 99 i plus plus right probably most</p>



<p class="wp-block-paragraph">programmers looking up assembly uh on the internet probably are interested in uh or probably already</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Well, you know, this for loop is going to run 99 times.</p>



<p class="wp-block-paragraph">It&#8217;s going to start at zero.</p>



<p class="wp-block-paragraph">And, you know, so we have like an initialization part right here.</p>



<p class="wp-block-paragraph">Maybe I should copy paste this and kind of break down the parts.</p>



<p class="wp-block-paragraph">We have an init part here for the for loop.</p>



<p class="wp-block-paragraph">We&#8217;ll say init part.</p>



<p class="wp-block-paragraph">And then here we have a check part.</p>



<p class="wp-block-paragraph">We&#8217;ll continue to loop as long as this expression is true.</p>



<p class="wp-block-paragraph">You can make a big complicated expression if you want to,</p>



<p class="wp-block-paragraph">but we&#8217;ll just keep it simple for this video.</p>



<p class="wp-block-paragraph">So I&#8217;ll call this the check part.</p>



<p class="wp-block-paragraph">And then we have sort of an update or a maintenance part.</p>



<p class="wp-block-paragraph">an update or a maintenance part. So I&#8217;m going to call this the update part where we increment our</p>



<p class="wp-block-paragraph">counter variable, move pointers, even call functions, do whatever it is you think you need</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">integers. But in this case, I think it&#8217;s better, it&#8217;s easier to understand what the loop is doing</p>



<p class="wp-block-paragraph">if we just kind of break down these parts. Anyway, so what is really a while loop? Sorry,</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">I&#8217;m going to initialize a variable size type I equals zero, right?</p>



<p class="wp-block-paragraph">That&#8217;s the initialization part that&#8217;ll only happen one time.</p>



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



<p class="wp-block-paragraph">the check part of the for loop.</p>



<p class="wp-block-paragraph">So I&#8217;ll just say while i is less than 99.</p>



<p class="wp-block-paragraph">And then for the update part, we just have to stick that inside of the while loop somewhere.</p>



<p class="wp-block-paragraph">Depending on your logic, you might want to put it at the top or the middle or the bottom.</p>



<p class="wp-block-paragraph">I&#8217;m going to put it at the bottom because I&#8217;m going to expect that it happens right</p>



<p class="wp-block-paragraph">before we do the next check.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So maybe if I update this code to print something, it&#8217;ll seem a little bit more clear.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">And then I can also put this inside of the while loop.</p>



<p class="wp-block-paragraph">And now we have transformed our for loop into a while loop.</p>



<p class="wp-block-paragraph">transform your for loop into a while loop it&#8217;s pretty easy to implement in assembly if you</p>



<p class="wp-block-paragraph">already know how to implement while loops so i should again say that there are lots of parts</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">not going to that i&#8217;m not going to explain specifically because i&#8217;ve already explained</p>



<p class="wp-block-paragraph">them in other videos so for example implementing a while loop is in another video so if you don&#8217;t</p>



<p class="wp-block-paragraph">already know how to implement while loops if you don&#8217;t know how to use conditional branching or</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">then you should probably check out my other videos first.</p>



<p class="wp-block-paragraph">But for now, I&#8217;m just going to assume that you know how to implement a while loop.</p>



<p class="wp-block-paragraph">And really the lesson is, hey, just take your for loop and convert it into a while loop.</p>



<p class="wp-block-paragraph">And then you can implement the while loop pretty easily.</p>



<p class="wp-block-paragraph">That&#8217;s the secret as far as I&#8217;m concerned.</p>



<p class="wp-block-paragraph">So imagine this now. Let&#8217;s do some assembly code.</p>



<p class="wp-block-paragraph">I have a source code file here called looper.asm for assembly.</p>



<p class="wp-block-paragraph">And you can imagine that I have a hybrid program running under the hood.</p>



<p class="wp-block-paragraph">I&#8217;m not going to show you all the code involved.</p>



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



<p class="wp-block-paragraph">We&#8217;re just going to write the assembly here.</p>



<p class="wp-block-paragraph">So, you know, if you&#8217;re writing pure assembly at home to practice, that&#8217;s fine.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So first off, I&#8217;m going to copy paste my data section.</p>



<p class="wp-block-paragraph">paste my data section of my assembly program. The first thing to note is we have a bunch of</p>



<p class="wp-block-paragraph">C strings here. We&#8217;re basically going to be telling the user, hey, we&#8217;re going to begin</p>



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



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



<p class="wp-block-paragraph">the number minus one, which is like the typical four loop that you usually write in the most</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">going to print the number back to the user that we&#8217;re looping through so if the user enters a</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">assembly already just c strings that will print with system calls see my other videos if you don&#8217;t</p>



<p class="wp-block-paragraph">And then a CRLF string, just basically doing a new line, a carriage return new line feed</p>



<p class="wp-block-paragraph">on the system.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to output using system call code one.</p>



<p class="wp-block-paragraph">And we&#8217;re going to use file descriptor one so we can print to standard output.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So now the real fun begins.</p>



<p class="wp-block-paragraph">Let&#8217;s start our text section, which is where the instructions of our assembly program go.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do text section, section text right there.</p>



<p class="wp-block-paragraph">And I&#8217;m going to use two external functions to just help me input and output numbers to the user.</p>



<p class="wp-block-paragraph">If you wanted to, you could use a system call that just inputted a character.</p>



<p class="wp-block-paragraph">And then you could just kind of like loop printing various characters.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Or if they hit, you know, F, it&#8217;ll just print F all the way to Z.</p>



<p class="wp-block-paragraph">to z you could do that without using an external library for printing integers this video is not</p>



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



<p class="wp-block-paragraph">hard code the start and end points when you&#8217;re practicing um you could you could use a different</p>



<p class="wp-block-paragraph">library or a different function call to get the inputs you could use a system call just to input</p>



<p class="wp-block-paragraph">one character um or you could you know use a hybrid program to to utilize printf and scanf</p>



<p class="wp-block-paragraph">f. Either way I&#8217;m just going to be using these two functions just to like get input and output.</p>



<p class="wp-block-paragraph">It&#8217;s not really part of the idea of looping. So now let&#8217;s start our entry point.</p>



<p class="wp-block-paragraph">Our function is called looper. Since this is a hybrid program you can imagine there&#8217;s a C++</p>



<p class="wp-block-paragraph">module elsewhere calling on the looper function and so that&#8217;s why I mark it as global so it can</p>



<p class="wp-block-paragraph">be called upon. And then I have another function called for test. I don&#8217;t really know why I chose</p>



<p class="wp-block-paragraph">it this way but i wanted to make another function that was called upon by our entry point here</p>



<p class="wp-block-paragraph">so the looper function really doesn&#8217;t do anything except recall the for test function</p>



<p class="wp-block-paragraph">so now let&#8217;s start the for test function actually maybe this is where the fun begins</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">And then we&#8217;re going to use R13 to keep track of where we&#8217;re going.</p>



<p class="wp-block-paragraph">So let&#8217;s see.</p>



<p class="wp-block-paragraph">I&#8217;m going to start by saying let&#8217;s preserve R12 and R13</p>



<p class="wp-block-paragraph">because you have to respect the application binary interface, the ABI,</p>



<p class="wp-block-paragraph">and that designates R12 and R13 as Kali saved registers.</p>



<p class="wp-block-paragraph">If I don&#8217;t preserve those and my program is even a little bit complicated,</p>



<p class="wp-block-paragraph">I&#8217;m probably going to be debugging forever</p>



<p class="wp-block-paragraph">debugging forever or I&#8217;m going to just crash my program for no reason. So I&#8217;m going to just</p>



<p class="wp-block-paragraph">do a push pop pair. Notice how the pops are in reverse order. This is not a push pop video,</p>



<p class="wp-block-paragraph">but just so you know, I guess while I&#8217;m here, I&#8217;m going to copy paste my crlf function,</p>



<p class="wp-block-paragraph">which really does nothing. It just prints out the crlf string with a system call. That&#8217;s all</p>



<p class="wp-block-paragraph">Okay, so do I even need CRLF in this program?</p>



<p class="wp-block-paragraph">I think I just modified this.</p>



<p class="wp-block-paragraph">Maybe I don&#8217;t even need it anymore.</p>



<p class="wp-block-paragraph">No, I guess I do.</p>



<p class="wp-block-paragraph">The first thing we&#8217;ll do is we&#8217;ll print an introduction message to the user.</p>



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



<p class="wp-block-paragraph">message to the user.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to call CRLF, which will just give us a new line.</p>



<p class="wp-block-paragraph">And yeah, you can hard code, you know, the 13, 10 at the end of these strings, but I</p>



<p class="wp-block-paragraph">don&#8217;t really like doing that.</p>



<p class="wp-block-paragraph">doing that. Okay, so we should have a working program at this point. Let me see if it actually</p>



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



<p class="wp-block-paragraph">driver code, but that&#8217;s what it&#8217;s doing. And then the for test prints the welcome message that we</p>



<p class="wp-block-paragraph">just added. And then the driver lets us know it&#8217;s retained control. And then it&#8217;s responsible for</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">we&#8217;re going to ask the user for a number so that we know how many times to loop.</p>



<p class="wp-block-paragraph">And again, you could hard code this number if you don&#8217;t want to do IO right now.</p>



<p class="wp-block-paragraph">You could even print a character a certain number of times</p>



<p class="wp-block-paragraph">if you don&#8217;t want to even deal with printing an integer.</p>



<p class="wp-block-paragraph">But I&#8217;m going to ask the user for a number.</p>



<p class="wp-block-paragraph">That&#8217;s going to be the prompt string.</p>



<p class="wp-block-paragraph">And if we run it again, now you should see it asks the user for a number.</p>



<p class="wp-block-paragraph">So it&#8217;s going to say, please enter a number.</p>



<p class="wp-block-paragraph">The loop will print from zero to N minus one.</p>



<p class="wp-block-paragraph">print from 0 to n minus 1. It doesn&#8217;t actually ask for the number though. It doesn&#8217;t actually</p>



<p class="wp-block-paragraph">I guess take the number so that&#8217;s going to be my external library that this video is not about</p>



<p class="wp-block-paragraph">where I just call a function called input assigned 64 integer and I&#8217;m going to receive that back in</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">a little tricky so we&#8217;re going to start off with the initialization part remember we had several</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">of the way my mind is supposed to be wrapped around this concept. And so I made another comment</p>



<p class="wp-block-paragraph">here with four and then empty parentheses just to denote that some comments that come below</p>



<p class="wp-block-paragraph">I guess the top of the for loop</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">The function names</p>



<p class="wp-block-paragraph">to be Fortest underscore something and since I&#8217;m only doing one thing inside of the Fortest function</p>



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



<p class="wp-block-paragraph">parts inside of your function and it started getting a little cluttered you might want to have</p>



<p class="wp-block-paragraph">you know another label you know that just sort of another I guess like part to your label that names</p>



<p class="wp-block-paragraph">the part of your function you&#8217;re in although this is assembly it gets really hard really fast so</p>



<p class="wp-block-paragraph">really hard really fast so if your function is even a little bit complicated you should probably</p>



<p class="wp-block-paragraph">consider breaking it up into multiple functions if you can. We can&#8217;t really do that at this point</p>



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



<p class="wp-block-paragraph">the first thing we had to do to initialize was you know setting i to zero or you know whatever</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">That&#8217;s the first part, the update part where we set size type i equals zero.</p>



<p class="wp-block-paragraph">In fact, maybe I could do another copy paste of this.</p>



<p class="wp-block-paragraph">Where instead of using a size type, we&#8217;ll just say that the register r13 equals zero.</p>



<p class="wp-block-paragraph">I know that&#8217;s not going to make sense in a higher level language right now,</p>



<p class="wp-block-paragraph">but just so that the for loop looks a little bit more like assembly.</p>



<p class="wp-block-paragraph">We&#8217;ll keep going as long as r13 is less than r12.</p>



<p class="wp-block-paragraph">and then we increase R13.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to put ink R13</p>



<p class="wp-block-paragraph">so it looks more assembly-like,</p>



<p class="wp-block-paragraph">even though this completely and totally</p>



<p class="wp-block-paragraph">would not compile in C++.</p>



<p class="wp-block-paragraph">I hope that this helps your understanding a little bit.</p>



<p class="wp-block-paragraph">So yeah, we can do everything</p>



<p class="wp-block-paragraph">except for just the increase part at the very top.</p>



<p class="wp-block-paragraph">I&#8217;m going to choose to increase it at the bottom.</p>



<p class="wp-block-paragraph">If you wanted to, I guess you could start off R13</p>



<p class="wp-block-paragraph">as a negative number and then increase it at the top.</p>



<p class="wp-block-paragraph">But I personally don&#8217;t feel that&#8217;s like very clean.</p>



<p class="wp-block-paragraph">feel that&#8217;s like very clean it also forces you to use signed integers maybe you wanted to use an</p>



<p class="wp-block-paragraph">unsigned integer so you could get like a gigantic um maximum number that you looped up to i don&#8217;t</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">part of the actual like looping like every time you loop up to the top of the for loop you should</p>



<p class="wp-block-paragraph">not repeat that part again it should only happen once so the next label that i have is called loop</p>



<p class="wp-block-paragraph">have is called loop top and that&#8217;s just going to be the top of the loop that I</p>



<p class="wp-block-paragraph">continue to go back up to every time I want to see if we&#8217;re supposed to</p>



<p class="wp-block-paragraph">continue looping and then go into the loops body so maybe I should say that&#8217;s</p>



<p class="wp-block-paragraph">why this is named to underscore loop top and I don&#8217;t know you don&#8217;t have to do</p>



<p class="wp-block-paragraph">camel casing and your labels you know you could just do loop in it or whatever</p>



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



<p class="wp-block-paragraph">Remember that was the check part, right?</p>



<p class="wp-block-paragraph">So we&#8217;re going to check to see that R13 is still less than R12.</p>



<p class="wp-block-paragraph">If it is, we&#8217;ll continue with the for loop.</p>



<p class="wp-block-paragraph">If it&#8217;s not, then we jump out of the for loop.</p>



<p class="wp-block-paragraph">So that means probably the true case where R13 is indeed less than R12,</p>



<p class="wp-block-paragraph">that&#8217;s going to be a short jump just into the loop&#8217;s body.</p>



<p class="wp-block-paragraph">And the false case where R13 is not less than R12,</p>



<p class="wp-block-paragraph">that should probably end up being a much longer jump.</p>



<p class="wp-block-paragraph">on how big your your loop is maybe that jump is too long too far away for a conditional branching</p>



<p class="wp-block-paragraph">instruction to reach if you&#8217;ve watched my previous videos you should know already that</p>



<p class="wp-block-paragraph">the conditional branching instructions like jl like jump less than they can only reach about 128</p>



<p class="wp-block-paragraph">bytes away if you try to go further than that the assembler will actually stop and block you from</p>



<p class="wp-block-paragraph">finishing your compilation it&#8217;ll say i think it&#8217;s like jump out of range or something like that</p>



<p class="wp-block-paragraph">So you want to try to keep the short jump points with your conditional branches.</p>



<p class="wp-block-paragraph">And then in the false case, where the conditional branch doesn&#8217;t actually do anything,</p>



<p class="wp-block-paragraph">then it falls through to the next instruction where you will have an unconditional jump.</p>



<p class="wp-block-paragraph">And remember, the unconditional jumps, they don&#8217;t have a limitation of 128 bytes.</p>



<p class="wp-block-paragraph">They can jump like all over the place.</p>



<p class="wp-block-paragraph">Like they can jump anywhere, basically.</p>



<p class="wp-block-paragraph">that means if you think about it, we come in to the loop top right here.</p>



<p class="wp-block-paragraph">We immediately do a compare instruction and a conditional branch.</p>



<p class="wp-block-paragraph">So if R13 is less than R12, meaning we should continue to for loop,</p>



<p class="wp-block-paragraph">then we&#8217;ll just do a short jump into the loop&#8217;s body and actually execute its body.</p>



<p class="wp-block-paragraph">And if not, we will end up falling through to line 92,</p>



<p class="wp-block-paragraph">where there&#8217;s just an unconditional jump instruction that just says,</p>



<p class="wp-block-paragraph">all right, let&#8217;s jump all the way down to being done.</p>



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



<p class="wp-block-paragraph">Okay, so we&#8217;ve done that.</p>



<p class="wp-block-paragraph">Now let&#8217;s implement the loops body because the first thing that we did is we wanted to</p>



<p class="wp-block-paragraph">jump into the loop body to actually execute our instructions, which in this case, we&#8217;ll</p>



<p class="wp-block-paragraph">just be, you know, printing a number every time we loop and then increasing that number.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do a little like new line there.</p>



<p class="wp-block-paragraph">I&#8217;m going to paste the loop body.</p>



<p class="wp-block-paragraph">little comments to help remind myself that this is actually the loop&#8217;s body. So for test loop body</p>



<p class="wp-block-paragraph">that takes care of the branching instruction, hitting on that and actually going into the body.</p>



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



<p class="wp-block-paragraph">we&#8217;ll just say the body. What does it do? It just prints a little message to the user.</p>



<p class="wp-block-paragraph">If you look at the message for current number string, if I go up real fast,</p>



<p class="wp-block-paragraph">message for current number, it&#8217;s just an arrow. So the user&#8217;s going to see an arrow and then</p>



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



<p class="wp-block-paragraph">current number.</p>



<p class="wp-block-paragraph">And then every time it loops, it&#8217;s just going to continue printing that arrow with a number</p>



<p class="wp-block-paragraph">on it.</p>



<p class="wp-block-paragraph">So we&#8217;re doing that.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to say R13, which is the current counter variable, which started</p>



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



<p class="wp-block-paragraph">the first integer argument for a function call.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to call this other function here, which is not part of the video to just</p>



<p class="wp-block-paragraph">which is not part of the video to just say hey please print this number for me so the first</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">going to print a new line so that the cursor goes to the next you know line of the terminal</p>



<p class="wp-block-paragraph">so that&#8217;s all the body does it just kind of like prints the current number with an arrow and does</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">plus plus or in more assembly speak increasing the counter variable so i&#8217;m just going to increase r13</p>



<p class="wp-block-paragraph">and then that way the loop can progress you know we&#8217;re always looking at r13</p>



<p class="wp-block-paragraph">to decide if we need to stop or not you know we&#8217;re comparing r13 to r12</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">So the regular jump instruction doesn&#8217;t have that limitation. So now we&#8217;re jumping up to the loop top.</p>



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



<p class="wp-block-paragraph">We&#8217;re just jumping up to the top here where we immediately ask you know are we done?</p>



<p class="wp-block-paragraph">If we&#8217;re not done, we jump into the loop body and print another number.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So this is a simple for loop, but you can see what it&#8217;s doing, right?</p>



<p class="wp-block-paragraph">It&#8217;s just going to be printing a number over and over again as the number increases.</p>



<p class="wp-block-paragraph">And then eventually it&#8217;ll stop when it hits the correct number.</p>



<p class="wp-block-paragraph">The last thing we need is the loop done label.</p>



<p class="wp-block-paragraph">If you look back up at the top here on line 92, if R13 was not less than R12,</p>



<p class="wp-block-paragraph">less than r12 then execution would fall through to line 92 and there is our unconditional jump</p>



<p class="wp-block-paragraph">instruction basically saying if r13 is not less than r12 then we&#8217;ll jump to the loop done label</p>



<p class="wp-block-paragraph">which means we&#8217;re just totally finished with this loop so we have to make that real fast</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">to the restoration functions.</p>



<p class="wp-block-paragraph">Oh, did I ruin my return somewhere?</p>



<p class="wp-block-paragraph">Uh-oh, what did I do wrong?</p>



<p class="wp-block-paragraph">I lost my return statement.</p>



<p class="wp-block-paragraph">Did I accidentally delete that somehow?</p>



<p class="wp-block-paragraph">That&#8217;s a bad program.</p>



<p class="wp-block-paragraph">Or did I not even…</p>



<p class="wp-block-paragraph">Hmm, I wonder if it like…</p>



<p class="wp-block-paragraph">I wonder if I didn&#8217;t have my return statement</p>



<p class="wp-block-paragraph">and it fell through into the CRLF function</p>



<p class="wp-block-paragraph">and then the CRLF function returned to the caller</p>



<p class="wp-block-paragraph">to the caller on behalf of the for test function I don&#8217;t even know only all only</p>



<p class="wp-block-paragraph">the spirits know at this point I don&#8217;t know leave a comment or something if</p>



<p class="wp-block-paragraph">you know what happened but every function has to have its own return for</p>



<p class="wp-block-paragraph">sure so you know the loop done label is usually where you want to jump to when</p>



<p class="wp-block-paragraph">you know the for loop is finished or the while loop is finished when it comes to</p>



<p class="wp-block-paragraph">saying goodbye it probably would be a little bit more clear of me to add an</p>



<p class="wp-block-paragraph">you know for test you know say goodbye just so that I that I remember that this</p>



<p class="wp-block-paragraph">is this is the place where we&#8217;re done and this other place this is just</p>



<p class="wp-block-paragraph">something else that&#8217;s happening maybe I&#8217;m doing more instructions or more</p>



<p class="wp-block-paragraph">operations or calling a function or whatever so obviously if I did it this</p>



<p class="wp-block-paragraph">way then the loop done label would just end up falling through to the goodbye</p>



<p class="wp-block-paragraph">label and it would be fine but it&#8217;s just more visually clear I&#8217;m gonna take it</p>



<p class="wp-block-paragraph">Anyway, so at this point we might have a program that actually works.</p>



<p class="wp-block-paragraph">Let&#8217;s see if it does.</p>



<p class="wp-block-paragraph">What else do I need to add?</p>



<p class="wp-block-paragraph">No, I think we&#8217;re done with that.</p>



<p class="wp-block-paragraph">Okay, let&#8217;s try it.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">crashes.</p>



<p class="wp-block-paragraph">It worked on the first try.</p>



<p class="wp-block-paragraph">So I had a solution though.</p>



<p class="wp-block-paragraph">Not fair, but I could have typoed.</p>



<p class="wp-block-paragraph">Anyway, so it says we entered a five, the loop will print from zero to N minus one.</p>



<p class="wp-block-paragraph">print from zero to n minus one so we should see from zero to four and so then every iteration of</p>



<p class="wp-block-paragraph">the loop it&#8217;s just that little message we&#8217;re just printing a zero printing one printing a two you</p>



<p class="wp-block-paragraph">know the number increases because we did did that little inc instruction the increase instruction</p>



<p class="wp-block-paragraph">we unconditionally jump to the top of the loop where we decide if we&#8217;re supposed to uh finish</p>



<p class="wp-block-paragraph">you know be done with the loop by jumping to the done label eventually after we uh let&#8217;s see</p>



<p class="wp-block-paragraph">it prints this four here it&#8217;ll do the increase instruction at the bottom of the loop&#8217;s body</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">just increased the four it will see that five is definitely not less than five so that&#8217;s a false</p>



<p class="wp-block-paragraph">which means execution will you know fall through where the heck is that it&#8217;ll fall through to the</p>



<p class="wp-block-paragraph">line 92 jump instruction which is just the loop being done so that&#8217;s here where it says goodbye</p>



<p class="wp-block-paragraph">says goodbye and then we have successfully implemented a basic for loop</p>



<p class="wp-block-paragraph">all right so thank you so much for watching this video I hope you learned</p>



<p class="wp-block-paragraph">a little bit and had a little bit of fun I will see you in the next video happy</p>



<p class="wp-block-paragraph">coding and happy studying hey everybody thanks for watching this video again</p>



<p class="wp-block-paragraph">from the bottom of my heart I really appreciate it I do hope you did learn</p>



<p class="wp-block-paragraph">hope you did learn something and have some fun. If you could do me a please, a small little favor,</p>



<p class="wp-block-paragraph">could you please subscribe and follow this channel or these videos or whatever it is you do on the</p>



<p class="wp-block-paragraph">current social media website that you&#8217;re looking at right now. It would really mean the world to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">longer videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please</p>



<p class="wp-block-paragraph">and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and I just wake up because</p>



<p class="wp-block-paragraph">I know somebody subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s</p>



<p class="wp-block-paragraph">exactly what happens every single time. So you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">screen right now you should see a QR code which you can scan in order to go to the website which</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">see all the videos I published and the services and tutorials and things that I offer and all</p>



<p class="wp-block-paragraph">that good stuff and if you have a suggestion for clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">you know just send me a comment whatever I also wake up for those in the middle of the night I get</p>



<p class="wp-block-paragraph">I wake up in a cold sweat and I&#8217;m like,</p>



<p class="wp-block-paragraph">it would really mean the world to me.</p>



<p class="wp-block-paragraph">I would really appreciate it.</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video</p>



<p class="wp-block-paragraph">and enjoy the cool music as I fade into the darkness,</p>



<p class="wp-block-paragraph">which is coming for us all.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Lots of practical tips about label naming conventions, register preservation (R12 in this case), and debugging flow are included along the way.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">All right, hello there. In this video, I&#8217;m going to teach you how to implement while loops in YASM</p>



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



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



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



<p class="wp-block-paragraph">assembly program, if you don&#8217;t know how to make a make file, if you don&#8217;t know a lot</p>



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



<p class="wp-block-paragraph">videos because I explain all of those things at length.</p>



<p class="wp-block-paragraph">For this video, I&#8217;m just going to be talking about while loops only.</p>



<p class="wp-block-paragraph">So for starters, let&#8217;s see here and open up a little notepad here and just type a while</p>



<p class="wp-block-paragraph">loop and try to explain a little bit about it conceptually.</p>



<p class="wp-block-paragraph">And then after that, we&#8217;re going to, we&#8217;re going to write a program in Yasm to show that</p>



<p class="wp-block-paragraph">we can get this to work.</p>



<p class="wp-block-paragraph">So what is a while loop in a higher level language?</p>



<p class="wp-block-paragraph">We&#8217;ll just suppose C or C++.</p>



<p class="wp-block-paragraph">You can imagine whatever language you want, but typically in a while loop,</p>



<p class="wp-block-paragraph">we&#8217;re not talking about a do while loop, although that would be really easy</p>



<p class="wp-block-paragraph">to implement after watching this video.</p>



<p class="wp-block-paragraph">But in a regular while loop, you have the keyword while usually,</p>



<p class="wp-block-paragraph">and then you have some sort of an expression like does a equal B or whatever.</p>



<p class="wp-block-paragraph">So maybe I&#8217;ll just say does a equal B?</p>



<p class="wp-block-paragraph">Expression evaluates to true then the body of the while loop will execute and then when it&#8217;s done executing</p>



<p class="wp-block-paragraph">Execution will go back up to look at the expression again and make sure that it&#8217;s still true</p>



<p class="wp-block-paragraph">So the loop will run forever until that expression evaluates to false</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">And that means the body runs an endless number of times</p>



<p class="wp-block-paragraph">Also, if the expression evaluates to false on the very first run</p>



<p class="wp-block-paragraph">then the body of the while loop will just never run at all.</p>



<p class="wp-block-paragraph">So that&#8217;s, you know, a basic idea of a while loop in a higher level language.</p>



<p class="wp-block-paragraph">Hopefully you kind of already know that.</p>



<p class="wp-block-paragraph">So let&#8217;s look at the parts right here.</p>



<p class="wp-block-paragraph">If we imagine this as assembly,</p>



<p class="wp-block-paragraph">then probably the first thing we should do is label the part</p>



<p class="wp-block-paragraph">where we look at the expression and decide whether we&#8217;re going to continue or not, right?</p>



<p class="wp-block-paragraph">So I&#8217;m going to put a little label here</p>



<p class="wp-block-paragraph">and maybe we&#8217;ll call it a while top or something.</p>



<p class="wp-block-paragraph">You know, whatever you want to do.</p>



<p class="wp-block-paragraph">just to label the part of the code</p>



<p class="wp-block-paragraph">So that we know when we write assembly</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">The while loop that we&#8217;re implementing in assembly</p>



<p class="wp-block-paragraph">So I&#8217;m gonna say this is the top or you can call it the eval part whatever you want</p>



<p class="wp-block-paragraph">Then we&#8217;ll make another label down here. We&#8217;ll call it</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">expression, we just have to basically jump to that label called while top.</p>



<p class="wp-block-paragraph">And if we want to jump to the body to actually execute the code,</p>



<p class="wp-block-paragraph">then we just have to jump to that other label instead. We should also have a label at the</p>



<p class="wp-block-paragraph">very bottom after the whole while loop is over and we&#8217;ll call it, you know, while finished</p>



<p class="wp-block-paragraph">or while, whoops, finished or while done or while over something like that. So I&#8217;m just going to</p>



<p class="wp-block-paragraph">Now you can imagine that when we&#8217;re at the top of the while loop and we see that the</p>



<p class="wp-block-paragraph">expression is false, then we can just jump down to the done label, thereby exiting the</p>



<p class="wp-block-paragraph">entire while loop.</p>



<p class="wp-block-paragraph">Then of course we probably want to have some jump statements in here.</p>



<p class="wp-block-paragraph">So for example, the expression part here, it&#8217;s probably going to jump either to the</p>



<p class="wp-block-paragraph">body or either to the done area based on whether or not the expression evaluated to true or</p>



<p class="wp-block-paragraph">false.</p>



<p class="wp-block-paragraph">label while top you know which contains a conditional branch and then like an unconditional</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">through to the next statement and do an unconditional jump to the done label if you don&#8217;t</p>



<p class="wp-block-paragraph">know conditional branching this is another one of the topics that i&#8217;ve covered in previous videos</p>



<p class="wp-block-paragraph">as we implement the while loop.</p>



<p class="wp-block-paragraph">So just keep in mind, there are other videos that I have</p>



<p class="wp-block-paragraph">that you should watch first</p>



<p class="wp-block-paragraph">if you don&#8217;t understand conditional branching or jumps.</p>



<p class="wp-block-paragraph">The reason that I&#8217;m going to kind of design</p>



<p class="wp-block-paragraph">the jumps like this where I&#8217;m going to say,</p>



<p class="wp-block-paragraph">let&#8217;s do a conditional branch.</p>



<p class="wp-block-paragraph">And basically if the loop evaluates to true,</p>



<p class="wp-block-paragraph">we&#8217;ll jump into the body of the loop.</p>



<p class="wp-block-paragraph">And if it doesn&#8217;t evaluate to true,</p>



<p class="wp-block-paragraph">then the conditional branch will allow execution</p>



<p class="wp-block-paragraph">to fall through down to the next statement,</p>



<p class="wp-block-paragraph">which will simply contain a jump to the done portion.</p>



<p class="wp-block-paragraph">to the done portion so basically as soon as the while loop is ready to break it gets uh implemented</p>



<p class="wp-block-paragraph">by an unconditional jump which was fallen through to by the conditional branch that would only jump</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">uh to jump to the true area you know the the body of the while loop and remember conditional</p>



<p class="wp-block-paragraph">branching instructions have a maximum you know jump reach of about 128 bytes so if you try to</p>



<p class="wp-block-paragraph">128 bytes. So if you try to jump too far, like for example, if you wanted to, you could reverse</p>



<p class="wp-block-paragraph">the logic to where you could say, if an expression is true, let&#8217;s jump to the done portion and say,</p>



<p class="wp-block-paragraph">we&#8217;re going to break the loop. If the expression was false, we&#8217;ll jump into the body. You could</p>



<p class="wp-block-paragraph">do that. But then if the body of your while loop was too long, maybe it was so long that the last</p>



<p class="wp-block-paragraph">instruction was greater than 128 bytes away from the top. Then by the time you wanted to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">instructions away so that&#8217;s a limitation of contentional branching so I just I want to put</p>



<p class="wp-block-paragraph">the biggest jump on an unconditional jump instruction because it doesn&#8217;t have a limitation</p>



<p class="wp-block-paragraph">like that I was having a hard time imagining what I was about to say just now for the</p>



<p class="wp-block-paragraph">the reversal I guess I&#8217;m just going to ignore the reversal scenario and we&#8217;ll just do it the</p>



<p class="wp-block-paragraph">and we&#8217;ll just do it the regular way.</p>



<p class="wp-block-paragraph">Anyway, so we want to have a conditional jump that either jumps into the body or to the done area.</p>



<p class="wp-block-paragraph">And then at the very bottom, we want to have an unconditional jump to the top,</p>



<p class="wp-block-paragraph">which basically means every time we get to the bottom of the while loop body,</p>



<p class="wp-block-paragraph">we&#8217;re just going to jump up to the while top.</p>



<p class="wp-block-paragraph">Actually, I&#8217;ll just say to while top.</p>



<p class="wp-block-paragraph">Remember again, the regular jump instructions,</p>



<p class="wp-block-paragraph">maybe I&#8217;ll take out the U here.</p>



<p class="wp-block-paragraph">The regular jump instructions don&#8217;t have a limitation</p>



<p class="wp-block-paragraph">on how far they can jump.</p>



<p class="wp-block-paragraph">So that should be fine.</p>



<p class="wp-block-paragraph">And that&#8217;s the basic idea of implementing a while loop.</p>



<p class="wp-block-paragraph">Maybe I should draw just a quick diagram</p>



<p class="wp-block-paragraph">before we start looking at the code,</p>



<p class="wp-block-paragraph">just to make sure everybody of different learning styles</p>



<p class="wp-block-paragraph">understands what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">So we&#8217;ll, let&#8217;s see, eval.</p>



<p class="wp-block-paragraph">I&#8217;m still adjusting my pen and you know what?</p>



<p class="wp-block-paragraph">You know what? It looked a lot better before I hit record.</p>



<p class="wp-block-paragraph">Eval the expression.</p>



<p class="wp-block-paragraph">So I&#8217;m going to first evaluate the expression.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Then this will be the body of the while loop.</p>



<p class="wp-block-paragraph">I&#8217;ll say like the body.</p>



<p class="wp-block-paragraph">If it evaluates to false, let&#8217;s see.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">right so you can imagine we evaluate an expression using the compare instruction in assembly and</p>



<p class="wp-block-paragraph">based on the results of comparing something whatever it is that your condition is then we</p>



<p class="wp-block-paragraph">you know we we either branch to the done area or we branch to the to the body area</p>



<p class="wp-block-paragraph">want to have a more complicated while loop and you don&#8217;t want to put a huge</p>



<p class="wp-block-paragraph">amount of expressions or if you don&#8217;t want to logically concatenate a bunch of</p>



<p class="wp-block-paragraph">different components into the expression you know that&#8217;s fair I usually write my</p>



<p class="wp-block-paragraph">while loops in real life as just while true and then I break when certain</p>



<p class="wp-block-paragraph">conditions are met you can do that too we&#8217;re not going to talk about that in</p>



<p class="wp-block-paragraph">this video but you could just have a while true so you always jump to the top</p>



<p class="wp-block-paragraph">and there&#8217;s no branching that goes to while done but then throughout the loop</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">loop then it just automatically jumps to the top although you got to make sure with the the reach</p>



<p class="wp-block-paragraph">of conditional branching you probably want to test to see if your condition is true or false or</p>



<p class="wp-block-paragraph">whatever if it means we&#8217;re going to continue the loop then you probably just want to do a conditional</p>



<p class="wp-block-paragraph">then you probably just want to do a conditional branch that jumps down a little bit</p>



<p class="wp-block-paragraph">so that it can continue the body of the loop.</p>



<p class="wp-block-paragraph">And then the part that it skipped over,</p>



<p class="wp-block-paragraph">just a little part should be an unconditional jump that jumps out of the loop.</p>



<p class="wp-block-paragraph">That way, it doesn&#8217;t matter how big your while loop body is,</p>



<p class="wp-block-paragraph">you can always jump out of the loop.</p>



<p class="wp-block-paragraph">But we&#8217;re not going to talk about that in this video.</p>



<p class="wp-block-paragraph">Anyway, so we evaluate the expression.</p>



<p class="wp-block-paragraph">If it&#8217;s true, we go to the body.</p>



<p class="wp-block-paragraph">If it&#8217;s false, we go to done.</p>



<p class="wp-block-paragraph">After the body is done executing,</p>



<p class="wp-block-paragraph">then we just jump back up to the top where we evaluate the expression.</p>



<p class="wp-block-paragraph">where we evaluate the expression.</p>



<p class="wp-block-paragraph">I&#8217;ll put while here just to make it more clear that we&#8217;re talking about the basic</p>



<p class="wp-block-paragraph">idea of a while loop.</p>



<p class="wp-block-paragraph">Not too hard, you know.</p>



<p class="wp-block-paragraph">And then if you wanted to implement a do while loop, just make sure that you always</p>



<p class="wp-block-paragraph">evaluate the body at least once.</p>



<p class="wp-block-paragraph">That&#8217;s really the only difference.</p>



<p class="wp-block-paragraph">But that&#8217;s up to you.</p>



<p class="wp-block-paragraph">This is just a regular while loop video.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we&#8217;ve kind of talked about it a little bit.</p>



<p class="wp-block-paragraph">We&#8217;ve sort of, you know, drawn it out in a diagram and put some regular code in.</p>



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



<p class="wp-block-paragraph">again i&#8217;m not going to show you my make file because i&#8217;ve already made other videos where i</p>



<p class="wp-block-paragraph">explained how to make a make file from scratch same thing goes for hybrid programs i&#8217;m going</p>



<p class="wp-block-paragraph">to have a driver which is a c plus plus module that just sort of calls on this assembly module</p>



<p class="wp-block-paragraph">if you want to know how to make hybrid programs or you know drivers or you know whatever see my</p>



<p class="wp-block-paragraph">to be talking about looping only. Okay, so let me get my solution up here. My source code is called</p>



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



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



<p class="wp-block-paragraph">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 class="wp-block-paragraph">like the actual array of bytes and then a length and all I&#8217;m doing is printing out messages you</p>



<p class="wp-block-paragraph">know begin the wild test your numbers will be printed back to you until you decide to quit so</p>



<p class="wp-block-paragraph">that&#8217;s going to be printed to the user right away then every time the program wants a number from</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">the echo back it&#8217;ll say you entered and then it will actually print the number and then when you</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">like input and output numbers this video is not about uh input and outputting and external libraries</p>



<p class="wp-block-paragraph">if you&#8217;re interested you could probably just link a hybrid program and use printf and scan</p>



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



<p class="wp-block-paragraph">then i have crlf which is just a carriage return in line feed uh you know select the cursor goes</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Then a file descriptor just to print a standard output again. That&#8217;s explained in other videos</p>



<p class="wp-block-paragraph">So now I&#8217;m ready to start copy pasting the main portion of my program</p>



<p class="wp-block-paragraph">So first off I&#8217;m going to start the text section, which is where the instructions go in the ASM</p>



<p class="wp-block-paragraph">So there it is and then I&#8217;m going to copy paste</p>



<p class="wp-block-paragraph">of external symbols. So I just have a little library that I&#8217;m using that makes it easier for</p>



<p class="wp-block-paragraph">me to input and output integers. So you can do this any way you want, or you can hard code a</p>



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



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



<p class="wp-block-paragraph">looper. So this is my function. And again, this is a hybrid program. So the main function or the</p>



<p class="wp-block-paragraph">be present in my assembly module the driver is going to be a c plus plus module that just calls</p>



<p class="wp-block-paragraph">on a function named looper so that means this function is going to get called from another</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">other functions that i&#8217;m about to create one is called while test which is going to actually do</p>



<p class="wp-block-paragraph">the while loop and the other is called crlf which just print which just prints a new line for me i</p>



<p class="wp-block-paragraph">which just prints a new line for me.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I do it that way, but I want to.</p>



<p class="wp-block-paragraph">Sorry, not sorry.</p>



<p class="wp-block-paragraph">So this is the real meat of the function here.</p>



<p class="wp-block-paragraph">Let&#8217;s start, well, the real meat of the code or the video.</p>



<p class="wp-block-paragraph">Let&#8217;s start a function called while test.</p>



<p class="wp-block-paragraph">You can see it&#8217;s got a void signature with no arguments,</p>



<p class="wp-block-paragraph">so it doesn&#8217;t really, you know, take anything or return anything.</p>



<p class="wp-block-paragraph">I&#8217;m going to use register R12 to hold the user&#8217;s input</p>



<p class="wp-block-paragraph">so that I can, you know, print it out and stuff.</p>



<p class="wp-block-paragraph">print it out and stuff so that means I have to do a preservation of R12 because it&#8217;s a</p>



<p class="wp-block-paragraph">callee saved for the ABI which you should respect so I&#8217;m going to push it at the beginning</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">at the end of the function let me just double check that there&#8217;s nothing else weird at the</p>



<p class="wp-block-paragraph">bottom of that nope okay so we&#8217;ve got the prologue and epilogue the wild test function</p>



<p class="wp-block-paragraph">maybe I should copy paste my crlf real fast my crlf function it&#8217;s just sad but</p>



<p class="wp-block-paragraph">it&#8217;s also kind of cute right it does nothing except just print out a new line</p>



<p class="wp-block-paragraph">that&#8217;s all it does this video is not about that so at this point I should</p>



<p class="wp-block-paragraph">have a program that probably works let&#8217;s see if it does I&#8217;m gonna do clear and</p>



<p class="wp-block-paragraph">make run again if you want to know how to use make files or compile or link or</p>



<p class="wp-block-paragraph">anything like that see my other videos so I&#8217;m gonna run it and it just says</p>



<p class="wp-block-paragraph">which is some code that we&#8217;re not looking at.</p>



<p class="wp-block-paragraph">And then it says it&#8217;s regain control.</p>



<p class="wp-block-paragraph">And this line in the middle, which is just an empty new line,</p>



<p class="wp-block-paragraph">that&#8217;s definitely from the assembly program.</p>



<p class="wp-block-paragraph">Because at the top here we have CRLF.</p>



<p class="wp-block-paragraph">If I call it multiple times CRLF,</p>



<p class="wp-block-paragraph">then you&#8217;ll see there are multiple blank lines.</p>



<p class="wp-block-paragraph">All right, I&#8217;m gonna take that out.</p>



<p class="wp-block-paragraph">Now let&#8217;s continue with while test.</p>



<p class="wp-block-paragraph">So what should we do here?</p>



<p class="wp-block-paragraph">The first thing that we should do is print an intro message</p>



<p class="wp-block-paragraph">message just to let the user know that we&#8217;re about to you know begin our while</p>



<p class="wp-block-paragraph">tests and if we run the program one more time we should see that message now</p>



<p class="wp-block-paragraph">begin the while test your numbers will be printed back to you system calls and</p>



<p class="wp-block-paragraph">simple printing is covered in other videos so now uh you know for me I kind</p>



<p class="wp-block-paragraph">of like to init all of my loops even if it&#8217;s a while loop and not just a for</p>



<p class="wp-block-paragraph">loop or anything so I always have an extra label that I like to call init</p>



<p class="wp-block-paragraph">or before or something like that.</p>



<p class="wp-block-paragraph">So I have a label now called while test underscore init,</p>



<p class="wp-block-paragraph">and it&#8217;s just where I&#8217;m gonna initialize</p>



<p class="wp-block-paragraph">whatever it is that I think I need to initialize</p>



<p class="wp-block-paragraph">so that the loop will actually work.</p>



<p class="wp-block-paragraph">If you look here, all I&#8217;m really doing is setting R12 to zero</p>



<p class="wp-block-paragraph">because what I&#8217;m gonna do is stop the loop.</p>



<p class="wp-block-paragraph">I&#8217;m gonna break the loop whenever the user enters a 99.</p>



<p class="wp-block-paragraph">So I don&#8217;t know what&#8217;s inside of R12</p>



<p class="wp-block-paragraph">when we first start this function.</p>



<p class="wp-block-paragraph">And I just wanna make sure that it&#8217;s not 99,</p>



<p class="wp-block-paragraph">the stopping number at the very start so I&#8217;m just going to set it to zero. So</p>



<p class="wp-block-paragraph">another note about my labels you don&#8217;t have to do it this way but I love to</p>



<p class="wp-block-paragraph">write labels where the first part of the label is always the function that the</p>



<p class="wp-block-paragraph">label is inside of so notice how the function is named while test and so my</p>



<p class="wp-block-paragraph">label is always while test underscore something and I&#8217;m putting init here</p>



<p class="wp-block-paragraph">just to say we&#8217;re initializing the loop but if you had a function that had a</p>



<p class="wp-block-paragraph">had a lot of stuff going on in it for starters you should probably be breaking up that function</p>



<p class="wp-block-paragraph">into multiple functions but but assuming you didn&#8217;t uh you should probably do another underscore</p>



<p class="wp-block-paragraph">and then like another component and then another underscore based on what giant chunk of your</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">you probably want to you know stick that into your labels your labels will get huge but for me</p>



<p class="wp-block-paragraph">infused in assembly and this makes it easier.</p>



<p class="wp-block-paragraph">So we&#8217;re going to initialize so that we can run our while loop and then the next thing</p>



<p class="wp-block-paragraph">we&#8217;re going to do is implement the top of the while loop.</p>



<p class="wp-block-paragraph">So remember if we looked at this code up here, maybe if I drag this over to the side and</p>



<p class="wp-block-paragraph">pin it to the top for a little while, you can see that the top of the while loop is</p>



<p class="wp-block-paragraph">where we kind of evaluate the expression to see if we need to keep going or not.</p>



<p class="wp-block-paragraph">jump into the ending area, the done area. So the top, whoops, the top is always for that.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">In this particular while loop, we want to compare the user&#8217;s input, which is R12 to the number 99.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">So by the way, you&#8217;re probably wondering how did R12 get the user&#8217;s input?</p>



<p class="wp-block-paragraph">Well, we&#8217;re going to do that as the next step.</p>



<p class="wp-block-paragraph">And of course, your design pattern may vary a little bit.</p>



<p class="wp-block-paragraph">No, that&#8217;s okay.</p>



<p class="wp-block-paragraph">I just like to implement it this way.</p>



<p class="wp-block-paragraph">So first thing we&#8217;re going to do is compare R12 with 99.</p>



<p class="wp-block-paragraph">And that&#8217;s why I&#8217;ve written this comment here, just like the blank while comparison part.</p>



<p class="wp-block-paragraph">And I guess I could have put this R12 not equal to 99 up at the top, but then it kind</p>



<p class="wp-block-paragraph">of feels like I&#8217;m leaving these other two instructions.</p>



<p class="wp-block-paragraph">So I moved it down one.</p>



<p class="wp-block-paragraph">these other two instructions so I moved it down one so basically as long as you know we compare</p>



<p class="wp-block-paragraph">R12 and 99 compare and conditional branching is covered in other videos but we compare those two</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">will be like a short jump which will be within the range of a conditional branch</p>



<p class="wp-block-paragraph">So anyway, if the not equal branch didn&#8217;t happen, that means R12 is equal to 99.</p>



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



<p class="wp-block-paragraph">So basically we&#8217;re saying, if it&#8217;s true, we continue looping.</p>



<p class="wp-block-paragraph">If it&#8217;s not true, we just jump outside of the loop.</p>



<p class="wp-block-paragraph">We&#8217;re just totally done with the while loop.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then we need a body, because obviously we&#8217;re going to jump into the body here.</p>



<p class="wp-block-paragraph">that means I&#8217;m going to just copy paste another little set of code here.</p>



<p class="wp-block-paragraph">Right after that unconditional jump.</p>



<p class="wp-block-paragraph">So now we got the body.</p>



<p class="wp-block-paragraph">Notice how I put a little comment here that has a brace just to indicate to you,</p>



<p class="wp-block-paragraph">hey, this is the beginning of the actual while loop body,</p>



<p class="wp-block-paragraph">just to make it a little bit more clear.</p>



<p class="wp-block-paragraph">And what are we going to do inside of the body?</p>



<p class="wp-block-paragraph">We&#8217;re just going to ask the user for some input.</p>



<p class="wp-block-paragraph">So I&#8217;m printing a simple message here,</p>



<p class="wp-block-paragraph">and then I&#8217;m calling on my helper function</p>



<p class="wp-block-paragraph">to just actually input a number from the user.</p>



<p class="wp-block-paragraph">I&#8217;m going to store that number into R12.</p>



<p class="wp-block-paragraph">So that&#8217;s how R12 gets the numbers, gets the user&#8217;s input.</p>



<p class="wp-block-paragraph">And the way I&#8217;ve written this, if the user enters a 99,</p>



<p class="wp-block-paragraph">it&#8217;ll echo it back to the user and then it&#8217;ll break the loop afterwards.</p>



<p class="wp-block-paragraph">So, you know, if you wanted to rearrange things like I talked about before,</p>



<p class="wp-block-paragraph">where you input before you check to see if you&#8217;re going to keep going, you could do that.</p>



<p class="wp-block-paragraph">But it would be a little harder to echo the user&#8217;s input back to them before you break.</p>



<p class="wp-block-paragraph">I don&#8217;t know. It&#8217;s up to you.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">message basically saying here&#8217;s the thing that you inputted no problem and then again we use</p>



<p class="wp-block-paragraph">one of my helper functions to actually spit the number back out at them so this is not</p>



<p class="wp-block-paragraph">a very complicated body it&#8217;s just asking for a number and then printing the number back to them</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">we don&#8217;t really have to do this label but for clarity I think it&#8217;s probably a good idea</p>



<p class="wp-block-paragraph">I think it&#8217;s probably a good idea.</p>



<p class="wp-block-paragraph">So the very bottom of the while loop&#8217;s body is usually where you don&#8217;t do any more instructions</p>



<p class="wp-block-paragraph">that are part of the work of the while loop&#8217;s body,</p>



<p class="wp-block-paragraph">but just sort of the place where you jump back up to the top</p>



<p class="wp-block-paragraph">so you can evaluate and decide to continue or not again.</p>



<p class="wp-block-paragraph">So notice how I&#8217;m using an unconditional jump here.</p>



<p class="wp-block-paragraph">That&#8217;s a good idea because again, if you have like a huge while loop body,</p>



<p class="wp-block-paragraph">you might end up surpassing the threshold of 128 bytes</p>



<p class="wp-block-paragraph">of 128 bytes and then you&#8217;ll get a assembler error that says</p>



<p class="wp-block-paragraph">I can never remember this. It&#8217;s like a</p>



<p class="wp-block-paragraph">jump out of range error or something like that. Basically, if you do a conditional branch to jump up the top</p>



<p class="wp-block-paragraph">because some people like to check to see</p>



<p class="wp-block-paragraph">if they should continue the loop, they like to check for that at the bottom. I&#8217;ve done that before.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">end of the body. Okay, no problem. Now let&#8217;s do the done label, which is basically where we jump</p>



<p class="wp-block-paragraph">if the loop is actually finished. So remember, if this expression right here evaluates to false,</p>



<p class="wp-block-paragraph">then execution is going to fall through to line 84, where we jump to this while test done label.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">here on line nine of the little notepad. And we can just kind of do whatever we want. At that</p>



<p class="wp-block-paragraph">point, we can return to a caller, we can just do other stuff, we can, you know, do a different</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">goodbye with this little print to just sort of like, you know, print an exit message. And then</p>



<p class="wp-block-paragraph">we&#8217;ll do the epilogue where we restore R12. And then we just return to the caller,</p>



<p class="wp-block-paragraph">the driver worry about exiting the program okay so if we&#8217;ve done this correctly we should now have</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">99 the program will just continue forever so this is a while loop if i want to quit i do 99</p>



<p class="wp-block-paragraph">breaks at the top it breaks by jumping down to the done area where we print our goodbye message</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">immediately breaks you know it prints out what you entered but then it immediately breaks</p>



<p class="wp-block-paragraph">and that&#8217;s it that&#8217;s uh the basics for how to write a while loop you just use basically</p>



<p class="wp-block-paragraph">layered on top of an abstract concept of what you think a while loop is,</p>



<p class="wp-block-paragraph">or what I guess the world thinks a while loop is.</p>



<p class="wp-block-paragraph">Okay, thank you so much for watching this video.</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and had a little bit of fun.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">if you could do me a please a small little favor could you please subscribe and follow this channel</p>



<p class="wp-block-paragraph">or these videos or whatever it is you do on the current social media website that you&#8217;re looking</p>



<p class="wp-block-paragraph">at right now it would really mean the world to me and it&#8217;ll help make more videos and grow this</p>



<p class="wp-block-paragraph">community so we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able</p>



<p class="wp-block-paragraph">to keep making videos in general so please do do me a kindness and uh and subscribe you know</p>



<p class="wp-block-paragraph">I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video.</p>



<p class="wp-block-paragraph">and it&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p class="wp-block-paragraph">i published and the services and tutorials and things that i offer and all that good stuff and</p>



<p class="wp-block-paragraph">if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">just send me a comment whatever i also wake up for those in the middle of the night i get</p>



<p class="wp-block-paragraph">it would really mean the world to me. I would really appreciate it.</p>



<p class="wp-block-paragraph">So again, thank you so much for watching this video and</p>



<p class="wp-block-paragraph">enjoy the cool music as I fade into</p>



<p class="wp-block-paragraph">the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">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>
		<item>
		<title>Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &#038; More</title>
		<link>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/</link>
					<comments>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 18 Jan 2026 00:49:38 +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[assembly programming]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional branching]]></category>
		<category><![CDATA[conditional jumps]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[JE JNE]]></category>
		<category><![CDATA[JL JG]]></category>
		<category><![CDATA[jump out of range]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[operating systems programming]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[signed unsigned jumps]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=287</guid>

					<description><![CDATA[<p>Learn how conditional branching really works in x86-64 assembly using CMP and conditional jumps like JE, JNE, JL, JG. See signed vs unsigned comparison instructions and real code examples that show how if-statements are implemented at the machine level.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</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="Learn Conditional Branching in x86-64 Assembly (YASM/NASM) - CMP, JE, JNE, JL, JG &amp; More" width="1380" height="776" src="https://www.youtube.com/embed/aB_BOp0KCh8?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 class="wp-block-paragraph">In this practical x86-64 assembly language tutorial we dive deep into conditional branching using YASM (also works with NASM) on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">We cover:</p>



<ul class="wp-block-list">
<li>The CMP instruction and how it sets the RFLAGS register</li>



<li>All major conditional jump instructions (JE, JNE, JL, JLE, JG, JGE, JB, JBE…)</li>



<li>Important difference between signed (JL/JG) vs unsigned (JB/JA) jumps</li>



<li>Real code examples showing how to implement if-style decisions</li>



<li>How to handle the &#8220;jump out of range&#8221; assembler error</li>



<li>Visual flow diagrams explaining control flow</li>
</ul>



<p class="wp-block-paragraph">Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.</p>



<p class="wp-block-paragraph">Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p class="wp-block-paragraph">Enjoy learning assembly!</p>



<p class="wp-block-paragraph">Intro to Conditional Branching in Assembly 00:00:00<br>Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23<br>Unconditional vs Conditional Jumps 00:01:39<br>How Conditional Branching Works 00:02:06<br>The CMP Instruction Explained 00:03:21<br>Overview of Conditional Jump Instructions 00:04:50<br>Signed vs Unsigned Jump Instructions 00:06:10<br>Jump Equal &amp; Jump Not Equal (JE/JNE) 00:07:00<br>Main Conditional Jumps Summary 00:08:14<br>Starting the Code Example 00:09:09<br>Program Structure &amp; Printing Setup 00:10:35<br>Function Prologue &amp; Getting User Input 00:13:21<br>First Branch: Is Number Greater Than 0? 00:15:17<br>Live Demo: Testing Greater Than Zero 00:21:53<br>Second Branch: Is Number Less Than 10? 00:22:52<br>Live Demo: Testing Range 0-10 00:26:06<br>Third Branch: Is Number == 5? 00:26:52<br>Live Demo: Testing Equal to 5 00:28:00<br>Jump Out of Range Error &amp; Workaround 00:29:00<br>Final Summary &amp; Closing 00:32:17<br>Thanks &amp; Call to Subscribe 00:32:57</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hey there. In this video,</p>



<p class="wp-block-paragraph">we&#8217;re going to talk about conditional branching in an assembly program.</p>



<p class="wp-block-paragraph">We&#8217;re going to be looking at YASM x86-64 assembly or AMD 64 assembly</p>



<p class="wp-block-paragraph">within Ubuntu, but this will probably be a useful video.</p>



<p class="wp-block-paragraph">If you&#8217;re just thinking about how to branch in assembly in general.</p>



<p class="wp-block-paragraph">So for starters, I&#8217;m going to promote one of my favorite books,</p>



<p class="wp-block-paragraph">as I often do. This is a book that is free and open source. You can get a copy yourself. I did</p>



<p class="wp-block-paragraph">not write this. The author of this book is a genius. His name is Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">He made this whole book and released it under an open source license. So you can literally just go</p>



<p class="wp-block-paragraph">to his website and get a copy of this for free. You can use this to make yourself into an assembly</p>



<p class="wp-block-paragraph">expert. So I highly recommend this to everybody. Anyway, so I&#8217;m going to open up this book. I&#8217;m</p>



<p class="wp-block-paragraph">up this book i&#8217;m going to assume that maybe you&#8217;ll get a copy or you&#8217;re just going to follow along</p>



<p class="wp-block-paragraph">with what i&#8217;m doing on the screen but i&#8217;m going to open this book and i&#8217;m going to go to section 7</p>



<p class="wp-block-paragraph">which is entitled instruction set overview i&#8217;m going to double click it so open out open the</p>



<p class="wp-block-paragraph">subsections and then i&#8217;m going to click on 7.7 control control instructions and then under that</p>



<p class="wp-block-paragraph">i&#8217;m going to click on 7.7.3 conditional control instructions so uh that&#8217;s a lot of subsections</p>



<p class="wp-block-paragraph">So that&#8217;s a lot of subsections, isn&#8217;t it? I love a book with a ton of subsections. It makes things so much easier to find</p>



<p class="wp-block-paragraph">If you or someone you know is going to write a book, especially if it&#8217;s technical, please use lots and lots of subsections</p>



<p class="wp-block-paragraph">Anyway, so conditional control instructions in my last video</p>



<p class="wp-block-paragraph">We talked about the jump instruction where we can just be sitting somewhere in our program and then just jump somewhere else</p>



<p class="wp-block-paragraph">Anywhere we want based on the label so conditional branching or conditional control instructions</p>



<p class="wp-block-paragraph">conditional control instructions are kind of the same thing except they will</p>



<p class="wp-block-paragraph">decide whether or not to actually jump they might fall through to the next</p>



<p class="wp-block-paragraph">instruction or they might jump away to somewhere else so conditional branching</p>



<p class="wp-block-paragraph">or conditional jumping it basically kind of goes like this step one you compare</p>



<p class="wp-block-paragraph">two values to each other they will probably be sitting in registers and then</p>



<p class="wp-block-paragraph">conditional branching instruction or a conditional jump instruction there&#8217;s</p>



<p class="wp-block-paragraph">lots of synonyms for these but basically in the regular jump instruction that I</p>



<p class="wp-block-paragraph">just talked about in the last video you just jumped and it was only one thing</p>



<p class="wp-block-paragraph">that you had to do but in this case we first have to compare two items and then</p>



<p class="wp-block-paragraph">we&#8217;ll issue the jumping instruction because the jumping instruction will</p>



<p class="wp-block-paragraph">have to decide whether to jump or not based on the result of your comparison</p>



<p class="wp-block-paragraph">which makes sense right like if you&#8217;re going to implement something that&#8217;s like</p>



<p class="wp-block-paragraph">if you know I&#8217;ll put like a expr for expression then well the first thing that we have to decide</p>



<p class="wp-block-paragraph">before we jump either into the block of the if or if we jump into the block of the else is whether</p>



<p class="wp-block-paragraph">or not that expression is true right so that&#8217;s kind of what the compare instruction is doing</p>



<p class="wp-block-paragraph">sort of in higher level languages like c++ it can get a lot more complicated than that but you know</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to do conditional branching instructions.</p>



<p class="wp-block-paragraph">Let me show you a few of those.</p>



<p class="wp-block-paragraph">Okay, so for starters, here&#8217;s the compare instruction right here on page 126.</p>



<p class="wp-block-paragraph">I think it&#8217;s actually 127 in the PDF, but basically, you know, 126.</p>



<p class="wp-block-paragraph">We do CMP followed by two operands.</p>



<p class="wp-block-paragraph">So if we want to compare, I don&#8217;t know, let&#8217;s say R12 with R13, that&#8217;s how you would do it.</p>



<p class="wp-block-paragraph">just a two operand instruction there&#8217;s no results that are going to get stored in one of these</p>



<p class="wp-block-paragraph">operands both of these operands are just for input the result actually gets stored in a special</p>



<p class="wp-block-paragraph">register called r flags which we hardly ever use or look at in in the most common assembly</p>



<p class="wp-block-paragraph">instructions and design patterns we just kind of like let the r flags do whatever it&#8217;s going to do</p>



<p class="wp-block-paragraph">and the compare instruction will fill that up so imagine that you know depending on whether r13 is</p>



<p class="wp-block-paragraph">is like greater than r12 or less than or they&#8217;re equal or something else is going on between the</p>



<p class="wp-block-paragraph">two of them the compare instruction just fills up all sorts of flags defining the relationship</p>



<p class="wp-block-paragraph">between those two registers or those two operands r12 and r13 and then after the r flags register</p>



<p class="wp-block-paragraph">is all set up then the conditional branching instruction will sort of rely on r flags in</p>



<p class="wp-block-paragraph">order to decide whether or not it&#8217;s going to jump or not okay so let&#8217;s see there are tons</p>



<p class="wp-block-paragraph">there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



<p class="wp-block-paragraph">of conditional branching instructions we can use the first two i want to show you and after this</p>



<p class="wp-block-paragraph">by the way i&#8217;m going to write a sample program just to show you how it is in practice but so the</p>



<p class="wp-block-paragraph">first one is je which means let&#8217;s jump if the two operands were equal so again you know we compare</p>



<p class="wp-block-paragraph">here we&#8217;ll say sets up r flags and then we&#8217;ll decide like what we&#8217;re going to do are we going</p>



<p class="wp-block-paragraph">to jump if they&#8217;re equal to some label i&#8217;ll write some label here so that means uh the je instruction</p>



<p class="wp-block-paragraph">will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p class="wp-block-paragraph">did then we&#8217;ll jump if not execution will fall down to the next line so i&#8217;ll put a nope instruction</p>



<p class="wp-block-paragraph">So if they were equal, then we&#8217;ll end up jumping to some label.</p>



<p class="wp-block-paragraph">And if they were not equal, we&#8217;ll end up letting execution fall through to the nope on line 10.</p>



<p class="wp-block-paragraph">And same thing for jump not equal and all these other ones.</p>



<p class="wp-block-paragraph">But there are actually two different versions of some of these instructions that kind of seem the same.</p>



<p class="wp-block-paragraph">Like for instance, this one right here, JL, that means jump if the first operand was less than the second operand.</p>



<p class="wp-block-paragraph">is a jump below that jumps also if the first operand was less than the second operand so</p>



<p class="wp-block-paragraph">what&#8217;s the difference between these two let me get my pen I love excuses to do my little pen now</p>



<p class="wp-block-paragraph">well I&#8217;ve got my pen it&#8217;s working oh I erased it okay okay it&#8217;s working anyway so notice how jump</p>



<p class="wp-block-paragraph">less than and jump below are basically doing the same thing but there&#8217;s one difference one of them</p>



<p class="wp-block-paragraph">One of them works on signed operands and one of them works on unsigned operands.</p>



<p class="wp-block-paragraph">So keep this in mind.</p>



<p class="wp-block-paragraph">If you&#8217;re comparing two signed integers, you want to use this group up here of instructions.</p>



<p class="wp-block-paragraph">But if you&#8217;re using unsigned integers or just bits, then you want to use this group of instructions down here.</p>



<p class="wp-block-paragraph">And you definitely want to know what kind of data you have that you&#8217;re comparing.</p>



<p class="wp-block-paragraph">Notice though that jump equal and jump not equal, they don&#8217;t really care.</p>



<p class="wp-block-paragraph">not equal they don&#8217;t really care if the operands are signed or unsigned that&#8217;s because if you want</p>



<p class="wp-block-paragraph">to check to see if two things are equal you just look at all of their bits and if all of their</p>



<p class="wp-block-paragraph">bits are the same then you just say they&#8217;re equal you don&#8217;t even care actually if it&#8217;s a signed or</p>



<p class="wp-block-paragraph">unsigned integer if it&#8217;s a float if it&#8217;s a whatever you just you just go are all the bits equal or are</p>



<p class="wp-block-paragraph">at least one of the bits unequal then we&#8217;ll say it&#8217;s equal or not equal that&#8217;s it so keep that in</p>



<p class="wp-block-paragraph">mind for all of your jump instructions where you&#8217;re trying to see if things are equal or not</p>



<p class="wp-block-paragraph">or not equal you&#8217;re going to definitely be using these whether you&#8217;re using signed or unsigned</p>



<p class="wp-block-paragraph">integers or floats or anything else like that but when it comes to actually you know integers that</p>



<p class="wp-block-paragraph">are signed or unsigned these are the instructions you use for signed and these are the instructions</p>



<p class="wp-block-paragraph">whoops that you use for what have i done that you&#8217;ll use for unsigned i keep confusing the on</p>



<p class="wp-block-paragraph">off button for the red button is what i&#8217;m doing oh okay whatever i guess we&#8217;re done anyway</p>



<p class="wp-block-paragraph">I guess we&#8217;re done anyway.</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re just going to be using the signed integer instructions</p>



<p class="wp-block-paragraph">because that&#8217;s the example that I have set up.</p>



<p class="wp-block-paragraph">But just imagine you could very easily translate this to unsigned integers or floats.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s see.</p>



<p class="wp-block-paragraph">We&#8217;ve got jump less than, we&#8217;ve got jump less than or equal to,</p>



<p class="wp-block-paragraph">then we&#8217;ve got jump greater than, and then we&#8217;ve got jump greater than or equal to.</p>



<p class="wp-block-paragraph">And when you combine those with jump equal to or jump not equal to,</p>



<p class="wp-block-paragraph">jump not equal to that&#8217;s pretty much all the comparison that you need and if you want to do</p>



<p class="wp-block-paragraph">something much much more complex well then you can just you know start stacking logic on top of</p>



<p class="wp-block-paragraph">logic and and branching on top of branching to do something that&#8217;s like this is what i&#8217;m talking</p>



<p class="wp-block-paragraph">like if you want to do this expression and expression or expression and then maybe like a</p>



<p class="wp-block-paragraph">expression, right? If you want to do something super complicated, like we can do in a higher</p>



<p class="wp-block-paragraph">level language, you&#8217;re just going to have to make a bunch of mini jumps or find another</p>



<p class="wp-block-paragraph">way to reduce your logic. But in this video, we&#8217;re just going to be taking a very simple</p>



<p class="wp-block-paragraph">comparison. All right. So I think that&#8217;s everything that I can show you inside of this book. Let&#8217;s</p>



<p class="wp-block-paragraph">go to the code now. So here&#8217;s a little empty code, source code, a page. I&#8217;m going to stick</p>



<p class="wp-block-paragraph">all my code inside of it. Another reminder that if you don&#8217;t know how to write assembly</p>



<p class="wp-block-paragraph">you don&#8217;t know how to write assembly at all you should see my other videos because i&#8217;m not going</p>



<p class="wp-block-paragraph">to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p class="wp-block-paragraph">or assemble or like do all the basics that i&#8217;m not talking about in this video i&#8217;ve covered them all</p>



<p class="wp-block-paragraph">in my other videos up to this point so for now we&#8217;re just going to focus only on conditional</p>



<p class="wp-block-paragraph">branching conditional jumping inside of an assembly module so i&#8217;m going to start off by setting up my</p>



<p class="wp-block-paragraph">and i&#8217;m gonna say paste and again you know this is covered in other videos but long story short</p>



<p class="wp-block-paragraph">i&#8217;m just going to be printing out a bunch of strings while this program executes so</p>



<p class="wp-block-paragraph">i&#8217;m going to announce that the branching test is going to begin and then i&#8217;m going to ask the user</p>



<p class="wp-block-paragraph">for some input and then i&#8217;m going to based on whatever the user inputs i&#8217;m going to just print</p>



<p class="wp-block-paragraph">out a message that sort of you know understands what the user inputted so if the user inputted</p>



<p class="wp-block-paragraph">something greater than zero it&#8217;s going to output that if they entered something that was less than</p>



<p class="wp-block-paragraph">10 it&#8217;s going to print that if they entered something that was equal to five it&#8217;s going to</p>



<p class="wp-block-paragraph">print that and then it&#8217;s going to say we&#8217;ve ended the branching tests then i have like a crlf again</p>



<p class="wp-block-paragraph">all this stuff is explained in other videos but we&#8217;re just basically printing so i&#8217;m going to</p>



<p class="wp-block-paragraph">start the text section of my program which is where all the instructions are and i&#8217;m going to</p>



<p class="wp-block-paragraph">a function that I can call on. Again, this video does not cover a bunch of other extra stuff. I&#8217;m</p>



<p class="wp-block-paragraph">just going to show you how to branch. Just trust me when I say that this helps me easily print</p>



<p class="wp-block-paragraph">numbers and things, or I guess in this particular case, it&#8217;ll help me input a number from the user.</p>



<p class="wp-block-paragraph">But for you, if you&#8217;re trying to run this at home, you can just start hard coding numbers just to</p>



<p class="wp-block-paragraph">make sure that you can branch correctly and then eventually work your way up to a library that can</p>



<p class="wp-block-paragraph">videos in the distant future. I don&#8217;t know years from now. I don&#8217;t know.</p>



<p class="wp-block-paragraph">I might release the library or some other stuff to the casual viewer.</p>



<p class="wp-block-paragraph">So now here&#8217;s our entry point. Again,</p>



<p class="wp-block-paragraph">I mentioned that there&#8217;s going to be other parts of this program that I&#8217;m not</p>



<p class="wp-block-paragraph">showing. So this is a hybrid program where a C++ driver actually will call on</p>



<p class="wp-block-paragraph">my cool function. So just imagine from somewhere else in the program,</p>



<p class="wp-block-paragraph">you&#8217;re calling on the cool function. How do we know it&#8217;s a function?</p>



<p class="wp-block-paragraph">a label called cool and then it has a return statement at the very end.</p>



<p class="wp-block-paragraph">That&#8217;s pretty much all we need to know right now.</p>



<p class="wp-block-paragraph">Let me get rid of that load.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I always have the word load in all of my things.</p>



<p class="wp-block-paragraph">I think I put it in there on an early example</p>



<p class="wp-block-paragraph">and then I just kept copy pasting to my heart&#8217;s content and never stopped.</p>



<p class="wp-block-paragraph">We mark the function cool as global so that other modules can call on it</p>



<p class="wp-block-paragraph">like the C++ module that I&#8217;m not showing you.</p>



<p class="wp-block-paragraph">But if you&#8217;re writing a pure assembly module,</p>



<p class="wp-block-paragraph">you don&#8217;t really need to mark your functions as global.</p>



<p class="wp-block-paragraph">if everything&#8217;s within the same piece of source code it&#8217;s totally fine so then I&#8217;m going to make</p>



<p class="wp-block-paragraph">a function call to a function called branch test so very quickly before I actually add branch test</p>



<p class="wp-block-paragraph">I&#8217;m just going to add another function that we&#8217;re not covering in this video called crlf and long</p>



<p class="wp-block-paragraph">story short all it does is just it prints a new line in the terminal and that&#8217;s it like it&#8217;s just</p>



<p class="wp-block-paragraph">it&#8217;s a convenience function I like to be able to call crlf and have a new line okay so let&#8217;s start</p>



<p class="wp-block-paragraph">our branch test function so here&#8217;s the signature it is just branch test with a</p>



<p class="wp-block-paragraph">void in front of it I&#8217;m gonna put a ret to say that this is a function that we</p>



<p class="wp-block-paragraph">will return from and I left myself a little note here saying that I&#8217;m gonna</p>



<p class="wp-block-paragraph">use register r12 as the number that the user inputs so that means I&#8217;m gonna have</p>



<p class="wp-block-paragraph">to preserve that because you have to respect the ABI and the ABI says that</p>



<p class="wp-block-paragraph">The ABI says that R12 is a Kali saved register.</p>



<p class="wp-block-paragraph">Again, see my other videos if you don&#8217;t know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to preserve it in a section that I call the prologue, or actually the</p>



<p class="wp-block-paragraph">book calls the prologue.</p>



<p class="wp-block-paragraph">I do that now though too.</p>



<p class="wp-block-paragraph">And, you know, restore it in a section called the epilogue.</p>



<p class="wp-block-paragraph">So we have prologue and epilogue.</p>



<p class="wp-block-paragraph">We can have a push-pop pair.</p>



<p class="wp-block-paragraph">And then in between, we can have, you know, the meat of our function.</p>



<p class="wp-block-paragraph">Okay, so for the introduction, I&#8217;m just going to write a few instructions that just print</p>



<p class="wp-block-paragraph">out a message.</p>



<p class="wp-block-paragraph">print out a message. So we&#8217;re going to print out that intro message and then call</p>



<p class="wp-block-paragraph">CRLF to print a new line and then return. So maybe actually at this point we could</p>



<p class="wp-block-paragraph">probably check to see if this program works by trying to run it.</p>



<p class="wp-block-paragraph">So let me say clear and make run.</p>



<p class="wp-block-paragraph">Okay. So now the driver just prints hello. The driver is</p>



<p class="wp-block-paragraph">elsewhere and we get that welcome message that says begin</p>



<p class="wp-block-paragraph">branching test and then the driver retains control after that. So</p>



<p class="wp-block-paragraph">let&#8217;s do let&#8217;s do a goodbye message real fast so we&#8217;ll do our goodbye message</p>



<p class="wp-block-paragraph">it&#8217;s just another string that I&#8217;m printing it&#8217;s no big deal right now but</p>



<p class="wp-block-paragraph">if I run the program again you can see that begin branching test and end</p>



<p class="wp-block-paragraph">branching test okay so far we&#8217;re not really doing branching yet the next thing</p>



<p class="wp-block-paragraph">we need to do is ask for a number so this is how I&#8217;m going to do it in my</p>



<p class="wp-block-paragraph">this is how I&#8217;m going to do it in my program. In your program it might be different or you might</p>



<p class="wp-block-paragraph">have a hard-coded number or something like that. So for now just trust me when I say that I&#8217;m going</p>



<p class="wp-block-paragraph">to print a prompt basically saying could you please enter a number and then the next two lines</p>



<p class="wp-block-paragraph">are going to be just calling on one of my custom functions that will input a number from the user</p>



<p class="wp-block-paragraph">so the user can type at the terminal and we will receive their number through REX which is the</p>



<p class="wp-block-paragraph">return value register for integer return values and we&#8217;re going to store it in R12. So that&#8217;s why</p>



<p class="wp-block-paragraph">And we&#8217;re going to store it in R12.</p>



<p class="wp-block-paragraph">So that&#8217;s why I had to preserve R12,</p>



<p class="wp-block-paragraph">because I&#8217;m actually going to start,</p>



<p class="wp-block-paragraph">you know, messing with the value of R12 now.</p>



<p class="wp-block-paragraph">Okay, so we ask for a number,</p>



<p class="wp-block-paragraph">and then we sort of don&#8217;t do anything after that.</p>



<p class="wp-block-paragraph">If we run the program again,</p>



<p class="wp-block-paragraph">it&#8217;s just asking for a number,</p>



<p class="wp-block-paragraph">and I can just like type some stuff and hit enter,</p>



<p class="wp-block-paragraph">and then the program ends, nothing actually happens.</p>



<p class="wp-block-paragraph">We&#8217;re building, we&#8217;re building.</p>



<p class="wp-block-paragraph">So now let&#8217;s ask ourselves,</p>



<p class="wp-block-paragraph">is the number greater than zero?</p>



<p class="wp-block-paragraph">So let me copy paste some code here.</p>



<p class="wp-block-paragraph">some code here so right now we&#8217;ve just finished inputting a number from the user and we&#8217;ll ask</p>



<p class="wp-block-paragraph">you know is that number that they inputted greater than zero so again the first thing we do is we use</p>



<p class="wp-block-paragraph">a compare instruction cmp we give it two operands the order doesn&#8217;t necessarily matter um for</p>



<p class="wp-block-paragraph">instance uh right now i&#8217;m asking is r12 greater than zero so i can do a jump less than or equal</p>



<p class="wp-block-paragraph">to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p class="wp-block-paragraph">in a different order, but I&#8217;m just choosing to say, let&#8217;s compare the two and then let&#8217;s jump</p>



<p class="wp-block-paragraph">if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



<p class="wp-block-paragraph">which is just branch test after greater test. So I&#8217;m going to do a print statement right after that</p>



<p class="wp-block-paragraph">and then I&#8217;m going to draw out what the code is kind of doing. So let me make a label here so</p>



<p class="wp-block-paragraph">actually will compile. Whoops. I&#8217;m in the wrong window. Okay. There we go. So what am I saying</p>



<p class="wp-block-paragraph">here? Okay. Do the comparison. And then if the condition seems to be satisfied, then we&#8217;ll jump</p>



<p class="wp-block-paragraph">to this, which means we&#8217;ll just say goodbye and then exit the program because we&#8217;ll return to the</p>



<p class="wp-block-paragraph">caller. But if that condition was not satisfied, then we&#8217;re going to end up executing this stuff</p>



<p class="wp-block-paragraph">in the middle. So think about this. This is kind of the idea. Let&#8217;s see if I can remember how to</p>



<p class="wp-block-paragraph">idea let&#8217;s see if I can remember how to draw this right now off the top of my head suppose we have</p>



<p class="wp-block-paragraph">like some sort of an input I&#8217;ll call this in put and then maybe the input is going to be you know</p>



<p class="wp-block-paragraph">whether r12 and r0 are equal or greater than or whatever so I&#8217;m just gonna say r12 and 0</p>



<p class="wp-block-paragraph">god that&#8217;s awful I really need to practice this draw pad I spend like a whole week practicing</p>



<p class="wp-block-paragraph">whole week practicing with a different draw pad and then when I come back to</p>



<p class="wp-block-paragraph">this one I&#8217;m even worse than when I started so we we come in and we kind of</p>



<p class="wp-block-paragraph">look at our 12 versus zero and then we will jump in one direction if that&#8217;s not</p>



<p class="wp-block-paragraph">you know greater than or equal to or less than or equal to and we&#8217;ll jump in</p>



<p class="wp-block-paragraph">another direction otherwise so basically here we&#8217;ll say jump less than equal to</p>



<p class="wp-block-paragraph">um less than or equal to whoops maybe i&#8217;ll do r12 uh i need to somehow adjust the pressure on this</p>



<p class="wp-block-paragraph">thing r12 less than or equal to and so this is sort of uh has to do with you know this branching</p>



<p class="wp-block-paragraph">path that we take like if r12 was less than or equal to zero then we&#8217;ll take the right branching</p>



<p class="wp-block-paragraph">to after, I&#8217;ll just put the word after,</p>



<p class="wp-block-paragraph">we&#8217;ll jump to the after greater test sub label.</p>



<p class="wp-block-paragraph">And otherwise, if R12 is, sorry,</p>



<p class="wp-block-paragraph">I said less than or equal to, yeah,</p>



<p class="wp-block-paragraph">if R12 is greater than zero,</p>



<p class="wp-block-paragraph">then we jump to this other thing,</p>



<p class="wp-block-paragraph">which is gonna be the fall through.</p>



<p class="wp-block-paragraph">So it&#8217;s gonna be line 96, but I&#8217;ll just say,</p>



<p class="wp-block-paragraph">I&#8217;ll say fall for fall through.</p>



<p class="wp-block-paragraph">basically the jump won&#8217;t happen if it&#8217;s greater than or equal to so you could imagine if you</p>



<p class="wp-block-paragraph">wanted to reverse the logic so you can use a jump greater than instruction you could but I&#8217;m choosing</p>



<p class="wp-block-paragraph">to do it this way and then both of those will eventually reach let&#8217;s see will eventually</p>



<p class="wp-block-paragraph">actually yeah the fall through instructions those will eventually reach the after instruction I&#8217;ll</p>



<p class="wp-block-paragraph">here and then the after instruction will just kind of like finish the program</p>



<p class="wp-block-paragraph">and exit and all that stuff. So if you think about the control path here,</p>



<p class="wp-block-paragraph">we have an input we&#8217;re looking at R12 with the zero with the compare instruction</p>



<p class="wp-block-paragraph">and then we use a conditional branching instruction. So maybe I should write,</p>



<p class="wp-block-paragraph">what do you want to write here? Maybe I&#8217;ll just put JLE up here because usually the compare</p>



<p class="wp-block-paragraph">usually the compare instruction and the conditional jump or branching instructions</p>



<p class="wp-block-paragraph">kind of you know come as a pair so both of these combined mean let&#8217;s look at r12 versus zero and</p>



<p class="wp-block-paragraph">if r12 is less than or equal to zero meaning if it will jump if we&#8217;re less than or equal to the</p>



<p class="wp-block-paragraph">the right operand r12 less than equal to the right operand then we go down this path on the right</p>



<p class="wp-block-paragraph">to the after label if that is false meaning r12 was greater than zero then we&#8217;ll fall through</p>



<p class="wp-block-paragraph">this stuff right here and then whether or not we actually fall through will always end up at the</p>



<p class="wp-block-paragraph">at the after area because we&#8217;re either going to jump directly to the after area or we&#8217;re going</p>



<p class="wp-block-paragraph">to fall through and then the fall through falls through to the after area anyway as well let me</p>



<p class="wp-block-paragraph">just show you that real fast oh man i&#8217;m having a hard time with this okay so let&#8217;s pretend that r12</p>



<p class="wp-block-paragraph">less than or equal to. So that means instructions are coming, you know,</p>



<p class="wp-block-paragraph">instructions are getting executed. We&#8217;re going down and down and down and down and down.</p>



<p class="wp-block-paragraph">Once we see jump less than or equal to, if that&#8217;s true, then we jump to the after label,</p>



<p class="wp-block-paragraph">which means we just kind of jump around this message. And so we&#8217;re not going to exit,</p>



<p class="wp-block-paragraph">sorry, we&#8217;re not going to execute that code. So in the case where that is true,</p>



<p class="wp-block-paragraph">where it is less than or equal to, we&#8217;re not going to execute those instructions.</p>



<p class="wp-block-paragraph">So we&#8217;re not going to say their number was greater than zero. We&#8217;re just going to simply</p>



<p class="wp-block-paragraph">We&#8217;re just going to simply say goodbye.</p>



<p class="wp-block-paragraph">However, on the other hand, if that is not true, meaning if R12 was indeed greater than</p>



<p class="wp-block-paragraph">zero, then we&#8217;re going to fall through because this jump less than or equal to instruction,</p>



<p class="wp-block-paragraph">it will only jump if the R flags, if the comparison instruction thought that R12 was less than</p>



<p class="wp-block-paragraph">or equal to zero.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, if it&#8217;s greater than, then instructions, the control path is just</p>



<p class="wp-block-paragraph">the control path is just going to fall through.</p>



<p class="wp-block-paragraph">So that means we will actually execute these.</p>



<p class="wp-block-paragraph">And then when they&#8217;re done,</p>



<p class="wp-block-paragraph">execution will continue to fall through to the rest.</p>



<p class="wp-block-paragraph">So that means we&#8217;re either going to see a message saying</p>



<p class="wp-block-paragraph">their number was greater than zero or not,</p>



<p class="wp-block-paragraph">based on whether their number actually was greater than zero.</p>



<p class="wp-block-paragraph">Okay, hopefully I explained that somewhat clearly.</p>



<p class="wp-block-paragraph">Now let&#8217;s run the code and see if it works.</p>



<p class="wp-block-paragraph">Hopefully I didn&#8217;t forget anything.</p>



<p class="wp-block-paragraph">This is kind of a partial program at this point.</p>



<p class="wp-block-paragraph">program at this point I&#8217;m gonna add a bunch more stuff to it so hopefully</p>



<p class="wp-block-paragraph">this actually compiles enter an integer five notice how it says your number was</p>



<p class="wp-block-paragraph">greater than zero and if I run it again and I just type zero it does not say</p>



<p class="wp-block-paragraph">that it was greater than zero if I run it again I type like a negative five it</p>



<p class="wp-block-paragraph">also does not say that it was greater than zero you know a huge giant number</p>



<p class="wp-block-paragraph">here it&#8217;ll say your number was greater than zero you could probably type a</p>



<p class="wp-block-paragraph">number that was just absolutely huge and have it overflow I actually don&#8217;t know</p>



<p class="wp-block-paragraph">nines I need to type and I don&#8217;t even know what the behavior is going to be.</p>



<p class="wp-block-paragraph">I don&#8217;t know if it&#8217;s just going to crash the program because this also relies on my ability</p>



<p class="wp-block-paragraph">to input a number which is happening in a different library.</p>



<p class="wp-block-paragraph">Let&#8217;s see if that even works greater than zero.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">It probably overflowed and went to a very, very low negative number.</p>



<p class="wp-block-paragraph">So okay.</p>



<p class="wp-block-paragraph">I guess at least I know my library will probably work sort of.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So let&#8217;s go back to the code here and let&#8217;s upgrade it.</p>



<p class="wp-block-paragraph">how to to check for jump less than or equal to um we&#8217;re going to say goodbye right there so now the</p>



<p class="wp-block-paragraph">next thing we should do is check to see if the number was less than 10. so after the greater test</p>



<p class="wp-block-paragraph">it&#8217;s going to be here so again the same thing like we can just reuse r12 because we&#8217;re not really</p>



<p class="wp-block-paragraph">modifying it and we&#8217;re not calling uh well i guess even if we were calling a function r12 was supposed</p>



<p class="wp-block-paragraph">supposed to be Kali preserved so we&#8217;re just comparing it and then maybe</p>



<p class="wp-block-paragraph">printing a message if we want to and regardless we&#8217;re going to end up at 103</p>



<p class="wp-block-paragraph">this after greater test label so then we&#8217;ll compare again r12 with the number</p>



<p class="wp-block-paragraph">10 and we&#8217;ll say jump if it&#8217;s greater than or equal to branch test after less</p>



<p class="wp-block-paragraph">so now at this point we&#8217;re saying compare r12 with 10 if it&#8217;s greater than or equal to 10 then</p>



<p class="wp-block-paragraph">just skip over the message so if it&#8217;s greater than or equal to 10 then wait a minute wait wait</p>



<p class="wp-block-paragraph">yeah yeah if it&#8217;s greater than or equal to 10 then skip the message otherwise the message</p>



<p class="wp-block-paragraph">that is printed is going to say that their number is less than 10. so again you could imagine a</p>



<p class="wp-block-paragraph">want me to write it out for you I don&#8217;t know I think once is probably enough but let me just do</p>



<p class="wp-block-paragraph">it again we&#8217;ll do input is coming in and it&#8217;s just going to be you know comparing and then</p>



<p class="wp-block-paragraph">jumping greater than or equal to the input is going to be where we&#8217;re just looking to see if</p>



<p class="wp-block-paragraph">R12 how it compares to zero god the number one is awful my entire penmanship is awful okay one</p>



<p class="wp-block-paragraph">we&#8217;ll do r12 and 10 and so that&#8217;s just sort of what we&#8217;re comparing with the compare instruction</p>



<p class="wp-block-paragraph">and then the jump instruction and so if we go on the left i&#8217;m going to say that&#8217;s the fall through</p>



<p class="wp-block-paragraph">path again and if we go to the right it&#8217;s going to be um r12 greater than or equal to 10 is going</p>



<p class="wp-block-paragraph">and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



<p class="wp-block-paragraph">so if we fall through it&#8217;s just going to print a message maybe I should do another little bubble</p>



<p class="wp-block-paragraph">here that&#8217;s just like print p for how about p for print p for print it&#8217;s going to fall through and</p>



<p class="wp-block-paragraph">then it&#8217;s going to print and then it&#8217;s going to go to the after label so you can see that</p>



<p class="wp-block-paragraph">the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



<p class="wp-block-paragraph">to 10 which is the same thing as saying it&#8217;s going to fall through if r12 is less than</p>



<p class="wp-block-paragraph">10.</p>



<p class="wp-block-paragraph">And then after we finally get to the after label, then we&#8217;re just going to do, you know,</p>



<p class="wp-block-paragraph">the rest of the instructions.</p>



<p class="wp-block-paragraph">We&#8217;re going to exit the program.</p>



<p class="wp-block-paragraph">We&#8217;re going to say goodbye.</p>



<p class="wp-block-paragraph">We&#8217;re going to do whatever.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So say there never was less than 10.</p>



<p class="wp-block-paragraph">And then regardless, here&#8217;s like the ending label that we were conditionally jumping to.</p>



<p class="wp-block-paragraph">Let&#8217;s see if this works.</p>



<p class="wp-block-paragraph">Make run.</p>



<p class="wp-block-paragraph">So we&#8217;ll enter five.</p>



<p class="wp-block-paragraph">we&#8217;ll enter five notice how it says your number was greater than zero and your</p>



<p class="wp-block-paragraph">number was less than ten let&#8217;s do an 11 I guess it says your number was greater</p>



<p class="wp-block-paragraph">than zero but it does not say that our number was less than ten because 11 is</p>



<p class="wp-block-paragraph">not less than ten if I do a negative three here it says that our number was</p>



<p class="wp-block-paragraph">less than ten but it doesn&#8217;t say that it&#8217;s greater than zero so we have like a</p>



<p class="wp-block-paragraph">lot of decisions that we could make right like imagine if instead of just</p>



<p class="wp-block-paragraph">based on something happening, you could call a full function or skip a bunch of extra code,</p>



<p class="wp-block-paragraph">you know, whatever you want it to do.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go to the next condition where we&#8217;re going to compare R12 to the number five.</p>



<p class="wp-block-paragraph">And we&#8217;re going to ask, you know, is R12 equal to five?</p>



<p class="wp-block-paragraph">So this is going to be the jump not equal branch.</p>



<p class="wp-block-paragraph">And again, you know, if you wanted to say jump, if it&#8217;s equal to the place where we</p>



<p class="wp-block-paragraph">and then maybe like right after that fall through and just do an unconditional jump you can</p>



<p class="wp-block-paragraph">I just think there are less jumps if I do it this way so uh here we&#8217;re just going to print you know</p>



<p class="wp-block-paragraph">your number was equal to five and so basically if it was not equal we should have another label here</p>



<p class="wp-block-paragraph">that allows us to skip that so all right so same thing I&#8217;m not going to draw the diagram this time</p>



<p class="wp-block-paragraph">R12 with 5 and if it&#8217;s not equal to 5 then jump to this label down here which just means</p>



<p class="wp-block-paragraph">let&#8217;s say goodbye and not actually say that their number was equal to 5.</p>



<p class="wp-block-paragraph">But if it was equal to 5 then this JNE conditional branch won&#8217;t actually jump anywhere.</p>



<p class="wp-block-paragraph">Execution will fall through to the next statements which is just printing the message that their</p>



<p class="wp-block-paragraph">number is equal to 5 and then when that&#8217;s done it&#8217;ll fall through to this other label</p>



<p class="wp-block-paragraph">and eventually fall through to the return statement.</p>



<p class="wp-block-paragraph">the whole thing already nice that was easy um let&#8217;s do another run and um let&#8217;s enter the</p>



<p class="wp-block-paragraph">integer one it says one is uh greater than zero and one is less than 10</p>



<p class="wp-block-paragraph">greater than zero and less than 10 is there any way i can get nothing to print out</p>



<p class="wp-block-paragraph">greater than zero and less than 10</p>



<p class="wp-block-paragraph">you don&#8217;t think i can do it let me try it nine no</p>



<p class="wp-block-paragraph">why did I even put 9 okay yeah it&#8217;s always gonna say 0 or 10 okay so let&#8217;s</p>



<p class="wp-block-paragraph">do a 4 we&#8217;ll get both of those greater than 0 and also less than 10 let&#8217;s do a</p>



<p class="wp-block-paragraph">5 now we should get all those messages your number was greater than 0 your</p>



<p class="wp-block-paragraph">number was less than 10 your number was equal to 5 and if I just do a 6 in there</p>



<p class="wp-block-paragraph">again it doesn&#8217;t print your number was equal to 5 and we have to enter an</p>



<p class="wp-block-paragraph">actual 5 for that so that&#8217;s the that&#8217;s the basics of</p>



<p class="wp-block-paragraph">conditional branching oh there&#8217;s one other thing that I wanted to make sure</p>



<p class="wp-block-paragraph">in my previous video where we only talked about jumps bear in mind that</p>



<p class="wp-block-paragraph">these conditional branching statements or instructions you know je j any all</p>



<p class="wp-block-paragraph">these things they have a limited range of jumps of where they can jump to so if</p>



<p class="wp-block-paragraph">you have a gigantic program and you&#8217;re trying to jump very very very far away</p>



<p class="wp-block-paragraph">based on a condition you might have an assembler error where your program won&#8217;t</p>



<p class="wp-block-paragraph">even compile won&#8217;t even assemble telling you i think the message is i wrote it</p>



<p class="wp-block-paragraph">I wrote it down somewhere jump out of range coming from the assembler that basically means that these</p>



<p class="wp-block-paragraph">instructions can only jump about 128 bytes away from whatever instruction they are at or whatever</p>



<p class="wp-block-paragraph">memory location they are at so you know after your assembler assembles your program and after</p>



<p class="wp-block-paragraph">your linker links your program and you just have like a binary you know if you were to inspect</p>



<p class="wp-block-paragraph">all the instructions inside of your executable your binary you would see that they have offsets</p>



<p class="wp-block-paragraph">offsets, right? You know, like one instruction is basically eight bytes away from the one that</p>



<p class="wp-block-paragraph">comes right after it. But some instructions are a lot further away. So if you&#8217;re trying to jump</p>



<p class="wp-block-paragraph">to an instruction that is 100 is more than 128 bytes away, it won&#8217;t work, you&#8217;ll get that error.</p>



<p class="wp-block-paragraph">So how do you solve that problem? Well, maybe I&#8217;ll just write this down real fast. I&#8217;m not going to</p>



<p class="wp-block-paragraph">make this part of the code example. But just imagine we have, you know, jump equal to some</p>



<p class="wp-block-paragraph">and maybe I&#8217;ll do a comment very very far away and here&#8217;s the label some label right</p>



<p class="wp-block-paragraph">so if your assembler says hey you can&#8217;t jump to some label because it&#8217;s too far away it&#8217;s a</p>



<p class="wp-block-paragraph">jump out of range assembler error then basically you can just make some other labels here you can</p>



<p class="wp-block-paragraph">say uh you can say short jump true and short jump false you know just make up some sort of a label</p>



<p class="wp-block-paragraph">label and basically say if it is um well false yeah okay i guess this is the way i&#8217;ll do it this</p>



<p class="wp-block-paragraph">is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p class="wp-block-paragraph">can just jump to the true label so i&#8217;m going to say jump uh to the short jump true label otherwise</p>



<p class="wp-block-paragraph">To the false label. So now you&#8217;re definitely jumping in either case and both of these labels are close by</p>



<p class="wp-block-paragraph">So then I can pretty much just have the close by true label do an unconditional</p>



<p class="wp-block-paragraph">Jump to some label and that overcomes the limitation of branching instructions</p>



<p class="wp-block-paragraph">So, you know JMP the regular jump instruction</p>



<p class="wp-block-paragraph">It has an unlimited jump that it can do it can go just to like very very very far away instructions</p>



<p class="wp-block-paragraph">And then for the false, you know, I don&#8217;t know, do whatever you want and then jump wherever you want.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really matter.</p>



<p class="wp-block-paragraph">I&#8217;m just trying to show you that if you make a short jump to a label that will only have an unconditional jump,</p>



<p class="wp-block-paragraph">so it&#8217;s sort of like two jumps, you know, like a short jump and then a long jump,</p>



<p class="wp-block-paragraph">then you can overcome that limitation.</p>



<p class="wp-block-paragraph">Again, bearing in mind that some label is supposed to be very, very far away and sort of unreachable.</p>



<p class="wp-block-paragraph">Okay, so let me just double check that I talked about everything that I wanted to.</p>



<p class="wp-block-paragraph">We looked at the branching instructions, the sign in this, I drew a diagram for you.</p>



<p class="wp-block-paragraph">We solved the jump out of range error.</p>



<p class="wp-block-paragraph">Okay, so that&#8217;s, I think that&#8217;s basically it.</p>



<p class="wp-block-paragraph">Hopefully you feel like an expert now at conditional branching and how to make decisions.</p>



<p class="wp-block-paragraph">In future videos, I&#8217;ll talk about how to implement an if else block and how to implement loops</p>



<p class="wp-block-paragraph">and all that stuff.</p>



<p class="wp-block-paragraph">decisions in yasm x86-64 assembly and conceptually you can apply this to other assembly languages too</p>



<p class="wp-block-paragraph">so thanks for watching my video i hope you learned a little bit of stuff and had a little bit of fun</p>



<p class="wp-block-paragraph">i&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">hey everybody thanks for watching this video again from the bottom of my heart i really appreciate it</p>



<p class="wp-block-paragraph">i do hope you did learn something and have some fun uh if you could do me a please a small little</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now?</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">or followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



<p class="wp-block-paragraph">subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a QR code which</p>



<p class="wp-block-paragraph">you can scan in order to go to the website which I think is also named</p>



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the</p>



<p class="wp-block-paragraph">all the videos i published and the services and tutorials and things that i offer and all that</p>



<p class="wp-block-paragraph">good stuff and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Complex If-ElseIf-Else in x86-64 YASM Assembly &#8211; Full Guide with Code Examples</title>
		<link>https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/</link>
					<comments>https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 04 Jan 2026 18:55:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language if statement]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[chained if else assembly]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[conditional branching assembly]]></category>
		<category><![CDATA[control flow assembly]]></category>
		<category><![CDATA[if else assembly]]></category>
		<category><![CDATA[intel assembly]]></category>
		<category><![CDATA[je jmp assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[nasm assembly]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=275</guid>

					<description><![CDATA[<p>Step-by-step x86-64 YASM assembly tutorial showing how to implement complex if-else and if-elseif-else control flow structures with multiple chained conditions, proper labels, conditional jumps, and fall-through logic. Includes complete working code and live execution.</p>
<p>The post <a href="https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/">Complex If-ElseIf-Else in x86-64 YASM Assembly &#8211; Full Guide with Code Examples</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="Complex If-ElseIf-Else in x86-64 YASM Assembly - Full Guide with Code Examples" width="1380" height="776" src="https://www.youtube.com/embed/G7am7avKi9w?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 class="wp-block-paragraph">In this in-depth x86-64 assembly tutorial using YASM, we dive deep into implementing complex if-else and if-elseif-else control structures from scratch. Starting with the fundamentals of conditional branching, we build up to full chained if-elseif-else blocks with multiple conditions &#8211; exactly how high-level languages handle them under the hood.</p>



<p class="wp-block-paragraph">You&#8217;ll see real working code that:</p>



<ul class="wp-block-list">
<li>Takes user integer input</li>



<li>Tests against specific values (5, 6, etc.)</li>



<li>Handles greater-than/less-than comparisons</li>



<li>Properly branches so only one block executes</li>



<li>Uses labels, cmp, conditional jumps (je, jl), and unconditional jumps (jmp) correctly</li>
</ul>



<p class="wp-block-paragraph">We cover the classic pattern: compare to conditional jump to true block to execute true code to jmp to end to false block falls through or jumps in. Everything is shown step-by-step with live compilation and runtime demos.</p>



<p class="wp-block-paragraph">Perfect for anyone learning low-level programming, reverse engineering, or wanting to understand how compilers translate if-else chains into machine code. Prerequisites: basic conditional jumps (see my earlier videos).</p>



<p class="wp-block-paragraph">Code shown works on Linux x86-64 with YASM/NASM syntax. Grab the concepts and apply them anywhere.</p>



<p class="wp-block-paragraph">Introduction to If-Else in Assembly 00:00:00<br>Explaining the If-Else Design Pattern 00:00:56<br>Drawing the Basic If-Else Flow 00:01:01<br>Comparison and Conditional Jumps 00:02:30<br>Labels for True and False Blocks 00:03:07<br>Unconditional Jump to End 00:04:50<br>Diagram of Execution Flow 00:05:51<br>Alternative Pattern with Inverted Jump 00:07:00<br>Recapping the If-Else Pattern 00:08:45<br>Starting the Code Example 00:09:16<br>Setting Up Input and Strings 00:09:40<br>Calling External Functions 00:10:57<br>Entry Point and Prologue 00:11:40<br>Asking User for Integer Input 00:13:09<br>Creating the if_test Function 00:14:56<br>Preserving Callee-Saved Registers 00:15:51<br>Printing Begin Message 00:17:03<br>Implementing Simple If Block 00:18:29<br>Comparison and je Jump 00:19:18<br>True Block: Equality Message 00:21:08<br>Testing Simple If Examples 00:23:48<br>Transition to If-Else Blocks 00:24:21<br>Creating if_else_test Function 00:24:47<br>Setting Up Complex If-Else 00:26:25<br>First If: Equal to 5 00:27:22<br>True Block for Equal 5 00:28:33<br>Else If: Equal to 6 00:30:29<br>Else If: Less Than 10 00:34:17<br>Final Else Block 00:37:33<br>Done Label and Goodbye 00:38:23<br>Recap of Full Flow 00:39:06<br>Live Demo of All Branches 00:40:54<br>Signed vs Unsigned Jumps Note 00:43:38<br>Recommended Assembly Book 00:44:12<br>Conditional Jump Families 00:45:05<br>Closing and Practice Advice 00:46:48</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">In this video we&#8217;re going to talk about implementing simple if-else blocks in YASM x86-64 assembly.</p>



<p class="wp-block-paragraph">Although if you&#8217;re writing in a different assembly language, this video will probably</p>



<p class="wp-block-paragraph">still be useful to you because I&#8217;m going to explain the design pattern or how we can achieve</p>



<p class="wp-block-paragraph">that at the assembly level.</p>



<p class="wp-block-paragraph">So x86-64 YASM assembly, also known as AMD64 YASM assembly.</p>



<p class="wp-block-paragraph">So if you have not seen my other videos about how to do conditional branching in the first</p>



<p class="wp-block-paragraph">place, you probably want to go check that first because that knowledge is required for</p>



<p class="wp-block-paragraph">this video.</p>



<p class="wp-block-paragraph">There&#8217;s also a lot of other stuff that I&#8217;m just not going to explain in this video, such</p>



<p class="wp-block-paragraph">as creating a make file, you know, compiling and linking your executable and so forth.</p>



<p class="wp-block-paragraph">So see my other videos where all those concepts are explained already.</p>



<p class="wp-block-paragraph">how to implement a simple if-else block.</p>



<p class="wp-block-paragraph">So for starters, maybe let me draw a little bit.</p>



<p class="wp-block-paragraph">Suppose we had, let me get rid of this thing</p>



<p class="wp-block-paragraph">and then I&#8217;ll just do a regular notepad.</p>



<p class="wp-block-paragraph">Suppose we had a higher level language,</p>



<p class="wp-block-paragraph">just so you know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">Suppose we had a higher level language</p>



<p class="wp-block-paragraph">and we wanted to say if, you know, some expression is true.</p>



<p class="wp-block-paragraph">We wanted to say if expression is true,</p>



<p class="wp-block-paragraph">then print, you know, it was true.</p>



<p class="wp-block-paragraph">Anybody remember that old movie, Little Nicky?</p>



<p class="wp-block-paragraph">Somebody got exploded and then the guy next to him goes it&#8217;s not true</p>



<p class="wp-block-paragraph">Okay, it was not true</p>



<p class="wp-block-paragraph">So this is the basic idea of what we&#8217;re going to implement in assembly</p>



<p class="wp-block-paragraph">I&#8217;m going to write a full program for assembly to show you this but um</p>



<p class="wp-block-paragraph">You know you&#8217;re in C++ you&#8217;re in C you&#8217;re in I don&#8217;t know whatever language you&#8217;re in and</p>



<p class="wp-block-paragraph">You obviously know how to use if-else blocks at this point hopefully</p>



<p class="wp-block-paragraph">And now we&#8217;re going to just try to figure out how to implement them in assembly</p>



<p class="wp-block-paragraph">So it&#8217;s important to understand that really under the hood</p>



<p class="wp-block-paragraph">really under the hood there&#8217;s a bunch of stuff happening surprise right okay so</p>



<p class="wp-block-paragraph">first off we look at this expression which could be I don&#8217;t know let&#8217;s say</p>



<p class="wp-block-paragraph">five is greater than ten or a is equal to B or whatever it is that you put in</p>



<p class="wp-block-paragraph">there you could make a very complicated expression we&#8217;re going to use simple</p>



<p class="wp-block-paragraph">expressions for this video so if we&#8217;re comparing a to B we&#8217;ll end up using the</p>



<p class="wp-block-paragraph">compare instruction remember there are two steps to conditional branching and</p>



<p class="wp-block-paragraph">in YASM we first use the compare instruction against two operands and then that will end up</p>



<p class="wp-block-paragraph">filling up the rflags register so that we can later conditionally jump based on the results</p>



<p class="wp-block-paragraph">of the comparison. So we do a comparison and then you can imagine that the beginning of this</p>



<p class="wp-block-paragraph">you know the the true body notice how it has a scope I&#8217;m going to just put this brace on another</p>



<p class="wp-block-paragraph">line here to indicate that there is a scope from line four to six indicating all this code in here</p>



<p class="wp-block-paragraph">in here is executed only if the if statement was true and then all of this other stuff is executed</p>



<p class="wp-block-paragraph">only if the original comparison was false right so you can imagine now label is something like</p>



<p class="wp-block-paragraph">my if was true so we can make a label for where that body starts and another label for where the</p>



<p class="wp-block-paragraph">label is something like if was false and then what will happen is we can use a</p>



<p class="wp-block-paragraph">conditional jump instruction after we do our comparison maybe I should put to</p>



<p class="wp-block-paragraph">compare right here oh I&#8217;m using I&#8217;m using assembly style comments I should</p>



<p class="wp-block-paragraph">be using a C style comments if I&#8217;m actually writing C++ here let me just do</p>



<p class="wp-block-paragraph">that okay so this is the comparison instruction here and then here&#8217;s the</p>



<p class="wp-block-paragraph">here and then here&#8217;s the beginning of the body of stuff to execute if it was true you can imagine</p>



<p class="wp-block-paragraph">that there could be many statements here um if it was true or if it was false</p>



<p class="wp-block-paragraph">just to prove to you that we can execute like a full body of stuff so we have a label that</p>



<p class="wp-block-paragraph">designates when that body starts and then we have another label designating when the else body</p>



<p class="wp-block-paragraph">starts and then we should have a label that uh that designates when the whole thing is over</p>



<p class="wp-block-paragraph">label is something like if and or if is done or something like that so basically what we want to</p>



<p class="wp-block-paragraph">do is to implement an if else a simple if else in assembly we&#8217;re going to say let&#8217;s do a comparison</p>



<p class="wp-block-paragraph">and then we&#8217;ll do a conditional jump where are we going to conditionally jump well if the comparison</p>



<p class="wp-block-paragraph">was true label. And then what will happen is execution will fall through.</p>



<p class="wp-block-paragraph">When it reaches the end, we want to have another jump statement that unconditionally jumps</p>



<p class="wp-block-paragraph">to the end of the if statement. If we didn&#8217;t, then whenever the expression was true,</p>



<p class="wp-block-paragraph">we would end up executing all of the true statements and then it would fall through</p>



<p class="wp-block-paragraph">to all of the false statements or the not true statements. So we have to have many jumps in here</p>



<p class="wp-block-paragraph">let&#8217;s get into the body and then let&#8217;s finish and jump out of the body on the other hand if the</p>



<p class="wp-block-paragraph">expression was false then our jump instruction is going to jump you know over the true body so</p>



<p class="wp-block-paragraph">it&#8217;s not even going to do that at all it&#8217;s going to execute all of the else stuff and then it&#8217;s</p>



<p class="wp-block-paragraph">you know sometimes a good idea if you have another jump here that just jumps to the end but you can</p>



<p class="wp-block-paragraph">kind of see by the way i&#8217;ve written this out that there&#8217;s not going to be anything between the end</p>



<p class="wp-block-paragraph">you know place where we&#8217;re finished with everything which means we don&#8217;t really</p>



<p class="wp-block-paragraph">need an unconditional jump at the end of the else body we can just let the</p>



<p class="wp-block-paragraph">execution fall through so maybe if I can draw a little diagram here I&#8217;ll say I</p>



<p class="wp-block-paragraph">don&#8217;t know I&#8217;ll do like if put it in a little bubble and we&#8217;ll say that if it</p>



<p class="wp-block-paragraph">was false we jump to one place and if it was true we jump to another place</p>



<p class="wp-block-paragraph">we jump to another place.</p>



<p class="wp-block-paragraph">I hope you&#8217;ve already started to understand this by now.</p>



<p class="wp-block-paragraph">So we&#8217;ll say if the expression is true, we jump here.</p>



<p class="wp-block-paragraph">If the expression is false, we jump over here.</p>



<p class="wp-block-paragraph">And so true would be saying, let&#8217;s jump to the if was true.</p>



<p class="wp-block-paragraph">So we&#8217;re going to jump to if was,</p>



<p class="wp-block-paragraph">I&#8217;ll put a T there because I&#8217;m running out of space.</p>



<p class="wp-block-paragraph">If was true.</p>



<p class="wp-block-paragraph">was I&#8217;ll put an F false and then at the end of the true we jump to the done</p>



<p class="wp-block-paragraph">label so I&#8217;m just gonna put done maybe down here</p>



<p class="wp-block-paragraph">so at the end of the true we just unconditionally jump to the done area</p>



<p class="wp-block-paragraph">and then at the end of the false we jump unconditionally to the done area as well</p>



<p class="wp-block-paragraph">here&#8217;s something interesting though when we have a comparison instruction and</p>



<p class="wp-block-paragraph">conditional branch instruction let&#8217;s say uh let&#8217;s say a equals b we did that comparison and then we</p>



<p class="wp-block-paragraph">wanted to jump into the true area if a equals b was true so that means we&#8217;ll say jump</p>



<p class="wp-block-paragraph">equal so if a is equal to b after we compare well let&#8217;s say a comma b we&#8217;re going to use registers</p>



<p class="wp-block-paragraph">when we come to the code we&#8217;ll say compare a and b and then jump if they&#8217;re equal to some label</p>



<p class="wp-block-paragraph">but the conditional branching instructions they only jump to one</p>



<p class="wp-block-paragraph">potential place or fall through so if the comparison was false meaning if those</p>



<p class="wp-block-paragraph">two things were not equal then we&#8217;re not going to actually be able to jump to a</p>



<p class="wp-block-paragraph">different label we&#8217;re going to simply fall through to the next instruction so</p>



<p class="wp-block-paragraph">in that case the very next instruction would get executed let&#8217;s just put a jump</p>



<p class="wp-block-paragraph">was</p>



<p class="wp-block-paragraph">false.</p>



<p class="wp-block-paragraph">Meaning</p>



<p class="wp-block-paragraph">we compare A and B</p>



<p class="wp-block-paragraph">and if the two things are equal</p>



<p class="wp-block-paragraph">we&#8217;ll jump into the true block.</p>



<p class="wp-block-paragraph">Otherwise we fall through to the next instruction</p>



<p class="wp-block-paragraph">which</p>



<p class="wp-block-paragraph">that&#8217;s a very poorly written J</p>



<p class="wp-block-paragraph">but we&#8217;re going to unconditionally jump to false.</p>



<p class="wp-block-paragraph">So if we did not</p>



<p class="wp-block-paragraph">jump to the true area we fall through to the next</p>



<p class="wp-block-paragraph">instruction where we will always</p>



<p class="wp-block-paragraph">jump to false and that implements the diagram</p>



<p class="wp-block-paragraph">that implements the diagram that you see up above.</p>



<p class="wp-block-paragraph">I mean, you know, if we have if statement, we compare,</p>



<p class="wp-block-paragraph">maybe I&#8217;ll do, you know, A equals B.</p>



<p class="wp-block-paragraph">We jumped to true, if was true.</p>



<p class="wp-block-paragraph">And otherwise we end up falling through</p>



<p class="wp-block-paragraph">and then jumping to if was false.</p>



<p class="wp-block-paragraph">And then at the end of both of those,</p>



<p class="wp-block-paragraph">we have an unconditional jump instruction</p>



<p class="wp-block-paragraph">that jumps to the done.</p>



<p class="wp-block-paragraph">So I&#8217;ll put JMP down here just to let you know,</p>



<p class="wp-block-paragraph">you know, at the end of each of these blocks,</p>



<p class="wp-block-paragraph">that they are jumping out of themselves at the very end to the done area maybe</p>



<p class="wp-block-paragraph">I&#8217;ll put an arrow here so that we know both of these jumps end up jumping to</p>



<p class="wp-block-paragraph">the done area that&#8217;s the basic idea for how to implement an if-else block a very</p>



<p class="wp-block-paragraph">basic one we&#8217;re gonna do more complicated ones later but for now we</p>



<p class="wp-block-paragraph">kind of have the idea down I think let&#8217;s look at some code dang I blew 10 minutes</p>



<p class="wp-block-paragraph">already on that okay so I&#8217;m gonna copy paste some code from my solution here</p>



<p class="wp-block-paragraph">code for my solution here this again this is not like a beginner&#8217;s assembly</p>



<p class="wp-block-paragraph">video if you need to learn how to write assembly in the first place how to</p>



<p class="wp-block-paragraph">compile you know link create a make file and so forth you need to see my other</p>



<p class="wp-block-paragraph">videos first but for now we&#8217;re just going to assume that you know how to</p>



<p class="wp-block-paragraph">make a data section in Yasm and we&#8217;re gonna say let&#8217;s make a bunch of strings</p>



<p class="wp-block-paragraph">so first I&#8217;m gonna ask the user for an integer and then I&#8217;m gonna make a bunch</p>



<p class="wp-block-paragraph">of decisions like I&#8217;m gonna do you know an if-else block to test what kind of</p>



<p class="wp-block-paragraph">what kind of number they put you know did they make a number that uh</p>



<p class="wp-block-paragraph">equal one if it was equal to something oh did I ask twice I can&#8217;t remember what I&#8217;m doing</p>



<p class="wp-block-paragraph">but basically we&#8217;re going to print something if their number was equal to something else</p>



<p class="wp-block-paragraph">we&#8217;re going to print uh something if their number that they inputted was equal to five we&#8217;re going</p>



<p class="wp-block-paragraph">to print something else if their number was equal to six we&#8217;re going to print again something else</p>



<p class="wp-block-paragraph">a bunch of stuff and then wait isn&#8217;t this the complicated example oh no i think i am using code</p>



<p class="wp-block-paragraph">for my more complicated example else less than 10 i don&#8217;t know maybe this is the simple one</p>



<p class="wp-block-paragraph">let&#8217;s double check i guess if i put more complicated code in here you&#8217;ll probably be happy</p>



<p class="wp-block-paragraph">but whatever i thought this was going to be a simple example so i&#8217;m just defining strings at</p>



<p class="wp-block-paragraph">codes stuff that is covered in other videos now i&#8217;m going to start my text section so my text</p>



<p class="wp-block-paragraph">section begins with a declaration that i&#8217;m going to use two external functions so this video is</p>



<p class="wp-block-paragraph">not about this library that takes input and sends output i have other videos for that but basically</p>



<p class="wp-block-paragraph">i&#8217;m just using a library that lets you type an integer into the terminal and then it will print</p>



<p class="wp-block-paragraph">a different number to the terminal for you so you can imagine if you wanted to follow along</p>



<p class="wp-block-paragraph">imagine if you wanted to follow along with this code at home and you don&#8217;t</p>



<p class="wp-block-paragraph">have this library you can just hard code your numbers just to prove to yourself</p>



<p class="wp-block-paragraph">that you can get it to work or you can use a different library if you have a</p>



<p class="wp-block-paragraph">hybrid program and you&#8217;re linking against GCC you can just use scanf and</p>



<p class="wp-block-paragraph">printf pretty much those take a little bit more work to do but you can do it</p>



<p class="wp-block-paragraph">anyway so our entry point is going to be called if tester it&#8217;s a function called</p>



<p class="wp-block-paragraph">Again, this is a hybrid program and hybrid programs are not covered in this video, but you can imagine that there is a C++ module elsewhere in my source that is just going to call on a function called if tester.</p>



<p class="wp-block-paragraph">And so that&#8217;s why I&#8217;m marking this as global so that other modules in my program can call on if tester.</p>



<p class="wp-block-paragraph">So it&#8217;s just a little label that we can jump into or in this case call into.</p>



<p class="wp-block-paragraph">Let me go down to the bottom of that.</p>



<p class="wp-block-paragraph">We&#8217;ll put a return statement at the very end.</p>



<p class="wp-block-paragraph">very end so now this is officially a function and notice how i made a note here that says r12 is</p>



<p class="wp-block-paragraph">the user&#8217;s inputted integer so that means i&#8217;m going to be using r12 and since r12 is designated</p>



<p class="wp-block-paragraph">in the abi the application binary interface as callee saved or callee preserved that means i</p>



<p class="wp-block-paragraph">have to do a push pop pair to preserve it or i&#8217;ll get in lots and lots of trouble my program will</p>



<p class="wp-block-paragraph">prologue which my favorite book also calls the prologue and then I&#8217;m going to</p>



<p class="wp-block-paragraph">say epilogue and so now we have a function that basically doesn&#8217;t do</p>



<p class="wp-block-paragraph">anything but we can at least jump into it let&#8217;s see if this compiles as is I</p>



<p class="wp-block-paragraph">think it probably will yeah so the driver just says hello that&#8217;s the C++</p>



<p class="wp-block-paragraph">program with the actual main function and it calls my if tester function but</p>



<p class="wp-block-paragraph">nothing happens the if tester function returns control to the driver and then</p>



<p class="wp-block-paragraph">the driver just says okay i got control back so nothing really happened so now let&#8217;s ask the user</p>



<p class="wp-block-paragraph">for some input so again this is not a video about this library or how to print with this with system</p>



<p class="wp-block-paragraph">calls see my other videos if you need help on that but basically we&#8217;re going to print a message to</p>



<p class="wp-block-paragraph">the user hey please input an integer and then we&#8217;re going to call on a library function that</p>



<p class="wp-block-paragraph">lets them type in a number and then returns it to us in rax we&#8217;re then going to store it</p>



<p class="wp-block-paragraph">well just basic stuff right so if I run this again it&#8217;s going to ask for an</p>



<p class="wp-block-paragraph">integer please enter an integer if I can type that and then nothing else happens</p>



<p class="wp-block-paragraph">okay now we can kind of start making more decisions so I&#8217;m going to let&#8217;s see</p>



<p class="wp-block-paragraph">if test if test if test okay so what I need to do now is run another function</p>



<p class="wp-block-paragraph">called if tests several times and I&#8217;m going to compare the number that the</p>



<p class="wp-block-paragraph">times and I&#8217;m going to compare the number that the user inputted so remember r12 is the user&#8217;s</p>



<p class="wp-block-paragraph">input so I&#8217;m going to load that as the first argument of a function call and then I&#8217;m going</p>



<p class="wp-block-paragraph">to load the number five as the second argument of a function call probably not a great idea to hard</p>



<p class="wp-block-paragraph">code numbers in your assembly you should probably define them up in the globals or the the data</p>



<p class="wp-block-paragraph">section at least as just regular defines and not numbers in memory but I&#8217;m going to just keep these</p>



<p class="wp-block-paragraph">So three times we&#8217;re gonna call if test and then also another function called CRLF. So first I&#8217;m gonna paste in</p>



<p class="wp-block-paragraph">CRLF</p>



<p class="wp-block-paragraph">Where the heck is that? Oh, dude?</p>



<p class="wp-block-paragraph">Again, this is not a video about the basics</p>



<p class="wp-block-paragraph">So you&#8217;re just gonna have to trust me or go watch my other videos if you don&#8217;t understand what I&#8217;m doing here</p>



<p class="wp-block-paragraph">But I have a function called</p>



<p class="wp-block-paragraph">CRLF and its whole job in life is just to print a new line for me just because I like to do it that way</p>



<p class="wp-block-paragraph">handles the CRLF call. Now let&#8217;s make another function called if test.</p>



<p class="wp-block-paragraph">So I&#8217;m going to start that by designating its label right after this block of</p>



<p class="wp-block-paragraph">code here, maybe before CRLF.</p>



<p class="wp-block-paragraph">So we have like a basic if test function and here&#8217;s my prototype just to remind</p>



<p class="wp-block-paragraph">myself what I&#8217;m going to be doing.</p>



<p class="wp-block-paragraph">It&#8217;s going to take an input and it&#8217;s going to take another input for a test</p>



<p class="wp-block-paragraph">against me. So the first one is like the user&#8217;s input.</p>



<p class="wp-block-paragraph">The second one is the number that I want to test it against.</p>



<p class="wp-block-paragraph">number that I want to test it against they&#8217;re both longs which means they are both integers</p>



<p class="wp-block-paragraph">which means the incoming arguments are going to be rdi and rsi if you&#8217;re respecting the abi</p>



<p class="wp-block-paragraph">and then some notes to remind myself I&#8217;m going to be using r12 and r13 inside of this function so</p>



<p class="wp-block-paragraph">I&#8217;m going to start by putting a return statement there since that is what it takes to make a label</p>



<p class="wp-block-paragraph">into a return into into a function then I&#8217;m going to preserve the callee saved registers again if</p>



<p class="wp-block-paragraph">don&#8217;t know what i&#8217;m talking about see my other videos we&#8217;re going to push r12 and push r13 so</p>



<p class="wp-block-paragraph">that they are not ruined for the caller we call this the prolog then at the very bottom of the</p>



<p class="wp-block-paragraph">function we have the epilog which just restores uh the registers in reverse order you&#8217;ve got to</p>



<p class="wp-block-paragraph">do it in reverse order see my other videos if you don&#8217;t understand why okay so that&#8217;s basically a</p>



<p class="wp-block-paragraph">function that can get called it doesn&#8217;t do anything let me double check that the program still actually</p>



<p class="wp-block-paragraph">works. 66 and nothing happens. We just printed CRLF a bunch of times. Okay, so now we&#8217;re ready</p>



<p class="wp-block-paragraph">to continue. So let&#8217;s grab the function arguments. Remember we were going to use R12 and R13 for the</p>



<p class="wp-block-paragraph">user&#8217;s input and the number we will test against. Those came into our function with RDI and RSI.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy those two incoming arguments into R12 and R13. And you&#8217;re supposed</p>



<p class="wp-block-paragraph">keep the user&#8217;s input in RDI then the moment I call any other function or system call I&#8217;m just</p>



<p class="wp-block-paragraph">going to lose that data so I&#8217;m going to keep it inside of R12 and R13 so I grab the function</p>



<p class="wp-block-paragraph">arguments and then I print a begin message just to let the user know that we&#8217;re going to start</p>



<p class="wp-block-paragraph">you know making tests against our number so this is just basically a message saying hey begin the</p>



<p class="wp-block-paragraph">if test and then print what we&#8217;re going to check against so the next thing is</p>



<p class="wp-block-paragraph">I&#8217;m going to let the user know what the second incoming argument was.</p>



<p class="wp-block-paragraph">If you look at R13 here, that was RSI, which was the second argument.</p>



<p class="wp-block-paragraph">So the test against me number.</p>



<p class="wp-block-paragraph">So we&#8217;re going to check the user&#8217;s input against whatever we called for the second argument.</p>



<p class="wp-block-paragraph">So I just wanted to print it out.</p>



<p class="wp-block-paragraph">You know, like we&#8217;re testing your number against whatever.</p>



<p class="wp-block-paragraph">So that means I need to make another call to my little printing library here.</p>



<p class="wp-block-paragraph">To RDI which is the first argument notice how we already we already destroyed RDI. That&#8217;s why I&#8217;m keeping the input in our 12 and our 13</p>



<p class="wp-block-paragraph">And then we&#8217;re gonna make that call and we can just assume everything will go according to plan at that point and then</p>



<p class="wp-block-paragraph">Then we&#8217;re gonna print a special message only if something actually happens</p>



<p class="wp-block-paragraph">So we&#8217;re gonna implement the if else block in a moment. Let me just run this real fast</p>



<p class="wp-block-paragraph">and then it says we&#8217;re calling the function three times and we&#8217;re saying the basic test has begun</p>



<p class="wp-block-paragraph">we&#8217;ll test against this number five so we&#8217;re testing your input against five and then six and</p>



<p class="wp-block-paragraph">then seven and then we didn&#8217;t actually do anything we&#8217;re about to okay so then the next thing is we&#8217;re</p>



<p class="wp-block-paragraph">going to print a special message only if the user entered the right number so first off remember we</p>



<p class="wp-block-paragraph">see if else block and converting it into assembly so i&#8217;m sort of placing that in comments for you</p>



<p class="wp-block-paragraph">so the comparison instruction i i didn&#8217;t want to put the r12 equals r13 inside of the same comment</p>



<p class="wp-block-paragraph">that lines up with the the compare instruction because the compare instruction as i&#8217;ve said in</p>



<p class="wp-block-paragraph">other videos already it doesn&#8217;t actually check to see if something&#8217;s equal it just makes a bunch of</p>



<p class="wp-block-paragraph">called r flags with information that we can later use to decide if the two things were equal or not</p>



<p class="wp-block-paragraph">equal or greater than or less than or whatever so that&#8217;s why i chose to put that on on the next line</p>



<p class="wp-block-paragraph">so at this point we&#8217;re saying if those two things were equal and that&#8217;s how i implement the expression</p>



<p class="wp-block-paragraph">in the middle then let&#8217;s jump to a special label called if test if was equal so you can come up</p>



<p class="wp-block-paragraph">with any scheme you want for your labels but for me when i have when i have sub labels inside of a</p>



<p class="wp-block-paragraph">when i have sub labels inside of a function i like to just suffix the function&#8217;s name with an</p>



<p class="wp-block-paragraph">underscore and then start thinking of sub labels after that so everything&#8217;s like kind of clean</p>



<p class="wp-block-paragraph">there&#8217;s less chance of overlap in labels if you have like a giant module with tons of functions</p>



<p class="wp-block-paragraph">so i&#8217;m going to say this is like my main if that i&#8217;m checking and uh i&#8217;m going to jump to a label</p>



<p class="wp-block-paragraph">called was equal meaning you know this evaluated to true that means that should be the true part</p>



<p class="wp-block-paragraph">block let&#8217;s see do i still have that code here yeah right so here i&#8217;m going to jump to</p>



<p class="wp-block-paragraph">the true block so you know if was true in the first example that we talked about so i&#8217;m going</p>



<p class="wp-block-paragraph">to say if it&#8217;s if those things were equal jump to the code for the true block okay that means i</p>



<p class="wp-block-paragraph">actually need the true block uh but i guess we&#8217;re going to set that up in a second otherwise if that</p>



<p class="wp-block-paragraph">did not jump away it means that those two things are not equal so i said i should jump to the</p>



<p class="wp-block-paragraph">I should jump to the else block or the false block.</p>



<p class="wp-block-paragraph">Wait, do I have else in this example?</p>



<p class="wp-block-paragraph">Oh, okay, okay.</p>



<p class="wp-block-paragraph">This first thing that we&#8217;re looking at is only if.</p>



<p class="wp-block-paragraph">So we don&#8217;t even have an else block yet.</p>



<p class="wp-block-paragraph">We&#8217;re going to do that as the second example.</p>



<p class="wp-block-paragraph">So we&#8217;re basically going to jump to the done area</p>



<p class="wp-block-paragraph">if we didn&#8217;t jump into the true area.</p>



<p class="wp-block-paragraph">And then at the end of the true area,</p>



<p class="wp-block-paragraph">we can either jump to the done area</p>



<p class="wp-block-paragraph">or we can let the execution fall through.</p>



<p class="wp-block-paragraph">fall through okay so now that means i need if was equal and if i just copy paste a big giant</p>



<p class="wp-block-paragraph">block of code and try to explain it to you real fast let&#8217;s do this</p>



<p class="wp-block-paragraph">okay so we were going to jump to if was equal if r12 was equal to r13 and then um</p>



<p class="wp-block-paragraph">we uh have this you know label here and notice how i&#8217;ve kind of like put a brace here indicating</p>



<p class="wp-block-paragraph">like put a brace here indicating hey this is the beginning of the true block body you should</p>



<p class="wp-block-paragraph">consider doing this too when you&#8217;re first learning and even after you&#8217;ve learned because let&#8217;s face</p>



<p class="wp-block-paragraph">it assembly is tough and so in the true area i&#8217;m just going to print the equality message i&#8217;m going</p>



<p class="wp-block-paragraph">to say hey your number was equal to you know whatever and then uh i&#8217;m going to actually print</p>



<p class="wp-block-paragraph">the let&#8217;s see r13 number so that was uh i think the number to compare against the test against</p>



<p class="wp-block-paragraph">against me number so that means here in this message we&#8217;re going to say hey your number was</p>



<p class="wp-block-paragraph">equal to the number that we tested against so then otherwise let&#8217;s see or sorry after that</p>



<p class="wp-block-paragraph">we&#8217;ll print the suffix of the message um and so you know i just i just like to make pretty</p>



<p class="wp-block-paragraph">pretty uh printed messages so let&#8217;s see where&#8217;s the suffix here i can&#8217;t even find it um how about</p>



<p class="wp-block-paragraph">oh i should have put suffix instead of the number two that would have been better so what i wanted</p>



<p class="wp-block-paragraph">to do is say your input was equal to and then print the number and then after that print an</p>



<p class="wp-block-paragraph">exclamation just to prove to you how easy it is to to make a pretty message that&#8217;s formatted nicely</p>



<p class="wp-block-paragraph">for the user um or your professor or whoever so uh you know you basically just print a number and</p>



<p class="wp-block-paragraph">then you or sorry you print the prefix and then you print the number and then you print the suffix</p>



<p class="wp-block-paragraph">suffix and then that&#8217;s the end of the true body and then since we&#8217;re done with the true body we</p>



<p class="wp-block-paragraph">can basically just say all right now we&#8217;re done with the if so the next instruction that follows</p>



<p class="wp-block-paragraph">is the done area again looking back at the example here that would be sort of like here after the</p>



<p class="wp-block-paragraph">whole entire block was finished we&#8217;re ignoring the else in this code but you can imagine we&#8217;ll do</p>



<p class="wp-block-paragraph">that soon so if the user&#8217;s number matched something we execute a true body if not we jump to the done</p>



<p class="wp-block-paragraph">to the done area and if the user&#8217;s uh number did not match then we just immediately jump to the</p>



<p class="wp-block-paragraph">done area so that we don&#8217;t do the true area and then we write comments to ourselves to help us</p>



<p class="wp-block-paragraph">remember oh look here&#8217;s like the comparison and then here&#8217;s the body of true and then uh the done</p>



<p class="wp-block-paragraph">area is like we could put another comment if we want we could say like this is done but i kind of</p>



<p class="wp-block-paragraph">think like i kind of think the label is self-explanatory so let&#8217;s see if this worked it&#8217;s</p>



<p class="wp-block-paragraph">was talking too fast we&#8217;ll run it and we&#8217;ll say 44. okay so nothing matched any of the numbers so</p>



<p class="wp-block-paragraph">let&#8217;s type the number six so that we get a little message on the second one so i&#8217;m going to type the</p>



<p class="wp-block-paragraph">number six and you can see it did not match the number five and it did match the number six so</p>



<p class="wp-block-paragraph">we got that true block executing when we called on that function where the number to compare to</p>



<p class="wp-block-paragraph">nothing there. So now we know simple if blocks.</p>



<p class="wp-block-paragraph">The next thing we&#8217;re going to do is if else blocks.</p>



<p class="wp-block-paragraph">All right.</p>



<p class="wp-block-paragraph">So let&#8217;s see.</p>



<p class="wp-block-paragraph">Not sure if I&#8217;m going to cut the video and split this up into multiple parts.</p>



<p class="wp-block-paragraph">Probably would have been a smart idea.</p>



<p class="wp-block-paragraph">Let me know if I ended up doing it.</p>



<p class="wp-block-paragraph">We&#8217;re at about 25 minutes now.</p>



<p class="wp-block-paragraph">But anyway,</p>



<p class="wp-block-paragraph">so now we&#8217;re going to look at if else blocks.</p>



<p class="wp-block-paragraph">if else blocks.</p>



<p class="wp-block-paragraph">So I&#8217;m going to start off with another function.</p>



<p class="wp-block-paragraph">I&#8217;m going to call it if else test.</p>



<p class="wp-block-paragraph">So here&#8217;s the tester.</p>



<p class="wp-block-paragraph">It called on if test.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to, you know, I had the if test function that I did previously.</p>



<p class="wp-block-paragraph">And so now we&#8217;re just going to make an if else test function.</p>



<p class="wp-block-paragraph">The if else test, it just has one input, one argument for input.</p>



<p class="wp-block-paragraph">And we&#8217;re going to just sort of compare it against different values.</p>



<p class="wp-block-paragraph">of compare it against different values we&#8217;re not going to call this multiple times with different</p>



<p class="wp-block-paragraph">values to compare against so that means let&#8217;s see we only need to use one register so</p>



<p class="wp-block-paragraph">i&#8217;ve designated r12 as the user&#8217;s input so that&#8217;s where we&#8217;re going to store it which means we</p>



<p class="wp-block-paragraph">should preserve it in the prologue so i&#8217;m going to push r12 and then again if you don&#8217;t understand</p>



<p class="wp-block-paragraph">some of the basic stuff that i&#8217;m that i&#8217;m skipping over see my other videos where i explained</p>



<p class="wp-block-paragraph">to know to actually have this kind of a program. So I&#8217;m going to have a function that enters and</p>



<p class="wp-block-paragraph">it returns at the end. It uses R12 so we will preserve it with a push pop pair.</p>



<p class="wp-block-paragraph">And then the first thing that I should do is grab the user&#8217;s input from the first argument.</p>



<p class="wp-block-paragraph">And if you&#8217;re respecting the ABI that means it should come from RDI. So I&#8217;m going to move RDI</p>



<p class="wp-block-paragraph">somebody remind me that the first part of the program has to actually call this function or</p>



<p class="wp-block-paragraph">nothing will happen. So we&#8217;re going to say hello. That&#8217;s just a simple message that we talked about</p>



<p class="wp-block-paragraph">before. And then now we&#8217;re going to actually implement the if else block. So it&#8217;s like a</p>



<p class="wp-block-paragraph">little bit more complicated than just the simple if block. It&#8217;s going to be this whole example that</p>



<p class="wp-block-paragraph">we talked about before. Let me see. Maybe I should add the calls to this block real fast. So we have</p>



<p class="wp-block-paragraph">to this block real fast so we have the if tester and then run the complex if else tests</p>



<p class="wp-block-paragraph">right before r12 okay let me just double check that i&#8217;m making a call at the right spot here</p>



<p class="wp-block-paragraph">so we have r12 and 7 and then epilog okay so now finally in our program we&#8217;re going to have a call</p>



<p class="wp-block-paragraph">to um the if else test function that we&#8217;re just making right now</p>



<p class="wp-block-paragraph">Okay, so we have that.</p>



<p class="wp-block-paragraph">And let me find that source again real fast.</p>



<p class="wp-block-paragraph">If else test, we got the prologue.</p>



<p class="wp-block-paragraph">We take their input and then we say hello.</p>



<p class="wp-block-paragraph">Okay, so now we need another label that begins the if block.</p>



<p class="wp-block-paragraph">So what we&#8217;re doing is we&#8217;re checking to see if the user&#8217;s input was equal to five.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to say something if it was.</p>



<p class="wp-block-paragraph">And otherwise, we&#8217;re going to do an else block on that.</p>



<p class="wp-block-paragraph">block on that so again i like to write my comparison instructions with a blank expression</p>



<p class="wp-block-paragraph">in terms of the c equivalent comment so notice how the we&#8217;re checking to see if r12 is equal to</p>



<p class="wp-block-paragraph">five i put that on the next block because we&#8217;re going to jump to the to the true part of the if</p>



<p class="wp-block-paragraph">block if it equals five so that&#8217;s why i put that there the comparison instruction pretty much just</p>



<p class="wp-block-paragraph">compares r12 with five sets a bunch of values into the r flags registers so that we can later</p>



<p class="wp-block-paragraph">so that we can later conditionally jump if we want to.</p>



<p class="wp-block-paragraph">So basically we&#8217;ll jump to the true place if that was true.</p>



<p class="wp-block-paragraph">And then if not, execution falls through to the next statement,</p>



<p class="wp-block-paragraph">which will just jump to the else place.</p>



<p class="wp-block-paragraph">So we need more labels is what I&#8217;m saying.</p>



<p class="wp-block-paragraph">So now we need a body for if the statement was true</p>



<p class="wp-block-paragraph">or the expression was true,</p>



<p class="wp-block-paragraph">we want to be able to execute the true portion of the if block.</p>



<p class="wp-block-paragraph">the if block so that&#8217;s this right here so we&#8217;re going to jump to if it did equal to five you</p>



<p class="wp-block-paragraph">could imagine you know making a better label instead of equal five you could say first if</p>



<p class="wp-block-paragraph">first else if complicated block true scope or true block or something but i just put equal to five</p>



<p class="wp-block-paragraph">basically saying we&#8217;ll execute this code if it was equal to five so again we&#8217;re just kind of like</p>



<p class="wp-block-paragraph">into this label equal five if r12 was indeed equal to five so that means if it was we execute</p>



<p class="wp-block-paragraph">all this code right here we know we&#8217;re finished when we have the very last line saying let&#8217;s jump</p>



<p class="wp-block-paragraph">to finish the the if else block again just just to clarify</p>



<p class="wp-block-paragraph">we do the comparison first and if the comparison was true then we&#8217;ll jump into the true area so</p>



<p class="wp-block-paragraph">you know we&#8217;ll jump into the true area and then all of these uh instructions get executed</p>



<p class="wp-block-paragraph">instructions get executed but if we don&#8217;t have a way to jump out of that block then whoops</p>



<p class="wp-block-paragraph">all of the else statements are going to get executed too right so we don&#8217;t want that</p>



<p class="wp-block-paragraph">we don&#8217;t want to execute both the true and the false statements we want to have a jump instruction</p>



<p class="wp-block-paragraph">at the very end i&#8217;ll put jmp just so that we jump to the to the end of the if else block</p>



<p class="wp-block-paragraph">you know jump to the place where it&#8217;s just all over and finished</p>



<p class="wp-block-paragraph">Okay, so now I&#8217;m going to look at, whoops, turn that off.</p>



<p class="wp-block-paragraph">So we have equal five.</p>



<p class="wp-block-paragraph">So we&#8217;re going to jump to if else test if done.</p>



<p class="wp-block-paragraph">Let me just double check to make sure I&#8217;m not forgetting anything.</p>



<p class="wp-block-paragraph">Oh, we need to jump here.</p>



<p class="wp-block-paragraph">Basically meaning we&#8217;re going to jump into the else portion.</p>



<p class="wp-block-paragraph">So here we covered jumping into the true portion, you know, the regular top block.</p>



<p class="wp-block-paragraph">Now here we&#8217;re going to jump into the else portion.</p>



<p class="wp-block-paragraph">We need a label and some code for that.</p>



<p class="wp-block-paragraph">code for that so this is the else six begin i copy that oh man it&#8217;s starting to get kind of</p>



<p class="wp-block-paragraph">hectic in my brain here the copy pasting is worse than actually writing the program</p>



<p class="wp-block-paragraph">so uh if the comparison was not equal like if r12 was not equal then execution falls through</p>



<p class="wp-block-paragraph">to line 216 and then we unconditionally jump to the l6 begin area which is like down here</p>



<p class="wp-block-paragraph">like down here and then we&#8217;ll print something else.</p>



<p class="wp-block-paragraph">We&#8217;ll, oh, we&#8217;ll, we&#8217;ll check again.</p>



<p class="wp-block-paragraph">So we&#8217;re doing like, um, if else, if else.</p>



<p class="wp-block-paragraph">Okay. So originally when I was talking about the, uh,</p>



<p class="wp-block-paragraph">if else block, I didn&#8217;t do a,</p>



<p class="wp-block-paragraph">I didn&#8217;t do like a very complex if else statement.</p>



<p class="wp-block-paragraph">We&#8217;ll say a is greater than B something like that.</p>



<p class="wp-block-paragraph">So I&#8217;ll just put a more code here just so you know that we can do if else,</p>



<p class="wp-block-paragraph">if else blocks but just you know again keep in mind that every scope here that you&#8217;re going to</p>



<p class="wp-block-paragraph">try to run based on some condition you just make it its own label and make sure that at the very</p>



<p class="wp-block-paragraph">end of the scope you jump away so that you reach the very end of all this stuff because just as a</p>



<p class="wp-block-paragraph">quick review if we are just writing in c or c plus plus only one of these blocks is going to execute</p>



<p class="wp-block-paragraph">right like if a is equal to b then only that first block will execute the second and third blocks</p>



<p class="wp-block-paragraph">will not execute it&#8217;ll jump after that all the way down to line 21 only if a does not equal b</p>



<p class="wp-block-paragraph">do we even have the chance of checking to see if a is more than b if it&#8217;s false then we&#8217;ll have the</p>



<p class="wp-block-paragraph">chance to check the else and if it&#8217;s true we will only execute the code in the line 12 block you</p>



<p class="wp-block-paragraph">know imagine there are more statements there and when that block is done then we will jump to line</p>



<p class="wp-block-paragraph">to b and a is not greater than b so just a quick uh you know c plus plus uh you know design pattern</p>



<p class="wp-block-paragraph">review if else block review so we have like an else if here we&#8217;re going to say check if the input</p>



<p class="wp-block-paragraph">was equal to six so we just do the same thing that we did before we compare r12 the user&#8217;s input with</p>



<p class="wp-block-paragraph">six we jump if they were equal to the else if equals true block and if not execution falls</p>



<p class="wp-block-paragraph">Less than 10 begin block. So this is going to be like really complicated</p>



<p class="wp-block-paragraph">How many lines did I actually I think I got excited when I wrote this</p>



<p class="wp-block-paragraph">Okay, else if equal six true</p>



<p class="wp-block-paragraph">My challenge to you is to come up with labels that are like way easier than the labels that I came up with</p>



<p class="wp-block-paragraph">All right, so we&#8217;re gonna do this I</p>



<p class="wp-block-paragraph">Can almost guarantee that when I&#8217;m done copy pasting everything something is not going to compile because I forgot a label somewhere</p>



<p class="wp-block-paragraph">Else if equals six true, okay</p>



<p class="wp-block-paragraph">equals six true okay so what&#8217;s happening here again um uh so at this point you know the first</p>



<p class="wp-block-paragraph">if uh expression was not true r12 was definitely not equal to five so we jumped down to else if</p>



<p class="wp-block-paragraph">equal six begin which was here and so then we just make another comparison to see well okay it was</p>



<p class="wp-block-paragraph">those two things weren&#8217;t equal it wasn&#8217;t equal to five so let&#8217;s check to see if it was equal to six</p>



<p class="wp-block-paragraph">to yet another scope.</p>



<p class="wp-block-paragraph">If it was false, we go down to line 237</p>



<p class="wp-block-paragraph">and jump to yet another scope.</p>



<p class="wp-block-paragraph">So here is what will get executed</p>



<p class="wp-block-paragraph">if R12 was indeed equal to six,</p>



<p class="wp-block-paragraph">and then we&#8217;re just basically gonna say it to the user</p>



<p class="wp-block-paragraph">and then jump to the done label,</p>



<p class="wp-block-paragraph">meaning like we&#8217;re totally done with our if else block.</p>



<p class="wp-block-paragraph">Notice how the first if here,</p>



<p class="wp-block-paragraph">when it was totally finished, it jumped to the done area.</p>



<p class="wp-block-paragraph">this else if block is also jumping down to the if done area.</p>



<p class="wp-block-paragraph">So eventually we&#8217;re going to need that label.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then next we&#8217;re going to check to see if the user&#8217;s input was equal to a 10.</p>



<p class="wp-block-paragraph">So like kind of the same thing here.</p>



<p class="wp-block-paragraph">We&#8217;re going to do another copy paste and we&#8217;re going to say, all right.</p>



<p class="wp-block-paragraph">So if the user&#8217;s input was not equal to, let&#8217;s see,</p>



<p class="wp-block-paragraph">if it was not equal to five, then we jump down here for our next comparison.</p>



<p class="wp-block-paragraph">comparison we check to see if it&#8217;s equal to six if it was not equal to six then we jump down to</p>



<p class="wp-block-paragraph">the less 10 begin line which is like all the way down here we do another comparison to see all right</p>



<p class="wp-block-paragraph">well is it less than 10 you know if it was not equal to five or not equal to six then we check</p>



<p class="wp-block-paragraph">is it less than 10 if it uh if it is then we uh jump to the else if equal less 10 true block which</p>



<p class="wp-block-paragraph">I made this way too complicated. I realize that now, but it&#8217;s too late. I&#8217;m going for it, man.</p>



<p class="wp-block-paragraph">Anyway, so if that statement is true, if R12 was indeed less than 10, then we jump to this block</p>



<p class="wp-block-paragraph">and we basically just say that to the user and then we jump to the done. So finally, notice how</p>



<p class="wp-block-paragraph">this part right here on line 259, it&#8217;s basically saying if R12 was not less than 10, then we&#8217;ll</p>



<p class="wp-block-paragraph">then we&#8217;ll jump somewhere else notice how it&#8217;s just else right so this is like the very the very</p>



<p class="wp-block-paragraph">bottom so i think the way that i wrote this uh code is uh we have two else ifs right we have like</p>



<p class="wp-block-paragraph">an if five else if six else if less than 10 so maybe i could do something like this um if r12</p>



<p class="wp-block-paragraph">r12 I think I said six just now right hopefully I actually did say six</p>



<p class="wp-block-paragraph">otherwise if r12 is less than 10 then do some stuff otherwise if nothing else</p>



<p class="wp-block-paragraph">matched then we&#8217;ll execute you know this block right here so again remember</p>



<p class="wp-block-paragraph">every single scope has to have its own label so that you know where to jump and</p>



<p class="wp-block-paragraph">it&#8217;s also a really really smart idea for every single scope to have you know</p>



<p class="wp-block-paragraph">to have a little jump instruction that jumps past all of the if else if else if else stuff</p>



<p class="wp-block-paragraph">so we&#8217;ll say like you know label you know done so that we can make sure that only one of these</p>



<p class="wp-block-paragraph">blocks actually executes which is how you&#8217;re supposed to imagine c and c plus plus work</p>



<p class="wp-block-paragraph">um and so it&#8217;s just complicated because there&#8217;s a lot of stuff to copy paste but you can just</p>



<p class="wp-block-paragraph">still see you know only one of these blocks ever is supposed to execute so we give the first one</p>



<p class="wp-block-paragraph">we jump into it if it&#8217;s true if it&#8217;s not true then we give the second one a chance</p>



<p class="wp-block-paragraph">we jump into it if it&#8217;s true if not we jump to compare the third one if it&#8217;s true then we jump</p>



<p class="wp-block-paragraph">to its scope if not we jump to the else and the else always executes if nothing else above</p>



<p class="wp-block-paragraph">was true so that&#8217;s the basic idea here oh dear i&#8217;ve probably lost my place</p>



<p class="wp-block-paragraph">looks like i just copy pasted less 10 true which was this right here so</p>



<p class="wp-block-paragraph">so um less than true or less than 10 true okay so we told the user your stuff is less than 10</p>



<p class="wp-block-paragraph">and then we jumped to the done area so that means we are probably working on the else area okay let</p>



<p class="wp-block-paragraph">me grab that so now finally we&#8217;re going to have the else area which is going to you know finish</p>



<p class="wp-block-paragraph">this all up that&#8217;s going to be after the if done so if nothing else matched then we will end up</p>



<p class="wp-block-paragraph">nothing else matched then we will end up jumping to the else area and then we&#8217;ll basically</p>



<p class="wp-block-paragraph">just tell the user none of the conditions seemed to have applied and then even at the</p>



<p class="wp-block-paragraph">end of the else block even though you could probably get away with just letting execution</p>



<p class="wp-block-paragraph">fall through just to save yourself one instruction you know you could comment that out assuming</p>



<p class="wp-block-paragraph">you were sure that the very next instruction was the beginning of the done area but otherwise</p>



<p class="wp-block-paragraph">I&#8217;m just going to play it safe and jump directly there then we have to make the actual done</p>



<p class="wp-block-paragraph">So, just so you know, a label doesn&#8217;t have to have any instructions.</p>



<p class="wp-block-paragraph">We could have something like this, if else test say goodbye.</p>



<p class="wp-block-paragraph">We could have two labels right next to each other and one doesn&#8217;t actually have instructions.</p>



<p class="wp-block-paragraph">That&#8217;s totally fine.</p>



<p class="wp-block-paragraph">If you jumped to the done area, then execution would just fall through to the next valid</p>



<p class="wp-block-paragraph">instruction which could go through another label.</p>



<p class="wp-block-paragraph">So you know, we could have a say goodbye label and an if done label.</p>



<p class="wp-block-paragraph">if done label, I&#8217;m just going to have the goodbye stuff happening inside of the done.</p>



<p class="wp-block-paragraph">But for clarity&#8217;s sake, you might want to keep that label in there that I just deleted.</p>



<p class="wp-block-paragraph">And all that we need to do at the very end when we&#8217;re done with everything is just say goodbye.</p>



<p class="wp-block-paragraph">So I&#8217;m just printing a message.</p>



<p class="wp-block-paragraph">So just to do a quick recap here, let&#8217;s see.</p>



<p class="wp-block-paragraph">We come in with the user&#8217;s input as R12.</p>



<p class="wp-block-paragraph">We ask, does R12 equal five?</p>



<p class="wp-block-paragraph">If that&#8217;s true, we&#8217;ll say that it was equal to 5, and then we&#8217;ll go to the done area,</p>



<p class="wp-block-paragraph">meaning we&#8217;ll skip past all the other blocks for the if-else block, or all the other scopes.</p>



<p class="wp-block-paragraph">But if that did not equal 5, then we&#8217;ll fall through to that jump instruction,</p>



<p class="wp-block-paragraph">which takes us to the next test, which is going to be our else-if equals 6.</p>



<p class="wp-block-paragraph">So we are checking now to see if R12 is equal to 6.</p>



<p class="wp-block-paragraph">If that&#8217;s true, we jump to this next block.</p>



<p class="wp-block-paragraph">Why do we need to jump to a block that&#8217;s so close?</p>



<p class="wp-block-paragraph">Well, if we don&#8217;t, then we&#8217;re going to end up definitely jumping to the next comparison</p>



<p class="wp-block-paragraph">and you know, that wouldn&#8217;t work.</p>



<p class="wp-block-paragraph">So we&#8217;re just kind of jumping over this unconditional jump statement and we&#8217;re saying, all right,</p>



<p class="wp-block-paragraph">your input was definitely equal to six.</p>



<p class="wp-block-paragraph">And then we&#8217;re jumping to the done area just past everything.</p>



<p class="wp-block-paragraph">But if that wasn&#8217;t true, then we hit this unconditional jump that takes us to the next</p>



<p class="wp-block-paragraph">comparison to see if the user&#8217;s input was less than 10.</p>



<p class="wp-block-paragraph">You know, if that&#8217;s true, then we go to the true area to print another message and then</p>



<p class="wp-block-paragraph">go to the done area.</p>



<p class="wp-block-paragraph">If it was false, we fall through to the else jump, which will take us to here.</p>



<p class="wp-block-paragraph">And notice how the else scope doesn&#8217;t actually make any comparisons because, you know, when</p>



<p class="wp-block-paragraph">you have like an if else, sorry, when you have an if else if else if else if, you know,</p>



<p class="wp-block-paragraph">any number of else if blocks, the else will always be executed if nothing above it actually</p>



<p class="wp-block-paragraph">did execute.</p>



<p class="wp-block-paragraph">execute so if you have an else block that means something will execute so we&#8217;re not doing any</p>



<p class="wp-block-paragraph">comparisons we&#8217;re just saying you know when that scope is done we&#8217;re just going to jump to the done</p>



<p class="wp-block-paragraph">area so all of these different scopes they&#8217;re jumping to the done area when they finish and</p>



<p class="wp-block-paragraph">the done area is just this label right here where we say goodbye and that&#8217;s it let&#8217;s see did i copy</p>



<p class="wp-block-paragraph">paste everything that i was supposed to i think so probably so let&#8217;s run the program and see if it</p>



<p class="wp-block-paragraph">All right, it at least compiled.</p>



<p class="wp-block-paragraph">So the basic if test, that was the first part of either this video or the previous video,</p>



<p class="wp-block-paragraph">depending on whether I chose to split this up.</p>



<p class="wp-block-paragraph">So let me comment out those calls real fast.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just comment out these calls real quick so we can only deal with the complicated</p>



<p class="wp-block-paragraph">if else block.</p>



<p class="wp-block-paragraph">So I&#8217;m going to enter like a three.</p>



<p class="wp-block-paragraph">Whoops, let me do it again.</p>



<p class="wp-block-paragraph">Three.</p>



<p class="wp-block-paragraph">And so begin the if else test.</p>



<p class="wp-block-paragraph">test your input was definitely less than 10 and then we end the if-else test so notice how only</p>



<p class="wp-block-paragraph">one scope executed three was definitely less than 10 what other numbers did we have let me write</p>



<p class="wp-block-paragraph">them down somewhere so I don&#8217;t forget oh they&#8217;re written down right here so we entered a three</p>



<p class="wp-block-paragraph">which was definitely not a five and not a six so that&#8217;s why the less than 10 block executed maybe</p>



<p class="wp-block-paragraph">only the first scope should execute so it says your input was equal to five notice how it didn&#8217;t</p>



<p class="wp-block-paragraph">mention that it was a six or less than ten and then if we do a six it should just tell us that</p>



<p class="wp-block-paragraph">we have a six it will not mention the five it will not mention the ten so we did we did five</p>



<p class="wp-block-paragraph">already and then we did six and we did like a two which was less than ten we could also do a one</p>



<p class="wp-block-paragraph">which was less than ten we can do anything that was less than ten and then the else would be if</p>



<p class="wp-block-paragraph">And then the else would be if our input is probably like 11 or greater,</p>



<p class="wp-block-paragraph">meaning it&#8217;s not less than 10 and it&#8217;s also not five or six.</p>



<p class="wp-block-paragraph">Just we can see the else block.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do 11, which is the first number that should trigger the else block.</p>



<p class="wp-block-paragraph">And it says no conditions were satisfied,</p>



<p class="wp-block-paragraph">which was the message inside of our else block.</p>



<p class="wp-block-paragraph">Let me just show you that again real fast.</p>



<p class="wp-block-paragraph">This was the else block.</p>



<p class="wp-block-paragraph">So message if else else or labeling on my port, I realize that.</p>



<p class="wp-block-paragraph">But it basically says no conditions were satisfied.</p>



<p class="wp-block-paragraph">The if else else.</p>



<p class="wp-block-paragraph">I think that&#8217;s basically everything that I wanted to show you.</p>



<p class="wp-block-paragraph">At this point, you should feel like you&#8217;re starting to become an expert at complicated</p>



<p class="wp-block-paragraph">if else if else blocks, converting those from a higher level language into assembly.</p>



<p class="wp-block-paragraph">assembly and I honestly recommend that you practice this like crazy while you&#8217;re trying</p>



<p class="wp-block-paragraph">to get more used to it but you know hopefully you have everything you need at this point</p>



<p class="wp-block-paragraph">keep in mind I&#8217;ve said this at the beginning I&#8217;ve actually I think I said this in a different video</p>



<p class="wp-block-paragraph">when we use these branching instructions notice how I have jump equal to where&#8217;s that less than</p>



<p class="wp-block-paragraph">yeah notice how I have a instruction jump less than the the family of conditional branching</p>



<p class="wp-block-paragraph">conditional branching instructions that compares less than greater than less than or equal to</p>



<p class="wp-block-paragraph">greater than or equal to the ones that I&#8217;m using here apply to signed integers and they won&#8217;t</p>



<p class="wp-block-paragraph">necessarily work with unsigned integers and they won&#8217;t work with floats if you compare floats later</p>



<p class="wp-block-paragraph">so just keep that in mind let me pull up my favorite book again real fast</p>



<p class="wp-block-paragraph">where what the heck is that book oh dude where&#8217;s my document viewer document viewer can I get</p>



<p class="wp-block-paragraph">document viewer can I get there there we go so I guess I didn&#8217;t introduce this</p>



<p class="wp-block-paragraph">book at the beginning of this video but I probably should have this is my</p>



<p class="wp-block-paragraph">favorite assembly book you can turn yourself into an expert with this book</p>



<p class="wp-block-paragraph">by just on its own it&#8217;s open source and free the author gives this away he&#8217;s a</p>



<p class="wp-block-paragraph">genius the person who wrote this book is a dr. Ed Jorgensen he&#8217;s a professor he</p>



<p class="wp-block-paragraph">wrote this for his own classes and again it&#8217;s like a free open source book you</p>



<p class="wp-block-paragraph">download a copy from his website if you look this up and and convert yourself</p>



<p class="wp-block-paragraph">into an expert so let me collapse everything here and then I&#8217;ll go to</p>



<p class="wp-block-paragraph">instruction set overview control instructions conditional control</p>



<p class="wp-block-paragraph">instructions and I just want you to see real fast just as a recap I talked about</p>



<p class="wp-block-paragraph">this in a previous video but when it comes to comparing things and checking</p>



<p class="wp-block-paragraph">operands because if you want to see if two operands are equal you just check to see if</p>



<p class="wp-block-paragraph">all of their bits are equal you don&#8217;t even really care whether they&#8217;re both integers or not</p>



<p class="wp-block-paragraph">on the other hand notice how there is like a family here jump less than jump less than equal</p>



<p class="wp-block-paragraph">to jump greater than jump greater than equal to that those instructions apply only if your</p>



<p class="wp-block-paragraph">if they are unsigned or if you&#8217;re using floats for your comparisons you have to use this other family</p>



<p class="wp-block-paragraph">of jump conditional branching instructions called jump below jump below equal jump above jump above</p>



<p class="wp-block-paragraph">equal and i&#8217;m sure you can infer that jump below is the same thing as jump less than right jump</p>



<p class="wp-block-paragraph">less than or equal to is the same thing as jump below or equal to it&#8217;s just that you need to use</p>



<p class="wp-block-paragraph">a different instruction uh if you&#8217;re using signed integers or floats versus sorry sorry</p>



<p class="wp-block-paragraph">a versus sorry sorry you need to use different instructions if you&#8217;re using signed integers</p>



<p class="wp-block-paragraph">versus unsigned integers or floats so keep that in mind the code demo that i just showed you it&#8217;s</p>



<p class="wp-block-paragraph">for signed integers if you were going to use unsigned integers or floats you would this</p>



<p class="wp-block-paragraph">program wouldn&#8217;t work for you you&#8217;d need to replace my my branching instructions with the</p>



<p class="wp-block-paragraph">the unsigned versions which i mean they work the same thing you&#8217;re still going to do a comparison</p>



<p class="wp-block-paragraph">it still works. Anyway, so after having shown you that and making sure that everything actually</p>



<p class="wp-block-paragraph">did work, I think we&#8217;ve talked about everything that we need to for this video. So yeah, I hope</p>



<p class="wp-block-paragraph">you feel like an expert practice this on your own, you know, write your own programs just to make</p>



<p class="wp-block-paragraph">sure you know how to convert a higher level language block into an assembly block. And you</p>



<p class="wp-block-paragraph">should be on your way. Thank you for watching this video. I hope you learned a little bit of</p>



<p class="wp-block-paragraph">bit of stuff and had a little bit of fun tell your friends and i&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">hey everybody thanks for watching this video again from the bottom of my heart i really</p>



<p class="wp-block-paragraph">appreciate it i do hope you did learn something and have some fun uh if you could do me a please</p>



<p class="wp-block-paragraph">a small little favor could you please subscribe and follow this channel or these videos or whatever</p>



<p class="wp-block-paragraph">it is you do on the current social media website that you&#8217;re looking at right now</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">just wake me up in the middle of the night just subscribe and then I&#8217;ll just wake up I promise</p>



<p class="wp-block-paragraph">that&#8217;s what will happen also uh if you look at the middle of the screen right now you should see a</p>



<p class="wp-block-paragraph">QR code which you can scan in order to go to the website which I think is also named somewhere at</p>



<p class="wp-block-paragraph">the bottom of this video and it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">all the videos I published and the services and tutorials and things that I offer and all that</p>



<p class="wp-block-paragraph">for clarifications or errata or just future videos that you want to see please leave a comment or if</p>



<p class="wp-block-paragraph">you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever I</p>



<p class="wp-block-paragraph">also wake up for those in the middle of the night I get I wake up in a cold sweat and I&#8217;m like</p>



<p class="wp-block-paragraph">it would really it really mean the world to me I would really appreciate it so again thank you so</p>



<p class="wp-block-paragraph">much for watching this video and enjoy the cool music as as I fade into the darkness which is</p>



<p class="wp-block-paragraph">the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/">Complex If-ElseIf-Else in x86-64 YASM Assembly &#8211; Full Guide with Code Examples</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/complex-if-elseif-else-in-x86-64-yasm-assembly-full-guide-with-code-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &#038; Rotates Tutorial</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 29 Dec 2025 08:55:41 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[AND OR XOR]]></category>
		<category><![CDATA[arithmetic shift]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[bit manipulation]]></category>
		<category><![CDATA[bit rotation]]></category>
		<category><![CDATA[bit shifting]]></category>
		<category><![CDATA[bitwise operations]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[gdb debugging]]></category>
		<category><![CDATA[logical shift]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[two's complement]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=272</guid>

					<description><![CDATA[<p>Learn bitwise operations in x86-64 assembly language including AND, OR, XOR, NOT, logical and arithmetic shifts, and bit rotations with practical examples and GDB debugging.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/">x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates 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 loading="lazy" title="x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/N8Nciq6l5IU?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 class="wp-block-paragraph">In this hands-on tutorial, we dive into bit manipulation in x86-64 assembly using Yasm on Ubuntu. We cover the basics of logical operations like AND, OR, XOR, and NOT, then move into shifting (logical vs arithmetic), rotating bits, and why signed vs unsigned matters for shifts.</p>



<p class="wp-block-paragraph">I explain each concept on paper first, then show real code examples running in GDB to prove how the bits change. We look at patterns in registers, see what happens with negative numbers in two&#8217;s complement, and even touch on how shifts can multiply or divide by powers of 2.</p>



<p class="wp-block-paragraph">Perfect if you&#8217;re learning low-level programming, reverse engineering, or just curious about how bits work under the hood. No prior assembly knowledge needed, but it&#8217;ll help if you&#8217;ve seen my other videos.</p>



<p class="wp-block-paragraph">Like and subscribe for more assembly tutorials!</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey everybody.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re going to talk all about manipulating bits in x86-64 Yasm assembly within</p>



<p class="wp-block-paragraph">Ubuntu.</p>



<p class="wp-block-paragraph">But if you&#8217;re not using Yasm or you&#8217;re not on Ubuntu, this video is probably going to</p>



<p class="wp-block-paragraph">be pretty useful to you anyway, because I&#8217;m going to talk about the basic concept of manipulating</p>



<p class="wp-block-paragraph">bits in various ways.</p>



<p class="wp-block-paragraph">We&#8217;re going to talk about some basic logical operators like ANDing, ORing, XORing, and</p>



<p class="wp-block-paragraph">XORing, and then we&#8217;re going to talk about shifting the bits to the left and the right,</p>



<p class="wp-block-paragraph">and then we&#8217;re going to talk about the difference between shifting without any respect for whether</p>



<p class="wp-block-paragraph">the number inside of the bits is an actual integer, or whether we just consider them</p>



<p class="wp-block-paragraph">to just be bits by themselves, or whether we&#8217;re actually respecting the bits as numbers,</p>



<p class="wp-block-paragraph">and also we&#8217;re going to look at the rotation instruction.</p>



<p class="wp-block-paragraph">So first I&#8217;m going to explain this, I&#8217;m going to write it down in a pad, and then I&#8217;m going</p>



<p class="wp-block-paragraph">show you a program that actually does it just so we can prove that it all works.</p>



<p class="wp-block-paragraph">Okay, so for starters, let me open up a little notepad here.</p>



<p class="wp-block-paragraph">What the heck am I even talking about? Let me write this down because I forgot this on my</p>



<p class="wp-block-paragraph">first take I spent like five minutes, blew right past the explanation of and and or and xor because</p>



<p class="wp-block-paragraph">I forgot that that was supposed to be part of this video. We&#8217;re going to look at and or and xor</p>



<p class="wp-block-paragraph">at least for now and then we&#8217;re going to look at shifting and then we&#8217;re going to look at rotating.</p>



<p class="wp-block-paragraph">at rotating. Okay so first off what is anding? I&#8217;ve explained that in another video but long</p>



<p class="wp-block-paragraph">story short and just requires that two bits that you&#8217;re comparing against each other are both a one</p>



<p class="wp-block-paragraph">in order to receive a one and so any other pair like a zero versus a one or one versus a zero or</p>



<p class="wp-block-paragraph">two zeros are just going to be a zero. So and means both bits that you&#8217;re comparing have to be a one</p>



<p class="wp-block-paragraph">The OR is pretty easy.</p>



<p class="wp-block-paragraph">If either of the bits is a 1, then the result is going to be 1.</p>



<p class="wp-block-paragraph">So if I say 1 versus 1, the result is going to be 1.</p>



<p class="wp-block-paragraph">If I say 0 versus 1, the result is going to be a 1.</p>



<p class="wp-block-paragraph">Or if I say 1 versus 0, the result is going to be a 1.</p>



<p class="wp-block-paragraph">In the only case where you end up with a 0, it&#8217;s just going to be 0 versus 0 results in a 0.</p>



<p class="wp-block-paragraph">To upgrade to exclusive OR, basically exactly 1 bit needs to be a 1.</p>



<p class="wp-block-paragraph">bit needs to be a one and if two or zero are a one then the result is a zero which means here when</p>



<p class="wp-block-paragraph">we have the two ones at the top the answer is going to be a zero these are the two stay the</p>



<p class="wp-block-paragraph">same and this one stays the same so that&#8217;s xor again i have another video where i explain this</p>



<p class="wp-block-paragraph">a little bit better but i&#8217;m just doing like a quick overview shifting is pretty interesting</p>



<p class="wp-block-paragraph">because let me do a pattern let&#8217;s say zero one one zero suppose we just have four bits</p>



<p class="wp-block-paragraph">to the left or to the right. So imagine left, if I shift them to the left, then what I&#8217;m really</p>



<p class="wp-block-paragraph">doing is I&#8217;m just taking off that zero on the end and padding on the right with a zero. And then if</p>



<p class="wp-block-paragraph">I&#8217;m shifting to the right, then well, it&#8217;s to the right. I&#8217;m just going to, you know, take off,</p>



<p class="wp-block-paragraph">let&#8217;s see the zero on the right side and pad with the zero on the left. Be careful though, because</p>



<p class="wp-block-paragraph">or logical shifting. I haven&#8217;t actually shifted with respect to the original number. Think</p>



<p class="wp-block-paragraph">about this. That number right there on line three, if we just consider that an unsigned</p>



<p class="wp-block-paragraph">integer or just bits, then what I just did with the shifting is fine. But what if that</p>



<p class="wp-block-paragraph">was a two&#8217;s complement number that can represent a signed integer? If it&#8217;s two&#8217;s complement</p>



<p class="wp-block-paragraph">or if it&#8217;s just like a signed integer, then the leftmost bit being a zero tells me that</p>



<p class="wp-block-paragraph">result of my left shift, there&#8217;s a one now in that first position, which tells me that&#8217;s</p>



<p class="wp-block-paragraph">probably a negative number.</p>



<p class="wp-block-paragraph">We could face a similar problem with shifting to the right.</p>



<p class="wp-block-paragraph">I mean, we didn&#8217;t this time.</p>



<p class="wp-block-paragraph">It just looks like it actually shifted and the answer is going to end up being right.</p>



<p class="wp-block-paragraph">But what if we shifted something else right?</p>



<p class="wp-block-paragraph">Let&#8217;s see, what am I going to do here?</p>



<p class="wp-block-paragraph">One, one, zero, zero.</p>



<p class="wp-block-paragraph">What if I shifted that number one to the right?</p>



<p class="wp-block-paragraph">The answer would be zero, you know, one, one, zero, right?</p>



<p class="wp-block-paragraph">you know 110 right but then that&#8217;s wrong because the original number was negative because there&#8217;s</p>



<p class="wp-block-paragraph">a one in the first position but the result is a zero so if i&#8217;m not careful about what instructions</p>



<p class="wp-block-paragraph">i&#8217;m using and how to respect whether the number i&#8217;m shifting is actually considered an integer</p>



<p class="wp-block-paragraph">assigned integer or if it&#8217;s just bits if i don&#8217;t respect that then i could end up ruining the</p>



<p class="wp-block-paragraph">number so keep that in mind we&#8217;re going to do several examples of that i&#8217;m not going to show</p>



<p class="wp-block-paragraph">shift by hand I&#8217;m just going to show you what the instructions in the assembler</p>



<p class="wp-block-paragraph">will do so then the other thing that we should talk about is rotating so imagine</p>



<p class="wp-block-paragraph">that I rotate this number one to the right well nothing really happens</p>



<p class="wp-block-paragraph">visually but what you&#8217;re supposed to do if we rotate to the right you take the</p>



<p class="wp-block-paragraph">number on the very right side and you just pop it onto the front so it sort of</p>



<p class="wp-block-paragraph">it doesn&#8217;t fall off and get replaced by a zero or get replaced by a one depending</p>



<p class="wp-block-paragraph">moves to the other side so if I shift this again maybe let&#8217;s shift it one more</p>



<p class="wp-block-paragraph">time to the right I&#8217;m gonna take that one and pop it off the right side and</p>



<p class="wp-block-paragraph">put it on the very front so see do you see how there it&#8217;s kind of rotating I do</p>



<p class="wp-block-paragraph">this one more time actually let me copy paste this I&#8217;ll take that number on the</p>



<p class="wp-block-paragraph">right side pop it off put it back on the front then if I do it again take that</p>



<p class="wp-block-paragraph">zero pop it off put it back on the front notice how it eventually gets back to</p>



<p class="wp-block-paragraph">the original pattern and I would have to rotate you know exactly four times to</p>



<p class="wp-block-paragraph">to get that to happen same same thing applies for rotating to the left i&#8217;m not going to show you</p>



<p class="wp-block-paragraph">because uh it&#8217;s it&#8217;s the same thing as rotating to the right just in a different direction</p>



<p class="wp-block-paragraph">one thing that i forgot to say a moment ago when i was talking about shifting is uh think about this</p>



<p class="wp-block-paragraph">if we shift to the right pretend that uh we don&#8217;t really care that much let&#8217;s let me put some extra</p>



<p class="wp-block-paragraph">zeros here so that we don&#8217;t have to carry too much we don&#8217;t have to care too much about what&#8217;s</p>



<p class="wp-block-paragraph">happening with positive or negative. If you know how to convert to binary, which I&#8217;ve explained in</p>



<p class="wp-block-paragraph">other videos, then you&#8217;ll know this number is, let&#8217;s see, there&#8217;s a one and a two, and then these</p>



<p class="wp-block-paragraph">positions are four and eight. So this should be a 12, right? If I shift this to the right,</p>



<p class="wp-block-paragraph">I should be dividing this by two because the base of binary is two. So every time you kind of like</p>



<p class="wp-block-paragraph">move to the left in terms of what digit you&#8217;re looking at, you&#8217;re increasing the power of that</p>



<p class="wp-block-paragraph">every time you move to the right you&#8217;re decreasing the power of the digit by two</p>



<p class="wp-block-paragraph">so that means if I shift the entire thing to the right I should be dividing the whole thing by two</p>



<p class="wp-block-paragraph">right not just one bit so if I just take a zero off of there and put it on there I&#8217;ve now shifted</p>



<p class="wp-block-paragraph">all the bits shifted all the bits one to the right I should end up with the six let&#8217;s just</p>



<p class="wp-block-paragraph">compute what it is there&#8217;s a one there and there&#8217;s a two there and there&#8217;s a four there what&#8217;s four</p>



<p class="wp-block-paragraph">plus two that&#8217;s six so this is actually six in binary or the original number divided by two</p>



<p class="wp-block-paragraph">because we shifted to the right on the other hand if I take this number one more time and</p>



<p class="wp-block-paragraph">just kind of shift it to the left I should multiply it by two so that means I should</p>



<p class="wp-block-paragraph">expect to see a 24 let me just remove one of those zeros and put it on the right side there</p>



<p class="wp-block-paragraph">this is not a rotation I&#8217;m just kind of like filling in zeros I&#8217;m keeping it simple with</p>



<p class="wp-block-paragraph">the shifted number this is a 1, a 2, and a 4 again if you don&#8217;t know how to convert</p>



<p class="wp-block-paragraph">from binary to decimal or back see my other videos so then we basically have</p>



<p class="wp-block-paragraph">an 8 here and a 16 what&#8217;s 8 plus 16 that&#8217;s 24 24 is twice of 12 so we have</p>



<p class="wp-block-paragraph">multiplied by 2 so you could multiply by 4 by shifting twice multiply by 8 by</p>



<p class="wp-block-paragraph">shifting 3 times you know whatever you want to do but shifting is is like a</p>



<p class="wp-block-paragraph">is like a shortcut besides just to move the bits it&#8217;s also a shortcut to multiply and divide by two</p>



<p class="wp-block-paragraph">okay so that&#8217;s the basic idea let me start working on a little program here for you before I continue</p>



<p class="wp-block-paragraph">I just want you to know that my program it&#8217;s got stuff in it already we&#8217;ve got a make file here</p>



<p class="wp-block-paragraph">if I uh I edit the make file I just have a make file set up to build the program so again this</p>



<p class="wp-block-paragraph">is specific to just you know bits we&#8217;re not going to talk about make files see my other videos if</p>



<p class="wp-block-paragraph">you want to know how to make your own make file same thing for this gdb.txt file I&#8217;ve just put</p>



<p class="wp-block-paragraph">commands that I&#8217;m going to send to gdb my debugger of choice this is not a gdb video I haven&#8217;t</p>



<p class="wp-block-paragraph">actually made my gdb video yet so look out for that in this in the somewhat near future</p>



<p class="wp-block-paragraph">so we&#8217;re just going to focus on the actual program for now anyway so I&#8217;ve got a solution</p>



<p class="wp-block-paragraph">Anyway, so I&#8217;ve got a solution already set up here.</p>



<p class="wp-block-paragraph">Let me start copy pasting some stuff in there.</p>



<p class="wp-block-paragraph">Going to copy paste my data section.</p>



<p class="wp-block-paragraph">So first off,</p>



<p class="wp-block-paragraph">my sample program is just gonna have codes for writing,</p>



<p class="wp-block-paragraph">you know, with the system call</p>



<p class="wp-block-paragraph">and exiting with the system call.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna output to standard output</p>



<p class="wp-block-paragraph">and I&#8217;m gonna do a zero return code.</p>



<p class="wp-block-paragraph">If you don&#8217;t know that,</p>



<p class="wp-block-paragraph">see my other videos that I&#8217;ve already put out.</p>



<p class="wp-block-paragraph">I&#8217;m gonna make a sample string where I just say,</p>



<p class="wp-block-paragraph">I just say hello my name is Gibson Montgomery Gibson that&#8217;s not my name but I love those names</p>



<p class="wp-block-paragraph">I&#8217;m just going to say hello basically when the program starts then I have some bit strings here</p>



<p class="wp-block-paragraph">that will help me with the you know help me demonstrate some of the bitwise operations</p>



<p class="wp-block-paragraph">the ending and the oring and the shifting and whatever and then I&#8217;m going to put a negative</p>



<p class="wp-block-paragraph">number here as a variable just to prove to you that sometimes the bitwise or logical shifting</p>



<p class="wp-block-paragraph">break a number if the number is supposed to actually be assigned integer and and use that</p>



<p class="wp-block-paragraph">as evidence for why you should use arithmetic shifting so now I&#8217;m going to start my program</p>



<p class="wp-block-paragraph">here let&#8217;s see my entry point should start at line 30 I got to be super careful about this</p>



<p class="wp-block-paragraph">because if I don&#8217;t know 26 if I don&#8217;t then all of my debug statements are going to have to be</p>



<p class="wp-block-paragraph">it lined up correctly so i&#8217;m basically just going to print the hello message and then maybe you know</p>



<p class="wp-block-paragraph">what at the very end i&#8217;m going to exit from the system with a system call so basically if we run</p>



<p class="wp-block-paragraph">this program again system calls and everything like that are not part of this video see my other</p>



<p class="wp-block-paragraph">videos but uh if this works we should see just like a simple message okay so that&#8217;s it my name</p>



<p class="wp-block-paragraph">Conmary Gibson. Okay.</p>



<p class="wp-block-paragraph">So then the next thing that I&#8217;m going to do is I&#8217;m going to show you a bitwise</p>



<p class="wp-block-paragraph">and operation. So I&#8217;m going to copy paste that should start on line 42.</p>



<p class="wp-block-paragraph">And if I get this wrong, the debugging is going to be awful.</p>



<p class="wp-block-paragraph">I was trying to make this a pure assembly program without a bunch of extra work.</p>



<p class="wp-block-paragraph">I guess I could have linked GCC so I could just print results to the terminal,</p>



<p class="wp-block-paragraph">but whatever too late now. So it starts on 42.</p>



<p class="wp-block-paragraph">and ends on 51 okay so I think that&#8217;s good so the instruction for ending doing</p>



<p class="wp-block-paragraph">a logical and between two registers which are full of bits and then what</p>



<p class="wp-block-paragraph">will happen is every bit gets anded against its own course or I guess the</p>



<p class="wp-block-paragraph">corresponding bit in the other other register is just and so what I&#8217;m doing</p>



<p class="wp-block-paragraph">first is I&#8217;m gonna set up the registers so I&#8217;m gonna move the a sequence which</p>



<p class="wp-block-paragraph">sequence, which if you look up is just, you know, I put a bunch of bits. So like in hex,</p>



<p class="wp-block-paragraph">I like to type hex more than binary when I&#8217;m just kind of putting stuff in there.</p>



<p class="wp-block-paragraph">So, you know, each F is basically four ones. So it&#8217;s going to be like, I guess, 16 ones,</p>



<p class="wp-block-paragraph">and then a bunch of zeros, and then, you know, a few ones, and then some zeros and ones.</p>



<p class="wp-block-paragraph">So I&#8217;m just making a little pattern for bits A. Don&#8217;t think that they mean anything right now.</p>



<p class="wp-block-paragraph">And I&#8217;m going to make a different pattern with bits B. And when we look into the debugger,</p>



<p class="wp-block-paragraph">And when we look into the debugger, we should see the before and after patterns so we can kind of see what the AND instruction did.</p>



<p class="wp-block-paragraph">So I&#8217;m going to move bits A and bits B into R10 and R11.</p>



<p class="wp-block-paragraph">Then I&#8217;m going to move, so these are going to be like the original bits that we can inspect later.</p>



<p class="wp-block-paragraph">this AND instruction against R11 which is the B pattern. So I&#8217;m basically taking the A pattern</p>



<p class="wp-block-paragraph">and I&#8217;m ANDing it against the B pattern but I&#8217;m also loading up the registers so we can see</p>



<p class="wp-block-paragraph">the A pattern then the B pattern and then the result of ANDing them together.</p>



<p class="wp-block-paragraph">Finally on line 50 here I&#8217;m using a NOPE which is just a non-operation so that I can easily</p>



<p class="wp-block-paragraph">break the debugger so we can sort of inspect the program and see what&#8217;s going on inside of there.</p>



<p class="wp-block-paragraph">program and see what&#8217;s going on inside of there. You know what, let me, uh, let me open this up</p>



<p class="wp-block-paragraph">again here so that I can control where I&#8217;m breaking. So 48 is where the nope is. That&#8217;s</p>



<p class="wp-block-paragraph">totally wrong. I&#8217;ve already ruined my program somehow. It&#8217;s going to be 50, I guess. All right,</p>



<ol start="50" class="wp-block-list">
<li>And what I&#8217;m doing inside of this, and again, this is not a GDB video. I&#8217;m just saying,</li>
</ol>



<p class="wp-block-paragraph">let&#8217;s break at line 50 so I can kind of poke around. And I&#8217;m going to comment all these</p>



<p class="wp-block-paragraph">And I&#8217;m going to comment all these things out for now.</p>



<p class="wp-block-paragraph">That&#8217;s going to happen at the very end.</p>



<p class="wp-block-paragraph">It&#8217;s going to be majestic.</p>



<p class="wp-block-paragraph">How many different things we can look at.</p>



<p class="wp-block-paragraph">So I&#8217;m telling GDB not to execute any of these instructions, except for the, uh,</p>



<p class="wp-block-paragraph">the break point instructions.</p>



<p class="wp-block-paragraph">So we can just break on line 50.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Hopefully this works.</p>



<p class="wp-block-paragraph">I&#8217;m going to now use make debug, which under the hood is just going to be compiling and</p>



<p class="wp-block-paragraph">then telling GDB to launch with the name of my program inside of it.</p>



<p class="wp-block-paragraph">And then to execute those things.</p>



<p class="wp-block-paragraph">and then to execute those things.</p>



<p class="wp-block-paragraph">So it hits breakpoint number 50.</p>



<p class="wp-block-paragraph">There&#8217;s a bunch of crap that gets printed.</p>



<p class="wp-block-paragraph">And I can now inspect my registers.</p>



<p class="wp-block-paragraph">I&#8217;m going to go info R for just info registers.</p>



<p class="wp-block-paragraph">And let&#8217;s see.</p>



<p class="wp-block-paragraph">So we had R10, 11, and 12.</p>



<p class="wp-block-paragraph">So if we look at the terminal, you can see that R10, the hex version,</p>



<p class="wp-block-paragraph">has the original pattern of the A sequence,</p>



<p class="wp-block-paragraph">and then R11 has the original pattern of the B sequence.</p>



<p class="wp-block-paragraph">original pattern of the b sequence if we do a logical and and put the result into r12 then</p>



<p class="wp-block-paragraph">you should see let me just double check that that&#8217;s definitely r12 yeah then you should see</p>



<p class="wp-block-paragraph">the result of ending all of the f&#8217;s together remember that an f is just four ones so i&#8217;m kind</p>



<p class="wp-block-paragraph">of simplifying it a little bit you could make yours more complicated with lots of randomness</p>



<p class="wp-block-paragraph">in your ones and zeros just to prove it to yourself but notice how r12 only has an f</p>



<p class="wp-block-paragraph">So like FF means F, F0 means 0, F0 means 0, you know, and so forth.</p>



<p class="wp-block-paragraph">0F means 0.</p>



<p class="wp-block-paragraph">So only where you see two Fs will you see an F in the final target pattern.</p>



<p class="wp-block-paragraph">So that&#8217;s a logical AND, and that&#8217;s how you do it in Yasm Assembly.</p>



<p class="wp-block-paragraph">You just load up a register with some bits, R12.</p>



<p class="wp-block-paragraph">We loaded them up with the first pattern.</p>



<p class="wp-block-paragraph">And then we take the second pattern and say that&#8217;s what I want to AND it against.</p>



<p class="wp-block-paragraph">that&#8217;s what I want to end it against. And you can have that in a register or I think an immediate</p>



<p class="wp-block-paragraph">or a memory location. So we&#8217;re just ending these registers together. And you know that they&#8217;re</p>



<p class="wp-block-paragraph">loaded up with patterns A and B. So nothing too hard at this point. Let&#8217;s do a, let&#8217;s look at</p>



<p class="wp-block-paragraph">bitwise or this should start on line 52 or I&#8217;m screwing myself. 52. Okay. 52. So same thing,</p>



<p class="wp-block-paragraph">up R10 and R11 with the bit patterns and then we&#8217;re going to move the first bit pattern</p>



<p class="wp-block-paragraph">into R12 so that we can or it against the second pattern and so basically R10 and 11</p>



<p class="wp-block-paragraph">will have the original bit patterns R12 will have the result of oring them.</p>



<p class="wp-block-paragraph">Then we&#8217;ll do a nope on line 60 so I want to break here on line 60.</p>



<p class="wp-block-paragraph">Yep I ruined all my line numbers. What did I do wrong? I think I just have it offset by 2 now</p>



<p class="wp-block-paragraph">two now for some reason so if we just kind of run the program now in the debugger again i&#8217;m going to</p>



<p class="wp-block-paragraph">quit and then yes if we clear and make debug probably clear was unnecessary we&#8217;re broken at</p>



<p class="wp-block-paragraph">line 60 if i say info registers the registers in question are oh shoot um 10 11 and 12 right so</p>



<p class="wp-block-paragraph">we look at 10 and 11 they have the same pattern as before and 12 has a lot more f&#8217;s because if</p>



<p class="wp-block-paragraph">has a lot more F&#8217;s because if either of the patterns had an F,</p>



<p class="wp-block-paragraph">then the resulting pattern would have an F.</p>



<p class="wp-block-paragraph">So this is a logical OR.</p>



<p class="wp-block-paragraph">Same idea as AND in terms of the way you format the instruction and the OPS,</p>



<p class="wp-block-paragraph">but it ORs instead of ANDs.</p>



<p class="wp-block-paragraph">Okay, no problem.</p>



<p class="wp-block-paragraph">Let&#8217;s move on.</p>



<p class="wp-block-paragraph">Next thing I want to show you is the XOR, which is pretty cool.</p>



<p class="wp-block-paragraph">By the way, so far we&#8217;re just operating on bits.</p>



<p class="wp-block-paragraph">We don&#8217;t consider these bits to actually be numbers.</p>



<p class="wp-block-paragraph">maybe they are but we should probably be careful about that anyway so I&#8217;m going to say let&#8217;s check</p>



<p class="wp-block-paragraph">out our bitwise xor which is exclusive or which means exactly one bit in any pair needs to be a</p>



<p class="wp-block-paragraph">one for the resulting bit to be a one and of course per usual all bits will be checked against each</p>



<p class="wp-block-paragraph">other in the pair of of bit patterns it&#8217;ll be one bit at a time from each pattern that gets checked</p>



<p class="wp-block-paragraph">Okay, so it starts on 62 and then we got a nope there.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So same thing that we did before.</p>



<p class="wp-block-paragraph">We&#8217;re just loading up two registers with the bit patterns and then R12 is going to be the</p>



<p class="wp-block-paragraph">result of exclusive oring or X oring the two bit patterns.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to break on line 70.</p>



<p class="wp-block-paragraph">Now, let me just get rid of my other break point, change that 68 to a 70.</p>



<p class="wp-block-paragraph">And now I should quit.</p>



<p class="wp-block-paragraph">bug we&#8217;re at 70 good info registers so I think we said it was 10 and 11 had the original bit patterns</p>



<p class="wp-block-paragraph">and notice the resulting bit pattern it only has an f in a position where there was exactly</p>



<p class="wp-block-paragraph">one f between the two input patterns so</p>



<p class="wp-block-paragraph">wait did I just do something wrong here what did I just do wrong</p>



<p class="wp-block-paragraph">hang on a second I&#8217;m seeing something bad x or r12 am I looking at r12</p>



<p class="wp-block-paragraph">Yeah.</p>



<p class="wp-block-paragraph">R10 and 11.</p>



<p class="wp-block-paragraph">Did I just do something wrong?</p>



<p class="wp-block-paragraph">10 and 11 and then we load 10 up into 12 and then we XOR it by 11 and then it stores it</p>



<p class="wp-block-paragraph">in 12.</p>



<p class="wp-block-paragraph">Something&#8217;s a little fishy here.</p>



<p class="wp-block-paragraph">Exactly one.</p>



<p class="wp-block-paragraph">XOR.</p>



<p class="wp-block-paragraph">I don&#8217;t like that.</p>



<p class="wp-block-paragraph">Oh, I understand now.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So that was a little scary.</p>



<p class="wp-block-paragraph">This has shifted because the printout is not great.</p>



<p class="wp-block-paragraph">Because the printout is not great. See how there&#8217;s like a blank space here?</p>



<p class="wp-block-paragraph">That threw me off. So you have to imagine that all of the bit patterns are kind of shifted.</p>



<p class="wp-block-paragraph">Maybe I should just copy paste this.</p>



<p class="wp-block-paragraph">I was worried that I suddenly didn&#8217;t know how to do an XOR in assembly.</p>



<p class="wp-block-paragraph">So let me get rid of these integers here.</p>



<p class="wp-block-paragraph">Notice how there&#8217;s just a blank space that shifted everything and threw it off in terms of the display.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do this.</p>



<p class="wp-block-paragraph">And what should really happen now is that there should be a zero here.</p>



<p class="wp-block-paragraph">A zero at the front.</p>



<p class="wp-block-paragraph">a zero at the front now everything would make more sense sometimes the display likes to just</p>



<p class="wp-block-paragraph">kind of truncate things and just assume that everything will be read correctly but it wasn&#8217;t</p>



<p class="wp-block-paragraph">so notice uh in the first position both of the uh both the input patterns are are an f so the</p>



<p class="wp-block-paragraph">output pattern will be a zero you can imagine under the hood there was a bunch of ones and</p>



<p class="wp-block-paragraph">then a bunch of ones and that resulted in a bunch of zeros uh here we&#8217;ve got like an f and a zero so</p>



<p class="wp-block-paragraph">the result is going to be an f and then f and a zero and after zero zero and an f is an f</p>



<p class="wp-block-paragraph">f um and then where you see zeros that means uh there was not exactly one uh one in each bit</p>



<p class="wp-block-paragraph">position so for the let&#8217;s say the first one that&#8217;s a bunch of zeros for both input patterns so that&#8217;s</p>



<p class="wp-block-paragraph">why there&#8217;s a zero there then for the next two each of them had all ones or just the two f&#8217;s</p>



<p class="wp-block-paragraph">uh so that&#8217;s why x or says it&#8217;s a zero and so forth right so like at the end two f&#8217;s making</p>



<p class="wp-block-paragraph">Okay, that was a little scary.</p>



<p class="wp-block-paragraph">I&#8217;m glad I figured that out quickly instead of taking a million years.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So now let&#8217;s do another example.</p>



<p class="wp-block-paragraph">We&#8217;re done with XOR.</p>



<p class="wp-block-paragraph">Now let&#8217;s look at bitwise knot.</p>



<p class="wp-block-paragraph">I forgot to explain knot.</p>



<p class="wp-block-paragraph">I&#8217;m not going to redo this video.</p>



<p class="wp-block-paragraph">But let me just say that a knot is just inverting the bit.</p>



<p class="wp-block-paragraph">So if we have a zero, it becomes a one.</p>



<p class="wp-block-paragraph">And if we have a one, it becomes a zero.</p>



<p class="wp-block-paragraph">If you knot against a register that has lots of bits or a memory location that has lots</p>



<p class="wp-block-paragraph">of bits, you can do that and just every single bit in the sequence will just be flipped from</p>



<p class="wp-block-paragraph">a zero to a one or vice versa.</p>



<p class="wp-block-paragraph">So let&#8217;s see, we just did bitwise XOR.</p>



<p class="wp-block-paragraph">Now we&#8217;re going to do a knot starting on line 72, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to do another nope there.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">All right.</p>



<p class="wp-block-paragraph">So now let&#8217;s get rid of the break point at the line 70 and put it at line 78 instead.</p>



<p class="wp-block-paragraph">78.</p>



<p class="wp-block-paragraph">Oh man, this video is going to take forever.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So now let&#8217;s check out bitwise knot.</p>



<p class="wp-block-paragraph">We&#8217;re going to have R10.</p>



<p class="wp-block-paragraph">Oh, I guess I should improve this because what I&#8217;m doing is I&#8217;m moving everything into</p>



<p class="wp-block-paragraph">everything into R10 and then I&#8217;m just nodding R10 so we can&#8217;t really compare R10 against itself.</p>



<p class="wp-block-paragraph">That&#8217;s not fun. Let me instead move R10 into R11 so that I can nod R11 so that we can</p>



<p class="wp-block-paragraph">see two different registers at least. Okay, so let me just update my solution real fast.</p>



<p class="wp-block-paragraph">move r11 r10 okay so now it stops at 75 and that one stops at 75 okay cool</p>



<p class="wp-block-paragraph">now we got our breakpoint should be at line 79 not 78 now we&#8217;re ready to run it and see what the</p>



<p class="wp-block-paragraph">does clear and make debug info registers so we should have r10 r11 again the formatting is a</p>



<p class="wp-block-paragraph">so I&#8217;m just going to copy paste it up here. I&#8217;m going to do this, get rid of the numbers,</p>



<p class="wp-block-paragraph">pad on the left for the missing zeros, and then now you can see clearly that all of the</p>



<p class="wp-block-paragraph">F&#8217;s were flipped to zero. Again, that means, you know, each F had four ones inside of it,</p>



<p class="wp-block-paragraph">so all those ones were flipped to a zero, so that&#8217;s why it&#8217;s a zero. Okay, so nodding, pretty easy,</p>



<p class="wp-block-paragraph">this and show the next example which was gonna be let&#8217;s see seven let&#8217;s check out bitwise rotation</p>



<p class="wp-block-paragraph">i&#8217;m going to copy paste all the way up to that nope there should start on line 81</p>



<p class="wp-block-paragraph">i paused before i took a drink of water hopefully i reminded myself later to edit the drink of water</p>



<p class="wp-block-paragraph">out i don&#8217;t know maybe you&#8217;ll be wondering why my mustache is wet right now if i did not edit it out</p>



<p class="wp-block-paragraph">I did not edit it out.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s check out bitwise rotation.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna set up R10 and 11 and 12 and 13 and 14,</p>



<p class="wp-block-paragraph">all with the A pattern.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna start rotating things.</p>



<p class="wp-block-paragraph">So first I&#8217;m gonna rotate to the left R11 and then R12.</p>



<p class="wp-block-paragraph">I&#8217;m gonna rotate by one position on R11</p>



<p class="wp-block-paragraph">and then two positions on R12.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to rotate</p>



<p class="wp-block-paragraph">rotate by one position for R13 and then two positions for R14 so we should see</p>



<p class="wp-block-paragraph">some interesting patterns. The NOPE is now on line 94 so I&#8217;m gonna break there</p>



<p class="wp-block-paragraph">instead whoops so I&#8217;m gonna change that and I&#8217;m now off by three it&#8217;s getting</p>



<p class="wp-block-paragraph">precarious and then I&#8217;m gonna uncomment all of my little print statements to</p>



<p class="wp-block-paragraph">show you the value of the registers after I rotate it so we should see like</p>



<p class="wp-block-paragraph">printed in the terminal just to prove to you what&#8217;s going to happen okay so man I got to</p>



<p class="wp-block-paragraph">remember what&#8217;s happening now first thing that I did was I printed r10 to show you one type of</p>



<p class="wp-block-paragraph">rotation and then I printed r10 again to show you another type of rotation so we should see the</p>



<p class="wp-block-paragraph">original and then the two rotated values and then the original again and then the other two rotated</p>



<p class="wp-block-paragraph">values so here&#8217;s the original we&#8217;re printing it in binary with the special print command in gdb</p>



<p class="wp-block-paragraph">in GDB, which is just like P slash T and then the register. So this is the original pattern</p>



<p class="wp-block-paragraph">right here. And if you just kind of squint at it for a while, you can realize that the dollar</p>



<p class="wp-block-paragraph">sign two pattern has been, uh, shifted once to the left. Actually, let me just make sure that</p>



<p class="wp-block-paragraph">I&#8217;m doing a shift and not a rotate. I&#8217;m accidentally saying the word, uh, rotate, I think. And, uh,</p>



<p class="wp-block-paragraph">that&#8217;s, oh no, no, it is rotate. Yeah. We&#8217;re going to shift later, I guess.</p>



<p class="wp-block-paragraph">I guess. Okay, so we are definitely rotating. So we should see some wrap around. I guess I used</p>



<p class="wp-block-paragraph">bad bit patterns for the wrap around. But just imagine that, you know, in this pattern right here,</p>



<p class="wp-block-paragraph">this one, if we&#8217;re rotating to the left, this one fell off and it got placed on the right side of</p>



<p class="wp-block-paragraph">of the next pattern it&#8217;s going to be the same thing basically so but this is rotation so we</p>



<p class="wp-block-paragraph">wouldn&#8217;t be reusing the bits that fell off but anyway you can see that if I rotated to the left</p>



<p class="wp-block-paragraph">by one position that whole thing is is kind of shifted to the left I shouldn&#8217;t use the word shift</p>



<p class="wp-block-paragraph">and then for the second pattern we did a rotation by two positions so all the bits are just kind of</p>



<p class="wp-block-paragraph">moved to the left compared to the very first pattern okay so no problem then the next thing</p>



<p class="wp-block-paragraph">that we&#8217;re looking at is just you know rotating to the right and you can tell that everything is</p>



<p class="wp-block-paragraph">offset by one here and it&#8217;s offset by two here so rotating to the left by one position by two</p>



<p class="wp-block-paragraph">positions rotating to the right by one position or by two positions that&#8217;s rotation in a nutshell</p>



<p class="wp-block-paragraph">okay so then the next thing that we&#8217;re going to do we&#8217;re on 94 now</p>



<p class="wp-block-paragraph">um let&#8217;s see logical shifting nope nope nope I&#8217;m gonna grab some code that will shift both</p>



<p class="wp-block-paragraph">logically and arithmetically so we can kind of see the difference</p>



<p class="wp-block-paragraph">okay so show maybe I should change this to show some some shifting</p>



<p class="wp-block-paragraph">because uh if I just say it&#8217;s logical then yeah sure uh the shift function uh the shift</p>



<p class="wp-block-paragraph">those are logical shifts but then down here when it&#8217;s a sal and sar those are arithmetic shifts</p>



<p class="wp-block-paragraph">I don&#8217;t know maybe maybe I can do that maybe I can just</p>



<p class="wp-block-paragraph">duplicate the comment maybe to make it more more intuitive so show arithmetic shifts</p>



<p class="wp-block-paragraph">Okay, I think that looks pretty good. Let me paste that into 105 on my solution so I don&#8217;t get out of sync here.</p>



<p class="wp-block-paragraph">So what&#8217;s going to happen here? We&#8217;re basically going to load up R10 with a negative number.</p>



<p class="wp-block-paragraph">I&#8217;ve chosen the number negative 12 because I kind of feel like it at least shows you a little bit for dividing and stuff like that.</p>



<p class="wp-block-paragraph">So we&#8217;re loading up R10 with a negative number, and then R11 is going to get R10, but then we&#8217;re going to shift it left by two positions.</p>



<p class="wp-block-paragraph">positions so that should be multiplying it by four but this is well a bitwise</p>



<p class="wp-block-paragraph">shifting so it it&#8217;s not going to necessarily respect the fact that the</p>



<p class="wp-block-paragraph">number is in twos complement so this is not a good idea if you consider r tend</p>



<p class="wp-block-paragraph">to be a number because sometimes the result might be right and sometimes the</p>



<p class="wp-block-paragraph">result might be wrong here it says math won&#8217;t work in my comment but sometimes</p>



<p class="wp-block-paragraph">it will accidentally work but you still want to consider it to be non-working</p>



<p class="wp-block-paragraph">It&#8217;s only good for bit shifting.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to put R12, we&#8217;re going to give it the same value as R10 and then we&#8217;re</p>



<p class="wp-block-paragraph">just going to shift it to the right by two positions.</p>



<p class="wp-block-paragraph">This will definitely break because when you shift to the right, the sine bit is going</p>



<p class="wp-block-paragraph">to get padded with a zero rather than what the actual sine bit was originally.</p>



<p class="wp-block-paragraph">So that will almost certainly break it almost every time.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to use arithmetic shifts which just respect the fact that the original</p>



<p class="wp-block-paragraph">number is two&#8217;s complement.</p>



<p class="wp-block-paragraph">two&#8217;s complement so you know like we have this negative 12 number it&#8217;s going to end up being</p>



<p class="wp-block-paragraph">bits but the uh the shl and the shr instructions are not going to respect the fact that it&#8217;s</p>



<p class="wp-block-paragraph">negative 12 they&#8217;re just going to consider them to be bits but the sal shifting to the left</p>



<p class="wp-block-paragraph">arithmetically and the sar shifting to the right arithmetically are going to respect that respect</p>



<p class="wp-block-paragraph">the fact that it&#8217;s two&#8217;s complement so let&#8217;s see we&#8217;ve got all this stuff and then we&#8217;ve got a</p>



<p class="wp-block-paragraph">We&#8217;ve got all this stuff and then we&#8217;ve got a non-operation here.</p>



<p class="wp-block-paragraph">You know, again, like per usual, when we&#8217;re shifting, when we&#8217;re rotating, when we&#8217;re</p>



<p class="wp-block-paragraph">oaring, anding, whatever, it&#8217;s just the instruction name followed by both the source and the</p>



<p class="wp-block-paragraph">destination operand.</p>



<p class="wp-block-paragraph">And then the second operand is only going to be source.</p>



<p class="wp-block-paragraph">So if we have a shifting left R11, then the result is going to be stored in R11, but it&#8217;s</p>



<p class="wp-block-paragraph">going to take as the source R11 and shift it to the left by two positions.</p>



<p class="wp-block-paragraph">to the left by two positions and so forth.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we have all of that.</p>



<p class="wp-block-paragraph">We&#8217;re going to nope on line one of what is it?</p>



<p class="wp-block-paragraph">One, one 14.</p>



<p class="wp-block-paragraph">So that means I&#8217;m going to stop breaking on 94 and I&#8217;m going to break on one 14.</p>



<p class="wp-block-paragraph">I thought it was four lines off and now I&#8217;m only two lines off.</p>



<p class="wp-block-paragraph">I think I just did something else wrong here between my source.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">Is the nope on one 14 up there?</p>



<p class="wp-block-paragraph">Yeah, it is.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Guess I didn&#8217;t do that much wrong.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s run this, but this code up here was just going to print bit patterns</p>



<p class="wp-block-paragraph">related to our rotations.</p>



<p class="wp-block-paragraph">I don&#8217;t really want that anymore.</p>



<p class="wp-block-paragraph">So I&#8217;m going to uncomment all of these other prints that relate to our shifts.</p>



<p class="wp-block-paragraph">And this is going to be a nightmare to read and interpret, but I&#8217;m going to try.</p>



<p class="wp-block-paragraph">I really should have written a more complicated program that can just print them in the terminal</p>



<p class="wp-block-paragraph">so I don&#8217;t have to like read the debugger.</p>



<p class="wp-block-paragraph">Whatever, it&#8217;s brain exercise.</p>



<p class="wp-block-paragraph">size. And I think this actually is the end of the program. 119 is the last line. Yeah. Okay.</p>



<p class="wp-block-paragraph">So we just need to run this and see what happens. So really we&#8217;re interested in these, uh, you know,</p>



<p class="wp-block-paragraph">shifting to the left twice, shifting to the right twice bit wise, and then shifting to the left</p>



<p class="wp-block-paragraph">twice, arithmetically shifting to the right twice, arithmetically. Okay. So we&#8217;ll go back to the</p>



<p class="wp-block-paragraph">terminal here and quit. And then I&#8217;m going to do make debug. And now there&#8217;s a bunch of junk.</p>



<p class="wp-block-paragraph">here&#8217;s the original number and then we&#8217;re gonna bitwise shift left binary</p>



<p class="wp-block-paragraph">notice how it just sort of you know it shifted everything to the left and you</p>



<p class="wp-block-paragraph">know we&#8217;re not using we&#8217;re not printing hex anymore because I want you to see</p>



<p class="wp-block-paragraph">the binary and then you know like the patterns are just shifted to to the left</p>



<p class="wp-block-paragraph">like see here&#8217;s the first one if we&#8217;re looking at the right side and then here</p>



<p class="wp-block-paragraph">is the first one if we&#8217;re looking at the right side for the shifted version</p>



<p class="wp-block-paragraph">A similar deal for shift right except we lost a couple of numbers there because of the display</p>



<p class="wp-block-paragraph">so I&#8217;m going to copy paste both of these real fast.</p>



<p class="wp-block-paragraph">Per usual we lost numbers due to the display so I&#8217;m going to say notice how we have two</p>



<p class="wp-block-paragraph">numbers missing here if I added zeros on the right side that&#8217;s going to change the actual</p>



<p class="wp-block-paragraph">value so I&#8217;m going to have to add them to the left and now we can clearly see that the</p>



<p class="wp-block-paragraph">shifted four positions I think to the left wait did I do that where did I where did I even just</p>



<p class="wp-block-paragraph">copy paste from original shift left and shift right okay so shift right is what it&#8217;s supposed</p>



<p class="wp-block-paragraph">to be doing let me just double check here shift right is printing r12 and what does r12 have it</p>



<p class="wp-block-paragraph">shifting right by two. Okay.</p>



<p class="wp-block-paragraph">Oh, I&#8217;m supposed to compare it to the very first one.</p>



<p class="wp-block-paragraph">Let me just double check that I have this correct now.</p>



<p class="wp-block-paragraph">Why is it</p>



<p class="wp-block-paragraph">shifting to the right? Oh gosh, I think I just lost track of myself. Let me just,</p>



<p class="wp-block-paragraph">give me a second here. Or 14.</p>



<p class="wp-block-paragraph">I&#8217;m forgetting where I copy and paste. Okay. Bitwise shift right is</p>



<p class="wp-block-paragraph">bitwise shift right is R12, not R14. Oh gosh. Did I do it wrong? Were you cringing at home?</p>



<p class="wp-block-paragraph">Maybe this will work. And the two, that&#8217;s kind of the same thing that I did before. No, no,</p>



<p class="wp-block-paragraph">it&#8217;s different. Okay. So I had it wrong before. Now I finally have it right. So you can see that</p>



<p class="wp-block-paragraph">So you can see that the bit pattern is shifted twice to the right and then on the left two zeros show up which would have destroyed a number.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So we shift it to the right and now we&#8217;re ready to look at arithmetic shift to the left and right.</p>



<p class="wp-block-paragraph">And let&#8217;s see do we have enough bits to actually print everything?</p>



<p class="wp-block-paragraph">This one should look like it&#8217;s shifted to the right just because of the way it prints.</p>



<p class="wp-block-paragraph">So that means this one is totally lined up.</p>



<p class="wp-block-paragraph">Okay so let me grab the original pattern here.</p>



<p class="wp-block-paragraph">grab the original pattern here so this is the original pattern and then if I go shift left</p>



<p class="wp-block-paragraph">in binary I&#8217;ll just put an L here and then I&#8217;ll put shift right in binary</p>



<p class="wp-block-paragraph">there we have enough bits I think in both cases so we don&#8217;t have to pad</p>



<p class="wp-block-paragraph">so now if we look at the original pattern and then we shift left arithmetically twice</p>



<p class="wp-block-paragraph">twice notice how it does kind of look like it survived right like this one moved over to the</p>



<p class="wp-block-paragraph">left twice because the sine bits are just kind of falling off but there&#8217;s enough of them that it&#8217;s</p>



<p class="wp-block-paragraph">not going to change the sine bit thank you two&#8217;s complement but when we shift to the right</p>



<p class="wp-block-paragraph">arithmetically normally we would have padded on the left with zeros remember we had zeros there</p>



<p class="wp-block-paragraph">on the left before but now that we&#8217;re respecting the arithmetic uh the the padding is correct now</p>



<p class="wp-block-paragraph">is correct now for two&#8217;s complement so that&#8217;s why there are ones there instead of zeros</p>



<p class="wp-block-paragraph">and then if you kind of look at it it&#8217;s like well we shifted twice to the right let me get</p>



<p class="wp-block-paragraph">rid of the left because we already understand that now it seems to be that the pattern is uh</p>



<p class="wp-block-paragraph">every bit is shifted over to the right the correct number of times the zeros dropped off the side</p>



<p class="wp-block-paragraph">and um the sine bits were padded on the left so everything seems to be correct and then just to</p>



<p class="wp-block-paragraph">to kind of prove to you that what we&#8217;re seeing is real, I just have the same things printed</p>



<p class="wp-block-paragraph">all over again from scratch in decimal. So originally we had our R10 as negative 12.</p>



<p class="wp-block-paragraph">And then if we do a bitwise shift to the left, it multiplies it by four. And so we&#8217;re accidentally</p>



<p class="wp-block-paragraph">correct here. Remember, this is a bitwise shift, so it doesn&#8217;t respect two&#8217;s complement. So just,</p>



<p class="wp-block-paragraph">we happen to have a starting number that would end up looking correct after we shifted it to the left.</p>



<p class="wp-block-paragraph">And then when we do a bitwise shift to the right, again, this could be disaster because we&#8217;re not</p>



<p class="wp-block-paragraph">respecting two&#8217;s complement or the sine bit. And that&#8217;s exactly what happened. You know, definitely</p>



<p class="wp-block-paragraph">if we&#8217;re shifting to the right, that should be 12 divided by four equals three or negative three.</p>



<p class="wp-block-paragraph">But instead we get this gigantic huge number because the sine bit got killed with the zeros</p>



<p class="wp-block-paragraph">killed with the zeros that were padded on the left. And then we finally try to shift left</p>



<p class="wp-block-paragraph">arithmetically. We end up still with negative 48, which is the correct number. If we&#8217;re going to</p>



<p class="wp-block-paragraph">shift left twice, that&#8217;s multiplying 12 by 4 and that&#8217;s 48 or negative 48. And then when we shift</p>



<p class="wp-block-paragraph">to the right, that should divide it by 2 twice because we&#8217;re shifting to the right by two</p>



<p class="wp-block-paragraph">positions. So that should be 12 divided by 4, which is negative 3 or sorry, negative 12 divided</p>



<p class="wp-block-paragraph">which is negative three so everything seems to be working except for a couple of moments of</p>



<p class="wp-block-paragraph">stumbling on my part which i hope you laughed at um and i think this is the end of the program so</p>



<p class="wp-block-paragraph">now hopefully you&#8217;re an expert in uh ending oring x oring nodding shifting left and right uh bitwise</p>



<p class="wp-block-paragraph">rotating. We&#8217;ve done it all in this one video. How long did this video take? 40 minutes?</p>



<p class="wp-block-paragraph">Anyway, thank you so much for watching this video. I hope you learned a little bit of stuff</p>



<p class="wp-block-paragraph">and you had a little bit of fun. I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">Hey everybody, thanks for watching this video again from the bottom of my heart. I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please,</p>



<p class="wp-block-paragraph">and follow this channel or these videos or whatever it is you do on the current social</p>



<p class="wp-block-paragraph">media website that you&#8217;re looking at right now. It would really mean the world to me and it&#8217;ll</p>



<p class="wp-block-paragraph">help make more videos and grow this community. So we&#8217;ll be able to do more videos, longer videos,</p>



<p class="wp-block-paragraph">better videos, or just I&#8217;ll be able to keep making videos in general. So please do me a kindness</p>



<p class="wp-block-paragraph">and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p class="wp-block-paragraph">because I know somebody subscribed or followed. It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">just wakes me up and I get filled with joy. That&#8217;s exactly what happens every</p>



<p class="wp-block-paragraph">single time. So you could do it as a nice favor to me or you could you could</p>



<p class="wp-block-paragraph">troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p class="wp-block-paragraph">and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also if you look</p>



<p class="wp-block-paragraph">at the middle of the screen right now you should see a QR code which you can</p>



<p class="wp-block-paragraph">scan in order to go to the website which I think is also named somewhere at the</p>



<p class="wp-block-paragraph">bottom of this video and it&#8217;ll take you to my main website where you can just</p>



<p class="wp-block-paragraph">kind of like see all the videos I published and the services and tutorials</p>



<p class="wp-block-paragraph">the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see</p>



<p class="wp-block-paragraph">Please leave a comment or if you just want to say hey, what&#8217;s up? What&#8217;s going on?</p>



<p class="wp-block-paragraph">You know, just send me a comment, whatever. I also wake up for those in the middle of the night</p>



<p class="wp-block-paragraph">I get I wake up in a cold sweat. I&#8217;m like</p>



<p class="wp-block-paragraph">It would really it really mean the world to me. I would really appreciate it. So</p>



<p class="wp-block-paragraph">Again, thank you so much for watching this video and enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/">x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates 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-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Yasm x86-64 Assembly: Instruction Formats, Operands, MOV, ADD, Memory &#038; Registers Explained</title>
		<link>https://www.NeuralLantern.com/yasm-x86-64-assembly-instruction-formats-operands-mov-add-memory-registers-explained/</link>
					<comments>https://www.NeuralLantern.com/yasm-x86-64-assembly-instruction-formats-operands-mov-add-memory-registers-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 02 Nov 2025 12:20:23 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[add instruction]]></category>
		<category><![CDATA[asm programming]]></category>
		<category><![CDATA[assembly crash course]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly operands]]></category>
		<category><![CDATA[assembly registers]]></category>
		<category><![CDATA[inc rax]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[memory dereference]]></category>
		<category><![CDATA[mov instruction]]></category>
		<category><![CDATA[qword byte size]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[ubuntu assembly book]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=245</guid>

					<description><![CDATA[<p>Yasm assembly tutorial: decode instruction formats, operands, mov, add, memory sizes &#038; registers. Fix common crashes. Code-along x86-64 guide for beginners.</p>
<p>The post <a href="https://www.NeuralLantern.com/yasm-x86-64-assembly-instruction-formats-operands-mov-add-memory-registers-explained/">Yasm x86-64 Assembly: Instruction Formats, Operands, MOV, ADD, Memory &amp; Registers Explained</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="Yasm x86-64 Assembly: Instruction Formats, Operands, MOV, ADD, Memory &amp; Registers Explained" width="1380" height="776" src="https://www.youtube.com/embed/dXQQh_ppqPU?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 class="wp-block-paragraph">Ever stared at &#8220;mov rdi, rsi&#8221; and wondered WHO goes WHERE?<br>This 18-minute crash course turns confusion into confidence.</p>



<p class="wp-block-paragraph">We start with the world’s simplest instruction (inc rax) and level-up to:</p>



<ul class="wp-block-list">
<li>Zero, one, two, and three-operand formats</li>



<li>Why &#8220;mov rdi, 89&#8221; and “mov rdi, [someVar]” are NOT the same</li>



<li>Brackets = dereference, no brackets = pointer</li>



<li>When ADD destroys your register (and how to stop it)</li>



<li>The memory-size trap that crashes 90% of beginners</li>



<li>Byte / word / dword / qword prefixes that save your life</li>



<li>Pro tip: keep everything in registers until the last microsecond</li>
</ul>



<p class="wp-block-paragraph">Code is shown live in Yasm, every line explained like you’re sitting next to me.<br>No fluff, no 2-hour theory—just the exact mental model you need to read and write assembly tomorrow.</p>



<p class="wp-block-paragraph">Grab the free PDF cheat-sheet (link in first comment) and code along.<br>Hit Subscribe—next video we build a complete program from scratch.</p>



<h1 class="wp-block-heading">Assembly #x86_64 #Yasm #LowLevel</h1>



<p class="wp-block-paragraph">Introduction to Instruction Formats 00:00:00<br>Simple INC Example 00:00:28<br>Instruction and Operand Basics 00:00:47<br>Zero to Three Operands 00:01:14<br>MOV Instruction Explained 00:02:02<br>Immediates and Registers 00:02:44<br>Memory Variables and Pointers 00:03:16<br>Dereferencing with Brackets 00:05:08<br>ADD with Two Operands 00:05:32<br>ADD with Three Operands 00:06:55<br>Consulting the Ubuntu Book 00:07:45<br>MOV Source vs Destination 00:08:52<br>Memory Size Matters 00:09:07<br>Specifying Data Size 00:11:40<br>INC on Memory Locations 00:13:19<br>Why Size Prefixes Prevent Bugs 00:14:08<br>Performance Tip &#8211; Use Registers 00:15:12<br>Book Recommendation Wrap-up 00:16:04</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hey there! Let&#8217;s talk about instruction formats in Yasm Assembly.</p>



<p class="wp-block-paragraph">So what am I talking about? Well, if we&#8217;re in Yasm Assembly and we&#8217;re going to learn how to</p>



<p class="wp-block-paragraph">use lots and lots of instructions, it&#8217;s probably a pretty good idea to understand the general idea</p>



<p class="wp-block-paragraph">of an instruction and its operands and the things that you can do to it. So the first thing I want</p>



<p class="wp-block-paragraph">show you is a really simple instruction called inc and we&#8217;ll put it we&#8217;ll put a</p>



<p class="wp-block-paragraph">register there we&#8217;ll say RAX we want to take whatever value is inside of the RAX</p>



<p class="wp-block-paragraph">register and just increase it by one simple enough but it&#8217;s important to</p>



<p class="wp-block-paragraph">understand that this is this follows a specific format the first thing that we</p>



<p class="wp-block-paragraph">put is the name of the instruction that we want to execute second thing we put</p>



<p class="wp-block-paragraph">is an operand you know what thing do we want to increase we could actually put</p>



<p class="wp-block-paragraph">We could actually put other types of operands there.</p>



<p class="wp-block-paragraph">We couldn&#8217;t put an immediate number, like just a hard-coded number,</p>



<p class="wp-block-paragraph">but we could put a memory location if we wanted to, and we were careful about it.</p>



<p class="wp-block-paragraph">So think about instructions as following this sort of format.</p>



<p class="wp-block-paragraph">Let&#8217;s see, INST, and then a space, and then, you know,</p>



<p class="wp-block-paragraph">sometimes instructions have zero operands, like there&#8217;s an instruction called CQO, I think,</p>



<p class="wp-block-paragraph">and it just takes two registers and sort of like lays them out against each other.</p>



<p class="wp-block-paragraph">against each other but for now we&#8217;ll just assume we&#8217;re going to write instructions that</p>



<p class="wp-block-paragraph">actually do something to named registers or memory locations or whatever.</p>



<p class="wp-block-paragraph">So the name of the instruction comes first and then we have an operand.</p>



<p class="wp-block-paragraph">One or more operands up to three actually.</p>



<p class="wp-block-paragraph">So basically the operands are separated by commas so we&#8217;ll say operand one, operand two,</p>



<p class="wp-block-paragraph">operand three and most instructions will follow this format.</p>



<p class="wp-block-paragraph">do what the operands are for it&#8217;s easy to tell that the first operand is the</p>



<p class="wp-block-paragraph">thing that will be increased in the increase operand but what about the move</p>



<p class="wp-block-paragraph">instruction what if we wanted to move you know one register to another let&#8217;s</p>



<p class="wp-block-paragraph">say RDI to RSI or sorry RDI from RSI or vice versa I mean how would you know</p>



<p class="wp-block-paragraph">remember this is the instruction and this is the first operand and then this</p>



<p class="wp-block-paragraph">And then this is the second operand.</p>



<p class="wp-block-paragraph">There&#8217;s not three operands in this instruction.</p>



<p class="wp-block-paragraph">Other instructions have three operands.</p>



<p class="wp-block-paragraph">The way it works with the move instruction,</p>



<p class="wp-block-paragraph">I hope you understand already at this point,</p>



<p class="wp-block-paragraph">is that operand two gets placed into operand one.</p>



<p class="wp-block-paragraph">So when this instruction is finished,</p>



<p class="wp-block-paragraph">then whatever value was inside of RSI is going to be inside of RDI.</p>



<p class="wp-block-paragraph">So they&#8217;re actually going to end up being copies of each other.</p>



<p class="wp-block-paragraph">Some instructions allow you to put immediates,</p>



<p class="wp-block-paragraph">like I could just put the number 89 right here and the number 89 will get loaded into RDI so I</p>



<p class="wp-block-paragraph">don&#8217;t really even need to use a register. Some instructions this one in particular also will</p>



<p class="wp-block-paragraph">allow you to place the name of some variable in brackets as the second operand and then what&#8217;ll</p>



<p class="wp-block-paragraph">happen is if you if you recall hopefully let me let me let me improve this a little bit.</p>



<p class="wp-block-paragraph">you have a data section and you have a text section.</p>



<p class="wp-block-paragraph">The text section is for your instructions.</p>



<p class="wp-block-paragraph">In the data section, we could say some variable</p>



<p class="wp-block-paragraph">and we could define this as data quad word, I guess,</p>



<p class="wp-block-paragraph">and put like a big giant number inside of there.</p>



<p class="wp-block-paragraph">And so now we have a variable, a global variable to that module</p>



<p class="wp-block-paragraph">that is sitting in memory somewhere and it&#8217;s got this value.</p>



<p class="wp-block-paragraph">So then what we can do is basically say,</p>



<p class="wp-block-paragraph">take the name of that variable, stick it inside of brackets,</p>



<p class="wp-block-paragraph">and then the move instruction will go grab that number, grab the value associated with that</p>



<p class="wp-block-paragraph">variable and then load that into RDI. What if we didn&#8217;t put brackets? What if we just said load and</p>



<p class="wp-block-paragraph">then some variable? This is how you place a pointer inside of a register. Remember all these registers,</p>



<p class="wp-block-paragraph">at least the general purpose registers with this R format, are 64-bit registers and in the 64-bit</p>



<p class="wp-block-paragraph">use that means you could hold a memory location in the register so it&#8217;s pretty convenient when</p>



<p class="wp-block-paragraph">when you define a variable up top here what you&#8217;re really doing is you&#8217;re saying i would like to</p>



<p class="wp-block-paragraph">allocate eight bytes for a quad word and i would like to fill it up with this data</p>



<p class="wp-block-paragraph">you know like let&#8217;s take eight bytes reserve it for us then we&#8217;ll convert all the ones and zeros</p>



<p class="wp-block-paragraph">appropriately so that it ends up equaling that value then when we&#8217;re done it&#8217;ll give us a pointer</p>



<p class="wp-block-paragraph">a normal variable that you usually would think of in C++ where it&#8217;s just like the variable is the</p>



<p class="wp-block-paragraph">variable and not a pointer to something. All these are actually just pointers. So if I load some</p>



<p class="wp-block-paragraph">variable into RDI I&#8217;m actually loading a pointer to RDI which by the way would make it pretty easy</p>



<p class="wp-block-paragraph">for you to pass a pointer into another function just by loading up RDI with some pointer to you</p>



<p class="wp-block-paragraph">know some of your data somewhere. But the moment I put the brackets around it it will dereference</p>



<p class="wp-block-paragraph">it will dereference the pointer for me</p>



<p class="wp-block-paragraph">and take the value and then send the value into that.</p>



<p class="wp-block-paragraph">Hopefully that makes sense.</p>



<p class="wp-block-paragraph">Okay, so we got pointers, dereference pointers,</p>



<p class="wp-block-paragraph">immediates and registers.</p>



<p class="wp-block-paragraph">That actually went by pretty fast.</p>



<p class="wp-block-paragraph">I thought this was gonna be a long video.</p>



<p class="wp-block-paragraph">Well, let&#8217;s look at my favorite book here,</p>



<p class="wp-block-paragraph">the Ubuntu coding book.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">Oh, maybe first let&#8217;s look at the add instruction</p>



<p class="wp-block-paragraph">because I wanted to get something with three operands.</p>



<p class="wp-block-paragraph">So let&#8217;s do an instruction with three operands.</p>



<p class="wp-block-paragraph">I&#8217;ll say INST and we&#8217;ll do operand one, operand two, operand three.</p>



<p class="wp-block-paragraph">So we have this add instruction here where we can just add two integers together.</p>



<p class="wp-block-paragraph">I could say let&#8217;s load the value of, you know what, let&#8217;s do this first with only two.</p>



<p class="wp-block-paragraph">Yeah, because that&#8217;ll be more fun.</p>



<p class="wp-block-paragraph">Let&#8217;s do RDI and RSI.</p>



<p class="wp-block-paragraph">Okay, so this is legal.</p>



<p class="wp-block-paragraph">You can have an instruction named add that uses either two or three operands.</p>



<p class="wp-block-paragraph">A lot of instructions do that.</p>



<p class="wp-block-paragraph">Usually in the two operand format, what they&#8217;re saying is,</p>



<p class="wp-block-paragraph">let&#8217;s do something with both of these values and then store the result</p>



<p class="wp-block-paragraph">of doing something in the first register.</p>



<p class="wp-block-paragraph">So for example, it&#8217;s going to take RDI and add it with RSI,</p>



<p class="wp-block-paragraph">assuming two&#8217;s complement integers.</p>



<p class="wp-block-paragraph">store the result of that addition inside of RDI. So if you think about it, it&#8217;s faster to type,</p>



<p class="wp-block-paragraph">but sometimes it might destroy data if you didn&#8217;t actually expect to modify RDI.</p>



<p class="wp-block-paragraph">If you wanted to store the result somewhere else, but still add RDI and RSI somewhere,</p>



<p class="wp-block-paragraph">then that wouldn&#8217;t work. So let&#8217;s do the three operand format, operand two, operand three.</p>



<p class="wp-block-paragraph">So now let&#8217;s say that we wanted to store in some other register without destroying</p>



<p class="wp-block-paragraph">RDI after we do the addition. So we can use the three operand format. Maybe I&#8217;ll</p>



<p class="wp-block-paragraph">hit space here a couple times just to make it a little more interesting. I</p>



<p class="wp-block-paragraph">guess I probably should have formatted the comment. Let me try to… No I guess I</p>



<p class="wp-block-paragraph">can&#8217;t. I don&#8217;t need to space it out. Well I could. I could anyway. So we&#8217;ll use the</p>



<p class="wp-block-paragraph">add instruction which is totally valid to use for three operands in addition to</p>



<p class="wp-block-paragraph">two operands. And what will happen now is it&#8217;ll take RDI and add it to RSI</p>



<p class="wp-block-paragraph">and add it to RSI and store the result in R11 in case you wanted to preserve the value of RDI and</p>



<p class="wp-block-paragraph">RSI. Lots and lots of different instructions with lots and lots of different things that you can do.</p>



<p class="wp-block-paragraph">Of course, we could put a memory location there. We could put like an immediate somewhere.</p>



<p class="wp-block-paragraph">But let me go to my favorite book here. Whoops, I already have it highlighted on the right section.</p>



<p class="wp-block-paragraph">x86 64 assembly language programming with Ubuntu by a brilliant doctor and I</p>



<p class="wp-block-paragraph">use his book all the time I&#8217;m not gonna say his name because I don&#8217;t want to</p>



<p class="wp-block-paragraph">accidentally mispronounce it I&#8217;m pretty sure I know how but I don&#8217;t even want to</p>



<p class="wp-block-paragraph">try because I suck at pronouncing things I&#8217;ve pronounced my own name wrong on</p>



<p class="wp-block-paragraph">many occasion so let&#8217;s see I&#8217;m gonna click on section 7 the instruction set</p>



<p class="wp-block-paragraph">the instruction set overview and basically you know it kind of goes over the fact that oh you</p>



<p class="wp-block-paragraph">know we can put registers as operands and we can do some certain things like if you see this</p>



<p class="wp-block-paragraph">notation it means you can put a register as an operand if you see this notation it means that&#8217;s</p>



<p class="wp-block-paragraph">going to be the source operand if you put if you see this notation it means you can put an immediate</p>



<p class="wp-block-paragraph">at that spot so the book just kind of tells you what to expect in terms of notation then if you</p>



<p class="wp-block-paragraph">go down it starts explaining a bunch of different instructions for example the move instruction</p>



<p class="wp-block-paragraph">instructions. For example, the move instruction that we&#8217;ve just been working with. Notice how it</p>



<p class="wp-block-paragraph">tells you the source is on the right, the destination is on the left. And if we are taking</p>



<p class="wp-block-paragraph">from a memory location, which I probably should have mentioned, let me see, did I mention that?</p>



<p class="wp-block-paragraph">No, I don&#8217;t think I did. If we&#8217;re taking from a memory location, it&#8217;s important to understand that</p>



<p class="wp-block-paragraph">the data size of the memory locations data and the register might actually be different. See,</p>



<p class="wp-block-paragraph">different. See here we kind of made a bad assumption. We made the assumption that RDI</p>



<p class="wp-block-paragraph">was a 64-bit register. That&#8217;s a good assumption. But then we also made the assumption that sitting</p>



<p class="wp-block-paragraph">at that pointer is eight bytes of memory. That&#8217;s a bad assumption. Up at the top, we know that</p>



<p class="wp-block-paragraph">when we put DQ for data quad word, then eight bytes are going to get allocated. But by the time we</p>



<p class="wp-block-paragraph">But by the time we get down to this instruction, you know assembly is not very smart.</p>



<p class="wp-block-paragraph">By the time we get down to this instruction, we can&#8217;t be sure if that pointer refers to</p>



<p class="wp-block-paragraph">something that is 8 bytes or 4 bytes or 2 bytes.</p>



<p class="wp-block-paragraph">We should be able to support loading a number.</p>



<p class="wp-block-paragraph">Like just as an example, I could lower this number, like let&#8217;s say with the number 25,</p>



<p class="wp-block-paragraph">but you know that the number 25 will fit into just a single byte.</p>



<p class="wp-block-paragraph">It&#8217;ll also fit into a word, 2 bytes.</p>



<p class="wp-block-paragraph">32 bit number, a D word, you know, four bytes.</p>



<p class="wp-block-paragraph">It&#8217;ll also fit into a quad word, 64 bits, eight bytes.</p>



<p class="wp-block-paragraph">So it&#8217;ll fit in different sizes of data.</p>



<p class="wp-block-paragraph">So what if you have a memory allocation,</p>



<p class="wp-block-paragraph">like instead of putting DQ,</p>



<p class="wp-block-paragraph">what if we put DB for just data byte?</p>



<p class="wp-block-paragraph">So just like one byte of memory to put that 25 in,</p>



<p class="wp-block-paragraph">that should work.</p>



<p class="wp-block-paragraph">But the point is this part of the code</p>



<p class="wp-block-paragraph">doesn&#8217;t actually know how big the memory location is.</p>



<p class="wp-block-paragraph">So it doesn&#8217;t know how many bytes to grab.</p>



<p class="wp-block-paragraph">many bytes to grab like what if we what if it just assumed that because we used a 64-bit register</p>



<p class="wp-block-paragraph">actually let&#8217;s go back up here let&#8217;s change this to a byte data byte so we know that the 25 occupies</p>



<p class="wp-block-paragraph">only one byte but here if we were unlucky then the assembler might assume oh they want to load</p>



<p class="wp-block-paragraph">eight bytes of memory interpreted as a number and then put that into a 64-bit register an eight</p>



<p class="wp-block-paragraph">But that probably wouldn&#8217;t work because we only actually modified one byte to make that 25.</p>



<p class="wp-block-paragraph">So there&#8217;s going to be seven other bytes of junk data.</p>



<p class="wp-block-paragraph">So now we just loaded RDI with some sort of a nonsense value.</p>



<p class="wp-block-paragraph">And even if it does work, it might not work on every run of the program.</p>



<p class="wp-block-paragraph">So then you have something which is probably undefined behavior,</p>



<p class="wp-block-paragraph">where it&#8217;s just like you made a bad mistake and your program&#8217;s going to work sometimes</p>



<p class="wp-block-paragraph">and maybe some other times not work.</p>



<p class="wp-block-paragraph">So how do we fix this?</p>



<p class="wp-block-paragraph">So how do we fix this? We just specify the size of the memory location or of</p>



<p class="wp-block-paragraph">the bytes that we&#8217;re going to grab at that memory location. So if I just put</p>



<p class="wp-block-paragraph">the word byte in front, now the assembler knows that I only want to grab</p>



<p class="wp-block-paragraph">one byte and interpret that as a number and then put that into RDI rather than</p>



<p class="wp-block-paragraph">all the extra junk data. So if I want to make sure that it understands that I want</p>



<p class="wp-block-paragraph">eight bytes and also to prevent it from refusing to compile, which it&#8217;s probably</p>



<p class="wp-block-paragraph">going to do if i don&#8217;t specify the size i&#8217;ll put q word to say yeah this 25 it&#8217;s uh it&#8217;s actually</p>



<p class="wp-block-paragraph">a q word it occupies eight bytes so i want you to load up eight bytes in ram and interpret all</p>



<p class="wp-block-paragraph">those bytes as just you know whatever numbers inside of them pretty sweet i love going to this</p>



<p class="wp-block-paragraph">book because i was going to forget to say that anyway just so you know there&#8217;s a bunch of</p>



<p class="wp-block-paragraph">instructions that you can look up i&#8217;m not going to go through all of them but um you know typically</p>



<p class="wp-block-paragraph">Leftmost operand in some sort of a modifying instruction is going to be the destination</p>



<p class="wp-block-paragraph">register or memory location.</p>



<p class="wp-block-paragraph">And if you have a two operand instruction, it&#8217;s probably going to take both operands</p>



<p class="wp-block-paragraph">and do something with them and then stick the result in the first operand.</p>



<p class="wp-block-paragraph">And then if you have a three operand instruction, it&#8217;s probably going to take the rightmost</p>



<p class="wp-block-paragraph">two operands, do something with them, store the result in the leftmost operand.</p>



<p class="wp-block-paragraph">wonderful, wonderful book will tell you exactly what to expect for every type of instruction.</p>



<p class="wp-block-paragraph">So there&#8217;s like move here, integer arithmetic. Oh yeah, there&#8217;s the add function or sorry,</p>



<p class="wp-block-paragraph">the add instruction. And then here&#8217;s like a bunch of sample code, which is awesome.</p>



<p class="wp-block-paragraph">And notice how here, if we want to increase something that is located at a memory location,</p>



<p class="wp-block-paragraph">we got to specify the size of the memory. Again, same problem that we just talked about up above.</p>



<p class="wp-block-paragraph">Did we do that at the beginning?</p>



<p class="wp-block-paragraph">I don&#8217;t remember if we did that.</p>



<p class="wp-block-paragraph">You know, if we have something where it&#8217;s like,</p>



<p class="wp-block-paragraph">if we said increase the RAX register,</p>



<p class="wp-block-paragraph">the computer knows that RAX is a 64-bit register</p>



<p class="wp-block-paragraph">because the way we typed it, RAX, that implies 64 bits.</p>



<p class="wp-block-paragraph">If we meant to only use 32 bits of the register,</p>



<p class="wp-block-paragraph">we would type a different, you know,</p>



<p class="wp-block-paragraph">set of letters to represent that register.</p>



<p class="wp-block-paragraph">But if we say, I would like you to increase</p>



<p class="wp-block-paragraph">the value sitting in memory at some variable,</p>



<p class="wp-block-paragraph">sitting in memory at some variable dereferenced, well then whoops,</p>



<p class="wp-block-paragraph">it doesn&#8217;t actually know how many bytes.</p>



<p class="wp-block-paragraph">If you increase a small</p>



<p class="wp-block-paragraph">value, like if it&#8217;s if you have one byte and you try to increase it,</p>



<p class="wp-block-paragraph">what if it overflows?</p>



<p class="wp-block-paragraph">Like, for example, if we had a one byte, if we had one byte of data here</p>



<p class="wp-block-paragraph">and we had the number, let&#8217;s say 255 already, that&#8217;s the highest value that a</p>



<p class="wp-block-paragraph">So if we then say let&#8217;s increase that,</p>



<p class="wp-block-paragraph">the computer is going to, you know, like overflow back down to just zero or not.</p>



<p class="wp-block-paragraph">What if it actually was a quad word?</p>



<p class="wp-block-paragraph">The computer didn&#8217;t understand whether it&#8217;s supposed to overflow or not overflow.</p>



<p class="wp-block-paragraph">Do you see what I&#8217;m saying?</p>



<p class="wp-block-paragraph">There&#8217;s lots of problems that can happen if you don&#8217;t know the size.</p>



<p class="wp-block-paragraph">So that just means we have to specify the size.</p>



<p class="wp-block-paragraph">Anytime we&#8217;re dealing with memory, we&#8217;ll say we&#8217;ll increase a keyword sitting at some</p>



<p class="wp-block-paragraph">variable. And of course, if you&#8217;re going to</p>



<p class="wp-block-paragraph">and of course if you&#8217;re gonna i don&#8217;t know why you&#8217;d want to do this but if you wanted to well</p>



<p class="wp-block-paragraph">i guess if you&#8217;re running through a string if you wanted to increase a pointer value uh then i think</p>



<p class="wp-block-paragraph">you don&#8217;t have to specify the data size although i&#8217;m not actually sure if you can change the value</p>



<p class="wp-block-paragraph">of that pointer i don&#8217;t know if it&#8217;s hard coded usually what i do is i would just move you know</p>



<p class="wp-block-paragraph">some kind of a i would move the pointer if we&#8217;re if we&#8217;re using a running pointer to run through</p>



<p class="wp-block-paragraph">I would load the pointers value into a register and then I would just start increasing the</p>



<p class="wp-block-paragraph">register just to make things a little bit easier because as I usually try to say it&#8217;s</p>



<p class="wp-block-paragraph">way slower to constantly be hitting memory.</p>



<p class="wp-block-paragraph">Anytime you can possibly keep all of your calculations and algorithms and stuff inside</p>



<p class="wp-block-paragraph">of only registers as long as possible, then your program will be way, way faster.</p>



<p class="wp-block-paragraph">So if you&#8217;re going to run through a bunch of data with a pointer, probably want to load</p>



<p class="wp-block-paragraph">it up into a register first and then keep increasing the register.</p>



<p class="wp-block-paragraph">When you finally figure out, you know, everything that&#8217;s calculated and finished,</p>



<p class="wp-block-paragraph">then go ahead and save it back to memory at the very end.</p>



<p class="wp-block-paragraph">But just to let you know, we&#8217;ve got lots of stuff.</p>



<p class="wp-block-paragraph">Okay, so there&#8217;s the add function.</p>



<p class="wp-block-paragraph">This is the two op version.</p>



<p class="wp-block-paragraph">You can see at the top there.</p>



<p class="wp-block-paragraph">Let&#8217;s see, where&#8217;s the three op version?</p>



<p class="wp-block-paragraph">Here&#8217;s somebody honking the horn outside.</p>



<p class="wp-block-paragraph">Someone&#8217;s upset with someone else.</p>



<p class="wp-block-paragraph">Someone&#8217;s upset.</p>



<p class="wp-block-paragraph">Someone&#8217;s upset. Do you ever honk the horn not angry, but just so that the other driver will think you&#8217;re angry and</p>



<p class="wp-block-paragraph">Then possibly be a good driver or a better driver later on in their life. I don&#8217;t know if that actually works. I have tried it</p>



<p class="wp-block-paragraph">And you do your eyebrows like this</p>



<p class="wp-block-paragraph">Even though you&#8217;re not upset</p>



<p class="wp-block-paragraph">I wonder if they just go home laughing and they&#8217;re like, yeah, you&#8217;re right. I was ridiculous</p>



<p class="wp-block-paragraph">Okay, so I guess I&#8217;m gonna stop scrolling</p>



<p class="wp-block-paragraph">a</p>



<p class="wp-block-paragraph">A good coder always reads the documentation and studies ahead and things like that</p>



<p class="wp-block-paragraph">I know you were hoping for me to tell you everything in this video, but there&#8217;s just not enough time or energy to do that</p>



<p class="wp-block-paragraph">I just wanted you to understand</p>



<p class="wp-block-paragraph">How how to how to interpret instruction formats?</p>



<p class="wp-block-paragraph">I wanted to make that part easy for you</p>



<p class="wp-block-paragraph">but all the different instructions that are available to you to multiply to add to increase values to do all this stuff</p>



<p class="wp-block-paragraph">It&#8217;s all in this nice wonderful book</p>



<p class="wp-block-paragraph">okay anyway I think I&#8217;m gonna cut the video I hope you enjoyed what you have</p>



<p class="wp-block-paragraph">learned I hope you have learned a lot of stuff and had a little bit of fun there</p>



<p class="wp-block-paragraph">and I&#8217;ll see you in the next video hey everybody thanks for watching this video</p>



<p class="wp-block-paragraph">again from the bottom of my heart I really appreciate it I do hope you did</p>



<p class="wp-block-paragraph">learn something and have some fun if you could do me a please a small little</p>



<p class="wp-block-paragraph">favor could you please subscribe and follow this channel or these videos or</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website that you&#8217;re looking at right now.</p>



<p class="wp-block-paragraph">It would really mean the world to me and it&#8217;ll help make more videos and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night</p>



<p class="wp-block-paragraph">and I just wake up because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">every single time so you could do it as a nice favor to me or you could you control me if you</p>



<p class="wp-block-paragraph">want to just wake me up in the middle of the night just subscribe and then i&#8217;ll i&#8217;ll just wake up i</p>



<p class="wp-block-paragraph">promise that&#8217;s what will happen also uh if you look at the middle of the screen right now you</p>



<p class="wp-block-paragraph">should see a qr code which you can scan in order to go to the website which i think is also named</p>



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website where you can just</p>



<p class="wp-block-paragraph">kind of like see all the videos i published and the services and tutorials and things that i offer</p>



<p class="wp-block-paragraph">all that good stuff and uh if you have a suggestion for uh uh clarifications or errata or just future</p>



<p class="wp-block-paragraph">videos that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s</p>



<p class="wp-block-paragraph">going on you know just send me a comment whatever i also wake up for those in the middle of the</p>



<p class="wp-block-paragraph">night i get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">and enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/yasm-x86-64-assembly-instruction-formats-operands-mov-add-memory-registers-explained/">Yasm x86-64 Assembly: Instruction Formats, Operands, MOV, ADD, Memory &amp; Registers Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/yasm-x86-64-assembly-instruction-formats-operands-mov-add-memory-registers-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
