<?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>NASM tutorial Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/nasm-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/nasm-tutorial/</link>
	<description></description>
	<lastBuildDate>Mon, 16 Mar 2026 06:39:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>NASM tutorial Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/nasm-tutorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>x86-64 Assembly: Local Variables, Stack Frames &#038; Alignment Explained</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 16 Mar 2026 06:39:32 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[16 byte alignment]]></category>
		<category><![CDATA[assembly array on stack]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly stack allocation]]></category>
		<category><![CDATA[calling convention]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[local variables stack]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[printf crash assembly]]></category>
		<category><![CDATA[rbp rbp stack]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[stack alignment]]></category>
		<category><![CDATA[stack frame assembly]]></category>
		<category><![CDATA[sub rsp assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x64 assembly]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=317</guid>

					<description><![CDATA[<p>Learn how local variables are allocated on the stack in x86-64 assembly using YASM. See exactly how to reserve space, address variables with offsets, understand downward stack growth, and fix mysterious printf segfaults caused by 16-byte stack misalignment. Includes two alignment solutions and a complete working example with a local integer array.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained" width="1380" height="776" src="https://www.youtube.com/embed/woCCcOlopNo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Quick but deep look at local variables on the stack in x86-64 assembly (YASM). We cover allocation with sub rsp, accessing via offsets, why the stack must be 16-byte aligned when calling libc functions like printf, and two practical ways to fix alignment crashes. Includes live segfault debugging and a full working example with a local array.</p>



<p class="wp-block-paragraph">Great for anyone studying systems programming, computer architecture, or just trying to figure out why their assembly program randomly crashes on a library call.</p>



<p class="wp-block-paragraph">Like + subscribe if you want more clear, practical assembly tutorials!</p>



<p class="wp-block-paragraph">00:00 Introduction to Local Variables on the Stack<br>00:28 C++ Example of Function and Local Variables<br>01:06 Incoming Arguments in RDI vs Stack Variables<br>03:24 Pointers as Local Variables on Stack<br>04:34 Why the Stack – Recursion and Multiple Calls<br>05:18 Visualizing Multiple Stack Frames<br>09:24 How Function Returns Adjust RSP<br>10:35 Stack Grows Downward in Memory<br>11:33 Program Setup – Hybrid C++/Assembly<br>12:46 Assembly Module Overview<br>14:00 Function Prologue – Register Push &amp; Alignment<br>15:30 Allocating Stack Space for Local Array<br>17:45 Initializing Array in Loop<br>20:10 Printing Loop with printf<br>49:48 First Run – Segfault Observed<br>51:00 16-Byte Stack Alignment Requirement<br>51:55 Fix 1 – Extra Push/Pop in Prologue/Epilogue<br>53:15 Fix 2 – Push/Pop Around Each printf Call<br>55:04 Testing Different Array Sizes<br>56:52 Debugging Alignment Behavior<br>58:54 Summary – Creating Any Local Data on Stack<br>59:59 Closing Remarks &amp; Subscribe Call</p>



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



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



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Please show your support!</p>



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



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



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



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



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



<p class="wp-block-paragraph">Hi there! Today I&#8217;d like to talk to you about local variables on the stack</p>



<p class="wp-block-paragraph">in an x8664 assembly program written in YASM.</p>



<p class="wp-block-paragraph">If you don&#8217;t understand assembly or local variables or other things like that you might</p>



<p class="wp-block-paragraph">want to see my other videos but I&#8217;m just going to give you a quick example. So what am I talking</p>



<p class="wp-block-paragraph">about with local variables on the stack? For starters forget what you&#8217;re seeing here for a</p>



<p class="wp-block-paragraph">blank code page and pretend that we&#8217;re coding in c plus plus just for a moment this is an assembly</p>



<p class="wp-block-paragraph">video but pretend this is c plus plus so suppose you have c plus plus you have a main function here</p>



<p class="wp-block-paragraph">and at the end of it you know this is your entry point for your program we return zero and maybe</p>



<p class="wp-block-paragraph">above it there&#8217;s a function called f i&#8217;m putting it above because i don&#8217;t want to use prototypes</p>



<p class="wp-block-paragraph">in this video but you should probably use prototypes um maybe main calls on f and so then</p>



<p class="wp-block-paragraph">what happens? I don&#8217;t know. Maybe we have some arguments. We&#8217;ll call this integer argument A</p>



<p class="wp-block-paragraph">that comes in. If you&#8217;ve watched my other videos, hopefully you have by now,</p>



<p class="wp-block-paragraph">you&#8217;ll know that A comes into the function as RDI because that&#8217;s the first integer argument</p>



<p class="wp-block-paragraph">register. But then when we start creating local variables, we&#8217;ll say integer B equals, let&#8217;s say,</p>



<p class="wp-block-paragraph">a five and then integer C is equal to an eight for whatever reason. Maybe there will be an array.</p>



<p class="wp-block-paragraph">So I&#8217;ll call this a int array, I guess.</p>



<p class="wp-block-paragraph">We&#8217;ll say that we have 100 integers in our array.</p>



<p class="wp-block-paragraph">We could also have a pointer, int pointer p,</p>



<p class="wp-block-paragraph">and then allocate it to some kind of new memory just to prove a point.</p>



<p class="wp-block-paragraph">And then, you know, later in your function,</p>



<p class="wp-block-paragraph">you&#8217;re probably going to want to do something with your data.</p>



<p class="wp-block-paragraph">So I don&#8217;t know, maybe a, how about like b is plus equal to a,</p>



<p class="wp-block-paragraph">of C++ and then oh C++ C is plus equal to B for some reason I&#8217;m just making random nonsense up</p>



<p class="wp-block-paragraph">honestly I&#8217;m just showing you that we use our variables and then maybe you want to build an</p>



<p class="wp-block-paragraph">array so we just declared the array up here on line six but maybe you want to actually do something</p>



<p class="wp-block-paragraph">with it you want to fill it with data maybe so I&#8217;ll do size type I is zero keep going until I</p>



<p class="wp-block-paragraph">is less than as long as I is less than 100 I plus plus and now I&#8217;m going through every single index</p>



<p class="wp-block-paragraph">array maybe I&#8217;ll say the array at index i is equal to c and then we&#8217;ll just say c is plus equal to b</p>



<p class="wp-block-paragraph">and then we&#8217;ll do b plus plus just to have something in there okay so let me explain the</p>



<p class="wp-block-paragraph">parts of your program real fast if you haven&#8217;t watched my other videos please do because this</p>



<p class="wp-block-paragraph">should help a little bit anyway so a is an incoming argument like I said before that&#8217;s</p>



<p class="wp-block-paragraph">usually coming to you in the RDI register because it&#8217;s the first integer argument so we don&#8217;t need</p>



<p class="wp-block-paragraph">to worry too much about that we know that a is actually just a register in your CPU and b that&#8217;s</p>



<p class="wp-block-paragraph">created on the stack and c is created on the stack also and the array is created on the stack by the</p>



<p class="wp-block-paragraph">way I could name the array anything if I wanted to like just v I&#8217;m using the name array because</p>



<p class="wp-block-paragraph">Notice also that we have a pointer here. I just wanted to make a point that the pointers we make in our function are considered local variables.</p>



<p class="wp-block-paragraph">And that means they do sit on the stack because what I&#8217;m trying to say is that local variables that are not arguments, they sit on the stack.</p>



<p class="wp-block-paragraph">But the memory we allocated and then gave to the pointer, that&#8217;s sitting in the heap somewhere.</p>



<p class="wp-block-paragraph">So when you use the new operator or the malloc operator, or you&#8217;re just like making dynamic memory,</p>



<p class="wp-block-paragraph">that dynamic memory sits in the heap, but the pointer itself, since we just declared it here,</p>



<p class="wp-block-paragraph">that sits on the stack. That means later on when your function ends, the p pointer itself will get</p>



<p class="wp-block-paragraph">cleaned up automatically and itself won&#8217;t be leaked memory. But if you forget to clean up your memory</p>



<p class="wp-block-paragraph">here, like I did, you&#8217;ll have a memory leak. But I&#8217;m going to talk about memory leaks probably in</p>



<p class="wp-block-paragraph">some other videos somewhere else at another time. Anyway, so then we have our local variables here.</p>



<p class="wp-block-paragraph">remember B and C and array are all on the stack and well we just have our</p>



<p class="wp-block-paragraph">for loop here where we just kind of start modifying data so why would we use</p>



<p class="wp-block-paragraph">the stack let me just do a quick tutorial this is not a stack video I&#8217;m</p>



<p class="wp-block-paragraph">gonna make a stack video in the future but I&#8217;ll just do like a quick little</p>



<p class="wp-block-paragraph">rundown of what is the stack and why is it really good for function calls</p>



<p class="wp-block-paragraph">consider that sometimes F might want to call itself maybe it&#8217;s a recursive</p>



<p class="wp-block-paragraph">be like a long chain of function calls like f calls g and g calls h and h calls you know whatever</p>



<p class="wp-block-paragraph">and it just goes on and on and on as long as it&#8217;s not infinite recursion like all the functions are</p>



<p class="wp-block-paragraph">calling themselves in one circle that never ends you should be allowed to do that we should be</p>



<p class="wp-block-paragraph">allowed to have a function f that sometimes calls itself or other functions or maybe it&#8217;s called</p>



<p class="wp-block-paragraph">multiple times within our giant call stack our call graph and this will work because these</p>



<p class="wp-block-paragraph">you know a b and c and array are sitting on the stack and the stack allows you to have sort of</p>



<p class="wp-block-paragraph">different instances of variables so for example and this is just a quick thing i&#8217;m not this is</p>



<p class="wp-block-paragraph">not supposed to be a full stack tutorial here but um you know if we have like the function f</p>



<p class="wp-block-paragraph">and let&#8217;s say we call it at some point maybe uh maybe main calls f uh and then let&#8217;s let&#8217;s pretend</p>



<p class="wp-block-paragraph">f and then f calls h and then maybe h calls f and um did i write an h there no i think that&#8217;s</p>



<p class="wp-block-paragraph">supposed to be a parenthesis i&#8217;m sorry i have bad penmanship oh god it&#8217;s even worse hang on let me</p>



<p class="wp-block-paragraph">let me do another h here i&#8217;ll just do it i&#8217;ll just do a g about that f g and then uh</p>



<p class="wp-block-paragraph">F again. Let&#8217;s just pretend that we can have a call graph where F is sometimes called by something</p>



<p class="wp-block-paragraph">and sometimes called something else or maybe sometimes F is called by a different thing. So</p>



<p class="wp-block-paragraph">like I guess this was supposed to be the G function. So maybe that&#8217;s why down here was the H.</p>



<p class="wp-block-paragraph">Should I delete this video? I don&#8217;t know. You know what? I&#8217;m going to start. I&#8217;m going to delete this</p>



<p class="wp-block-paragraph">and then G calls H and then H calls F and then F calls I I guess and then</p>



<p class="wp-block-paragraph">eventually these functions start returning right oops how come my greens</p>



<p class="wp-block-paragraph">not working green there we go too late now so we can have like a really</p>



<p class="wp-block-paragraph">complicated call graph and F might appear in there multiple times and the</p>



<p class="wp-block-paragraph">Each call to F along the call graph should have its own copy, unique copy of local variables.</p>



<p class="wp-block-paragraph">So the B for this first F call, let me just do an arrow here, will have a B.</p>



<p class="wp-block-paragraph">And you can imagine it as B subscript 1.</p>



<p class="wp-block-paragraph">And down here when F executes, you can imagine that it&#8217;s got its own copy of B.</p>



<p class="wp-block-paragraph">So we can imagine this as B subscript 2.</p>



<p class="wp-block-paragraph">So these are two totally different Bs.</p>



<p class="wp-block-paragraph">If you tried to use global variables for this, it would be really, really, really hard to get the code to work.</p>



<p class="wp-block-paragraph">get the code to work and it would be really, really hard to debug.</p>



<p class="wp-block-paragraph">So that&#8217;s kind of why we have local variables.</p>



<p class="wp-block-paragraph">The stack allows us to do this.</p>



<p class="wp-block-paragraph">So what is the stack itself?</p>



<p class="wp-block-paragraph">Again, this is not a full tutorial on the stack,</p>



<p class="wp-block-paragraph">but I just want you to kind of see what&#8217;s happening.</p>



<p class="wp-block-paragraph">Imagine a data structure that kind of grows upward.</p>



<p class="wp-block-paragraph">I&#8217;ll say that here&#8217;s the floor at the bottom.</p>



<p class="wp-block-paragraph">And when you put an item onto the stack,</p>



<p class="wp-block-paragraph">the items kind of stack on top of each other.</p>



<p class="wp-block-paragraph">So imagine that this is the call to F.</p>



<p class="wp-block-paragraph">is the call to f and embedded within this little stack frame area we&#8217;ll call it a stack frame but</p>



<p class="wp-block-paragraph">actually the stack continues to grow as you create and destroy local variables within a function call</p>



<p class="wp-block-paragraph">we&#8217;ll just imagine that there&#8217;s i don&#8217;t know a mini stack sitting inside of the stack that</p>



<p class="wp-block-paragraph">contains all the local variables i&#8217;ll just put i&#8217;m not going to put a because a is an argument so</p>



<p class="wp-block-paragraph">it&#8217;s sitting in a register but we could do dude i got to learn how to how to draw with this tablet</p>



<p class="wp-block-paragraph">We got to do B and C and the array.</p>



<p class="wp-block-paragraph">I&#8217;ll put AR for array.</p>



<p class="wp-block-paragraph">Sitting in, you know, their own little spots on the stack within the major portion of the stack that is designated for that first F call.</p>



<p class="wp-block-paragraph">And then, you know, maybe F calls G and some other stuff happens.</p>



<p class="wp-block-paragraph">I&#8217;ll just pretend that a bunch of other stuff happened.</p>



<p class="wp-block-paragraph">And then eventually F is called again.</p>



<p class="wp-block-paragraph">But there&#8217;s a different, this is a different instance of the call to F.</p>



<p class="wp-block-paragraph">it&#8217;s got its own little area that is separate from the the previous call to f and again we&#8217;ll</p>



<p class="wp-block-paragraph">also have variables that we can create locally that are supposed to be separate from the original</p>



<p class="wp-block-paragraph">variables oh god that&#8217;s awful and ugly i need to maybe decrease the size of my eraser or something</p>



<p class="wp-block-paragraph">but imagine this is uh you know these are two separate copies so that&#8217;s like what i said before</p>



<p class="wp-block-paragraph">when we have like a b1 and a b2 basically they&#8217;re not going to be called b1 and b2 but they&#8217;re just</p>



<p class="wp-block-paragraph">They&#8217;re not going to be called B1 and B2, but they&#8217;re just two separate instances.</p>



<p class="wp-block-paragraph">And I just want you to know.</p>



<p class="wp-block-paragraph">So then when we start returning from functions, like when this F eventually returns, and by</p>



<p class="wp-block-paragraph">the way, I know that what I&#8217;m drawing on the right doesn&#8217;t match the code because this</p>



<p class="wp-block-paragraph">is not a function that calls itself, but just suppose that your code is a lot more complicated</p>



<p class="wp-block-paragraph">than what I drew up.</p>



<p class="wp-block-paragraph">When it eventually returns, all that happens is those items on the stack just sort of get</p>



<p class="wp-block-paragraph">not necessarily deallocated, but ignored.</p>



<p class="wp-block-paragraph">We&#8217;ll just say that they&#8217;re gone.</p>



<p class="wp-block-paragraph">They&#8217;re still sitting as junk data in system RAM somewhere.</p>



<p class="wp-block-paragraph">And in assembly talk, we know that we have a stack pointer called RSP.</p>



<p class="wp-block-paragraph">We have a register called RSP that just sort of points to the location in the</p>



<p class="wp-block-paragraph">stack that is considered the top of the stack, like the most recent piece of data</p>



<p class="wp-block-paragraph">that we have available.</p>



<p class="wp-block-paragraph">So all the other data is actually still kind of above, but we&#8217;re not pointing to it</p>



<p class="wp-block-paragraph">anymore, so we consider that it doesn&#8217;t exist.</p>



<p class="wp-block-paragraph">So then when G eventually returns, you know, we just change the stack pointer,</p>



<p class="wp-block-paragraph">Rsp, to point to that other piece of data.</p>



<p class="wp-block-paragraph">The G data frame and the other F data frame are still sitting above somewhere in memory,</p>



<p class="wp-block-paragraph">but we just ignore them, right?</p>



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



<p class="wp-block-paragraph">And that&#8217;s how we have local function call copies of all of our local variables.</p>



<p class="wp-block-paragraph">Something to note.</p>



<p class="wp-block-paragraph">Something to note, this is not a stack video, but you know, just something to note that</p>



<p class="wp-block-paragraph">even though I draw the stack visually as growing vertically up, when you actually manipulate</p>



<p class="wp-block-paragraph">the stack in assembly or just like in any language, the stack grows downward in terms</p>



<p class="wp-block-paragraph">of memory locations.</p>



<p class="wp-block-paragraph">So you can imagine, I&#8217;m trying so hard not to make this like a huge stack video.</p>



<p class="wp-block-paragraph">Imagine this is a memory location 80, we&#8217;ll say.</p>



<p class="wp-block-paragraph">You would think that memory location 81 would be the next item of the stack, or I guess</p>



<p class="wp-block-paragraph">the stack or i guess if you if you&#8217;re considering the fact that the items on the stack are quad words</p>



<p class="wp-block-paragraph">we would say it goes up to 88 but that&#8217;s not true it goes down to 72 so the memory location goes down</p>



<p class="wp-block-paragraph">even though we imagine the stack growing uh upward vertically just so you know that&#8217;s the kind of</p>



<p class="wp-block-paragraph">thing we&#8217;re going to do so what i&#8217;m going to do is just show you an assembly program where we can</p>



<p class="wp-block-paragraph">create local variables and i&#8217;m just going to show you how to create an array because this array is</p>



<p class="wp-block-paragraph">just like a bunch of integers and you can imagine it would be really easy to create only one integer</p>



<p class="wp-block-paragraph">by just imagining an example where the array is a size of one so keep that in mind and i&#8217;m not going</p>



<p class="wp-block-paragraph">to show you malloc or anything like that we&#8217;re just going to look at the local variables okay</p>



<p class="wp-block-paragraph">so for starters i have a make file here that&#8217;s just going to compile a hybrid program if you don&#8217;t</p>



<p class="wp-block-paragraph">know make files or you don&#8217;t know hybrid programs that&#8217;s okay just see my other videos i&#8217;ve explained</p>



<p class="wp-block-paragraph">The first source code file here is just driver.cpp.</p>



<p class="wp-block-paragraph">Again, this is a hybrid program,</p>



<p class="wp-block-paragraph">so I&#8217;m going to mix my C++ modules with my assembly modules,</p>



<p class="wp-block-paragraph">which is pretty cool.</p>



<p class="wp-block-paragraph">The whole point of the driver is just to contain the entry point, you know, main.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to print a hello message.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to call the real function that I&#8217;m interested in,</p>



<p class="wp-block-paragraph">which I&#8217;ve named local underscore varrs.</p>



<p class="wp-block-paragraph">And that&#8217;s going to be all the assembly stuff that we talked about.</p>



<p class="wp-block-paragraph">block so that C++ can call an assembly module that&#8217;s explained in other videos. And then</p>



<p class="wp-block-paragraph">here&#8217;s the real heart of what we got to do. Let&#8217;s write up an assembly module</p>



<p class="wp-block-paragraph">that can do local variables. Okay. So again, if you don&#8217;t know assembly, that&#8217;s okay,</p>



<p class="wp-block-paragraph">but you need to watch my other videos before you can understand this one. So I&#8217;m going to just</p>



<p class="wp-block-paragraph">copy paste some starter code here. This is Yasm assembly in x86-64. So I&#8217;ve got a data section up</p>



<p class="wp-block-paragraph">So I&#8217;ve got a data section up top and I&#8217;m just going to define some messages.</p>



<p class="wp-block-paragraph">So, you know, I&#8217;ve got like an intro message that just says, hello, I&#8217;m so and so.</p>



<p class="wp-block-paragraph">And that&#8217;s not my name, but I like those kinds of names.</p>



<p class="wp-block-paragraph">And then over here, I&#8217;m going to do some printf formatted strings.</p>



<p class="wp-block-paragraph">That&#8217;s why I&#8217;m using a hybrid program for this video.</p>



<p class="wp-block-paragraph">I don&#8217;t want to import my own personal library.</p>



<p class="wp-block-paragraph">I want you to be able to do this at home with just the GCC libraries.</p>



<p class="wp-block-paragraph">link a hybrid library you know linking against gcc instead of linking against ld again if you don&#8217;t</p>



<p class="wp-block-paragraph">know that stuff check my other videos then we&#8217;re allowed to call c functions in this case we&#8217;re</p>



<p class="wp-block-paragraph">going to call printf and we&#8217;re just going to give it the string percent lu meaning i would like you</p>



<p class="wp-block-paragraph">to print just you know an unsigned long integer so i&#8217;m going to give it a value at some point on the</p>



<p class="wp-block-paragraph">stack representing a local variable and then i want it to print as just like a long like a string</p>



<p class="wp-block-paragraph">along like a string that a human can read then after that this is the carriage return line feed</p>



<p class="wp-block-paragraph">the crlf printf won&#8217;t flush its output unless that is sitting at the very end of the string so</p>



<p class="wp-block-paragraph">i&#8217;m just going to use printf to also print my new lines and then i&#8217;m going to null terminate the</p>



<p class="wp-block-paragraph">string so that printf doesn&#8217;t freak out and try to print a bunch of stuff after the crlf and uh</p>



<p class="wp-block-paragraph">oh i this was from another video let me get rid of that we don&#8217;t really need crlf in this video</p>



<p class="wp-block-paragraph">CRLF in this video because we&#8217;re just putting it directly inside of the printf string we&#8217;re not</p>



<p class="wp-block-paragraph">making our own function to do that so then I&#8217;m going to make some defines I&#8217;m going to define</p>



<p class="wp-block-paragraph">that we&#8217;re going to have 50 integers so I&#8217;m calling this define 50 I&#8217;m calling it num integers</p>



<p class="wp-block-paragraph">and I&#8217;m saying that it has a value of 50 so I want to make an array that has 50 integers I don&#8217;t</p>



<p class="wp-block-paragraph">know maybe if you want to imagine 100 you know like the example that I just showed I&#8217;m going to</p>



<p class="wp-block-paragraph">going to define what is the integer size so i&#8217;m going to use quad words which are 64 bit integers</p>



<p class="wp-block-paragraph">so i&#8217;m just going to say that there are eight bytes per integer that will help me multiply later</p>



<p class="wp-block-paragraph">and then i&#8217;m going to decide to fill up the array on the stack with just some numbers just to prove</p>



<p class="wp-block-paragraph">that i can just to prove that we can like uh you know manipulate and and fetch the values</p>



<p class="wp-block-paragraph">of all this stuff going on in the stack and i&#8217;m going to say that the starting value is seven so</p>



<p class="wp-block-paragraph">with this we should expect to see like an array of numbers that starts with seven and it just</p>



<p class="wp-block-paragraph">kind of increases somehow then i&#8217;m going to do some system call codes we talked about that in</p>



<p class="wp-block-paragraph">a different video and then some file descriptors i don&#8217;t think we actually need anything but standard</p>



<p class="wp-block-paragraph">output but i put it in there anyway then the next thing we&#8217;re going to add is the text section</p>



<p class="wp-block-paragraph">let me just do copy paste on my solution here so here&#8217;s the text section section text</p>



<p class="wp-block-paragraph">section text in Yasm and I&#8217;m going to let my module know that I want to be able to call printf</p>



<p class="wp-block-paragraph">which is a function sitting in the GCC libraries when I link against a GCC I have the ability to</p>



<p class="wp-block-paragraph">do that that way I don&#8217;t have to come up with like a complicated printing method or use one</p>



<p class="wp-block-paragraph">of my own shared libraries or something so we can just ask printf to do everything so now here&#8217;s the</p>



<p class="wp-block-paragraph">entry point for the module it&#8217;s just a function called local VAERS I mark it as global so it&#8217;s</p>



<p class="wp-block-paragraph">it&#8217;s accessible to outside modules ie or eg driver dot cpp and then so here&#8217;s the label saying the</p>



<p class="wp-block-paragraph">function starts and here&#8217;s the return statement saying that we&#8217;re done with the function i&#8217;m not</p>



<p class="wp-block-paragraph">going to manipulate any registers inside of the function so i don&#8217;t really need to do any push pop</p>



<p class="wp-block-paragraph">to preserve them first thing i&#8217;m going to do is call a welcome let me comment this part out by</p>



<p class="wp-block-paragraph">the way i&#8217;m going to call a welcome a function and the whole job of the welcome function is just to</p>



<p class="wp-block-paragraph">is just to, you know, print a welcome message to the user.</p>



<p class="wp-block-paragraph">So nothing that I haven&#8217;t talked about before in other videos.</p>



<p class="wp-block-paragraph">So it&#8217;s just we&#8217;re using a system call to print a string.</p>



<p class="wp-block-paragraph">Okay, so with that in mind, let me open this up here and see if this is going to work.</p>



<p class="wp-block-paragraph">I just want to basically print the welcome message at this point.</p>



<p class="wp-block-paragraph">Clear and make run.</p>



<p class="wp-block-paragraph">And again, if you don&#8217;t know make files or anything like that, see my other videos.</p>



<p class="wp-block-paragraph">So this is the driver, I think, that prints.</p>



<p class="wp-block-paragraph">think that Prince maybe I should change the driver&#8217;s message to make it more</p>



<p class="wp-block-paragraph">clear hello about this is the driver and my name is whatever I&#8217;m gonna do it again</p>



<p class="wp-block-paragraph">and now it says hello this is the driver okay so that&#8217;s the CPP module and then</p>



<p class="wp-block-paragraph">here is the assembly module and then finally the driver gets control back and</p>



<p class="wp-block-paragraph">then the program exits so nothing really happened so now let&#8217;s upgrade the</p>



<p class="wp-block-paragraph">So now let&#8217;s upgrade the assembly module a little bit.</p>



<p class="wp-block-paragraph">Next thing I want to add is the actual demo function, which is going to be absolutely huge.</p>



<p class="wp-block-paragraph">So first I&#8217;m going to start off with, how about just the signature here?</p>



<p class="wp-block-paragraph">Let&#8217;s go right after the welcome module.</p>



<p class="wp-block-paragraph">And I&#8217;m just going to copy paste the signature, put a return at the end of it.</p>



<p class="wp-block-paragraph">So we&#8217;ll consider this a function that can be called.</p>



<p class="wp-block-paragraph">instruction on line 47 of the entry point and then now we have a demo</p>



<p class="wp-block-paragraph">function that is being called but it does nothing for starters you know I in</p>



<p class="wp-block-paragraph">my comments I like to put the signature of the function and I like to remind</p>



<p class="wp-block-paragraph">myself of how I&#8217;m using my registers hopefully I&#8217;m not using the same</p>



<p class="wp-block-paragraph">register in two different ways but you know sometimes it happens if it does and</p>



<p class="wp-block-paragraph">I&#8217;m able to break my function up into multiple parts I&#8217;ll probably do it</p>



<p class="wp-block-paragraph">you know with modular thinking modular programming but in this case I&#8217;m just</p>



<p class="wp-block-paragraph">using these registers it&#8217;s fine to use them they are all designated per the ABI</p>



<p class="wp-block-paragraph">as callee saved so that means the callee which is the demo function is</p>



<p class="wp-block-paragraph">responsible for preserving them so if you don&#8217;t remember that or if you don&#8217;t</p>



<p class="wp-block-paragraph">know about that see my other videos this is not an ABI video so I&#8217;m just going to</p>



<p class="wp-block-paragraph">push all of them to make sure that they&#8217;re preserved I&#8217;m gonna call this</p>



<p class="wp-block-paragraph">call this the prolog then at the end of my function i&#8217;m going to call this the epilog where</p>



<p class="wp-block-paragraph">i just restore them in reverse order because that&#8217;s the way the stack returns data to you</p>



<p class="wp-block-paragraph">the stack returns data to you in reverse order so i have to pop in the reverse register order to</p>



<p class="wp-block-paragraph">un-reverse the reversal if that makes sense pop r13 pop r12 okay so i think 15 14 13 okay</p>



<p class="wp-block-paragraph">so i got that next thing we&#8217;ll do is um let&#8217;s remember where the stack pointer started because</p>



<p class="wp-block-paragraph">started because we have our register here that we&#8217;re going to mess with let me just type rsp</p>



<p class="wp-block-paragraph">real fast so this is the stack pointer register rsp this helps all programs know where where</p>



<p class="wp-block-paragraph">they&#8217;re looking at in the stack all of your functions have to be really really careful about</p>



<p class="wp-block-paragraph">messing with the stack pointer if you do it wrong you will crash the entire program because not only</p>



<p class="wp-block-paragraph">will your local function not really know where its local variables end and begin it probably also</p>



<p class="wp-block-paragraph">It&#8217;s return address when you try to return from the function because that is also sitting on the stack</p>



<p class="wp-block-paragraph">And even if you were lucky enough to be able to jump back correctly to whoever called you</p>



<p class="wp-block-paragraph">If you messed up the stack pointer then you&#8217;ve also messed it up for any caller of you and any of their callers</p>



<p class="wp-block-paragraph">So the whole program is ruined. So we&#8217;ll start off by trying to remember where the stack pointer was</p>



<p class="wp-block-paragraph">We&#8217;ll move</p>



<p class="wp-block-paragraph">The stack pointer into the base pointer</p>



<p class="wp-block-paragraph">didn&#8217;t do here that I that I want to do we should since we&#8217;re messing with the</p>



<p class="wp-block-paragraph">base pointer and other programs sorry other functions or modules might also</p>



<p class="wp-block-paragraph">rely on the base pointer and it&#8217;s considered callie saved we probably also</p>



<p class="wp-block-paragraph">want to preserve that too so I&#8217;m going to do push RBP to basically say I would</p>



<p class="wp-block-paragraph">like to restore I would like to preserve the base pointer so I don&#8217;t mess it up</p>



<p class="wp-block-paragraph">for my callers so that means I have to restore it with the pop so RBP the base</p>



<p class="wp-block-paragraph">The base pointer isn&#8217;t necessarily a pointer to the stack, but it&#8217;s often used as kind of like a bookmark.</p>



<p class="wp-block-paragraph">So we have RBP at the front and the back there.</p>



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



<p class="wp-block-paragraph">Next thing I want to add is, so now that we&#8217;ve restored it, we&#8217;re allowed to just overwrite it because, you know, we&#8217;re kind of like keeping its value at the top.</p>



<p class="wp-block-paragraph">Then we&#8217;re restoring its value at the bottom.</p>



<p class="wp-block-paragraph">And that means we can actually mess it up in the middle if we want to, and it&#8217;ll be fine.</p>



<p class="wp-block-paragraph">let me show you real fast what happens what happens is nothing the program is</p>



<p class="wp-block-paragraph">still okay because we we restored it so now we&#8217;re using the base pointer to</p>



<p class="wp-block-paragraph">remember where the stack was now we&#8217;ve got to sort of calculate how much room</p>



<p class="wp-block-paragraph">we want to make on the stack let me show you what I&#8217;m talking about here</p>



<p class="wp-block-paragraph">remember all of our local variables are going to be on the stack and before we</p>



<p class="wp-block-paragraph">drew this thing where it was like well we&#8217;ve got like a stack sitting here and</p>



<p class="wp-block-paragraph">let&#8217;s just pretend that there&#8217;s some kind of data sitting on the stack data</p>



<p class="wp-block-paragraph">data data right if the stack pointer dude green green there we go oh I erased my</p>



<p class="wp-block-paragraph">green RSP if the stack pointer is currently pointing to this frame then</p>



<p class="wp-block-paragraph">in order for us to make room on the stack to hold our array well if the</p>



<p class="wp-block-paragraph">whole array is going to be sitting on the stack that just basically means</p>



<p class="wp-block-paragraph">have five integers suppose five suppose that we want to make five integers on the stack that just</p>



<p class="wp-block-paragraph">means we need to do five extra slots let me draw it in red here well let&#8217;s see can we get a green</p>



<p class="wp-block-paragraph">no how about a yellow my green is just having a hard time we can do it in red even though this is</p>



<p class="wp-block-paragraph">not a bad thing to do so i&#8217;ll just draw like five extra frames on top of on top of the stack here</p>



<p class="wp-block-paragraph">imagine these are all 64-bit integers and so they take eight bytes on the stack even though in our</p>



<p class="wp-block-paragraph">previous example we were using just regular ints which are 32 we&#8217;ll just say we&#8217;re going to make</p>



<p class="wp-block-paragraph">five 64-bit integers because that&#8217;s easier so they&#8217;re quad words so every frame is actually</p>



<p class="wp-block-paragraph">eight bytes and not just four bytes and it&#8217;s definitely not one byte so we make five of those</p>



<p class="wp-block-paragraph">how do we make five slots it&#8217;s pretty easy we we literally just move the stack pointer let&#8217;s see</p>



<p class="wp-block-paragraph">we just move the stack pointer to or the you know the rsp register to just say let&#8217;s let&#8217;s point you</p>



<p class="wp-block-paragraph">know further out and how do we get that number we&#8217;re just going to multiply the size of one</p>



<p class="wp-block-paragraph">integer so you know the size of one integer here we know it&#8217;s going to be eight bytes we just</p>



<p class="wp-block-paragraph">multiply that by the number of integers that we want you know that&#8217;s going to be 40 so we&#8217;ll just</p>



<p class="wp-block-paragraph">increase uh sorry decrease the stack pointers value by 40 because remember again when the</p>



<p class="wp-block-paragraph">vertically, it&#8217;s actually growing downward in memory. So we&#8217;re going to decrease by 40 there,</p>



<p class="wp-block-paragraph">at least in this drawing example. And that gives us a bunch of junk data, you know, because there</p>



<p class="wp-block-paragraph">is always going to be some kind of a value sitting at every memory location in your computer.</p>



<p class="wp-block-paragraph">It&#8217;s impossible that there is literally nothing at some memory location,</p>



<p class="wp-block-paragraph">unless you&#8217;re trying to go beyond your RAM stick. But then the system will still acknowledge that,</p>



<p class="wp-block-paragraph">you know, you&#8217;ve done that and it won&#8217;t just give you back nothing if you try to read</p>



<p class="wp-block-paragraph">nothing if you try to read.</p>



<p class="wp-block-paragraph">So there&#8217;s going to be junk data sitting on there and then we&#8217;ll loop</p>



<p class="wp-block-paragraph">through all those slots on the stack and we&#8217;ll just modify the data one by one</p>



<p class="wp-block-paragraph">so that we can control what it is instead of just printing whatever junk data we end</p>



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



<p class="wp-block-paragraph">So really, we&#8217;re just moving the stack pointer, just making room and then just</p>



<p class="wp-block-paragraph">remembering where our array is.</p>



<p class="wp-block-paragraph">We could put, you know, another frame on top if we wanted to make just like one</p>



<p class="wp-block-paragraph">integer as a local variable.</p>



<p class="wp-block-paragraph">You just got to remember where it is.</p>



<p class="wp-block-paragraph">You know, what is it?</p>



<p class="wp-block-paragraph">What is its offset?</p>



<p class="wp-block-paragraph">is it what is its offset okay so i&#8217;m going to erase this because we&#8217;re going to do a lot more than</p>



<p class="wp-block-paragraph">than five uh integers on the stack but i just want you to understand what we&#8217;re doing before we do it</p>



<p class="wp-block-paragraph">okay so the next thing i&#8217;m going to grab is a move instruction and i&#8217;m going to put it right here so</p>



<p class="wp-block-paragraph">to move the stack pointer rsp so the first thing i&#8217;m going to do is i&#8217;m going to use a temporary</p>



<p class="wp-block-paragraph">register we don&#8217;t need to preserve this in the push and pop because it&#8217;s marked as temp so we&#8217;re</p>



<p class="wp-block-paragraph">not responsible for preserving it and so i&#8217;m going to say r10 is just going to be the number of</p>



<p class="wp-block-paragraph">integers that we want to create if you recall at the top of our program here num integers is just</p>



<p class="wp-block-paragraph">50 okay so then the next thing that i&#8217;m going to grab is well i&#8217;m not going to grab it i&#8217;m going</p>



<p class="wp-block-paragraph">it I&#8217;m going to straight multiply by integer size so again if you look at integer size that&#8217;s going</p>



<p class="wp-block-paragraph">to be eight because we&#8217;re using quad words for our integers so we&#8217;re really just going to take</p>



<p class="wp-block-paragraph">50 times 8 whatever number that is is that 400 tell me in the comments if that&#8217;s a right or wrong</p>



<p class="wp-block-paragraph">and so uh you may or may not know if you don&#8217;t see more videos see more textbooks you may or may not</p>



<p class="wp-block-paragraph">instruction just multiplies two numbers.</p>



<p class="wp-block-paragraph">If you use the three operand format,</p>



<p class="wp-block-paragraph">then the last two operands get multiplied</p>



<p class="wp-block-paragraph">and the results stored in the first operand.</p>



<p class="wp-block-paragraph">But if we use the two operand format like I&#8217;ve done here,</p>



<p class="wp-block-paragraph">then both of those operands get multiplied</p>



<p class="wp-block-paragraph">and then the result gets stored in the first operand.</p>



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



<p class="wp-block-paragraph">R10 should hold the number of memory locations</p>



<p class="wp-block-paragraph">that we should move the stack pointer</p>



<p class="wp-block-paragraph">in order to make room for all those integers</p>



<p class="wp-block-paragraph">like I showed you a second ago.</p>



<p class="wp-block-paragraph">showed you a second ago. So then we&#8217;re going to move the stack pointer.</p>



<p class="wp-block-paragraph">And maybe I&#8217;ll leave a little comment here. Remember, the stack grows downward</p>



<p class="wp-block-paragraph">in memory. And so all I&#8217;m doing is subtracting the stack pointer. Remember the stack pointer</p>



<p class="wp-block-paragraph">register, it just holds a number, which is a memory location. So if you subtract some numbers</p>



<p class="wp-block-paragraph">from it, you&#8217;re really having it go downward in memory. And that&#8217;s what we want to do</p>



<p class="wp-block-paragraph">memory and that&#8217;s what we want to do to you know grow the stack for a local variable so I&#8217;m going</p>



<p class="wp-block-paragraph">to say well I should also say that we&#8217;re using the two operand version so just like I&#8217;m all if we</p>



<p class="wp-block-paragraph">had the three operand version then the last two operands would have one subtracted from the other</p>



<p class="wp-block-paragraph">and the result will be stored in the first one but since I&#8217;m using the two operand version</p>



<p class="wp-block-paragraph">basically it&#8217;s taking RSP minus R10 and then storing it in RSP so this this instruction just</p>



<p class="wp-block-paragraph">this instruction just says let&#8217;s move the stack pointer downward in memory</p>



<p class="wp-block-paragraph">enough times that we have room for all of our integers okay no problem next thing that we&#8217;re</p>



<p class="wp-block-paragraph">going to do is um we&#8217;re going to move r12 we&#8217;re going to move into r12 the current value of rsp</p>



<p class="wp-block-paragraph">word in memory right less okay so the first integer you know it&#8217;s up to you</p>



<p class="wp-block-paragraph">how you style this because once we do the subtraction then RSP is actually</p>



<p class="wp-block-paragraph">going to be pointing towards an integer you could consider that to be the first</p>



<p class="wp-block-paragraph">integer or the last integer because all we have is an array of integers so you</p>



<p class="wp-block-paragraph">you know rsp wherever it&#8217;s sitting when we&#8217;re finished we could say that&#8217;s pointing to the</p>



<p class="wp-block-paragraph">first integer or we could say it&#8217;s pointing to the last integer but um if we decide to say that it is</p>



<p class="wp-block-paragraph">pointing to the let&#8217;s see yeah if we&#8217;re deciding that it points to the first integer let me just do</p>



<p class="wp-block-paragraph">a little comment here first integer it just makes it a little bit easier for me to write our loop</p>



<p class="wp-block-paragraph">You could start by pointing to the one that RSP is pointing to,</p>



<p class="wp-block-paragraph">or you could start by pointing to the other one that was like the first one that you added onto the stack.</p>



<p class="wp-block-paragraph">You could call either one of those the first integer, as long as you remember where you started.</p>



<p class="wp-block-paragraph">So you can increase or decrease the memory location to get to the next integer.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to do it in this style.</p>



<p class="wp-block-paragraph">But keep in mind, as long as the only thing that you modify and read is within that range, it&#8217;s okay.</p>



<p class="wp-block-paragraph">So let&#8217;s remember where is RSP.</p>



<p class="wp-block-paragraph">So that&#8217;s like the top of the stack.</p>



<p class="wp-block-paragraph">We&#8217;re going to say R12 holds the stack pointer so that we can use R12 as sort of a running</p>



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



<p class="wp-block-paragraph">I think that&#8217;s the way I&#8217;m going to use it.</p>



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



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



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



<p class="wp-block-paragraph">a calculation where we remember where the last integer is so we know where the first integer</p>



<p class="wp-block-paragraph">we&#8217;re just going to call this the first integer i&#8217;ll put it in quotes just to remind you that</p>



<p class="wp-block-paragraph">this is just the style i happen to be using so here we&#8217;re going to say that the first integer</p>



<p class="wp-block-paragraph">is wherever rsp is pointing and then in r13 we&#8217;re going to remember where the other side of the</p>



<p class="wp-block-paragraph">array is in this case we&#8217;re calling it the last integer and pretty much it&#8217;s just r12 which is</p>



<p class="wp-block-paragraph">And then we add to it a memory location.</p>



<p class="wp-block-paragraph">So remember we said before that the stack grows downward in memory.</p>



<p class="wp-block-paragraph">So if we consider the top of the stack to be the first integer,</p>



<p class="wp-block-paragraph">then that means previous items, maybe I should draw this,</p>



<p class="wp-block-paragraph">are going to be increased memory locations.</p>



<p class="wp-block-paragraph">So that&#8217;s kind of like the backwards of what you imagine a stack is doing,</p>



<p class="wp-block-paragraph">but it&#8217;s kind of the way that I like it sometimes.</p>



<p class="wp-block-paragraph">So imagine just in a very simple stack,</p>



<p class="wp-block-paragraph">let&#8217;s pretend that the stack has one byte values,</p>



<p class="wp-block-paragraph">one byte of values, which it doesn&#8217;t, but let&#8217;s pretend if you, if you do it,</p>



<p class="wp-block-paragraph">I got to learn how to draw. I can&#8217;t get that five right. Okay. I&#8217;m defeated. So let&#8217;s pretend</p>



<p class="wp-block-paragraph">that that address on the thing sitting at the bottom of the stack is five. So then that means</p>



<p class="wp-block-paragraph">the next address would be four, right? But if we decided, let&#8217;s say that the, well, let&#8217;s do like</p>



<p class="wp-block-paragraph">like a few more just to make it a little bit more interesting.</p>



<p class="wp-block-paragraph">Let&#8217;s do a total of five.</p>



<p class="wp-block-paragraph">So we&#8217;ll say four, three, two, one.</p>



<p class="wp-block-paragraph">Maybe I should just add some numbers in front of those values.</p>



<p class="wp-block-paragraph">So it doesn&#8217;t feel like we&#8217;re hitting zero.</p>



<p class="wp-block-paragraph">But so just pretend we have a memory location of 15.</p>



<p class="wp-block-paragraph">That&#8217;s not going to be the case in real life.</p>



<p class="wp-block-paragraph">Pretend that we have one byte integers on the stack, which is not going to be the case.</p>



<p class="wp-block-paragraph">We&#8217;re going to have quad words.</p>



<p class="wp-block-paragraph">But you can see the memory locations go downward, right?</p>



<p class="wp-block-paragraph">to the stack pointer is pointing to the topmost location we have to remember that anything up here</p>



<p class="wp-block-paragraph">might exist in system ram but it&#8217;s not considered valid data because we didn&#8217;t</p>



<p class="wp-block-paragraph">you know make it part of the stack by by growing the stack pointer so that means uh these</p>



<p class="wp-block-paragraph">these items well maybe i&#8217;ll do a check instead of an x because x looks bad i&#8217;ll do a check</p>



<p class="wp-block-paragraph">check and a check and a check and a check these items are okay to use so if I have</p>



<p class="wp-block-paragraph">RSP or in this case we just remembered where RSP was by storing r12 if we have</p>



<p class="wp-block-paragraph">that memory location 11 and we want to get an additional integer like somewhere</p>



<p class="wp-block-paragraph">else like the next integer well that would be you know this one down here we</p>



<p class="wp-block-paragraph">wouldn&#8217;t go in the other direction we wouldn&#8217;t go you know up we would just go</p>



<p class="wp-block-paragraph">know up we would just go down in the stack but down visually is actually growing upwards in memory</p>



<p class="wp-block-paragraph">because remember when we grow upwards visually we&#8217;re growing downward in memory so and you can</p>



<p class="wp-block-paragraph">see here too if we&#8217;re if we&#8217;re increasing 11 to 12 that means we&#8217;re adding memory locations to get</p>



<p class="wp-block-paragraph">to the next uh integer that we have in the stack so that&#8217;s why here my green is just like frustrating</p>



<p class="wp-block-paragraph">fix this yeah so that&#8217;s why here we&#8217;re adding a little formula instead of subtracting because</p>



<p class="wp-block-paragraph">the rsp started there we&#8217;re saying that the top of the stack is the first integer</p>



<p class="wp-block-paragraph">just so we can add in a more convenient nice way so what are we adding to it we&#8217;re just adding to it</p>



<p class="wp-block-paragraph">the number of integers minus one and then so that&#8217;s that&#8217;s the number of slots that</p>



<p class="wp-block-paragraph">size. So if you imagine, you know, if we had 10 integers, then, you know, 10 minus one is nine</p>



<p class="wp-block-paragraph">slots. So if you imagine that zero is the first integer, let&#8217;s say the memory location again,</p>



<p class="wp-block-paragraph">then that means one, two, three, four, five, six, seven, eight, nine, 10 minus one is nine. So if we</p>



<p class="wp-block-paragraph">added the number of integers minus one from the start, which we&#8217;ll consider zero here, that means</p>



<p class="wp-block-paragraph">directly so that&#8217;s just like a little math because sometimes when you when you</p>



<p class="wp-block-paragraph">think of adding two numbers together or taking the difference or including the</p>



<p class="wp-block-paragraph">first number or not including the first number it&#8217;s a little confusing right so</p>



<p class="wp-block-paragraph">keep in mind for this particular calculation we are adding to it the</p>



<p class="wp-block-paragraph">number of integers minus one so that will be sitting on the last integer</p>



<p class="wp-block-paragraph">rather than going past it so start with the first integers memory location and</p>



<p class="wp-block-paragraph">the number of integers minus one or size minus one times the integer size,</p>



<p class="wp-block-paragraph">because remember, every integer is going to be eight bytes.</p>



<p class="wp-block-paragraph">And that will give us the memory location of the last integer in R13.</p>



<p class="wp-block-paragraph">And then I&#8217;ve said this in other videos,</p>



<p class="wp-block-paragraph">but basically you&#8217;re only allowed to make these sorts of calculations in Yasm</p>



<p class="wp-block-paragraph">when you have the calculation inside of brackets,</p>



<p class="wp-block-paragraph">but brackets will automatically dereference the value on the inside.</p>



<p class="wp-block-paragraph">It&#8217;ll consider it as a pointer that needs to be dereferenced,</p>



<p class="wp-block-paragraph">but we don&#8217;t want to dereference anything.</p>



<p class="wp-block-paragraph">anything we don&#8217;t want to go to a memory location and take a value the memory location is the value</p>



<p class="wp-block-paragraph">so these dereferencing brackets which are required for the formula are kind of bad so that&#8217;s why we</p>



<p class="wp-block-paragraph">use the lea instruction instead of the move instruction if i put move there it would</p>



<p class="wp-block-paragraph">definitely dereference the memory location and give me a value in r13 so r13 wouldn&#8217;t actually</p>



<p class="wp-block-paragraph">be a pointer it would just be the value of the junk data of the last integer keep that in mind</p>



<p class="wp-block-paragraph">keep that in mind okay so uh where&#8217;s RSP so we got that so now let&#8217;s do a loop</p>



<p class="wp-block-paragraph">I&#8217;m gonna start off with a label called demo loop init so for me personally when</p>



<p class="wp-block-paragraph">I&#8217;m looping you know I like to make my labels inside of my loops start with a</p>



<p class="wp-block-paragraph">prefix that matches the function so it&#8217;s like demo and then everything else is</p>



<p class="wp-block-paragraph">gonna be demo underscore something so uh init loop init I&#8217;m gonna make a loop</p>



<p class="wp-block-paragraph">to make a loop where i initialize the values of the array so i&#8217;m calling this loop the init loop</p>



<p class="wp-block-paragraph">and then the last part is just like this is the initialization part of the loop this is where we</p>



<p class="wp-block-paragraph">sort of like set up the initial values to loop so we have r12 and 13 that point to the first</p>



<p class="wp-block-paragraph">and last integers now we&#8217;re going to set up r14 and 15 where r14 is the running pointer i think</p>



<p class="wp-block-paragraph">before i might have accidentally said that r13 is the running pointer it just points to the last</p>



<p class="wp-block-paragraph">it just points to the last integer but if you look back up at my comments r14 is the running pointer</p>



<p class="wp-block-paragraph">to the current integer what is a running pointer it&#8217;s just a pointer that runs it&#8217;s just a pointer</p>



<p class="wp-block-paragraph">that just keeps increasing so we can look at different data values so i&#8217;m going to start it</p>



<p class="wp-block-paragraph">by looking at the first integer so now r14 is pointing to the first integer and then r15 is</p>



<p class="wp-block-paragraph">going to be the value that i want to put into that position in the array so like the first integer</p>



<p class="wp-block-paragraph">integer I want to put some kind of a starting value into it you can put the</p>



<p class="wp-block-paragraph">number zero or whatever you want I just wanted to have a start value so that it</p>



<p class="wp-block-paragraph">sort of looks more like I&#8217;m putting data and less like I have a loop counter so</p>



<p class="wp-block-paragraph">remember the integer start value up here is just seven so I&#8217;m just going to start</p>



<p class="wp-block-paragraph">at the number seven and now I&#8217;m done initializing my loop then the next thing</p>



<p class="wp-block-paragraph">I&#8217;m going to add is the top of my loop so you can imagine this as the top of a</p>



<p class="wp-block-paragraph">top of a while loop where you start comparing some sort of an expression maybe i&#8217;ll say uh</p>



<p class="wp-block-paragraph">you know expr to say that we&#8217;re comparing some sort of an expression</p>



<p class="wp-block-paragraph">and if that expression evaluates to true the loop continues if it evaluates to false</p>



<p class="wp-block-paragraph">then the loop does not continue let&#8217;s see so i&#8217;m going to compare r14 with r13 inside of the</p>



<p class="wp-block-paragraph">and 13 remember R14 is the running pointer and R13 is the last integer so basically I&#8217;m trying to</p>



<p class="wp-block-paragraph">figure out am I looking at or like let&#8217;s compare the running pointer with the pointer of the last</p>



<p class="wp-block-paragraph">integer then I&#8217;m going to say if the running pointer has a greater memory location than than</p>



<p class="wp-block-paragraph">the last integer that means I&#8217;ve gone beyond the last integer and again the way I arranged the first</p>



<p class="wp-block-paragraph">last integers just makes it easier for me to think of them as having increasing</p>



<p class="wp-block-paragraph">memory locations so I&#8217;m going to jump if the running pointer has already</p>



<p class="wp-block-paragraph">surpassed the last integer by saying let&#8217;s jump if it&#8217;s greater than so you</p>



<p class="wp-block-paragraph">can imagine maybe in the expression here I should probably say while not</p>



<p class="wp-block-paragraph">r14 is greater than r13 not a great expression but it&#8217;ll do so that&#8217;s what</p>



<p class="wp-block-paragraph">up there at the top let me just put that into my notes too so that my notes match</p>



<p class="wp-block-paragraph">the video okay so we are comparing and then we&#8217;re jumping to the end of the</p>



<p class="wp-block-paragraph">loop if we end up you know going beyond the last integer so that labels not</p>



<p class="wp-block-paragraph">created yet I&#8217;ll create that in a moment but pretty much that&#8217;s a label that&#8217;s</p>



<p class="wp-block-paragraph">just going to be below the loop just to say like we&#8217;re finished with the loop</p>



<p class="wp-block-paragraph">the loop body I like to put comments here to help myself remember oh this is</p>



<p class="wp-block-paragraph">the part of the while loop that I&#8217;m currently inside of just makes things a</p>



<p class="wp-block-paragraph">little bit easier to understand you know you put a block comment up top of every</p>



<p class="wp-block-paragraph">label or every you know chunk of instructions just to let you know the</p>



<p class="wp-block-paragraph">general idea and then you know sometimes you put comments also on the right side</p>



<p class="wp-block-paragraph">to help you remember what each instruction is actually doing so then</p>



<p class="wp-block-paragraph">what am I going to do here remember R14 is the running pointer if I D ref R14</p>



<p class="wp-block-paragraph">ref r14 that means i want to move an actual value into that memory location rather than changing the</p>



<p class="wp-block-paragraph">memory location that r14 points to so r15 is going to be the uh the integer that we want to</p>



<p class="wp-block-paragraph">write into the array so all i&#8217;m doing here is i&#8217;m saying let&#8217;s take that value seven which is what</p>



<p class="wp-block-paragraph">it starts as and just move it in to you know the ram stick at that memory location so i&#8217;m setting</p>



<p class="wp-block-paragraph">seven right now then I&#8217;m going to increase our 15 so that means every time</p>



<p class="wp-block-paragraph">we iterate the loop we should see that the value increases so the first integer</p>



<p class="wp-block-paragraph">should be seven the second one should be eight next one should be nine and so</p>



<p class="wp-block-paragraph">forth so just a simple loop where I&#8217;m just writing data into my array so now</p>



<p class="wp-block-paragraph">that we&#8217;ve ended the loop body let&#8217;s write the bottom of the loop which is</p>



<p class="wp-block-paragraph">just going to increase the running pointer and jump back up to the top and</p>



<p class="wp-block-paragraph">And this is not necessarily the only style for translating while loops.</p>



<p class="wp-block-paragraph">I&#8217;m just doing it.</p>



<p class="wp-block-paragraph">And, you know, I&#8217;m going to make another video in the future where we talk about, you know, for loops and while loops and all that stuff.</p>



<p class="wp-block-paragraph">But this video is just really about local variables on the stack.</p>



<p class="wp-block-paragraph">So I&#8217;m not going to go over all the different ways you can do it.</p>



<p class="wp-block-paragraph">Anyway, so R14 is the running pointer.</p>



<p class="wp-block-paragraph">So I&#8217;m going to just make the running pointer jump to the next integer.</p>



<p class="wp-block-paragraph">And we can do that by increasing its memory location by the size of one integer.</p>



<p class="wp-block-paragraph">Again, this is another benefit of the first and last pointers that I chose at the beginning.</p>



<p class="wp-block-paragraph">I can just increase to go to the next integer.</p>



<p class="wp-block-paragraph">So we&#8217;re going to increase by 8 bytes to just go to the next integer.</p>



<p class="wp-block-paragraph">If you increase by 1 byte, you&#8217;ll probably have a huge corrupted mess because you&#8217;re</p>



<p class="wp-block-paragraph">messing with 8 byte integers but you&#8217;re only increasing by 1 byte.</p>



<p class="wp-block-paragraph">And then after we increase, we&#8217;re just going to jump to the top of the loop.</p>



<p class="wp-block-paragraph">So notice how I have a jump statement here.</p>



<p class="wp-block-paragraph">It&#8217;s going to go just to loop top.</p>



<p class="wp-block-paragraph">loop top. So now this part is here is basically just going to execute over and over and over again</p>



<p class="wp-block-paragraph">until we finally scan through all of the integers in our array. So that&#8217;s the bottom of the loop.</p>



<p class="wp-block-paragraph">And then I&#8217;ll make the label for the loop being done. It&#8217;s not really going to do anything</p>



<p class="wp-block-paragraph">except just be done. And do you, you know, I don&#8217;t know, depending on your style, maybe you can let</p>



<p class="wp-block-paragraph">drop through if the loop&#8217;s done rather than always jumping to the top but i&#8217;m just going to say when</p>



<p class="wp-block-paragraph">we&#8217;re done we jump to the loop done label and therefore there&#8217;s no more looping of that</p>



<p class="wp-block-paragraph">initialization loop okay so we got that done let me just run the program real fast to make sure</p>



<p class="wp-block-paragraph">that i haven&#8217;t screwed it up we actually should not see anything right now oh what did i do</p>



<p class="wp-block-paragraph">must have done something naughty maybe if i finish</p>



<p class="wp-block-paragraph">uh this program then everything will be okay oh my gosh what did i even do</p>



<p class="wp-block-paragraph">well i&#8217;ve got a working solution in the other window so hopefully when i paste all the extra</p>



<p class="wp-block-paragraph">steps everything will be fine you never know subtract the stack pointer oh did i forget to</p>



<p class="wp-block-paragraph">oh did i forget to restore something at the very end print body move the base pointer into the</p>



<p class="wp-block-paragraph">RSP stack pointer oh yeah okay that&#8217;s definitely what you got that&#8217;s why I</p>



<p class="wp-block-paragraph">crashed okay so um remember I said you got to be very careful about the stack</p>



<p class="wp-block-paragraph">this is a great lesson so I did preserve the base pointer but I didn&#8217;t actually</p>



<p class="wp-block-paragraph">preserve the stack pointer notice how right here I subtracted from the stack</p>



<p class="wp-block-paragraph">pointer but I did not restore the stack pointer anywhere so that means I</p>



<p class="wp-block-paragraph">corrupted the stack for anyone that called me and also for my return address</p>



<p class="wp-block-paragraph">address. So I&#8217;m kind of trying to copy paste my instructions from top to bottom, but I think I&#8217;m</p>



<p class="wp-block-paragraph">just going to copy paste something else to make sure that we can actually run this. So I&#8217;m going</p>



<p class="wp-block-paragraph">to copy paste into the epilog a restoration of the stack pointer. And that&#8217;s why we saved the</p>



<p class="wp-block-paragraph">stack pointer in the base pointer, just to remember where it was when we originally started our</p>



<p class="wp-block-paragraph">function. So now on line 128, it should be restored and the program should work. Let me just double</p>



<p class="wp-block-paragraph">yeah it worked okay nothing happened that we can see but it did write values</p>



<p class="wp-block-paragraph">into the array now let&#8217;s do another loop where we just print the array so let&#8217;s</p>



<p class="wp-block-paragraph">see loop in it done looping it bottom and looping it done okay so now there&#8217;s</p>



<p class="wp-block-paragraph">like gonna be another loop here we&#8217;re gonna call this the print loop and so</p>



<p class="wp-block-paragraph">the print loop is gonna be kind of the same pattern we&#8217;re just gonna loop</p>



<p class="wp-block-paragraph">going to loop through all the integers in the array but instead of modifying them we&#8217;re just</p>



<p class="wp-block-paragraph">going to print them so now you know the first thing we&#8217;ll do is we&#8217;ll set r12 we&#8217;ll store that</p>



<p class="wp-block-paragraph">inside of r14 in order to start the loop at the first integer what was r14 again that was the</p>



<p class="wp-block-paragraph">running pointer remember let&#8217;s see where is that yeah it was the running pointer so now we&#8217;re</p>



<p class="wp-block-paragraph">resetting the r14 running pointer to the very beginning of the array and we know where the</p>



<p class="wp-block-paragraph">thing we&#8217;ll do is we will set up the top of the loop and the body so I&#8217;m just</p>



<p class="wp-block-paragraph">going to copy paste again this stuff right here</p>



<p class="wp-block-paragraph">right there okay so the top of the loop we&#8217;re asking ourselves you know we&#8217;re</p>



<p class="wp-block-paragraph">gonna compare I&#8217;m not gonna put all the extra while stuff that I put in the</p>



<p class="wp-block-paragraph">previous loop because hopefully by now you understand loops a little bit better</p>



<p class="wp-block-paragraph">and if we&#8217;re beyond the last integer because the running point of r14 is beyond the memory</p>



<p class="wp-block-paragraph">location of r13 that means we&#8217;re totally done so we should jump if it&#8217;s greater than</p>



<p class="wp-block-paragraph">now i feel bad let&#8217;s uh let&#8217;s put a comment in here on the top let&#8217;s go uh</p>



<p class="wp-block-paragraph">basically that you know if uh keep going as long as r14 is not greater than r13 so if it is</p>



<p class="wp-block-paragraph">in the body and in the body all i&#8217;m going to do is use r14 the running pointer to print uh you know</p>



<p class="wp-block-paragraph">whatever value is sitting in that particular integer so how do we do that i&#8217;m just going to</p>



<p class="wp-block-paragraph">use the printf statement or sorry the printf function which is provided by the c libraries</p>



<p class="wp-block-paragraph">that&#8217;s why we&#8217;re doing you know a modular or a hybrid program with multiple modules and c linking</p>



<p class="wp-block-paragraph">very quickly there is a function called printf which I can call it it takes multiple arguments</p>



<p class="wp-block-paragraph">but the first two arguments that I can give it are the string that represents the formatting</p>



<p class="wp-block-paragraph">that I want to print like I could do like a regular string message I could do tokens</p>



<p class="wp-block-paragraph">to format some data inside of them and then the second argument is going to be the piece of data</p>



<p class="wp-block-paragraph">that I actually want to use let me see if I can just type that up for you real fast</p>



<p class="wp-block-paragraph">So, you know, the printf instruction, or sorry, this is not an instruction.</p>



<p class="wp-block-paragraph">This is a function in C.</p>



<p class="wp-block-paragraph">We would typically, you know, give it some kind of string.</p>



<p class="wp-block-paragraph">The string should be null terminated, and it should have a new line at the very end of it</p>



<p class="wp-block-paragraph">to make sure that printf actually flushes.</p>



<p class="wp-block-paragraph">It won&#8217;t flush if you don&#8217;t have a new line, so the program will look really weird.</p>



<p class="wp-block-paragraph">But I guess it&#8217;s more performant if you have a way to delay the flushing,</p>



<p class="wp-block-paragraph">flushing and you know that you can flush it later at the very end but for now i&#8217;m just going to</p>



<p class="wp-block-paragraph">flush every time and then every argument after that is some sort of you know data that we can</p>



<p class="wp-block-paragraph">print so imagine we have a long and we&#8217;ll call it a and we&#8217;ll say that it has like some giant value</p>



<p class="wp-block-paragraph">so that means we would give that long as the next argument the rsi argument and then for the string</p>



<p class="wp-block-paragraph">what I&#8217;m using right here %lu so you can imagine instead of this string it is this</p>



<p class="wp-block-paragraph">string right here whoops too many too many quotes it&#8217;s just this string right</p>



<p class="wp-block-paragraph">here and then instead of a 10 13 that&#8217;s the same thing as just doing an or if</p>



<p class="wp-block-paragraph">you want to be you know more of a windows windows person slash r slash n</p>



<p class="wp-block-paragraph">it&#8217;s all good and the zero is not needed because the string if you put a string</p>



<p class="wp-block-paragraph">literal it&#8217;s automatically going to be null terminated which means there&#8217;s just</p>



<p class="wp-block-paragraph">null terminated which means there&#8217;s just a zero at the end of the string in memory so this is</p>



<p class="wp-block-paragraph">basically what I&#8217;m doing I&#8217;m making a an integer in the case of the assembly program it&#8217;s going to</p>



<p class="wp-block-paragraph">grab an integer from that position in the array that we&#8217;re looking at and it&#8217;s going to give it</p>



<p class="wp-block-paragraph">as an argument and then the first argument is going to say let&#8217;s just print this as a unsigned</p>



<p class="wp-block-paragraph">long so that&#8217;s why I have that string here let me search for it and go down a little bit again so</p>



<p class="wp-block-paragraph">I&#8217;m saying first argument is this the format that I want to be printed second argument is the actual</p>



<p class="wp-block-paragraph">is the actual value and then I&#8217;m gonna make a call to printf why do I have this</p>



<p class="wp-block-paragraph">weird push and pop pair sitting around printf so this is not a video about</p>



<p class="wp-block-paragraph">stack alignment in GCC but basically the GCC libraries expect that your stack is</p>



<p class="wp-block-paragraph">aligned to I think 16 bytes but since we use 8 byte integers every single time we</p>



<p class="wp-block-paragraph">address to the stack which is eight bytes and then every time we do one single push or pop</p>



<p class="wp-block-paragraph">we&#8217;re modifying the alignment of the stack by eight bytes so if you think about it when we&#8217;re</p>



<p class="wp-block-paragraph">programming in assembly for the most part the stack is going in and out of alignment because</p>



<p class="wp-block-paragraph">every time we modify it by eight bytes it it might line up with a 16 byte alignment or it might not</p>



<p class="wp-block-paragraph">it&#8217;s just kind of like oscillating right so when I first wrote this solution I wasn&#8217;t doing the</p>



<p class="wp-block-paragraph">what happened oh actually maybe i guess i don&#8217;t need to jump to the top right now i don&#8217;t need</p>



<p class="wp-block-paragraph">to finish the loop let&#8217;s see if this prints just one number well let me let me let me see if this</p>



<p class="wp-block-paragraph">prints one number certainly and if it&#8217;s an okay assembly program just to print one number is it</p>



<p class="wp-block-paragraph">going to work okay we got to do the done symbol okay so i&#8217;ll show you in a minute why we need</p>



<p class="wp-block-paragraph">that for stack alignment but i guess i&#8217;ll just finish the loop so demo print loop done so we</p>



<p class="wp-block-paragraph">just did print loop top and that means we need the bottom and the done so i&#8217;m just going to copy paste</p>



<p class="wp-block-paragraph">into program here i&#8217;ll just say nada because we&#8217;re not really doing anything and then at the bottom</p>



<p class="wp-block-paragraph">you know that&#8217;s the epilogue that&#8217;s separate from the the other label so basically now let me finish</p>



<p class="wp-block-paragraph">know we already know the loops but I&#8217;ll just I&#8217;ll just say it at the bottom of</p>



<p class="wp-block-paragraph">the loop we do the same thing that we did with the initialization loop we just</p>



<p class="wp-block-paragraph">increase the running pointer you know we move it along to the next integer and</p>



<p class="wp-block-paragraph">then we jump to the top of the loop that&#8217;s it and then the done label we</p>



<p class="wp-block-paragraph">don&#8217;t really do anything we&#8217;re just letting execution drop through down to</p>



<p class="wp-block-paragraph">that point so that the loop doesn&#8217;t continue so now we should be able to run</p>



<p class="wp-block-paragraph">the program don&#8217;t get excited oh actually you know what get excited I</p>



<p class="wp-block-paragraph">was gonna say don&#8217;t get excited because it was gonna totally work now but now</p>



<p class="wp-block-paragraph">because it was going to totally work now,</p>



<p class="wp-block-paragraph">but now I think we can just say that it&#8217;s going to crash.</p>



<p class="wp-block-paragraph">So if I run it, notice how there&#8217;s a segpult.</p>



<p class="wp-block-paragraph">So the GCC libraries, many functions expect your stack</p>



<p class="wp-block-paragraph">to be aligned to 16 bytes.</p>



<p class="wp-block-paragraph">So if you see mysterious crashes</p>



<p class="wp-block-paragraph">and you are absolutely sure that you&#8217;re not ruining</p>



<p class="wp-block-paragraph">the stack pointer or ruining something else,</p>



<p class="wp-block-paragraph">you&#8217;re doing everything correctly,</p>



<p class="wp-block-paragraph">but the program still crashes, it might be stack alignment.</p>



<p class="wp-block-paragraph">So one way to get around stack alignment</p>



<p class="wp-block-paragraph">is just to move the stack pointer.</p>



<p class="wp-block-paragraph">move the stack pointer like at the top here we could have said oh we&#8217;ve got one two three four</p>



<p class="wp-block-paragraph">five we&#8217;ve got five pushes and then here we&#8217;re moving the stack by I don&#8217;t know how many other</p>



<p class="wp-block-paragraph">addresses the stack might be out of alignment somehow so we could add an extra push up here</p>



<p class="wp-block-paragraph">and then add a corresponding pop down at the bottom like we could easily do this let me just</p>



<p class="wp-block-paragraph">show you real fast we could push r15 twice for no reason I acknowledge and then at the bottom</p>



<p class="wp-block-paragraph">and then at the bottom we pop R15 twice,</p>



<p class="wp-block-paragraph">that would change the alignment because that&#8217;s one more 8 byte push.</p>



<p class="wp-block-paragraph">But in my case, and actually that would be a little bit smarter</p>



<p class="wp-block-paragraph">because if you have our loop where it&#8217;s constantly calling on printf,</p>



<p class="wp-block-paragraph">this is a lot of hits to memory, right?</p>



<p class="wp-block-paragraph">This is like 100 hits to memory because every single time we do a push-pop pair</p>



<p class="wp-block-paragraph">around a call to printf, we&#8217;re like touching memory.</p>



<p class="wp-block-paragraph">Whereas if I did it at the beginning and the end,</p>



<p class="wp-block-paragraph">Maybe I should just do it this way.</p>



<p class="wp-block-paragraph">I want to do it both ways so you understand, but it&#8217;s more efficient, I think, if we do</p>



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



<p class="wp-block-paragraph">Anyway, so we&#8217;ll do pop twice at the bottom and then push twice at the top.</p>



<p class="wp-block-paragraph">And so then we don&#8217;t really need to surround it with a push-pop pair.</p>



<p class="wp-block-paragraph">I think I haven&#8217;t tested this.</p>



<p class="wp-block-paragraph">We&#8217;ll hope now that the stack is in alignment at all times in our functions so that it doesn&#8217;t</p>



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



<p class="wp-block-paragraph">Yeah, so now see how the program works.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do it the other way now, which is the less efficient way, because here</p>



<p class="wp-block-paragraph">now which is the less efficient way because here we just have one extra push</p>



<p class="wp-block-paragraph">pop pair but if we do it the other way it&#8217;ll still work but we&#8217;ll be hitting</p>



<p class="wp-block-paragraph">memory much more often so I&#8217;m just gonna do it this way just to show you you can</p>



<p class="wp-block-paragraph">surround any call because sometimes in your programs you might have the stack</p>



<p class="wp-block-paragraph">like you know modified throughout the function many different times so it</p>



<p class="wp-block-paragraph">wouldn&#8217;t make too much sense for you to add an extra push pop pair in the prologue</p>



<p class="wp-block-paragraph">and epilogue because that might not solve it for all of your calls to all of</p>



<p class="wp-block-paragraph">to all of your seed library functions.</p>



<p class="wp-block-paragraph">So in that case,</p>



<p class="wp-block-paragraph">where you can&#8217;t really predict the stack well enough,</p>



<p class="wp-block-paragraph">you can just surround your call with a push-pop pair.</p>



<p class="wp-block-paragraph">It hits memory more, but it&#8217;ll work.</p>



<p class="wp-block-paragraph">So this is basically, you can imagine,</p>



<p class="wp-block-paragraph">by the time we get to line 139,</p>



<p class="wp-block-paragraph">the stack is out of alignment.</p>



<p class="wp-block-paragraph">So I just do a push that puts it into alignment.</p>



<p class="wp-block-paragraph">And then after the call comes back,</p>



<p class="wp-block-paragraph">I just pop it so it&#8217;s back out of alignment again,</p>



<p class="wp-block-paragraph">but I don&#8217;t have extra erroneous data sitting on the stack.</p>



<p class="wp-block-paragraph">Because if I only had push and not pop,</p>



<p class="wp-block-paragraph">then it&#8217;s going to push it more and more out of alignment.</p>



<p class="wp-block-paragraph">It&#8217;s going to push it in and out and in and out,</p>



<p class="wp-block-paragraph">but it&#8217;s going to add a bunch of junk data to the stack</p>



<p class="wp-block-paragraph">that I&#8217;ll never recover from.</p>



<p class="wp-block-paragraph">Or actually, I guess I will recover at the very end</p>



<p class="wp-block-paragraph">when I restore the stack pointer, but it&#8217;s pointless.</p>



<p class="wp-block-paragraph">It&#8217;s going to consume too much memory.</p>



<p class="wp-block-paragraph">What if I was writing like a billion</p>



<p class="wp-block-paragraph">or like a million items on the stack, right?</p>



<p class="wp-block-paragraph">a billion iterations of the loop would probably be a bad idea to start adding onto the stack</p>



<p class="wp-block-paragraph">we&#8217;ll probably end up stack overflowing probably with far less than a billion items so anyway i&#8217;m</p>



<p class="wp-block-paragraph">going to surround the call with the push pop pair and then at the bottom we just do the same thing</p>



<p class="wp-block-paragraph">you know increase the integer and then go to the top and so now we you know you already can see it</p>



<p class="wp-block-paragraph">it starts off printing well it starts off initializing the array but then the thing that we</p>



<p class="wp-block-paragraph">can see starts off printing the first integer and then the next iteration of the loop prints the</p>



<p class="wp-block-paragraph">second integer and the third and the fourth and so forth until we get all the way down just to</p>



<p class="wp-block-paragraph">prove to you maybe we can increase it by two instead of one each time I&#8217;ll just modify that</p>



<p class="wp-block-paragraph">Oh no, that&#8217;s the print loop. Let&#8217;s do the init loop.</p>



<p class="wp-block-paragraph">Init loop bottom.</p>



<p class="wp-block-paragraph">Oh, right there, when we&#8217;re in the body.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to increase R15 twice.</p>



<p class="wp-block-paragraph">Just to show you, you know, we can kind of control what&#8217;s going inside of the local array.</p>



<p class="wp-block-paragraph">So see how it goes from 7 to 9 to 11 instead of 7, 8, 9.</p>



<p class="wp-block-paragraph">So I&#8217;m going to take that out and then just show you that we can control</p>



<p class="wp-block-paragraph">how many integers we have in our local array with just the number of integers.</p>



<p class="wp-block-paragraph">just the number of integers so I&#8217;m going to change the 50 to a 5 and run it again and you can see</p>



<p class="wp-block-paragraph">what the dang did I do number of integers is 5 and then it was 50 did I put the stack out of</p>



<p class="wp-block-paragraph">alignment no I I have a push put pair there number 50 is not hard-coded anywhere anywhere</p>



<p class="wp-block-paragraph">there. Oh, I have a bug that I can debug. That&#8217;s nice. I guess the bug debugging is not for this</p>



<p class="wp-block-paragraph">video. I wonder if some other value would work for that. There&#8217;s a number of things that could</p>



<p class="wp-block-paragraph">be the problem. It could be like stack alignment somehow. It could also be, let&#8217;s see, am I making</p>



<p class="wp-block-paragraph">any calls here inside of my function demo? I&#8217;m saying sub and LEA and then start and then jump</p>



<p class="wp-block-paragraph">and start and then jump and increase and jump and then RDI.</p>



<p class="wp-block-paragraph">Not really doing anything else.</p>



<p class="wp-block-paragraph">So I don&#8217;t think it&#8217;s stack alignment.</p>



<p class="wp-block-paragraph">I must have miscalculated somewhere somehow for changing that.</p>



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



<p class="wp-block-paragraph">Well, if I figured it out, then I will release another video.</p>



<p class="wp-block-paragraph">But basically, this is the idea.</p>



<p class="wp-block-paragraph">100 works, 50 works.</p>



<p class="wp-block-paragraph">What about 99?</p>



<p class="wp-block-paragraph">Would that work?</p>



<p class="wp-block-paragraph">Save faults on 99.</p>



<p class="wp-block-paragraph">faults on 99 and then 98. 98 works so like every two seems to work but that is</p>



<p class="wp-block-paragraph">two quad words or 16 oh right so like a hundred integers if we assume that</p>



<p class="wp-block-paragraph">that&#8217;s in alignment then 99 would be eight bytes less that we&#8217;re moving the</p>



<p class="wp-block-paragraph">98, notice how it is okay.</p>



<p class="wp-block-paragraph">And then 97, it&#8217;s going to segfault again.</p>



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



<p class="wp-block-paragraph">This is a great stack alignment video, I guess.</p>



<p class="wp-block-paragraph">96, it won&#8217;t segfault.</p>



<p class="wp-block-paragraph">So it&#8217;s going in and out of alignment.</p>



<p class="wp-block-paragraph">I think I figured it out.</p>



<p class="wp-block-paragraph">So if we use the number 100, it&#8217;s in alignment.</p>



<p class="wp-block-paragraph">Sorry, it&#8217;s out of alignment.</p>



<p class="wp-block-paragraph">If we use the number 100, the program works because the number 100 throws it out of alignment.</p>



<p class="wp-block-paragraph">number 100 throws it out of alignment but then we have this push pop pair here around the call to</p>



<p class="wp-block-paragraph">printf which puts it back in alignment so if I change the number of integers I&#8217;m actually</p>



<p class="wp-block-paragraph">changing the number of memory locations that I modify the stack pointer so I have to do it by</p>



<p class="wp-block-paragraph">twos if I wanted to do 99 here then that means the stack is in alignment by the time I call</p>



<p class="wp-block-paragraph">by the time I&#8217;m getting ready to call printf which means the push pop pair around it throws</p>



<p class="wp-block-paragraph">push pop pair around it throws the stack out of alignment so just watch here I&#8217;ll</p>



<p class="wp-block-paragraph">prove it okay so it&#8217;s sake false I comment that out now it won&#8217;t throw it</p>



<p class="wp-block-paragraph">out of alignment okay should have known that before I recorded the video it&#8217;s</p>



<p class="wp-block-paragraph">fun to guess sometimes though you know I get a little nervous oh no my program</p>



<p class="wp-block-paragraph">broke on camera can I debug it live well I guess I can but kind of slowly</p>



<p class="wp-block-paragraph">anyway so we I think we&#8217;ve gone over every single part that I wanted to show you</p>



<p class="wp-block-paragraph">we know how to create a local array on the stack and and therefore you also know how to create like</p>



<p class="wp-block-paragraph">any other data type on the stack if you want you want to create a long a 64-bit integer</p>



<p class="wp-block-paragraph">just move it by eight bytes instead of moving it by eight times however many integers we were doing</p>



<p class="wp-block-paragraph">in this video you want to I don&#8217;t know put a character on the stack you can do that if you</p>



<p class="wp-block-paragraph">Just, you know, move it by one memory location instead of eight memory locations.</p>



<p class="wp-block-paragraph">So, you know, one byte instead of eight bytes.</p>



<p class="wp-block-paragraph">You want to store a short, you know, a two byte integer?</p>



<p class="wp-block-paragraph">Well, just move it by two bytes instead of eight bytes, right?</p>



<p class="wp-block-paragraph">So you can do this as many times as you want.</p>



<p class="wp-block-paragraph">You want to have several local variables?</p>



<p class="wp-block-paragraph">Just move it one time for every local variable.</p>



<p class="wp-block-paragraph">Same thing for accessing.</p>



<p class="wp-block-paragraph">You just have to remember where everything is.</p>



<p class="wp-block-paragraph">offset of the of the first variable local variable and then the offset of the second</p>



<p class="wp-block-paragraph">local variable you can store those in globals or store those in registers if you can you just</p>



<p class="wp-block-paragraph">got to remember somehow where everything starts but it&#8217;s all sitting on the stack if it&#8217;s a local</p>



<p class="wp-block-paragraph">variable okay I guess that&#8217;s everything that I really have to say I hope you enjoyed this video</p>



<p class="wp-block-paragraph">I hope you learned a little bit of stuff and had a little bit of fun I&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">have a good one</p>



<p class="wp-block-paragraph">you</p>



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



<p class="wp-block-paragraph">I do hope you did learn something and have some fun</p>



<p class="wp-block-paragraph">If you could do me a please a small little favor</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is?</p>



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



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



<p class="wp-block-paragraph">more videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general.</p>



<p class="wp-block-paragraph">So please do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of</p>



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed. It just wakes</p>



<p class="wp-block-paragraph">me up and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do</p>



<p class="wp-block-paragraph">it as a nice favor to me or you could troll me if you want to just wake me up in the middle of</p>



<p class="wp-block-paragraph">the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now, you should see a QR code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video and</p>



<p class="wp-block-paragraph">It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p class="wp-block-paragraph">I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Please leave a comment or if you just want to say hey, what&#8217;s up? What&#8217;s going on?</p>



<p class="wp-block-paragraph">You know just send me a comment whatever I also wake up for those in the middle of the night</p>



<p class="wp-block-paragraph">I get I wake up in a cold sweat. I&#8217;m like</p>



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



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/">x86-64 Assembly: Local Variables, Stack Frames &amp; Alignment Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-local-variables-stack-frames-alignment-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &#038; More</title>
		<link>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/</link>
					<comments>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 18 Jan 2026 00:49:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional branching]]></category>
		<category><![CDATA[conditional jumps]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[JE JNE]]></category>
		<category><![CDATA[JL JG]]></category>
		<category><![CDATA[jump out of range]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[operating systems programming]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[signed unsigned jumps]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=287</guid>

					<description><![CDATA[<p>Learn how conditional branching really works in x86-64 assembly using CMP and conditional jumps like JE, JNE, JL, JG. See signed vs unsigned comparison instructions and real code examples that show how if-statements are implemented at the machine level.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Learn Conditional Branching in x86-64 Assembly (YASM/NASM) - CMP, JE, JNE, JL, JG &amp; More" width="1380" height="776" src="https://www.youtube.com/embed/aB_BOp0KCh8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">In this practical x86-64 assembly language tutorial we dive deep into conditional branching using YASM (also works with NASM) on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">We cover:</p>



<ul class="wp-block-list">
<li>The CMP instruction and how it sets the RFLAGS register</li>



<li>All major conditional jump instructions (JE, JNE, JL, JLE, JG, JGE, JB, JBE…)</li>



<li>Important difference between signed (JL/JG) vs unsigned (JB/JA) jumps</li>



<li>Real code examples showing how to implement if-style decisions</li>



<li>How to handle the &#8220;jump out of range&#8221; assembler error</li>



<li>Visual flow diagrams explaining control flow</li>
</ul>



<p class="wp-block-paragraph">Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.</p>



<p class="wp-block-paragraph">Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p class="wp-block-paragraph">Enjoy learning assembly!</p>



<p class="wp-block-paragraph">Intro to Conditional Branching in Assembly 00:00:00<br>Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23<br>Unconditional vs Conditional Jumps 00:01:39<br>How Conditional Branching Works 00:02:06<br>The CMP Instruction Explained 00:03:21<br>Overview of Conditional Jump Instructions 00:04:50<br>Signed vs Unsigned Jump Instructions 00:06:10<br>Jump Equal &amp; Jump Not Equal (JE/JNE) 00:07:00<br>Main Conditional Jumps Summary 00:08:14<br>Starting the Code Example 00:09:09<br>Program Structure &amp; Printing Setup 00:10:35<br>Function Prologue &amp; Getting User Input 00:13:21<br>First Branch: Is Number Greater Than 0? 00:15:17<br>Live Demo: Testing Greater Than Zero 00:21:53<br>Second Branch: Is Number Less Than 10? 00:22:52<br>Live Demo: Testing Range 0-10 00:26:06<br>Third Branch: Is Number == 5? 00:26:52<br>Live Demo: Testing Equal to 5 00:28:00<br>Jump Out of Range Error &amp; Workaround 00:29:00<br>Final Summary &amp; Closing 00:32:17<br>Thanks &amp; Call to Subscribe 00:32:57</p>



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



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



<p class="wp-block-paragraph">Find us on other social media here:</p>



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Please show your support!</p>



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



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



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



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



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



<p class="wp-block-paragraph">Hey there. In this video,</p>



<p class="wp-block-paragraph">we&#8217;re going to talk about conditional branching in an assembly program.</p>



<p class="wp-block-paragraph">We&#8217;re going to be looking at YASM x86-64 assembly or AMD 64 assembly</p>



<p class="wp-block-paragraph">within Ubuntu, but this will probably be a useful video.</p>



<p class="wp-block-paragraph">If you&#8217;re just thinking about how to branch in assembly in general.</p>



<p class="wp-block-paragraph">So for starters, I&#8217;m going to promote one of my favorite books,</p>



<p class="wp-block-paragraph">as I often do. This is a book that is free and open source. You can get a copy yourself. I did</p>



<p class="wp-block-paragraph">not write this. The author of this book is a genius. His name is Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">He made this whole book and released it under an open source license. So you can literally just go</p>



<p class="wp-block-paragraph">to his website and get a copy of this for free. You can use this to make yourself into an assembly</p>



<p class="wp-block-paragraph">expert. So I highly recommend this to everybody. Anyway, so I&#8217;m going to open up this book. I&#8217;m</p>



<p class="wp-block-paragraph">up this book i&#8217;m going to assume that maybe you&#8217;ll get a copy or you&#8217;re just going to follow along</p>



<p class="wp-block-paragraph">with what i&#8217;m doing on the screen but i&#8217;m going to open this book and i&#8217;m going to go to section 7</p>



<p class="wp-block-paragraph">which is entitled instruction set overview i&#8217;m going to double click it so open out open the</p>



<p class="wp-block-paragraph">subsections and then i&#8217;m going to click on 7.7 control control instructions and then under that</p>



<p class="wp-block-paragraph">i&#8217;m going to click on 7.7.3 conditional control instructions so uh that&#8217;s a lot of subsections</p>



<p class="wp-block-paragraph">So that&#8217;s a lot of subsections, isn&#8217;t it? I love a book with a ton of subsections. It makes things so much easier to find</p>



<p class="wp-block-paragraph">If you or someone you know is going to write a book, especially if it&#8217;s technical, please use lots and lots of subsections</p>



<p class="wp-block-paragraph">Anyway, so conditional control instructions in my last video</p>



<p class="wp-block-paragraph">We talked about the jump instruction where we can just be sitting somewhere in our program and then just jump somewhere else</p>



<p class="wp-block-paragraph">Anywhere we want based on the label so conditional branching or conditional control instructions</p>



<p class="wp-block-paragraph">conditional control instructions are kind of the same thing except they will</p>



<p class="wp-block-paragraph">decide whether or not to actually jump they might fall through to the next</p>



<p class="wp-block-paragraph">instruction or they might jump away to somewhere else so conditional branching</p>



<p class="wp-block-paragraph">or conditional jumping it basically kind of goes like this step one you compare</p>



<p class="wp-block-paragraph">two values to each other they will probably be sitting in registers and then</p>



<p class="wp-block-paragraph">conditional branching instruction or a conditional jump instruction there&#8217;s</p>



<p class="wp-block-paragraph">lots of synonyms for these but basically in the regular jump instruction that I</p>



<p class="wp-block-paragraph">just talked about in the last video you just jumped and it was only one thing</p>



<p class="wp-block-paragraph">that you had to do but in this case we first have to compare two items and then</p>



<p class="wp-block-paragraph">we&#8217;ll issue the jumping instruction because the jumping instruction will</p>



<p class="wp-block-paragraph">have to decide whether to jump or not based on the result of your comparison</p>



<p class="wp-block-paragraph">which makes sense right like if you&#8217;re going to implement something that&#8217;s like</p>



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



<p class="wp-block-paragraph">before we jump either into the block of the if or if we jump into the block of the else is whether</p>



<p class="wp-block-paragraph">or not that expression is true right so that&#8217;s kind of what the compare instruction is doing</p>



<p class="wp-block-paragraph">sort of in higher level languages like c++ it can get a lot more complicated than that but you know</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to do conditional branching instructions.</p>



<p class="wp-block-paragraph">Let me show you a few of those.</p>



<p class="wp-block-paragraph">Okay, so for starters, here&#8217;s the compare instruction right here on page 126.</p>



<p class="wp-block-paragraph">I think it&#8217;s actually 127 in the PDF, but basically, you know, 126.</p>



<p class="wp-block-paragraph">We do CMP followed by two operands.</p>



<p class="wp-block-paragraph">So if we want to compare, I don&#8217;t know, let&#8217;s say R12 with R13, that&#8217;s how you would do it.</p>



<p class="wp-block-paragraph">just a two operand instruction there&#8217;s no results that are going to get stored in one of these</p>



<p class="wp-block-paragraph">operands both of these operands are just for input the result actually gets stored in a special</p>



<p class="wp-block-paragraph">register called r flags which we hardly ever use or look at in in the most common assembly</p>



<p class="wp-block-paragraph">instructions and design patterns we just kind of like let the r flags do whatever it&#8217;s going to do</p>



<p class="wp-block-paragraph">and the compare instruction will fill that up so imagine that you know depending on whether r13 is</p>



<p class="wp-block-paragraph">is like greater than r12 or less than or they&#8217;re equal or something else is going on between the</p>



<p class="wp-block-paragraph">two of them the compare instruction just fills up all sorts of flags defining the relationship</p>



<p class="wp-block-paragraph">between those two registers or those two operands r12 and r13 and then after the r flags register</p>



<p class="wp-block-paragraph">is all set up then the conditional branching instruction will sort of rely on r flags in</p>



<p class="wp-block-paragraph">order to decide whether or not it&#8217;s going to jump or not okay so let&#8217;s see there are tons</p>



<p class="wp-block-paragraph">there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



<p class="wp-block-paragraph">of conditional branching instructions we can use the first two i want to show you and after this</p>



<p class="wp-block-paragraph">by the way i&#8217;m going to write a sample program just to show you how it is in practice but so the</p>



<p class="wp-block-paragraph">first one is je which means let&#8217;s jump if the two operands were equal so again you know we compare</p>



<p class="wp-block-paragraph">here we&#8217;ll say sets up r flags and then we&#8217;ll decide like what we&#8217;re going to do are we going</p>



<p class="wp-block-paragraph">to jump if they&#8217;re equal to some label i&#8217;ll write some label here so that means uh the je instruction</p>



<p class="wp-block-paragraph">will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p class="wp-block-paragraph">did then we&#8217;ll jump if not execution will fall down to the next line so i&#8217;ll put a nope instruction</p>



<p class="wp-block-paragraph">So if they were equal, then we&#8217;ll end up jumping to some label.</p>



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



<p class="wp-block-paragraph">And same thing for jump not equal and all these other ones.</p>



<p class="wp-block-paragraph">But there are actually two different versions of some of these instructions that kind of seem the same.</p>



<p class="wp-block-paragraph">Like for instance, this one right here, JL, that means jump if the first operand was less than the second operand.</p>



<p class="wp-block-paragraph">is a jump below that jumps also if the first operand was less than the second operand so</p>



<p class="wp-block-paragraph">what&#8217;s the difference between these two let me get my pen I love excuses to do my little pen now</p>



<p class="wp-block-paragraph">well I&#8217;ve got my pen it&#8217;s working oh I erased it okay okay it&#8217;s working anyway so notice how jump</p>



<p class="wp-block-paragraph">less than and jump below are basically doing the same thing but there&#8217;s one difference one of them</p>



<p class="wp-block-paragraph">One of them works on signed operands and one of them works on unsigned operands.</p>



<p class="wp-block-paragraph">So keep this in mind.</p>



<p class="wp-block-paragraph">If you&#8217;re comparing two signed integers, you want to use this group up here of instructions.</p>



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



<p class="wp-block-paragraph">And you definitely want to know what kind of data you have that you&#8217;re comparing.</p>



<p class="wp-block-paragraph">Notice though that jump equal and jump not equal, they don&#8217;t really care.</p>



<p class="wp-block-paragraph">not equal they don&#8217;t really care if the operands are signed or unsigned that&#8217;s because if you want</p>



<p class="wp-block-paragraph">to check to see if two things are equal you just look at all of their bits and if all of their</p>



<p class="wp-block-paragraph">bits are the same then you just say they&#8217;re equal you don&#8217;t even care actually if it&#8217;s a signed or</p>



<p class="wp-block-paragraph">unsigned integer if it&#8217;s a float if it&#8217;s a whatever you just you just go are all the bits equal or are</p>



<p class="wp-block-paragraph">at least one of the bits unequal then we&#8217;ll say it&#8217;s equal or not equal that&#8217;s it so keep that in</p>



<p class="wp-block-paragraph">mind for all of your jump instructions where you&#8217;re trying to see if things are equal or not</p>



<p class="wp-block-paragraph">or not equal you&#8217;re going to definitely be using these whether you&#8217;re using signed or unsigned</p>



<p class="wp-block-paragraph">integers or floats or anything else like that but when it comes to actually you know integers that</p>



<p class="wp-block-paragraph">are signed or unsigned these are the instructions you use for signed and these are the instructions</p>



<p class="wp-block-paragraph">whoops that you use for what have i done that you&#8217;ll use for unsigned i keep confusing the on</p>



<p class="wp-block-paragraph">off button for the red button is what i&#8217;m doing oh okay whatever i guess we&#8217;re done anyway</p>



<p class="wp-block-paragraph">I guess we&#8217;re done anyway.</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re just going to be using the signed integer instructions</p>



<p class="wp-block-paragraph">because that&#8217;s the example that I have set up.</p>



<p class="wp-block-paragraph">But just imagine you could very easily translate this to unsigned integers or floats.</p>



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



<p class="wp-block-paragraph">We&#8217;ve got jump less than, we&#8217;ve got jump less than or equal to,</p>



<p class="wp-block-paragraph">then we&#8217;ve got jump greater than, and then we&#8217;ve got jump greater than or equal to.</p>



<p class="wp-block-paragraph">And when you combine those with jump equal to or jump not equal to,</p>



<p class="wp-block-paragraph">jump not equal to that&#8217;s pretty much all the comparison that you need and if you want to do</p>



<p class="wp-block-paragraph">something much much more complex well then you can just you know start stacking logic on top of</p>



<p class="wp-block-paragraph">logic and and branching on top of branching to do something that&#8217;s like this is what i&#8217;m talking</p>



<p class="wp-block-paragraph">like if you want to do this expression and expression or expression and then maybe like a</p>



<p class="wp-block-paragraph">expression, right? If you want to do something super complicated, like we can do in a higher</p>



<p class="wp-block-paragraph">level language, you&#8217;re just going to have to make a bunch of mini jumps or find another</p>



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



<p class="wp-block-paragraph">comparison. All right. So I think that&#8217;s everything that I can show you inside of this book. Let&#8217;s</p>



<p class="wp-block-paragraph">go to the code now. So here&#8217;s a little empty code, source code, a page. I&#8217;m going to stick</p>



<p class="wp-block-paragraph">all my code inside of it. Another reminder that if you don&#8217;t know how to write assembly</p>



<p class="wp-block-paragraph">you don&#8217;t know how to write assembly at all you should see my other videos because i&#8217;m not going</p>



<p class="wp-block-paragraph">to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p class="wp-block-paragraph">or assemble or like do all the basics that i&#8217;m not talking about in this video i&#8217;ve covered them all</p>



<p class="wp-block-paragraph">in my other videos up to this point so for now we&#8217;re just going to focus only on conditional</p>



<p class="wp-block-paragraph">branching conditional jumping inside of an assembly module so i&#8217;m going to start off by setting up my</p>



<p class="wp-block-paragraph">and i&#8217;m gonna say paste and again you know this is covered in other videos but long story short</p>



<p class="wp-block-paragraph">i&#8217;m just going to be printing out a bunch of strings while this program executes so</p>



<p class="wp-block-paragraph">i&#8217;m going to announce that the branching test is going to begin and then i&#8217;m going to ask the user</p>



<p class="wp-block-paragraph">for some input and then i&#8217;m going to based on whatever the user inputs i&#8217;m going to just print</p>



<p class="wp-block-paragraph">out a message that sort of you know understands what the user inputted so if the user inputted</p>



<p class="wp-block-paragraph">something greater than zero it&#8217;s going to output that if they entered something that was less than</p>



<p class="wp-block-paragraph">10 it&#8217;s going to print that if they entered something that was equal to five it&#8217;s going to</p>



<p class="wp-block-paragraph">print that and then it&#8217;s going to say we&#8217;ve ended the branching tests then i have like a crlf again</p>



<p class="wp-block-paragraph">all this stuff is explained in other videos but we&#8217;re just basically printing so i&#8217;m going to</p>



<p class="wp-block-paragraph">start the text section of my program which is where all the instructions are and i&#8217;m going to</p>



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



<p class="wp-block-paragraph">just going to show you how to branch. Just trust me when I say that this helps me easily print</p>



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



<p class="wp-block-paragraph">But for you, if you&#8217;re trying to run this at home, you can just start hard coding numbers just to</p>



<p class="wp-block-paragraph">make sure that you can branch correctly and then eventually work your way up to a library that can</p>



<p class="wp-block-paragraph">videos in the distant future. I don&#8217;t know years from now. I don&#8217;t know.</p>



<p class="wp-block-paragraph">I might release the library or some other stuff to the casual viewer.</p>



<p class="wp-block-paragraph">So now here&#8217;s our entry point. Again,</p>



<p class="wp-block-paragraph">I mentioned that there&#8217;s going to be other parts of this program that I&#8217;m not</p>



<p class="wp-block-paragraph">showing. So this is a hybrid program where a C++ driver actually will call on</p>



<p class="wp-block-paragraph">my cool function. So just imagine from somewhere else in the program,</p>



<p class="wp-block-paragraph">you&#8217;re calling on the cool function. How do we know it&#8217;s a function?</p>



<p class="wp-block-paragraph">a label called cool and then it has a return statement at the very end.</p>



<p class="wp-block-paragraph">That&#8217;s pretty much all we need to know right now.</p>



<p class="wp-block-paragraph">Let me get rid of that load.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I always have the word load in all of my things.</p>



<p class="wp-block-paragraph">I think I put it in there on an early example</p>



<p class="wp-block-paragraph">and then I just kept copy pasting to my heart&#8217;s content and never stopped.</p>



<p class="wp-block-paragraph">We mark the function cool as global so that other modules can call on it</p>



<p class="wp-block-paragraph">like the C++ module that I&#8217;m not showing you.</p>



<p class="wp-block-paragraph">But if you&#8217;re writing a pure assembly module,</p>



<p class="wp-block-paragraph">you don&#8217;t really need to mark your functions as global.</p>



<p class="wp-block-paragraph">if everything&#8217;s within the same piece of source code it&#8217;s totally fine so then I&#8217;m going to make</p>



<p class="wp-block-paragraph">a function call to a function called branch test so very quickly before I actually add branch test</p>



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



<p class="wp-block-paragraph">story short all it does is just it prints a new line in the terminal and that&#8217;s it like it&#8217;s just</p>



<p class="wp-block-paragraph">it&#8217;s a convenience function I like to be able to call crlf and have a new line okay so let&#8217;s start</p>



<p class="wp-block-paragraph">our branch test function so here&#8217;s the signature it is just branch test with a</p>



<p class="wp-block-paragraph">void in front of it I&#8217;m gonna put a ret to say that this is a function that we</p>



<p class="wp-block-paragraph">will return from and I left myself a little note here saying that I&#8217;m gonna</p>



<p class="wp-block-paragraph">use register r12 as the number that the user inputs so that means I&#8217;m gonna have</p>



<p class="wp-block-paragraph">to preserve that because you have to respect the ABI and the ABI says that</p>



<p class="wp-block-paragraph">The ABI says that R12 is a Kali saved register.</p>



<p class="wp-block-paragraph">Again, see my other videos if you don&#8217;t know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to preserve it in a section that I call the prologue, or actually the</p>



<p class="wp-block-paragraph">book calls the prologue.</p>



<p class="wp-block-paragraph">I do that now though too.</p>



<p class="wp-block-paragraph">And, you know, restore it in a section called the epilogue.</p>



<p class="wp-block-paragraph">So we have prologue and epilogue.</p>



<p class="wp-block-paragraph">We can have a push-pop pair.</p>



<p class="wp-block-paragraph">And then in between, we can have, you know, the meat of our function.</p>



<p class="wp-block-paragraph">Okay, so for the introduction, I&#8217;m just going to write a few instructions that just print</p>



<p class="wp-block-paragraph">out a message.</p>



<p class="wp-block-paragraph">print out a message. So we&#8217;re going to print out that intro message and then call</p>



<p class="wp-block-paragraph">CRLF to print a new line and then return. So maybe actually at this point we could</p>



<p class="wp-block-paragraph">probably check to see if this program works by trying to run it.</p>



<p class="wp-block-paragraph">So let me say clear and make run.</p>



<p class="wp-block-paragraph">Okay. So now the driver just prints hello. The driver is</p>



<p class="wp-block-paragraph">elsewhere and we get that welcome message that says begin</p>



<p class="wp-block-paragraph">branching test and then the driver retains control after that. So</p>



<p class="wp-block-paragraph">let&#8217;s do let&#8217;s do a goodbye message real fast so we&#8217;ll do our goodbye message</p>



<p class="wp-block-paragraph">it&#8217;s just another string that I&#8217;m printing it&#8217;s no big deal right now but</p>



<p class="wp-block-paragraph">if I run the program again you can see that begin branching test and end</p>



<p class="wp-block-paragraph">branching test okay so far we&#8217;re not really doing branching yet the next thing</p>



<p class="wp-block-paragraph">we need to do is ask for a number so this is how I&#8217;m going to do it in my</p>



<p class="wp-block-paragraph">this is how I&#8217;m going to do it in my program. In your program it might be different or you might</p>



<p class="wp-block-paragraph">have a hard-coded number or something like that. So for now just trust me when I say that I&#8217;m going</p>



<p class="wp-block-paragraph">to print a prompt basically saying could you please enter a number and then the next two lines</p>



<p class="wp-block-paragraph">are going to be just calling on one of my custom functions that will input a number from the user</p>



<p class="wp-block-paragraph">so the user can type at the terminal and we will receive their number through REX which is the</p>



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



<p class="wp-block-paragraph">And we&#8217;re going to store it in R12.</p>



<p class="wp-block-paragraph">So that&#8217;s why I had to preserve R12,</p>



<p class="wp-block-paragraph">because I&#8217;m actually going to start,</p>



<p class="wp-block-paragraph">you know, messing with the value of R12 now.</p>



<p class="wp-block-paragraph">Okay, so we ask for a number,</p>



<p class="wp-block-paragraph">and then we sort of don&#8217;t do anything after that.</p>



<p class="wp-block-paragraph">If we run the program again,</p>



<p class="wp-block-paragraph">it&#8217;s just asking for a number,</p>



<p class="wp-block-paragraph">and I can just like type some stuff and hit enter,</p>



<p class="wp-block-paragraph">and then the program ends, nothing actually happens.</p>



<p class="wp-block-paragraph">We&#8217;re building, we&#8217;re building.</p>



<p class="wp-block-paragraph">So now let&#8217;s ask ourselves,</p>



<p class="wp-block-paragraph">is the number greater than zero?</p>



<p class="wp-block-paragraph">So let me copy paste some code here.</p>



<p class="wp-block-paragraph">some code here so right now we&#8217;ve just finished inputting a number from the user and we&#8217;ll ask</p>



<p class="wp-block-paragraph">you know is that number that they inputted greater than zero so again the first thing we do is we use</p>



<p class="wp-block-paragraph">a compare instruction cmp we give it two operands the order doesn&#8217;t necessarily matter um for</p>



<p class="wp-block-paragraph">instance uh right now i&#8217;m asking is r12 greater than zero so i can do a jump less than or equal</p>



<p class="wp-block-paragraph">to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p class="wp-block-paragraph">in a different order, but I&#8217;m just choosing to say, let&#8217;s compare the two and then let&#8217;s jump</p>



<p class="wp-block-paragraph">if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



<p class="wp-block-paragraph">which is just branch test after greater test. So I&#8217;m going to do a print statement right after that</p>



<p class="wp-block-paragraph">and then I&#8217;m going to draw out what the code is kind of doing. So let me make a label here so</p>



<p class="wp-block-paragraph">actually will compile. Whoops. I&#8217;m in the wrong window. Okay. There we go. So what am I saying</p>



<p class="wp-block-paragraph">here? Okay. Do the comparison. And then if the condition seems to be satisfied, then we&#8217;ll jump</p>



<p class="wp-block-paragraph">to this, which means we&#8217;ll just say goodbye and then exit the program because we&#8217;ll return to the</p>



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



<p class="wp-block-paragraph">in the middle. So think about this. This is kind of the idea. Let&#8217;s see if I can remember how to</p>



<p class="wp-block-paragraph">idea let&#8217;s see if I can remember how to draw this right now off the top of my head suppose we have</p>



<p class="wp-block-paragraph">like some sort of an input I&#8217;ll call this in put and then maybe the input is going to be you know</p>



<p class="wp-block-paragraph">whether r12 and r0 are equal or greater than or whatever so I&#8217;m just gonna say r12 and 0</p>



<p class="wp-block-paragraph">god that&#8217;s awful I really need to practice this draw pad I spend like a whole week practicing</p>



<p class="wp-block-paragraph">whole week practicing with a different draw pad and then when I come back to</p>



<p class="wp-block-paragraph">this one I&#8217;m even worse than when I started so we we come in and we kind of</p>



<p class="wp-block-paragraph">look at our 12 versus zero and then we will jump in one direction if that&#8217;s not</p>



<p class="wp-block-paragraph">you know greater than or equal to or less than or equal to and we&#8217;ll jump in</p>



<p class="wp-block-paragraph">another direction otherwise so basically here we&#8217;ll say jump less than equal to</p>



<p class="wp-block-paragraph">um less than or equal to whoops maybe i&#8217;ll do r12 uh i need to somehow adjust the pressure on this</p>



<p class="wp-block-paragraph">thing r12 less than or equal to and so this is sort of uh has to do with you know this branching</p>



<p class="wp-block-paragraph">path that we take like if r12 was less than or equal to zero then we&#8217;ll take the right branching</p>



<p class="wp-block-paragraph">to after, I&#8217;ll just put the word after,</p>



<p class="wp-block-paragraph">we&#8217;ll jump to the after greater test sub label.</p>



<p class="wp-block-paragraph">And otherwise, if R12 is, sorry,</p>



<p class="wp-block-paragraph">I said less than or equal to, yeah,</p>



<p class="wp-block-paragraph">if R12 is greater than zero,</p>



<p class="wp-block-paragraph">then we jump to this other thing,</p>



<p class="wp-block-paragraph">which is gonna be the fall through.</p>



<p class="wp-block-paragraph">So it&#8217;s gonna be line 96, but I&#8217;ll just say,</p>



<p class="wp-block-paragraph">I&#8217;ll say fall for fall through.</p>



<p class="wp-block-paragraph">basically the jump won&#8217;t happen if it&#8217;s greater than or equal to so you could imagine if you</p>



<p class="wp-block-paragraph">wanted to reverse the logic so you can use a jump greater than instruction you could but I&#8217;m choosing</p>



<p class="wp-block-paragraph">to do it this way and then both of those will eventually reach let&#8217;s see will eventually</p>



<p class="wp-block-paragraph">actually yeah the fall through instructions those will eventually reach the after instruction I&#8217;ll</p>



<p class="wp-block-paragraph">here and then the after instruction will just kind of like finish the program</p>



<p class="wp-block-paragraph">and exit and all that stuff. So if you think about the control path here,</p>



<p class="wp-block-paragraph">we have an input we&#8217;re looking at R12 with the zero with the compare instruction</p>



<p class="wp-block-paragraph">and then we use a conditional branching instruction. So maybe I should write,</p>



<p class="wp-block-paragraph">what do you want to write here? Maybe I&#8217;ll just put JLE up here because usually the compare</p>



<p class="wp-block-paragraph">usually the compare instruction and the conditional jump or branching instructions</p>



<p class="wp-block-paragraph">kind of you know come as a pair so both of these combined mean let&#8217;s look at r12 versus zero and</p>



<p class="wp-block-paragraph">if r12 is less than or equal to zero meaning if it will jump if we&#8217;re less than or equal to the</p>



<p class="wp-block-paragraph">the right operand r12 less than equal to the right operand then we go down this path on the right</p>



<p class="wp-block-paragraph">to the after label if that is false meaning r12 was greater than zero then we&#8217;ll fall through</p>



<p class="wp-block-paragraph">this stuff right here and then whether or not we actually fall through will always end up at the</p>



<p class="wp-block-paragraph">at the after area because we&#8217;re either going to jump directly to the after area or we&#8217;re going</p>



<p class="wp-block-paragraph">to fall through and then the fall through falls through to the after area anyway as well let me</p>



<p class="wp-block-paragraph">just show you that real fast oh man i&#8217;m having a hard time with this okay so let&#8217;s pretend that r12</p>



<p class="wp-block-paragraph">less than or equal to. So that means instructions are coming, you know,</p>



<p class="wp-block-paragraph">instructions are getting executed. We&#8217;re going down and down and down and down and down.</p>



<p class="wp-block-paragraph">Once we see jump less than or equal to, if that&#8217;s true, then we jump to the after label,</p>



<p class="wp-block-paragraph">which means we just kind of jump around this message. And so we&#8217;re not going to exit,</p>



<p class="wp-block-paragraph">sorry, we&#8217;re not going to execute that code. So in the case where that is true,</p>



<p class="wp-block-paragraph">where it is less than or equal to, we&#8217;re not going to execute those instructions.</p>



<p class="wp-block-paragraph">So we&#8217;re not going to say their number was greater than zero. We&#8217;re just going to simply</p>



<p class="wp-block-paragraph">We&#8217;re just going to simply say goodbye.</p>



<p class="wp-block-paragraph">However, on the other hand, if that is not true, meaning if R12 was indeed greater than</p>



<p class="wp-block-paragraph">zero, then we&#8217;re going to fall through because this jump less than or equal to instruction,</p>



<p class="wp-block-paragraph">it will only jump if the R flags, if the comparison instruction thought that R12 was less than</p>



<p class="wp-block-paragraph">or equal to zero.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, if it&#8217;s greater than, then instructions, the control path is just</p>



<p class="wp-block-paragraph">the control path is just going to fall through.</p>



<p class="wp-block-paragraph">So that means we will actually execute these.</p>



<p class="wp-block-paragraph">And then when they&#8217;re done,</p>



<p class="wp-block-paragraph">execution will continue to fall through to the rest.</p>



<p class="wp-block-paragraph">So that means we&#8217;re either going to see a message saying</p>



<p class="wp-block-paragraph">their number was greater than zero or not,</p>



<p class="wp-block-paragraph">based on whether their number actually was greater than zero.</p>



<p class="wp-block-paragraph">Okay, hopefully I explained that somewhat clearly.</p>



<p class="wp-block-paragraph">Now let&#8217;s run the code and see if it works.</p>



<p class="wp-block-paragraph">Hopefully I didn&#8217;t forget anything.</p>



<p class="wp-block-paragraph">This is kind of a partial program at this point.</p>



<p class="wp-block-paragraph">program at this point I&#8217;m gonna add a bunch more stuff to it so hopefully</p>



<p class="wp-block-paragraph">this actually compiles enter an integer five notice how it says your number was</p>



<p class="wp-block-paragraph">greater than zero and if I run it again and I just type zero it does not say</p>



<p class="wp-block-paragraph">that it was greater than zero if I run it again I type like a negative five it</p>



<p class="wp-block-paragraph">also does not say that it was greater than zero you know a huge giant number</p>



<p class="wp-block-paragraph">here it&#8217;ll say your number was greater than zero you could probably type a</p>



<p class="wp-block-paragraph">number that was just absolutely huge and have it overflow I actually don&#8217;t know</p>



<p class="wp-block-paragraph">nines I need to type and I don&#8217;t even know what the behavior is going to be.</p>



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



<p class="wp-block-paragraph">to input a number which is happening in a different library.</p>



<p class="wp-block-paragraph">Let&#8217;s see if that even works greater than zero.</p>



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



<p class="wp-block-paragraph">It probably overflowed and went to a very, very low negative number.</p>



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



<p class="wp-block-paragraph">I guess at least I know my library will probably work sort of.</p>



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



<p class="wp-block-paragraph">So let&#8217;s go back to the code here and let&#8217;s upgrade it.</p>



<p class="wp-block-paragraph">how to to check for jump less than or equal to um we&#8217;re going to say goodbye right there so now the</p>



<p class="wp-block-paragraph">next thing we should do is check to see if the number was less than 10. so after the greater test</p>



<p class="wp-block-paragraph">it&#8217;s going to be here so again the same thing like we can just reuse r12 because we&#8217;re not really</p>



<p class="wp-block-paragraph">modifying it and we&#8217;re not calling uh well i guess even if we were calling a function r12 was supposed</p>



<p class="wp-block-paragraph">supposed to be Kali preserved so we&#8217;re just comparing it and then maybe</p>



<p class="wp-block-paragraph">printing a message if we want to and regardless we&#8217;re going to end up at 103</p>



<p class="wp-block-paragraph">this after greater test label so then we&#8217;ll compare again r12 with the number</p>



<p class="wp-block-paragraph">10 and we&#8217;ll say jump if it&#8217;s greater than or equal to branch test after less</p>



<p class="wp-block-paragraph">so now at this point we&#8217;re saying compare r12 with 10 if it&#8217;s greater than or equal to 10 then</p>



<p class="wp-block-paragraph">just skip over the message so if it&#8217;s greater than or equal to 10 then wait a minute wait wait</p>



<p class="wp-block-paragraph">yeah yeah if it&#8217;s greater than or equal to 10 then skip the message otherwise the message</p>



<p class="wp-block-paragraph">that is printed is going to say that their number is less than 10. so again you could imagine a</p>



<p class="wp-block-paragraph">want me to write it out for you I don&#8217;t know I think once is probably enough but let me just do</p>



<p class="wp-block-paragraph">it again we&#8217;ll do input is coming in and it&#8217;s just going to be you know comparing and then</p>



<p class="wp-block-paragraph">jumping greater than or equal to the input is going to be where we&#8217;re just looking to see if</p>



<p class="wp-block-paragraph">R12 how it compares to zero god the number one is awful my entire penmanship is awful okay one</p>



<p class="wp-block-paragraph">we&#8217;ll do r12 and 10 and so that&#8217;s just sort of what we&#8217;re comparing with the compare instruction</p>



<p class="wp-block-paragraph">and then the jump instruction and so if we go on the left i&#8217;m going to say that&#8217;s the fall through</p>



<p class="wp-block-paragraph">path again and if we go to the right it&#8217;s going to be um r12 greater than or equal to 10 is going</p>



<p class="wp-block-paragraph">and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



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



<p class="wp-block-paragraph">here that&#8217;s just like print p for how about p for print p for print it&#8217;s going to fall through and</p>



<p class="wp-block-paragraph">then it&#8217;s going to print and then it&#8217;s going to go to the after label so you can see that</p>



<p class="wp-block-paragraph">the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



<p class="wp-block-paragraph">to 10 which is the same thing as saying it&#8217;s going to fall through if r12 is less than</p>



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



<p class="wp-block-paragraph">And then after we finally get to the after label, then we&#8217;re just going to do, you know,</p>



<p class="wp-block-paragraph">the rest of the instructions.</p>



<p class="wp-block-paragraph">We&#8217;re going to exit the program.</p>



<p class="wp-block-paragraph">We&#8217;re going to say goodbye.</p>



<p class="wp-block-paragraph">We&#8217;re going to do whatever.</p>



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



<p class="wp-block-paragraph">So say there never was less than 10.</p>



<p class="wp-block-paragraph">And then regardless, here&#8217;s like the ending label that we were conditionally jumping to.</p>



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



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



<p class="wp-block-paragraph">So we&#8217;ll enter five.</p>



<p class="wp-block-paragraph">we&#8217;ll enter five notice how it says your number was greater than zero and your</p>



<p class="wp-block-paragraph">number was less than ten let&#8217;s do an 11 I guess it says your number was greater</p>



<p class="wp-block-paragraph">than zero but it does not say that our number was less than ten because 11 is</p>



<p class="wp-block-paragraph">not less than ten if I do a negative three here it says that our number was</p>



<p class="wp-block-paragraph">less than ten but it doesn&#8217;t say that it&#8217;s greater than zero so we have like a</p>



<p class="wp-block-paragraph">lot of decisions that we could make right like imagine if instead of just</p>



<p class="wp-block-paragraph">based on something happening, you could call a full function or skip a bunch of extra code,</p>



<p class="wp-block-paragraph">you know, whatever you want it to do.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go to the next condition where we&#8217;re going to compare R12 to the number five.</p>



<p class="wp-block-paragraph">And we&#8217;re going to ask, you know, is R12 equal to five?</p>



<p class="wp-block-paragraph">So this is going to be the jump not equal branch.</p>



<p class="wp-block-paragraph">And again, you know, if you wanted to say jump, if it&#8217;s equal to the place where we</p>



<p class="wp-block-paragraph">and then maybe like right after that fall through and just do an unconditional jump you can</p>



<p class="wp-block-paragraph">I just think there are less jumps if I do it this way so uh here we&#8217;re just going to print you know</p>



<p class="wp-block-paragraph">your number was equal to five and so basically if it was not equal we should have another label here</p>



<p class="wp-block-paragraph">that allows us to skip that so all right so same thing I&#8217;m not going to draw the diagram this time</p>



<p class="wp-block-paragraph">R12 with 5 and if it&#8217;s not equal to 5 then jump to this label down here which just means</p>



<p class="wp-block-paragraph">let&#8217;s say goodbye and not actually say that their number was equal to 5.</p>



<p class="wp-block-paragraph">But if it was equal to 5 then this JNE conditional branch won&#8217;t actually jump anywhere.</p>



<p class="wp-block-paragraph">Execution will fall through to the next statements which is just printing the message that their</p>



<p class="wp-block-paragraph">number is equal to 5 and then when that&#8217;s done it&#8217;ll fall through to this other label</p>



<p class="wp-block-paragraph">and eventually fall through to the return statement.</p>



<p class="wp-block-paragraph">the whole thing already nice that was easy um let&#8217;s do another run and um let&#8217;s enter the</p>



<p class="wp-block-paragraph">integer one it says one is uh greater than zero and one is less than 10</p>



<p class="wp-block-paragraph">greater than zero and less than 10 is there any way i can get nothing to print out</p>



<p class="wp-block-paragraph">greater than zero and less than 10</p>



<p class="wp-block-paragraph">you don&#8217;t think i can do it let me try it nine no</p>



<p class="wp-block-paragraph">why did I even put 9 okay yeah it&#8217;s always gonna say 0 or 10 okay so let&#8217;s</p>



<p class="wp-block-paragraph">do a 4 we&#8217;ll get both of those greater than 0 and also less than 10 let&#8217;s do a</p>



<p class="wp-block-paragraph">5 now we should get all those messages your number was greater than 0 your</p>



<p class="wp-block-paragraph">number was less than 10 your number was equal to 5 and if I just do a 6 in there</p>



<p class="wp-block-paragraph">again it doesn&#8217;t print your number was equal to 5 and we have to enter an</p>



<p class="wp-block-paragraph">actual 5 for that so that&#8217;s the that&#8217;s the basics of</p>



<p class="wp-block-paragraph">conditional branching oh there&#8217;s one other thing that I wanted to make sure</p>



<p class="wp-block-paragraph">in my previous video where we only talked about jumps bear in mind that</p>



<p class="wp-block-paragraph">these conditional branching statements or instructions you know je j any all</p>



<p class="wp-block-paragraph">these things they have a limited range of jumps of where they can jump to so if</p>



<p class="wp-block-paragraph">you have a gigantic program and you&#8217;re trying to jump very very very far away</p>



<p class="wp-block-paragraph">based on a condition you might have an assembler error where your program won&#8217;t</p>



<p class="wp-block-paragraph">even compile won&#8217;t even assemble telling you i think the message is i wrote it</p>



<p class="wp-block-paragraph">I wrote it down somewhere jump out of range coming from the assembler that basically means that these</p>



<p class="wp-block-paragraph">instructions can only jump about 128 bytes away from whatever instruction they are at or whatever</p>



<p class="wp-block-paragraph">memory location they are at so you know after your assembler assembles your program and after</p>



<p class="wp-block-paragraph">your linker links your program and you just have like a binary you know if you were to inspect</p>



<p class="wp-block-paragraph">all the instructions inside of your executable your binary you would see that they have offsets</p>



<p class="wp-block-paragraph">offsets, right? You know, like one instruction is basically eight bytes away from the one that</p>



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



<p class="wp-block-paragraph">to an instruction that is 100 is more than 128 bytes away, it won&#8217;t work, you&#8217;ll get that error.</p>



<p class="wp-block-paragraph">So how do you solve that problem? Well, maybe I&#8217;ll just write this down real fast. I&#8217;m not going to</p>



<p class="wp-block-paragraph">make this part of the code example. But just imagine we have, you know, jump equal to some</p>



<p class="wp-block-paragraph">and maybe I&#8217;ll do a comment very very far away and here&#8217;s the label some label right</p>



<p class="wp-block-paragraph">so if your assembler says hey you can&#8217;t jump to some label because it&#8217;s too far away it&#8217;s a</p>



<p class="wp-block-paragraph">jump out of range assembler error then basically you can just make some other labels here you can</p>



<p class="wp-block-paragraph">say uh you can say short jump true and short jump false you know just make up some sort of a label</p>



<p class="wp-block-paragraph">label and basically say if it is um well false yeah okay i guess this is the way i&#8217;ll do it this</p>



<p class="wp-block-paragraph">is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p class="wp-block-paragraph">can just jump to the true label so i&#8217;m going to say jump uh to the short jump true label otherwise</p>



<p class="wp-block-paragraph">To the false label. So now you&#8217;re definitely jumping in either case and both of these labels are close by</p>



<p class="wp-block-paragraph">So then I can pretty much just have the close by true label do an unconditional</p>



<p class="wp-block-paragraph">Jump to some label and that overcomes the limitation of branching instructions</p>



<p class="wp-block-paragraph">So, you know JMP the regular jump instruction</p>



<p class="wp-block-paragraph">It has an unlimited jump that it can do it can go just to like very very very far away instructions</p>



<p class="wp-block-paragraph">And then for the false, you know, I don&#8217;t know, do whatever you want and then jump wherever you want.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really matter.</p>



<p class="wp-block-paragraph">I&#8217;m just trying to show you that if you make a short jump to a label that will only have an unconditional jump,</p>



<p class="wp-block-paragraph">so it&#8217;s sort of like two jumps, you know, like a short jump and then a long jump,</p>



<p class="wp-block-paragraph">then you can overcome that limitation.</p>



<p class="wp-block-paragraph">Again, bearing in mind that some label is supposed to be very, very far away and sort of unreachable.</p>



<p class="wp-block-paragraph">Okay, so let me just double check that I talked about everything that I wanted to.</p>



<p class="wp-block-paragraph">We looked at the branching instructions, the sign in this, I drew a diagram for you.</p>



<p class="wp-block-paragraph">We solved the jump out of range error.</p>



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



<p class="wp-block-paragraph">Hopefully you feel like an expert now at conditional branching and how to make decisions.</p>



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



<p class="wp-block-paragraph">and all that stuff.</p>



<p class="wp-block-paragraph">decisions in yasm x86-64 assembly and conceptually you can apply this to other assembly languages too</p>



<p class="wp-block-paragraph">so thanks for watching my video i hope you learned a little bit of stuff and had a little bit of fun</p>



<p class="wp-block-paragraph">i&#8217;ll see you in the next video</p>



<p class="wp-block-paragraph">hey everybody thanks for watching this video again from the bottom of my heart i really appreciate it</p>



<p class="wp-block-paragraph">i do hope you did learn something and have some fun uh if you could do me a please a small little</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now?</p>



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



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



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



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



<p class="wp-block-paragraph">or followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



<p class="wp-block-paragraph">subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a QR code which</p>



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



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



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the</p>



<p class="wp-block-paragraph">all the videos i published and the services and tutorials and things that i offer and all that</p>



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



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
