<?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>cmp instruction Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/cmp-instruction/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/cmp-instruction/</link>
	<description></description>
	<lastBuildDate>Sun, 18 Jan 2026 00:49:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>cmp instruction Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/cmp-instruction/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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 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>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>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>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>Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p>Enjoy learning assembly!</p>



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



<p>Thanks for watching!</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>Hey there. In this video,</p>



<p>we&#8217;re going to talk about conditional branching in an assembly program.</p>



<p>We&#8217;re going to be looking at YASM x86-64 assembly or AMD 64 assembly</p>



<p>within Ubuntu, but this will probably be a useful video.</p>



<p>If you&#8217;re just thinking about how to branch in assembly in general.</p>



<p>So for starters, I&#8217;m going to promote one of my favorite books,</p>



<p>as I often do. This is a book that is free and open source. You can get a copy yourself. I did</p>



<p>not write this. The author of this book is a genius. His name is Professor Ed Jorgensen, PhD.</p>



<p>He made this whole book and released it under an open source license. So you can literally just go</p>



<p>to his website and get a copy of this for free. You can use this to make yourself into an assembly</p>



<p>expert. So I highly recommend this to everybody. Anyway, so I&#8217;m going to open up this book. I&#8217;m</p>



<p>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>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>which is entitled instruction set overview i&#8217;m going to double click it so open out open the</p>



<p>subsections and then i&#8217;m going to click on 7.7 control control instructions and then under that</p>



<p>i&#8217;m going to click on 7.7.3 conditional control instructions so uh that&#8217;s a lot of subsections</p>



<p>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>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>Anyway, so conditional control instructions in my last video</p>



<p>We talked about the jump instruction where we can just be sitting somewhere in our program and then just jump somewhere else</p>



<p>Anywhere we want based on the label so conditional branching or conditional control instructions</p>



<p>conditional control instructions are kind of the same thing except they will</p>



<p>decide whether or not to actually jump they might fall through to the next</p>



<p>instruction or they might jump away to somewhere else so conditional branching</p>



<p>or conditional jumping it basically kind of goes like this step one you compare</p>



<p>two values to each other they will probably be sitting in registers and then</p>



<p>conditional branching instruction or a conditional jump instruction there&#8217;s</p>



<p>lots of synonyms for these but basically in the regular jump instruction that I</p>



<p>just talked about in the last video you just jumped and it was only one thing</p>



<p>that you had to do but in this case we first have to compare two items and then</p>



<p>we&#8217;ll issue the jumping instruction because the jumping instruction will</p>



<p>have to decide whether to jump or not based on the result of your comparison</p>



<p>which makes sense right like if you&#8217;re going to implement something that&#8217;s like</p>



<p>if you know I&#8217;ll put like a expr for expression then well the first thing that we have to decide</p>



<p>before we jump either into the block of the if or if we jump into the block of the else is whether</p>



<p>or not that expression is true right so that&#8217;s kind of what the compare instruction is doing</p>



<p>sort of in higher level languages like c++ it can get a lot more complicated than that but you know</p>



<p>Okay, so we&#8217;re going to do conditional branching instructions.</p>



<p>Let me show you a few of those.</p>



<p>Okay, so for starters, here&#8217;s the compare instruction right here on page 126.</p>



<p>I think it&#8217;s actually 127 in the PDF, but basically, you know, 126.</p>



<p>We do CMP followed by two operands.</p>



<p>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>just a two operand instruction there&#8217;s no results that are going to get stored in one of these</p>



<p>operands both of these operands are just for input the result actually gets stored in a special</p>



<p>register called r flags which we hardly ever use or look at in in the most common assembly</p>



<p>instructions and design patterns we just kind of like let the r flags do whatever it&#8217;s going to do</p>



<p>and the compare instruction will fill that up so imagine that you know depending on whether r13 is</p>



<p>is like greater than r12 or less than or they&#8217;re equal or something else is going on between the</p>



<p>two of them the compare instruction just fills up all sorts of flags defining the relationship</p>



<p>between those two registers or those two operands r12 and r13 and then after the r flags register</p>



<p>is all set up then the conditional branching instruction will sort of rely on r flags in</p>



<p>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>there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



<p>of conditional branching instructions we can use the first two i want to show you and after this</p>



<p>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>first one is je which means let&#8217;s jump if the two operands were equal so again you know we compare</p>



<p>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>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>will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p>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>So if they were equal, then we&#8217;ll end up jumping to some label.</p>



<p>And if they were not equal, we&#8217;ll end up letting execution fall through to the nope on line 10.</p>



<p>And same thing for jump not equal and all these other ones.</p>



<p>But there are actually two different versions of some of these instructions that kind of seem the same.</p>



<p>Like for instance, this one right here, JL, that means jump if the first operand was less than the second operand.</p>



<p>is a jump below that jumps also if the first operand was less than the second operand so</p>



<p>what&#8217;s the difference between these two let me get my pen I love excuses to do my little pen now</p>



<p>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>less than and jump below are basically doing the same thing but there&#8217;s one difference one of them</p>



<p>One of them works on signed operands and one of them works on unsigned operands.</p>



<p>So keep this in mind.</p>



<p>If you&#8217;re comparing two signed integers, you want to use this group up here of instructions.</p>



<p>But if you&#8217;re using unsigned integers or just bits, then you want to use this group of instructions down here.</p>



<p>And you definitely want to know what kind of data you have that you&#8217;re comparing.</p>



<p>Notice though that jump equal and jump not equal, they don&#8217;t really care.</p>



<p>not equal they don&#8217;t really care if the operands are signed or unsigned that&#8217;s because if you want</p>



<p>to check to see if two things are equal you just look at all of their bits and if all of their</p>



<p>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>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>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>mind for all of your jump instructions where you&#8217;re trying to see if things are equal or not</p>



<p>or not equal you&#8217;re going to definitely be using these whether you&#8217;re using signed or unsigned</p>



<p>integers or floats or anything else like that but when it comes to actually you know integers that</p>



<p>are signed or unsigned these are the instructions you use for signed and these are the instructions</p>



<p>whoops that you use for what have i done that you&#8217;ll use for unsigned i keep confusing the on</p>



<p>off button for the red button is what i&#8217;m doing oh okay whatever i guess we&#8217;re done anyway</p>



<p>I guess we&#8217;re done anyway.</p>



<p>So keep that in mind.</p>



<p>In this video, we&#8217;re just going to be using the signed integer instructions</p>



<p>because that&#8217;s the example that I have set up.</p>



<p>But just imagine you could very easily translate this to unsigned integers or floats.</p>



<p>Okay, so let&#8217;s see.</p>



<p>We&#8217;ve got jump less than, we&#8217;ve got jump less than or equal to,</p>



<p>then we&#8217;ve got jump greater than, and then we&#8217;ve got jump greater than or equal to.</p>



<p>And when you combine those with jump equal to or jump not equal to,</p>



<p>jump not equal to that&#8217;s pretty much all the comparison that you need and if you want to do</p>



<p>something much much more complex well then you can just you know start stacking logic on top of</p>



<p>logic and and branching on top of branching to do something that&#8217;s like this is what i&#8217;m talking</p>



<p>like if you want to do this expression and expression or expression and then maybe like a</p>



<p>expression, right? If you want to do something super complicated, like we can do in a higher</p>



<p>level language, you&#8217;re just going to have to make a bunch of mini jumps or find another</p>



<p>way to reduce your logic. But in this video, we&#8217;re just going to be taking a very simple</p>



<p>comparison. All right. So I think that&#8217;s everything that I can show you inside of this book. Let&#8217;s</p>



<p>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>all my code inside of it. Another reminder that if you don&#8217;t know how to write assembly</p>



<p>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>to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p>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>in my other videos up to this point so for now we&#8217;re just going to focus only on conditional</p>



<p>branching conditional jumping inside of an assembly module so i&#8217;m going to start off by setting up my</p>



<p>and i&#8217;m gonna say paste and again you know this is covered in other videos but long story short</p>



<p>i&#8217;m just going to be printing out a bunch of strings while this program executes so</p>



<p>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>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>out a message that sort of you know understands what the user inputted so if the user inputted</p>



<p>something greater than zero it&#8217;s going to output that if they entered something that was less than</p>



<p>10 it&#8217;s going to print that if they entered something that was equal to five it&#8217;s going to</p>



<p>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>all this stuff is explained in other videos but we&#8217;re just basically printing so i&#8217;m going to</p>



<p>start the text section of my program which is where all the instructions are and i&#8217;m going to</p>



<p>a function that I can call on. Again, this video does not cover a bunch of other extra stuff. I&#8217;m</p>



<p>just going to show you how to branch. Just trust me when I say that this helps me easily print</p>



<p>numbers and things, or I guess in this particular case, it&#8217;ll help me input a number from the user.</p>



<p>But for you, if you&#8217;re trying to run this at home, you can just start hard coding numbers just to</p>



<p>make sure that you can branch correctly and then eventually work your way up to a library that can</p>



<p>videos in the distant future. I don&#8217;t know years from now. I don&#8217;t know.</p>



<p>I might release the library or some other stuff to the casual viewer.</p>



<p>So now here&#8217;s our entry point. Again,</p>



<p>I mentioned that there&#8217;s going to be other parts of this program that I&#8217;m not</p>



<p>showing. So this is a hybrid program where a C++ driver actually will call on</p>



<p>my cool function. So just imagine from somewhere else in the program,</p>



<p>you&#8217;re calling on the cool function. How do we know it&#8217;s a function?</p>



<p>a label called cool and then it has a return statement at the very end.</p>



<p>That&#8217;s pretty much all we need to know right now.</p>



<p>Let me get rid of that load.</p>



<p>I don&#8217;t know why I always have the word load in all of my things.</p>



<p>I think I put it in there on an early example</p>



<p>and then I just kept copy pasting to my heart&#8217;s content and never stopped.</p>



<p>We mark the function cool as global so that other modules can call on it</p>



<p>like the C++ module that I&#8217;m not showing you.</p>



<p>But if you&#8217;re writing a pure assembly module,</p>



<p>you don&#8217;t really need to mark your functions as global.</p>



<p>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>a function call to a function called branch test so very quickly before I actually add branch test</p>



<p>I&#8217;m just going to add another function that we&#8217;re not covering in this video called crlf and long</p>



<p>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>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>our branch test function so here&#8217;s the signature it is just branch test with a</p>



<p>void in front of it I&#8217;m gonna put a ret to say that this is a function that we</p>



<p>will return from and I left myself a little note here saying that I&#8217;m gonna</p>



<p>use register r12 as the number that the user inputs so that means I&#8217;m gonna have</p>



<p>to preserve that because you have to respect the ABI and the ABI says that</p>



<p>The ABI says that R12 is a Kali saved register.</p>



<p>Again, see my other videos if you don&#8217;t know what I&#8217;m talking about.</p>



<p>But I&#8217;m just going to preserve it in a section that I call the prologue, or actually the</p>



<p>book calls the prologue.</p>



<p>I do that now though too.</p>



<p>And, you know, restore it in a section called the epilogue.</p>



<p>So we have prologue and epilogue.</p>



<p>We can have a push-pop pair.</p>



<p>And then in between, we can have, you know, the meat of our function.</p>



<p>Okay, so for the introduction, I&#8217;m just going to write a few instructions that just print</p>



<p>out a message.</p>



<p>print out a message. So we&#8217;re going to print out that intro message and then call</p>



<p>CRLF to print a new line and then return. So maybe actually at this point we could</p>



<p>probably check to see if this program works by trying to run it.</p>



<p>So let me say clear and make run.</p>



<p>Okay. So now the driver just prints hello. The driver is</p>



<p>elsewhere and we get that welcome message that says begin</p>



<p>branching test and then the driver retains control after that. So</p>



<p>let&#8217;s do let&#8217;s do a goodbye message real fast so we&#8217;ll do our goodbye message</p>



<p>it&#8217;s just another string that I&#8217;m printing it&#8217;s no big deal right now but</p>



<p>if I run the program again you can see that begin branching test and end</p>



<p>branching test okay so far we&#8217;re not really doing branching yet the next thing</p>



<p>we need to do is ask for a number so this is how I&#8217;m going to do it in my</p>



<p>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>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>to print a prompt basically saying could you please enter a number and then the next two lines</p>



<p>are going to be just calling on one of my custom functions that will input a number from the user</p>



<p>so the user can type at the terminal and we will receive their number through REX which is the</p>



<p>return value register for integer return values and we&#8217;re going to store it in R12. So that&#8217;s why</p>



<p>And we&#8217;re going to store it in R12.</p>



<p>So that&#8217;s why I had to preserve R12,</p>



<p>because I&#8217;m actually going to start,</p>



<p>you know, messing with the value of R12 now.</p>



<p>Okay, so we ask for a number,</p>



<p>and then we sort of don&#8217;t do anything after that.</p>



<p>If we run the program again,</p>



<p>it&#8217;s just asking for a number,</p>



<p>and I can just like type some stuff and hit enter,</p>



<p>and then the program ends, nothing actually happens.</p>



<p>We&#8217;re building, we&#8217;re building.</p>



<p>So now let&#8217;s ask ourselves,</p>



<p>is the number greater than zero?</p>



<p>So let me copy paste some code here.</p>



<p>some code here so right now we&#8217;ve just finished inputting a number from the user and we&#8217;ll ask</p>



<p>you know is that number that they inputted greater than zero so again the first thing we do is we use</p>



<p>a compare instruction cmp we give it two operands the order doesn&#8217;t necessarily matter um for</p>



<p>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>to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p>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>if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



<p>which is just branch test after greater test. So I&#8217;m going to do a print statement right after that</p>



<p>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>actually will compile. Whoops. I&#8217;m in the wrong window. Okay. There we go. So what am I saying</p>



<p>here? Okay. Do the comparison. And then if the condition seems to be satisfied, then we&#8217;ll jump</p>



<p>to this, which means we&#8217;ll just say goodbye and then exit the program because we&#8217;ll return to the</p>



<p>caller. But if that condition was not satisfied, then we&#8217;re going to end up executing this stuff</p>



<p>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>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>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>whether r12 and r0 are equal or greater than or whatever so I&#8217;m just gonna say r12 and 0</p>



<p>god that&#8217;s awful I really need to practice this draw pad I spend like a whole week practicing</p>



<p>whole week practicing with a different draw pad and then when I come back to</p>



<p>this one I&#8217;m even worse than when I started so we we come in and we kind of</p>



<p>look at our 12 versus zero and then we will jump in one direction if that&#8217;s not</p>



<p>you know greater than or equal to or less than or equal to and we&#8217;ll jump in</p>



<p>another direction otherwise so basically here we&#8217;ll say jump less than equal to</p>



<p>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>thing r12 less than or equal to and so this is sort of uh has to do with you know this branching</p>



<p>path that we take like if r12 was less than or equal to zero then we&#8217;ll take the right branching</p>



<p>to after, I&#8217;ll just put the word after,</p>



<p>we&#8217;ll jump to the after greater test sub label.</p>



<p>And otherwise, if R12 is, sorry,</p>



<p>I said less than or equal to, yeah,</p>



<p>if R12 is greater than zero,</p>



<p>then we jump to this other thing,</p>



<p>which is gonna be the fall through.</p>



<p>So it&#8217;s gonna be line 96, but I&#8217;ll just say,</p>



<p>I&#8217;ll say fall for fall through.</p>



<p>basically the jump won&#8217;t happen if it&#8217;s greater than or equal to so you could imagine if you</p>



<p>wanted to reverse the logic so you can use a jump greater than instruction you could but I&#8217;m choosing</p>



<p>to do it this way and then both of those will eventually reach let&#8217;s see will eventually</p>



<p>actually yeah the fall through instructions those will eventually reach the after instruction I&#8217;ll</p>



<p>here and then the after instruction will just kind of like finish the program</p>



<p>and exit and all that stuff. So if you think about the control path here,</p>



<p>we have an input we&#8217;re looking at R12 with the zero with the compare instruction</p>



<p>and then we use a conditional branching instruction. So maybe I should write,</p>



<p>what do you want to write here? Maybe I&#8217;ll just put JLE up here because usually the compare</p>



<p>usually the compare instruction and the conditional jump or branching instructions</p>



<p>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>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>the right operand r12 less than equal to the right operand then we go down this path on the right</p>



<p>to the after label if that is false meaning r12 was greater than zero then we&#8217;ll fall through</p>



<p>this stuff right here and then whether or not we actually fall through will always end up at the</p>



<p>at the after area because we&#8217;re either going to jump directly to the after area or we&#8217;re going</p>



<p>to fall through and then the fall through falls through to the after area anyway as well let me</p>



<p>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>less than or equal to. So that means instructions are coming, you know,</p>



<p>instructions are getting executed. We&#8217;re going down and down and down and down and down.</p>



<p>Once we see jump less than or equal to, if that&#8217;s true, then we jump to the after label,</p>



<p>which means we just kind of jump around this message. And so we&#8217;re not going to exit,</p>



<p>sorry, we&#8217;re not going to execute that code. So in the case where that is true,</p>



<p>where it is less than or equal to, we&#8217;re not going to execute those instructions.</p>



<p>So we&#8217;re not going to say their number was greater than zero. We&#8217;re just going to simply</p>



<p>We&#8217;re just going to simply say goodbye.</p>



<p>However, on the other hand, if that is not true, meaning if R12 was indeed greater than</p>



<p>zero, then we&#8217;re going to fall through because this jump less than or equal to instruction,</p>



<p>it will only jump if the R flags, if the comparison instruction thought that R12 was less than</p>



<p>or equal to zero.</p>



<p>If that&#8217;s not true, if it&#8217;s greater than, then instructions, the control path is just</p>



<p>the control path is just going to fall through.</p>



<p>So that means we will actually execute these.</p>



<p>And then when they&#8217;re done,</p>



<p>execution will continue to fall through to the rest.</p>



<p>So that means we&#8217;re either going to see a message saying</p>



<p>their number was greater than zero or not,</p>



<p>based on whether their number actually was greater than zero.</p>



<p>Okay, hopefully I explained that somewhat clearly.</p>



<p>Now let&#8217;s run the code and see if it works.</p>



<p>Hopefully I didn&#8217;t forget anything.</p>



<p>This is kind of a partial program at this point.</p>



<p>program at this point I&#8217;m gonna add a bunch more stuff to it so hopefully</p>



<p>this actually compiles enter an integer five notice how it says your number was</p>



<p>greater than zero and if I run it again and I just type zero it does not say</p>



<p>that it was greater than zero if I run it again I type like a negative five it</p>



<p>also does not say that it was greater than zero you know a huge giant number</p>



<p>here it&#8217;ll say your number was greater than zero you could probably type a</p>



<p>number that was just absolutely huge and have it overflow I actually don&#8217;t know</p>



<p>nines I need to type and I don&#8217;t even know what the behavior is going to be.</p>



<p>I don&#8217;t know if it&#8217;s just going to crash the program because this also relies on my ability</p>



<p>to input a number which is happening in a different library.</p>



<p>Let&#8217;s see if that even works greater than zero.</p>



<p>Okay.</p>



<p>It probably overflowed and went to a very, very low negative number.</p>



<p>So okay.</p>



<p>I guess at least I know my library will probably work sort of.</p>



<p>Okay.</p>



<p>So let&#8217;s go back to the code here and let&#8217;s upgrade it.</p>



<p>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>next thing we should do is check to see if the number was less than 10. so after the greater test</p>



<p>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>modifying it and we&#8217;re not calling uh well i guess even if we were calling a function r12 was supposed</p>



<p>supposed to be Kali preserved so we&#8217;re just comparing it and then maybe</p>



<p>printing a message if we want to and regardless we&#8217;re going to end up at 103</p>



<p>this after greater test label so then we&#8217;ll compare again r12 with the number</p>



<p>10 and we&#8217;ll say jump if it&#8217;s greater than or equal to branch test after less</p>



<p>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>just skip over the message so if it&#8217;s greater than or equal to 10 then wait a minute wait wait</p>



<p>yeah yeah if it&#8217;s greater than or equal to 10 then skip the message otherwise the message</p>



<p>that is printed is going to say that their number is less than 10. so again you could imagine a</p>



<p>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>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>jumping greater than or equal to the input is going to be where we&#8217;re just looking to see if</p>



<p>R12 how it compares to zero god the number one is awful my entire penmanship is awful okay one</p>



<p>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>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>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>and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



<p>so if we fall through it&#8217;s just going to print a message maybe I should do another little bubble</p>



<p>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>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>the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



<p>to 10 which is the same thing as saying it&#8217;s going to fall through if r12 is less than</p>



<p>10.</p>



<p>And then after we finally get to the after label, then we&#8217;re just going to do, you know,</p>



<p>the rest of the instructions.</p>



<p>We&#8217;re going to exit the program.</p>



<p>We&#8217;re going to say goodbye.</p>



<p>We&#8217;re going to do whatever.</p>



<p>Okay.</p>



<p>So say there never was less than 10.</p>



<p>And then regardless, here&#8217;s like the ending label that we were conditionally jumping to.</p>



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



<p>Make run.</p>



<p>So we&#8217;ll enter five.</p>



<p>we&#8217;ll enter five notice how it says your number was greater than zero and your</p>



<p>number was less than ten let&#8217;s do an 11 I guess it says your number was greater</p>



<p>than zero but it does not say that our number was less than ten because 11 is</p>



<p>not less than ten if I do a negative three here it says that our number was</p>



<p>less than ten but it doesn&#8217;t say that it&#8217;s greater than zero so we have like a</p>



<p>lot of decisions that we could make right like imagine if instead of just</p>



<p>based on something happening, you could call a full function or skip a bunch of extra code,</p>



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



<p>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>And we&#8217;re going to ask, you know, is R12 equal to five?</p>



<p>So this is going to be the jump not equal branch.</p>



<p>And again, you know, if you wanted to say jump, if it&#8217;s equal to the place where we</p>



<p>and then maybe like right after that fall through and just do an unconditional jump you can</p>



<p>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>your number was equal to five and so basically if it was not equal we should have another label here</p>



<p>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>R12 with 5 and if it&#8217;s not equal to 5 then jump to this label down here which just means</p>



<p>let&#8217;s say goodbye and not actually say that their number was equal to 5.</p>



<p>But if it was equal to 5 then this JNE conditional branch won&#8217;t actually jump anywhere.</p>



<p>Execution will fall through to the next statements which is just printing the message that their</p>



<p>number is equal to 5 and then when that&#8217;s done it&#8217;ll fall through to this other label</p>



<p>and eventually fall through to the return statement.</p>



<p>the whole thing already nice that was easy um let&#8217;s do another run and um let&#8217;s enter the</p>



<p>integer one it says one is uh greater than zero and one is less than 10</p>



<p>greater than zero and less than 10 is there any way i can get nothing to print out</p>



<p>greater than zero and less than 10</p>



<p>you don&#8217;t think i can do it let me try it nine no</p>



<p>why did I even put 9 okay yeah it&#8217;s always gonna say 0 or 10 okay so let&#8217;s</p>



<p>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>5 now we should get all those messages your number was greater than 0 your</p>



<p>number was less than 10 your number was equal to 5 and if I just do a 6 in there</p>



<p>again it doesn&#8217;t print your number was equal to 5 and we have to enter an</p>



<p>actual 5 for that so that&#8217;s the that&#8217;s the basics of</p>



<p>conditional branching oh there&#8217;s one other thing that I wanted to make sure</p>



<p>in my previous video where we only talked about jumps bear in mind that</p>



<p>these conditional branching statements or instructions you know je j any all</p>



<p>these things they have a limited range of jumps of where they can jump to so if</p>



<p>you have a gigantic program and you&#8217;re trying to jump very very very far away</p>



<p>based on a condition you might have an assembler error where your program won&#8217;t</p>



<p>even compile won&#8217;t even assemble telling you i think the message is i wrote it</p>



<p>I wrote it down somewhere jump out of range coming from the assembler that basically means that these</p>



<p>instructions can only jump about 128 bytes away from whatever instruction they are at or whatever</p>



<p>memory location they are at so you know after your assembler assembles your program and after</p>



<p>your linker links your program and you just have like a binary you know if you were to inspect</p>



<p>all the instructions inside of your executable your binary you would see that they have offsets</p>



<p>offsets, right? You know, like one instruction is basically eight bytes away from the one that</p>



<p>comes right after it. But some instructions are a lot further away. So if you&#8217;re trying to jump</p>



<p>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>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>make this part of the code example. But just imagine we have, you know, jump equal to some</p>



<p>and maybe I&#8217;ll do a comment very very far away and here&#8217;s the label some label right</p>



<p>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>jump out of range assembler error then basically you can just make some other labels here you can</p>



<p>say uh you can say short jump true and short jump false you know just make up some sort of a label</p>



<p>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>is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p>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>To the false label. So now you&#8217;re definitely jumping in either case and both of these labels are close by</p>



<p>So then I can pretty much just have the close by true label do an unconditional</p>



<p>Jump to some label and that overcomes the limitation of branching instructions</p>



<p>So, you know JMP the regular jump instruction</p>



<p>It has an unlimited jump that it can do it can go just to like very very very far away instructions</p>



<p>And then for the false, you know, I don&#8217;t know, do whatever you want and then jump wherever you want.</p>



<p>It doesn&#8217;t really matter.</p>



<p>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>so it&#8217;s sort of like two jumps, you know, like a short jump and then a long jump,</p>



<p>then you can overcome that limitation.</p>



<p>Again, bearing in mind that some label is supposed to be very, very far away and sort of unreachable.</p>



<p>Okay, so let me just double check that I talked about everything that I wanted to.</p>



<p>We looked at the branching instructions, the sign in this, I drew a diagram for you.</p>



<p>We solved the jump out of range error.</p>



<p>Okay, so that&#8217;s, I think that&#8217;s basically it.</p>



<p>Hopefully you feel like an expert now at conditional branching and how to make decisions.</p>



<p>In future videos, I&#8217;ll talk about how to implement an if else block and how to implement loops</p>



<p>and all that stuff.</p>



<p>decisions in yasm x86-64 assembly and conceptually you can apply this to other assembly languages too</p>



<p>so thanks for watching my video i hope you learned a little bit of stuff and had a little bit of fun</p>



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



<p>hey everybody thanks for watching this video again from the bottom of my heart i really appreciate it</p>



<p>i do hope you did learn something and have some fun uh if you could do me a please a small little</p>



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



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



<p>So please do me a kindness and subscribe.</p>



<p>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>or followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p>happens every single time so you could do it as a nice favor to me or you could</p>



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



<p>subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p>you look at the middle of the screen right now you should see a QR code which</p>



<p>you can scan in order to go to the website which I think is also named</p>



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



<p>where you can just kind of like see all the videos I published and the</p>



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



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



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



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



<p>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>appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



<p>as as i fade into the darkness which is coming for us all</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/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 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>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>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>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>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>Code shown works on Linux x86-64 with YASM/NASM syntax. Grab the concepts and apply them anywhere.</p>



<p>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>Thanks for watching!</p>



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



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



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



<p>In this video we&#8217;re going to talk about implementing simple if-else blocks in YASM x86-64 assembly.</p>



<p>Although if you&#8217;re writing in a different assembly language, this video will probably</p>



<p>still be useful to you because I&#8217;m going to explain the design pattern or how we can achieve</p>



<p>that at the assembly level.</p>



<p>So x86-64 YASM assembly, also known as AMD64 YASM assembly.</p>



<p>So if you have not seen my other videos about how to do conditional branching in the first</p>



<p>place, you probably want to go check that first because that knowledge is required for</p>



<p>this video.</p>



<p>There&#8217;s also a lot of other stuff that I&#8217;m just not going to explain in this video, such</p>



<p>as creating a make file, you know, compiling and linking your executable and so forth.</p>



<p>So see my other videos where all those concepts are explained already.</p>



<p>how to implement a simple if-else block.</p>



<p>So for starters, maybe let me draw a little bit.</p>



<p>Suppose we had, let me get rid of this thing</p>



<p>and then I&#8217;ll just do a regular notepad.</p>



<p>Suppose we had a higher level language,</p>



<p>just so you know what I&#8217;m talking about.</p>



<p>Suppose we had a higher level language</p>



<p>and we wanted to say if, you know, some expression is true.</p>



<p>We wanted to say if expression is true,</p>



<p>then print, you know, it was true.</p>



<p>Anybody remember that old movie, Little Nicky?</p>



<p>Somebody got exploded and then the guy next to him goes it&#8217;s not true</p>



<p>Okay, it was not true</p>



<p>So this is the basic idea of what we&#8217;re going to implement in assembly</p>



<p>I&#8217;m going to write a full program for assembly to show you this but um</p>



<p>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>You obviously know how to use if-else blocks at this point hopefully</p>



<p>And now we&#8217;re going to just try to figure out how to implement them in assembly</p>



<p>So it&#8217;s important to understand that really under the hood</p>



<p>really under the hood there&#8217;s a bunch of stuff happening surprise right okay so</p>



<p>first off we look at this expression which could be I don&#8217;t know let&#8217;s say</p>



<p>five is greater than ten or a is equal to B or whatever it is that you put in</p>



<p>there you could make a very complicated expression we&#8217;re going to use simple</p>



<p>expressions for this video so if we&#8217;re comparing a to B we&#8217;ll end up using the</p>



<p>compare instruction remember there are two steps to conditional branching and</p>



<p>in YASM we first use the compare instruction against two operands and then that will end up</p>



<p>filling up the rflags register so that we can later conditionally jump based on the results</p>



<p>of the comparison. So we do a comparison and then you can imagine that the beginning of this</p>



<p>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>line here to indicate that there is a scope from line four to six indicating all this code in here</p>



<p>in here is executed only if the if statement was true and then all of this other stuff is executed</p>



<p>only if the original comparison was false right so you can imagine now label is something like</p>



<p>my if was true so we can make a label for where that body starts and another label for where the</p>



<p>label is something like if was false and then what will happen is we can use a</p>



<p>conditional jump instruction after we do our comparison maybe I should put to</p>



<p>compare right here oh I&#8217;m using I&#8217;m using assembly style comments I should</p>



<p>be using a C style comments if I&#8217;m actually writing C++ here let me just do</p>



<p>that okay so this is the comparison instruction here and then here&#8217;s the</p>



<p>here and then here&#8217;s the beginning of the body of stuff to execute if it was true you can imagine</p>



<p>that there could be many statements here um if it was true or if it was false</p>



<p>just to prove to you that we can execute like a full body of stuff so we have a label that</p>



<p>designates when that body starts and then we have another label designating when the else body</p>



<p>starts and then we should have a label that uh that designates when the whole thing is over</p>



<p>label is something like if and or if is done or something like that so basically what we want to</p>



<p>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>and then we&#8217;ll do a conditional jump where are we going to conditionally jump well if the comparison</p>



<p>was true label. And then what will happen is execution will fall through.</p>



<p>When it reaches the end, we want to have another jump statement that unconditionally jumps</p>



<p>to the end of the if statement. If we didn&#8217;t, then whenever the expression was true,</p>



<p>we would end up executing all of the true statements and then it would fall through</p>



<p>to all of the false statements or the not true statements. So we have to have many jumps in here</p>



<p>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>expression was false then our jump instruction is going to jump you know over the true body so</p>



<p>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>you know sometimes a good idea if you have another jump here that just jumps to the end but you can</p>



<p>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>you know place where we&#8217;re finished with everything which means we don&#8217;t really</p>



<p>need an unconditional jump at the end of the else body we can just let the</p>



<p>execution fall through so maybe if I can draw a little diagram here I&#8217;ll say I</p>



<p>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>was false we jump to one place and if it was true we jump to another place</p>



<p>we jump to another place.</p>



<p>I hope you&#8217;ve already started to understand this by now.</p>



<p>So we&#8217;ll say if the expression is true, we jump here.</p>



<p>If the expression is false, we jump over here.</p>



<p>And so true would be saying, let&#8217;s jump to the if was true.</p>



<p>So we&#8217;re going to jump to if was,</p>



<p>I&#8217;ll put a T there because I&#8217;m running out of space.</p>



<p>If was true.</p>



<p>was I&#8217;ll put an F false and then at the end of the true we jump to the done</p>



<p>label so I&#8217;m just gonna put done maybe down here</p>



<p>so at the end of the true we just unconditionally jump to the done area</p>



<p>and then at the end of the false we jump unconditionally to the done area as well</p>



<p>here&#8217;s something interesting though when we have a comparison instruction and</p>



<p>conditional branch instruction let&#8217;s say uh let&#8217;s say a equals b we did that comparison and then we</p>



<p>wanted to jump into the true area if a equals b was true so that means we&#8217;ll say jump</p>



<p>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>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>but the conditional branching instructions they only jump to one</p>



<p>potential place or fall through so if the comparison was false meaning if those</p>



<p>two things were not equal then we&#8217;re not going to actually be able to jump to a</p>



<p>different label we&#8217;re going to simply fall through to the next instruction so</p>



<p>in that case the very next instruction would get executed let&#8217;s just put a jump</p>



<p>was</p>



<p>false.</p>



<p>Meaning</p>



<p>we compare A and B</p>



<p>and if the two things are equal</p>



<p>we&#8217;ll jump into the true block.</p>



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



<p>which</p>



<p>that&#8217;s a very poorly written J</p>



<p>but we&#8217;re going to unconditionally jump to false.</p>



<p>So if we did not</p>



<p>jump to the true area we fall through to the next</p>



<p>instruction where we will always</p>



<p>jump to false and that implements the diagram</p>



<p>that implements the diagram that you see up above.</p>



<p>I mean, you know, if we have if statement, we compare,</p>



<p>maybe I&#8217;ll do, you know, A equals B.</p>



<p>We jumped to true, if was true.</p>



<p>And otherwise we end up falling through</p>



<p>and then jumping to if was false.</p>



<p>And then at the end of both of those,</p>



<p>we have an unconditional jump instruction</p>



<p>that jumps to the done.</p>



<p>So I&#8217;ll put JMP down here just to let you know,</p>



<p>you know, at the end of each of these blocks,</p>



<p>that they are jumping out of themselves at the very end to the done area maybe</p>



<p>I&#8217;ll put an arrow here so that we know both of these jumps end up jumping to</p>



<p>the done area that&#8217;s the basic idea for how to implement an if-else block a very</p>



<p>basic one we&#8217;re gonna do more complicated ones later but for now we</p>



<p>kind of have the idea down I think let&#8217;s look at some code dang I blew 10 minutes</p>



<p>already on that okay so I&#8217;m gonna copy paste some code from my solution here</p>



<p>code for my solution here this again this is not like a beginner&#8217;s assembly</p>



<p>video if you need to learn how to write assembly in the first place how to</p>



<p>compile you know link create a make file and so forth you need to see my other</p>



<p>videos first but for now we&#8217;re just going to assume that you know how to</p>



<p>make a data section in Yasm and we&#8217;re gonna say let&#8217;s make a bunch of strings</p>



<p>so first I&#8217;m gonna ask the user for an integer and then I&#8217;m gonna make a bunch</p>



<p>of decisions like I&#8217;m gonna do you know an if-else block to test what kind of</p>



<p>what kind of number they put you know did they make a number that uh</p>



<p>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>but basically we&#8217;re going to print something if their number was equal to something else</p>



<p>we&#8217;re going to print uh something if their number that they inputted was equal to five we&#8217;re going</p>



<p>to print something else if their number was equal to six we&#8217;re going to print again something else</p>



<p>a bunch of stuff and then wait isn&#8217;t this the complicated example oh no i think i am using code</p>



<p>for my more complicated example else less than 10 i don&#8217;t know maybe this is the simple one</p>



<p>let&#8217;s double check i guess if i put more complicated code in here you&#8217;ll probably be happy</p>



<p>but whatever i thought this was going to be a simple example so i&#8217;m just defining strings at</p>



<p>codes stuff that is covered in other videos now i&#8217;m going to start my text section so my text</p>



<p>section begins with a declaration that i&#8217;m going to use two external functions so this video is</p>



<p>not about this library that takes input and sends output i have other videos for that but basically</p>



<p>i&#8217;m just using a library that lets you type an integer into the terminal and then it will print</p>



<p>a different number to the terminal for you so you can imagine if you wanted to follow along</p>



<p>imagine if you wanted to follow along with this code at home and you don&#8217;t</p>



<p>have this library you can just hard code your numbers just to prove to yourself</p>



<p>that you can get it to work or you can use a different library if you have a</p>



<p>hybrid program and you&#8217;re linking against GCC you can just use scanf and</p>



<p>printf pretty much those take a little bit more work to do but you can do it</p>



<p>anyway so our entry point is going to be called if tester it&#8217;s a function called</p>



<p>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>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>So it&#8217;s just a little label that we can jump into or in this case call into.</p>



<p>Let me go down to the bottom of that.</p>



<p>We&#8217;ll put a return statement at the very end.</p>



<p>very end so now this is officially a function and notice how i made a note here that says r12 is</p>



<p>the user&#8217;s inputted integer so that means i&#8217;m going to be using r12 and since r12 is designated</p>



<p>in the abi the application binary interface as callee saved or callee preserved that means i</p>



<p>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>prologue which my favorite book also calls the prologue and then I&#8217;m going to</p>



<p>say epilogue and so now we have a function that basically doesn&#8217;t do</p>



<p>anything but we can at least jump into it let&#8217;s see if this compiles as is I</p>



<p>think it probably will yeah so the driver just says hello that&#8217;s the C++</p>



<p>program with the actual main function and it calls my if tester function but</p>



<p>nothing happens the if tester function returns control to the driver and then</p>



<p>the driver just says okay i got control back so nothing really happened so now let&#8217;s ask the user</p>



<p>for some input so again this is not a video about this library or how to print with this with system</p>



<p>calls see my other videos if you need help on that but basically we&#8217;re going to print a message to</p>



<p>the user hey please input an integer and then we&#8217;re going to call on a library function that</p>



<p>lets them type in a number and then returns it to us in rax we&#8217;re then going to store it</p>



<p>well just basic stuff right so if I run this again it&#8217;s going to ask for an</p>



<p>integer please enter an integer if I can type that and then nothing else happens</p>



<p>okay now we can kind of start making more decisions so I&#8217;m going to let&#8217;s see</p>



<p>if test if test if test okay so what I need to do now is run another function</p>



<p>called if tests several times and I&#8217;m going to compare the number that the</p>



<p>times and I&#8217;m going to compare the number that the user inputted so remember r12 is the user&#8217;s</p>



<p>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>to load the number five as the second argument of a function call probably not a great idea to hard</p>



<p>code numbers in your assembly you should probably define them up in the globals or the the data</p>



<p>section at least as just regular defines and not numbers in memory but I&#8217;m going to just keep these</p>



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



<p>Where the heck is that? Oh, dude?</p>



<p>Again, this is not a video about the basics</p>



<p>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>But I have a function called</p>



<p>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>handles the CRLF call. Now let&#8217;s make another function called if test.</p>



<p>So I&#8217;m going to start that by designating its label right after this block of</p>



<p>code here, maybe before CRLF.</p>



<p>So we have like a basic if test function and here&#8217;s my prototype just to remind</p>



<p>myself what I&#8217;m going to be doing.</p>



<p>It&#8217;s going to take an input and it&#8217;s going to take another input for a test</p>



<p>against me. So the first one is like the user&#8217;s input.</p>



<p>The second one is the number that I want to test it against.</p>



<p>number that I want to test it against they&#8217;re both longs which means they are both integers</p>



<p>which means the incoming arguments are going to be rdi and rsi if you&#8217;re respecting the abi</p>



<p>and then some notes to remind myself I&#8217;m going to be using r12 and r13 inside of this function so</p>



<p>I&#8217;m going to start by putting a return statement there since that is what it takes to make a label</p>



<p>into a return into into a function then I&#8217;m going to preserve the callee saved registers again if</p>



<p>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>that they are not ruined for the caller we call this the prolog then at the very bottom of the</p>



<p>function we have the epilog which just restores uh the registers in reverse order you&#8217;ve got to</p>



<p>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>function that can get called it doesn&#8217;t do anything let me double check that the program still actually</p>



<p>works. 66 and nothing happens. We just printed CRLF a bunch of times. Okay, so now we&#8217;re ready</p>



<p>to continue. So let&#8217;s grab the function arguments. Remember we were going to use R12 and R13 for the</p>



<p>user&#8217;s input and the number we will test against. Those came into our function with RDI and RSI.</p>



<p>So I&#8217;m just going to copy those two incoming arguments into R12 and R13. And you&#8217;re supposed</p>



<p>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>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>arguments and then I print a begin message just to let the user know that we&#8217;re going to start</p>



<p>you know making tests against our number so this is just basically a message saying hey begin the</p>



<p>if test and then print what we&#8217;re going to check against so the next thing is</p>



<p>I&#8217;m going to let the user know what the second incoming argument was.</p>



<p>If you look at R13 here, that was RSI, which was the second argument.</p>



<p>So the test against me number.</p>



<p>So we&#8217;re going to check the user&#8217;s input against whatever we called for the second argument.</p>



<p>So I just wanted to print it out.</p>



<p>You know, like we&#8217;re testing your number against whatever.</p>



<p>So that means I need to make another call to my little printing library here.</p>



<p>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>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>Then we&#8217;re gonna print a special message only if something actually happens</p>



<p>So we&#8217;re gonna implement the if else block in a moment. Let me just run this real fast</p>



<p>and then it says we&#8217;re calling the function three times and we&#8217;re saying the basic test has begun</p>



<p>we&#8217;ll test against this number five so we&#8217;re testing your input against five and then six and</p>



<p>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>going to print a special message only if the user entered the right number so first off remember we</p>



<p>see if else block and converting it into assembly so i&#8217;m sort of placing that in comments for you</p>



<p>so the comparison instruction i i didn&#8217;t want to put the r12 equals r13 inside of the same comment</p>



<p>that lines up with the the compare instruction because the compare instruction as i&#8217;ve said in</p>



<p>other videos already it doesn&#8217;t actually check to see if something&#8217;s equal it just makes a bunch of</p>



<p>called r flags with information that we can later use to decide if the two things were equal or not</p>



<p>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>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>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>with any scheme you want for your labels but for me when i have when i have sub labels inside of a</p>



<p>when i have sub labels inside of a function i like to just suffix the function&#8217;s name with an</p>



<p>underscore and then start thinking of sub labels after that so everything&#8217;s like kind of clean</p>



<p>there&#8217;s less chance of overlap in labels if you have like a giant module with tons of functions</p>



<p>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>called was equal meaning you know this evaluated to true that means that should be the true part</p>



<p>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>the true block so you know if was true in the first example that we talked about so i&#8217;m going</p>



<p>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>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>did not jump away it means that those two things are not equal so i said i should jump to the</p>



<p>I should jump to the else block or the false block.</p>



<p>Wait, do I have else in this example?</p>



<p>Oh, okay, okay.</p>



<p>This first thing that we&#8217;re looking at is only if.</p>



<p>So we don&#8217;t even have an else block yet.</p>



<p>We&#8217;re going to do that as the second example.</p>



<p>So we&#8217;re basically going to jump to the done area</p>



<p>if we didn&#8217;t jump into the true area.</p>



<p>And then at the end of the true area,</p>



<p>we can either jump to the done area</p>



<p>or we can let the execution fall through.</p>



<p>fall through okay so now that means i need if was equal and if i just copy paste a big giant</p>



<p>block of code and try to explain it to you real fast let&#8217;s do this</p>



<p>okay so we were going to jump to if was equal if r12 was equal to r13 and then um</p>



<p>we uh have this you know label here and notice how i&#8217;ve kind of like put a brace here indicating</p>



<p>like put a brace here indicating hey this is the beginning of the true block body you should</p>



<p>consider doing this too when you&#8217;re first learning and even after you&#8217;ve learned because let&#8217;s face</p>



<p>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>to say hey your number was equal to you know whatever and then uh i&#8217;m going to actually print</p>



<p>the let&#8217;s see r13 number so that was uh i think the number to compare against the test against</p>



<p>against me number so that means here in this message we&#8217;re going to say hey your number was</p>



<p>equal to the number that we tested against so then otherwise let&#8217;s see or sorry after that</p>



<p>we&#8217;ll print the suffix of the message um and so you know i just i just like to make pretty</p>



<p>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>oh i should have put suffix instead of the number two that would have been better so what i wanted</p>



<p>to do is say your input was equal to and then print the number and then after that print an</p>



<p>exclamation just to prove to you how easy it is to to make a pretty message that&#8217;s formatted nicely</p>



<p>for the user um or your professor or whoever so uh you know you basically just print a number and</p>



<p>then you or sorry you print the prefix and then you print the number and then you print the suffix</p>



<p>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>can basically just say all right now we&#8217;re done with the if so the next instruction that follows</p>



<p>is the done area again looking back at the example here that would be sort of like here after the</p>



<p>whole entire block was finished we&#8217;re ignoring the else in this code but you can imagine we&#8217;ll do</p>



<p>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>to the done area and if the user&#8217;s uh number did not match then we just immediately jump to the</p>



<p>done area so that we don&#8217;t do the true area and then we write comments to ourselves to help us</p>



<p>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>area is like we could put another comment if we want we could say like this is done but i kind of</p>



<p>think like i kind of think the label is self-explanatory so let&#8217;s see if this worked it&#8217;s</p>



<p>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>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>number six and you can see it did not match the number five and it did match the number six so</p>



<p>we got that true block executing when we called on that function where the number to compare to</p>



<p>nothing there. So now we know simple if blocks.</p>



<p>The next thing we&#8217;re going to do is if else blocks.</p>



<p>All right.</p>



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



<p>Not sure if I&#8217;m going to cut the video and split this up into multiple parts.</p>



<p>Probably would have been a smart idea.</p>



<p>Let me know if I ended up doing it.</p>



<p>We&#8217;re at about 25 minutes now.</p>



<p>But anyway,</p>



<p>so now we&#8217;re going to look at if else blocks.</p>



<p>if else blocks.</p>



<p>So I&#8217;m going to start off with another function.</p>



<p>I&#8217;m going to call it if else test.</p>



<p>So here&#8217;s the tester.</p>



<p>It called on if test.</p>



<p>And then I&#8217;m going to, you know, I had the if test function that I did previously.</p>



<p>And so now we&#8217;re just going to make an if else test function.</p>



<p>The if else test, it just has one input, one argument for input.</p>



<p>And we&#8217;re going to just sort of compare it against different values.</p>



<p>of compare it against different values we&#8217;re not going to call this multiple times with different</p>



<p>values to compare against so that means let&#8217;s see we only need to use one register so</p>



<p>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>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>some of the basic stuff that i&#8217;m that i&#8217;m skipping over see my other videos where i explained</p>



<p>to know to actually have this kind of a program. So I&#8217;m going to have a function that enters and</p>



<p>it returns at the end. It uses R12 so we will preserve it with a push pop pair.</p>



<p>And then the first thing that I should do is grab the user&#8217;s input from the first argument.</p>



<p>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>somebody remind me that the first part of the program has to actually call this function or</p>



<p>nothing will happen. So we&#8217;re going to say hello. That&#8217;s just a simple message that we talked about</p>



<p>before. And then now we&#8217;re going to actually implement the if else block. So it&#8217;s like a</p>



<p>little bit more complicated than just the simple if block. It&#8217;s going to be this whole example that</p>



<p>we talked about before. Let me see. Maybe I should add the calls to this block real fast. So we have</p>



<p>to this block real fast so we have the if tester and then run the complex if else tests</p>



<p>right before r12 okay let me just double check that i&#8217;m making a call at the right spot here</p>



<p>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>to um the if else test function that we&#8217;re just making right now</p>



<p>Okay, so we have that.</p>



<p>And let me find that source again real fast.</p>



<p>If else test, we got the prologue.</p>



<p>We take their input and then we say hello.</p>



<p>Okay, so now we need another label that begins the if block.</p>



<p>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>And then we&#8217;re going to say something if it was.</p>



<p>And otherwise, we&#8217;re going to do an else block on that.</p>



<p>block on that so again i like to write my comparison instructions with a blank expression</p>



<p>in terms of the c equivalent comment so notice how the we&#8217;re checking to see if r12 is equal to</p>



<p>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>block if it equals five so that&#8217;s why i put that there the comparison instruction pretty much just</p>



<p>compares r12 with five sets a bunch of values into the r flags registers so that we can later</p>



<p>so that we can later conditionally jump if we want to.</p>



<p>So basically we&#8217;ll jump to the true place if that was true.</p>



<p>And then if not, execution falls through to the next statement,</p>



<p>which will just jump to the else place.</p>



<p>So we need more labels is what I&#8217;m saying.</p>



<p>So now we need a body for if the statement was true</p>



<p>or the expression was true,</p>



<p>we want to be able to execute the true portion of the if block.</p>



<p>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>could imagine you know making a better label instead of equal five you could say first if</p>



<p>first else if complicated block true scope or true block or something but i just put equal to five</p>



<p>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>into this label equal five if r12 was indeed equal to five so that means if it was we execute</p>



<p>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>to finish the the if else block again just just to clarify</p>



<p>we do the comparison first and if the comparison was true then we&#8217;ll jump into the true area so</p>



<p>you know we&#8217;ll jump into the true area and then all of these uh instructions get executed</p>



<p>instructions get executed but if we don&#8217;t have a way to jump out of that block then whoops</p>



<p>all of the else statements are going to get executed too right so we don&#8217;t want that</p>



<p>we don&#8217;t want to execute both the true and the false statements we want to have a jump instruction</p>



<p>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>you know jump to the place where it&#8217;s just all over and finished</p>



<p>Okay, so now I&#8217;m going to look at, whoops, turn that off.</p>



<p>So we have equal five.</p>



<p>So we&#8217;re going to jump to if else test if done.</p>



<p>Let me just double check to make sure I&#8217;m not forgetting anything.</p>



<p>Oh, we need to jump here.</p>



<p>Basically meaning we&#8217;re going to jump into the else portion.</p>



<p>So here we covered jumping into the true portion, you know, the regular top block.</p>



<p>Now here we&#8217;re going to jump into the else portion.</p>



<p>We need a label and some code for that.</p>



<p>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>hectic in my brain here the copy pasting is worse than actually writing the program</p>



<p>so uh if the comparison was not equal like if r12 was not equal then execution falls through</p>



<p>to line 216 and then we unconditionally jump to the l6 begin area which is like down here</p>



<p>like down here and then we&#8217;ll print something else.</p>



<p>We&#8217;ll, oh, we&#8217;ll, we&#8217;ll check again.</p>



<p>So we&#8217;re doing like, um, if else, if else.</p>



<p>Okay. So originally when I was talking about the, uh,</p>



<p>if else block, I didn&#8217;t do a,</p>



<p>I didn&#8217;t do like a very complex if else statement.</p>



<p>We&#8217;ll say a is greater than B something like that.</p>



<p>So I&#8217;ll just put a more code here just so you know that we can do if else,</p>



<p>if else blocks but just you know again keep in mind that every scope here that you&#8217;re going to</p>



<p>try to run based on some condition you just make it its own label and make sure that at the very</p>



<p>end of the scope you jump away so that you reach the very end of all this stuff because just as a</p>



<p>quick review if we are just writing in c or c plus plus only one of these blocks is going to execute</p>



<p>right like if a is equal to b then only that first block will execute the second and third blocks</p>



<p>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>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>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>know imagine there are more statements there and when that block is done then we will jump to line</p>



<p>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>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>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>six we jump if they were equal to the else if equals true block and if not execution falls</p>



<p>Less than 10 begin block. So this is going to be like really complicated</p>



<p>How many lines did I actually I think I got excited when I wrote this</p>



<p>Okay, else if equal six true</p>



<p>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>All right, so we&#8217;re gonna do this I</p>



<p>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>Else if equals six true, okay</p>



<p>equals six true okay so what&#8217;s happening here again um uh so at this point you know the first</p>



<p>if uh expression was not true r12 was definitely not equal to five so we jumped down to else if</p>



<p>equal six begin which was here and so then we just make another comparison to see well okay it was</p>



<p>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>to yet another scope.</p>



<p>If it was false, we go down to line 237</p>



<p>and jump to yet another scope.</p>



<p>So here is what will get executed</p>



<p>if R12 was indeed equal to six,</p>



<p>and then we&#8217;re just basically gonna say it to the user</p>



<p>and then jump to the done label,</p>



<p>meaning like we&#8217;re totally done with our if else block.</p>



<p>Notice how the first if here,</p>



<p>when it was totally finished, it jumped to the done area.</p>



<p>this else if block is also jumping down to the if done area.</p>



<p>So eventually we&#8217;re going to need that label.</p>



<p>Okay.</p>



<p>So then next we&#8217;re going to check to see if the user&#8217;s input was equal to a 10.</p>



<p>So like kind of the same thing here.</p>



<p>We&#8217;re going to do another copy paste and we&#8217;re going to say, all right.</p>



<p>So if the user&#8217;s input was not equal to, let&#8217;s see,</p>



<p>if it was not equal to five, then we jump down here for our next comparison.</p>



<p>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>the less 10 begin line which is like all the way down here we do another comparison to see all right</p>



<p>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>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>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>Anyway, so if that statement is true, if R12 was indeed less than 10, then we jump to this block</p>



<p>and we basically just say that to the user and then we jump to the done. So finally, notice how</p>



<p>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>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>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>an if five else if six else if less than 10 so maybe i could do something like this um if r12</p>



<p>r12 I think I said six just now right hopefully I actually did say six</p>



<p>otherwise if r12 is less than 10 then do some stuff otherwise if nothing else</p>



<p>matched then we&#8217;ll execute you know this block right here so again remember</p>



<p>every single scope has to have its own label so that you know where to jump and</p>



<p>it&#8217;s also a really really smart idea for every single scope to have you know</p>



<p>to have a little jump instruction that jumps past all of the if else if else if else stuff</p>



<p>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>blocks actually executes which is how you&#8217;re supposed to imagine c and c plus plus work</p>



<p>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>still see you know only one of these blocks ever is supposed to execute so we give the first one</p>



<p>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>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>to its scope if not we jump to the else and the else always executes if nothing else above</p>



<p>was true so that&#8217;s the basic idea here oh dear i&#8217;ve probably lost my place</p>



<p>looks like i just copy pasted less 10 true which was this right here so</p>



<p>so um less than true or less than 10 true okay so we told the user your stuff is less than 10</p>



<p>and then we jumped to the done area so that means we are probably working on the else area okay let</p>



<p>me grab that so now finally we&#8217;re going to have the else area which is going to you know finish</p>



<p>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>nothing else matched then we will end up jumping to the else area and then we&#8217;ll basically</p>



<p>just tell the user none of the conditions seemed to have applied and then even at the</p>



<p>end of the else block even though you could probably get away with just letting execution</p>



<p>fall through just to save yourself one instruction you know you could comment that out assuming</p>



<p>you were sure that the very next instruction was the beginning of the done area but otherwise</p>



<p>I&#8217;m just going to play it safe and jump directly there then we have to make the actual done</p>



<p>So, just so you know, a label doesn&#8217;t have to have any instructions.</p>



<p>We could have something like this, if else test say goodbye.</p>



<p>We could have two labels right next to each other and one doesn&#8217;t actually have instructions.</p>



<p>That&#8217;s totally fine.</p>



<p>If you jumped to the done area, then execution would just fall through to the next valid</p>



<p>instruction which could go through another label.</p>



<p>So you know, we could have a say goodbye label and an if done label.</p>



<p>if done label, I&#8217;m just going to have the goodbye stuff happening inside of the done.</p>



<p>But for clarity&#8217;s sake, you might want to keep that label in there that I just deleted.</p>



<p>And all that we need to do at the very end when we&#8217;re done with everything is just say goodbye.</p>



<p>So I&#8217;m just printing a message.</p>



<p>So just to do a quick recap here, let&#8217;s see.</p>



<p>We come in with the user&#8217;s input as R12.</p>



<p>We ask, does R12 equal five?</p>



<p>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>meaning we&#8217;ll skip past all the other blocks for the if-else block, or all the other scopes.</p>



<p>But if that did not equal 5, then we&#8217;ll fall through to that jump instruction,</p>



<p>which takes us to the next test, which is going to be our else-if equals 6.</p>



<p>So we are checking now to see if R12 is equal to 6.</p>



<p>If that&#8217;s true, we jump to this next block.</p>



<p>Why do we need to jump to a block that&#8217;s so close?</p>



<p>Well, if we don&#8217;t, then we&#8217;re going to end up definitely jumping to the next comparison</p>



<p>and you know, that wouldn&#8217;t work.</p>



<p>So we&#8217;re just kind of jumping over this unconditional jump statement and we&#8217;re saying, all right,</p>



<p>your input was definitely equal to six.</p>



<p>And then we&#8217;re jumping to the done area just past everything.</p>



<p>But if that wasn&#8217;t true, then we hit this unconditional jump that takes us to the next</p>



<p>comparison to see if the user&#8217;s input was less than 10.</p>



<p>You know, if that&#8217;s true, then we go to the true area to print another message and then</p>



<p>go to the done area.</p>



<p>If it was false, we fall through to the else jump, which will take us to here.</p>



<p>And notice how the else scope doesn&#8217;t actually make any comparisons because, you know, when</p>



<p>you have like an if else, sorry, when you have an if else if else if else if, you know,</p>



<p>any number of else if blocks, the else will always be executed if nothing above it actually</p>



<p>did execute.</p>



<p>execute so if you have an else block that means something will execute so we&#8217;re not doing any</p>



<p>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>area so all of these different scopes they&#8217;re jumping to the done area when they finish and</p>



<p>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>paste everything that i was supposed to i think so probably so let&#8217;s run the program and see if it</p>



<p>All right, it at least compiled.</p>



<p>So the basic if test, that was the first part of either this video or the previous video,</p>



<p>depending on whether I chose to split this up.</p>



<p>So let me comment out those calls real fast.</p>



<p>So I&#8217;m going to just comment out these calls real quick so we can only deal with the complicated</p>



<p>if else block.</p>



<p>So I&#8217;m going to enter like a three.</p>



<p>Whoops, let me do it again.</p>



<p>Three.</p>



<p>And so begin the if else test.</p>



<p>test your input was definitely less than 10 and then we end the if-else test so notice how only</p>



<p>one scope executed three was definitely less than 10 what other numbers did we have let me write</p>



<p>them down somewhere so I don&#8217;t forget oh they&#8217;re written down right here so we entered a three</p>



<p>which was definitely not a five and not a six so that&#8217;s why the less than 10 block executed maybe</p>



<p>only the first scope should execute so it says your input was equal to five notice how it didn&#8217;t</p>



<p>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>we have a six it will not mention the five it will not mention the ten so we did we did five</p>



<p>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>which was less than ten we can do anything that was less than ten and then the else would be if</p>



<p>And then the else would be if our input is probably like 11 or greater,</p>



<p>meaning it&#8217;s not less than 10 and it&#8217;s also not five or six.</p>



<p>Just we can see the else block.</p>



<p>So I&#8217;m going to do 11, which is the first number that should trigger the else block.</p>



<p>And it says no conditions were satisfied,</p>



<p>which was the message inside of our else block.</p>



<p>Let me just show you that again real fast.</p>



<p>This was the else block.</p>



<p>So message if else else or labeling on my port, I realize that.</p>



<p>But it basically says no conditions were satisfied.</p>



<p>The if else else.</p>



<p>I think that&#8217;s basically everything that I wanted to show you.</p>



<p>At this point, you should feel like you&#8217;re starting to become an expert at complicated</p>



<p>if else if else blocks, converting those from a higher level language into assembly.</p>



<p>assembly and I honestly recommend that you practice this like crazy while you&#8217;re trying</p>



<p>to get more used to it but you know hopefully you have everything you need at this point</p>



<p>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>when we use these branching instructions notice how I have jump equal to where&#8217;s that less than</p>



<p>yeah notice how I have a instruction jump less than the the family of conditional branching</p>



<p>conditional branching instructions that compares less than greater than less than or equal to</p>



<p>greater than or equal to the ones that I&#8217;m using here apply to signed integers and they won&#8217;t</p>



<p>necessarily work with unsigned integers and they won&#8217;t work with floats if you compare floats later</p>



<p>so just keep that in mind let me pull up my favorite book again real fast</p>



<p>where what the heck is that book oh dude where&#8217;s my document viewer document viewer can I get</p>



<p>document viewer can I get there there we go so I guess I didn&#8217;t introduce this</p>



<p>book at the beginning of this video but I probably should have this is my</p>



<p>favorite assembly book you can turn yourself into an expert with this book</p>



<p>by just on its own it&#8217;s open source and free the author gives this away he&#8217;s a</p>



<p>genius the person who wrote this book is a dr. Ed Jorgensen he&#8217;s a professor he</p>



<p>wrote this for his own classes and again it&#8217;s like a free open source book you</p>



<p>download a copy from his website if you look this up and and convert yourself</p>



<p>into an expert so let me collapse everything here and then I&#8217;ll go to</p>



<p>instruction set overview control instructions conditional control</p>



<p>instructions and I just want you to see real fast just as a recap I talked about</p>



<p>this in a previous video but when it comes to comparing things and checking</p>



<p>operands because if you want to see if two operands are equal you just check to see if</p>



<p>all of their bits are equal you don&#8217;t even really care whether they&#8217;re both integers or not</p>



<p>on the other hand notice how there is like a family here jump less than jump less than equal</p>



<p>to jump greater than jump greater than equal to that those instructions apply only if your</p>



<p>if they are unsigned or if you&#8217;re using floats for your comparisons you have to use this other family</p>



<p>of jump conditional branching instructions called jump below jump below equal jump above jump above</p>



<p>equal and i&#8217;m sure you can infer that jump below is the same thing as jump less than right jump</p>



<p>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>a different instruction uh if you&#8217;re using signed integers or floats versus sorry sorry</p>



<p>a versus sorry sorry you need to use different instructions if you&#8217;re using signed integers</p>



<p>versus unsigned integers or floats so keep that in mind the code demo that i just showed you it&#8217;s</p>



<p>for signed integers if you were going to use unsigned integers or floats you would this</p>



<p>program wouldn&#8217;t work for you you&#8217;d need to replace my my branching instructions with the</p>



<p>the unsigned versions which i mean they work the same thing you&#8217;re still going to do a comparison</p>



<p>it still works. Anyway, so after having shown you that and making sure that everything actually</p>



<p>did work, I think we&#8217;ve talked about everything that we need to for this video. So yeah, I hope</p>



<p>you feel like an expert practice this on your own, you know, write your own programs just to make</p>



<p>sure you know how to convert a higher level language block into an assembly block. And you</p>



<p>should be on your way. Thank you for watching this video. I hope you learned a little bit of</p>



<p>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>hey everybody thanks for watching this video again from the bottom of my heart i really</p>



<p>appreciate it i do hope you did learn something and have some fun uh if you could do me a please</p>



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



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



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



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



<p>So please do me a kindness and subscribe.</p>



<p>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>It just wakes me up and I get filled with joy.</p>



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



<p>So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.</p>



<p>just wake me up in the middle of the night just subscribe and then I&#8217;ll just wake up I promise</p>



<p>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>QR code which you can scan in order to go to the website which I think is also named somewhere at</p>



<p>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>all the videos I published and the services and tutorials and things that I offer and all that</p>



<p>for clarifications or errata or just future videos that you want to see please leave a comment or if</p>



<p>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>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>it would really it really mean the world to me I would really appreciate it so again thank you so</p>



<p>much for watching this video and enjoy the cool music as as I fade into the darkness which is</p>



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



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