<?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>JL JG Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/jl-jg/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/jl-jg/</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>JL JG Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/jl-jg/</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>
	</channel>
</rss>
