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

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>computer architecture Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/computer-architecture/</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>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>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>Like + subscribe if you want more clear, practical assembly tutorials!</p>



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



<p>Thanks for watching!</p>



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



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



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



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



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



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



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



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



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



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



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



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



<li>Subscribe to 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>Hi there! Today I&#8217;d like to talk to you about local variables on the stack</p>



<p>in an x8664 assembly program written in YASM.</p>



<p>If you don&#8217;t understand assembly or local variables or other things like that you might</p>



<p>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>about with local variables on the stack? For starters forget what you&#8217;re seeing here for a</p>



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



<p>video but pretend this is c plus plus so suppose you have c plus plus you have a main function here</p>



<p>and at the end of it you know this is your entry point for your program we return zero and maybe</p>



<p>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>in this video but you should probably use prototypes um maybe main calls on f and so then</p>



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



<p>that comes in. If you&#8217;ve watched my other videos, hopefully you have by now,</p>



<p>you&#8217;ll know that A comes into the function as RDI because that&#8217;s the first integer argument</p>



<p>register. But then when we start creating local variables, we&#8217;ll say integer B equals, let&#8217;s say,</p>



<p>a five and then integer C is equal to an eight for whatever reason. Maybe there will be an array.</p>



<p>So I&#8217;ll call this a int array, I guess.</p>



<p>We&#8217;ll say that we have 100 integers in our array.</p>



<p>We could also have a pointer, int pointer p,</p>



<p>and then allocate it to some kind of new memory just to prove a point.</p>



<p>And then, you know, later in your function,</p>



<p>you&#8217;re probably going to want to do something with your data.</p>



<p>So I don&#8217;t know, maybe a, how about like b is plus equal to a,</p>



<p>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>honestly I&#8217;m just showing you that we use our variables and then maybe you want to build an</p>



<p>array so we just declared the array up here on line six but maybe you want to actually do something</p>



<p>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>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>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>and then we&#8217;ll do b plus plus just to have something in there okay so let me explain the</p>



<p>parts of your program real fast if you haven&#8217;t watched my other videos please do because this</p>



<p>should help a little bit anyway so a is an incoming argument like I said before that&#8217;s</p>



<p>usually coming to you in the RDI register because it&#8217;s the first integer argument so we don&#8217;t need</p>



<p>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>created on the stack and c is created on the stack also and the array is created on the stack by the</p>



<p>way I could name the array anything if I wanted to like just v I&#8217;m using the name array because</p>



<p>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>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>But the memory we allocated and then gave to the pointer, that&#8217;s sitting in the heap somewhere.</p>



<p>So when you use the new operator or the malloc operator, or you&#8217;re just like making dynamic memory,</p>



<p>that dynamic memory sits in the heap, but the pointer itself, since we just declared it here,</p>



<p>that sits on the stack. That means later on when your function ends, the p pointer itself will get</p>



<p>cleaned up automatically and itself won&#8217;t be leaked memory. But if you forget to clean up your memory</p>



<p>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>some other videos somewhere else at another time. Anyway, so then we have our local variables here.</p>



<p>remember B and C and array are all on the stack and well we just have our</p>



<p>for loop here where we just kind of start modifying data so why would we use</p>



<p>the stack let me just do a quick tutorial this is not a stack video I&#8217;m</p>



<p>gonna make a stack video in the future but I&#8217;ll just do like a quick little</p>



<p>rundown of what is the stack and why is it really good for function calls</p>



<p>consider that sometimes F might want to call itself maybe it&#8217;s a recursive</p>



<p>be like a long chain of function calls like f calls g and g calls h and h calls you know whatever</p>



<p>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>calling themselves in one circle that never ends you should be allowed to do that we should be</p>



<p>allowed to have a function f that sometimes calls itself or other functions or maybe it&#8217;s called</p>



<p>multiple times within our giant call stack our call graph and this will work because these</p>



<p>you know a b and c and array are sitting on the stack and the stack allows you to have sort of</p>



<p>different instances of variables so for example and this is just a quick thing i&#8217;m not this is</p>



<p>not supposed to be a full stack tutorial here but um you know if we have like the function f</p>



<p>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>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>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>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>F again. Let&#8217;s just pretend that we can have a call graph where F is sometimes called by something</p>



<p>and sometimes called something else or maybe sometimes F is called by a different thing. So</p>



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



<p>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>and then G calls H and then H calls F and then F calls I I guess and then</p>



<p>eventually these functions start returning right oops how come my greens</p>



<p>not working green there we go too late now so we can have like a really</p>



<p>complicated call graph and F might appear in there multiple times and the</p>



<p>Each call to F along the call graph should have its own copy, unique copy of local variables.</p>



<p>So the B for this first F call, let me just do an arrow here, will have a B.</p>



<p>And you can imagine it as B subscript 1.</p>



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



<p>So we can imagine this as B subscript 2.</p>



<p>So these are two totally different Bs.</p>



<p>If you tried to use global variables for this, it would be really, really, really hard to get the code to work.</p>



<p>get the code to work and it would be really, really hard to debug.</p>



<p>So that&#8217;s kind of why we have local variables.</p>



<p>The stack allows us to do this.</p>



<p>So what is the stack itself?</p>



<p>Again, this is not a full tutorial on the stack,</p>



<p>but I just want you to kind of see what&#8217;s happening.</p>



<p>Imagine a data structure that kind of grows upward.</p>



<p>I&#8217;ll say that here&#8217;s the floor at the bottom.</p>



<p>And when you put an item onto the stack,</p>



<p>the items kind of stack on top of each other.</p>



<p>So imagine that this is the call to F.</p>



<p>is the call to f and embedded within this little stack frame area we&#8217;ll call it a stack frame but</p>



<p>actually the stack continues to grow as you create and destroy local variables within a function call</p>



<p>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>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>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>We got to do B and C and the array.</p>



<p>I&#8217;ll put AR for array.</p>



<p>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>And then, you know, maybe F calls G and some other stuff happens.</p>



<p>I&#8217;ll just pretend that a bunch of other stuff happened.</p>



<p>And then eventually F is called again.</p>



<p>But there&#8217;s a different, this is a different instance of the call to F.</p>



<p>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>also have variables that we can create locally that are supposed to be separate from the original</p>



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



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



<p>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>They&#8217;re not going to be called B1 and B2, but they&#8217;re just two separate instances.</p>



<p>And I just want you to know.</p>



<p>So then when we start returning from functions, like when this F eventually returns, and by</p>



<p>the way, I know that what I&#8217;m drawing on the right doesn&#8217;t match the code because this</p>



<p>is not a function that calls itself, but just suppose that your code is a lot more complicated</p>



<p>than what I drew up.</p>



<p>When it eventually returns, all that happens is those items on the stack just sort of get</p>



<p>not necessarily deallocated, but ignored.</p>



<p>We&#8217;ll just say that they&#8217;re gone.</p>



<p>They&#8217;re still sitting as junk data in system RAM somewhere.</p>



<p>And in assembly talk, we know that we have a stack pointer called RSP.</p>



<p>We have a register called RSP that just sort of points to the location in the</p>



<p>stack that is considered the top of the stack, like the most recent piece of data</p>



<p>that we have available.</p>



<p>So all the other data is actually still kind of above, but we&#8217;re not pointing to it</p>



<p>anymore, so we consider that it doesn&#8217;t exist.</p>



<p>So then when G eventually returns, you know, we just change the stack pointer,</p>



<p>Rsp, to point to that other piece of data.</p>



<p>The G data frame and the other F data frame are still sitting above somewhere in memory,</p>



<p>but we just ignore them, right?</p>



<p>So that&#8217;s how the stack works.</p>



<p>And that&#8217;s how we have local function call copies of all of our local variables.</p>



<p>Something to note.</p>



<p>Something to note, this is not a stack video, but you know, just something to note that</p>



<p>even though I draw the stack visually as growing vertically up, when you actually manipulate</p>



<p>the stack in assembly or just like in any language, the stack grows downward in terms</p>



<p>of memory locations.</p>



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



<p>Imagine this is a memory location 80, we&#8217;ll say.</p>



<p>You would think that memory location 81 would be the next item of the stack, or I guess</p>



<p>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>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>even though we imagine the stack growing uh upward vertically just so you know that&#8217;s the kind of</p>



<p>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>create local variables and i&#8217;m just going to show you how to create an array because this array is</p>



<p>just like a bunch of integers and you can imagine it would be really easy to create only one integer</p>



<p>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>to show you malloc or anything like that we&#8217;re just going to look at the local variables okay</p>



<p>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>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>The first source code file here is just driver.cpp.</p>



<p>Again, this is a hybrid program,</p>



<p>so I&#8217;m going to mix my C++ modules with my assembly modules,</p>



<p>which is pretty cool.</p>



<p>The whole point of the driver is just to contain the entry point, you know, main.</p>



<p>And I&#8217;m just going to print a hello message.</p>



<p>And then I&#8217;m going to call the real function that I&#8217;m interested in,</p>



<p>which I&#8217;ve named local underscore varrs.</p>



<p>And that&#8217;s going to be all the assembly stuff that we talked about.</p>



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



<p>here&#8217;s the real heart of what we got to do. Let&#8217;s write up an assembly module</p>



<p>that can do local variables. Okay. So again, if you don&#8217;t know assembly, that&#8217;s okay,</p>



<p>but you need to watch my other videos before you can understand this one. So I&#8217;m going to just</p>



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



<p>So I&#8217;ve got a data section up top and I&#8217;m just going to define some messages.</p>



<p>So, you know, I&#8217;ve got like an intro message that just says, hello, I&#8217;m so and so.</p>



<p>And that&#8217;s not my name, but I like those kinds of names.</p>



<p>And then over here, I&#8217;m going to do some printf formatted strings.</p>



<p>That&#8217;s why I&#8217;m using a hybrid program for this video.</p>



<p>I don&#8217;t want to import my own personal library.</p>



<p>I want you to be able to do this at home with just the GCC libraries.</p>



<p>link a hybrid library you know linking against gcc instead of linking against ld again if you don&#8217;t</p>



<p>know that stuff check my other videos then we&#8217;re allowed to call c functions in this case we&#8217;re</p>



<p>going to call printf and we&#8217;re just going to give it the string percent lu meaning i would like you</p>



<p>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>stack representing a local variable and then i want it to print as just like a long like a string</p>



<p>along like a string that a human can read then after that this is the carriage return line feed</p>



<p>the crlf printf won&#8217;t flush its output unless that is sitting at the very end of the string so</p>



<p>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>string so that printf doesn&#8217;t freak out and try to print a bunch of stuff after the crlf and uh</p>



<p>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>CRLF in this video because we&#8217;re just putting it directly inside of the printf string we&#8217;re not</p>



<p>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>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>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>know maybe if you want to imagine 100 you know like the example that I just showed I&#8217;m going to</p>



<p>going to define what is the integer size so i&#8217;m going to use quad words which are 64 bit integers</p>



<p>so i&#8217;m just going to say that there are eight bytes per integer that will help me multiply later</p>



<p>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>that i can just to prove that we can like uh you know manipulate and and fetch the values</p>



<p>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>with this we should expect to see like an array of numbers that starts with seven and it just</p>



<p>kind of increases somehow then i&#8217;m going to do some system call codes we talked about that in</p>



<p>a different video and then some file descriptors i don&#8217;t think we actually need anything but standard</p>



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



<p>let me just do copy paste on my solution here so here&#8217;s the text section section text</p>



<p>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>which is a function sitting in the GCC libraries when I link against a GCC I have the ability to</p>



<p>do that that way I don&#8217;t have to come up with like a complicated printing method or use one</p>



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



<p>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>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>function starts and here&#8217;s the return statement saying that we&#8217;re done with the function i&#8217;m not</p>



<p>going to manipulate any registers inside of the function so i don&#8217;t really need to do any push pop</p>



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



<p>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>is just to, you know, print a welcome message to the user.</p>



<p>So nothing that I haven&#8217;t talked about before in other videos.</p>



<p>So it&#8217;s just we&#8217;re using a system call to print a string.</p>



<p>Okay, so with that in mind, let me open this up here and see if this is going to work.</p>



<p>I just want to basically print the welcome message at this point.</p>



<p>Clear and make run.</p>



<p>And again, if you don&#8217;t know make files or anything like that, see my other videos.</p>



<p>So this is the driver, I think, that prints.</p>



<p>think that Prince maybe I should change the driver&#8217;s message to make it more</p>



<p>clear hello about this is the driver and my name is whatever I&#8217;m gonna do it again</p>



<p>and now it says hello this is the driver okay so that&#8217;s the CPP module and then</p>



<p>here is the assembly module and then finally the driver gets control back and</p>



<p>then the program exits so nothing really happened so now let&#8217;s upgrade the</p>



<p>So now let&#8217;s upgrade the assembly module a little bit.</p>



<p>Next thing I want to add is the actual demo function, which is going to be absolutely huge.</p>



<p>So first I&#8217;m going to start off with, how about just the signature here?</p>



<p>Let&#8217;s go right after the welcome module.</p>



<p>And I&#8217;m just going to copy paste the signature, put a return at the end of it.</p>



<p>So we&#8217;ll consider this a function that can be called.</p>



<p>instruction on line 47 of the entry point and then now we have a demo</p>



<p>function that is being called but it does nothing for starters you know I in</p>



<p>my comments I like to put the signature of the function and I like to remind</p>



<p>myself of how I&#8217;m using my registers hopefully I&#8217;m not using the same</p>



<p>register in two different ways but you know sometimes it happens if it does and</p>



<p>I&#8217;m able to break my function up into multiple parts I&#8217;ll probably do it</p>



<p>you know with modular thinking modular programming but in this case I&#8217;m just</p>



<p>using these registers it&#8217;s fine to use them they are all designated per the ABI</p>



<p>as callee saved so that means the callee which is the demo function is</p>



<p>responsible for preserving them so if you don&#8217;t remember that or if you don&#8217;t</p>



<p>know about that see my other videos this is not an ABI video so I&#8217;m just going to</p>



<p>push all of them to make sure that they&#8217;re preserved I&#8217;m gonna call this</p>



<p>call this the prolog then at the end of my function i&#8217;m going to call this the epilog where</p>



<p>i just restore them in reverse order because that&#8217;s the way the stack returns data to you</p>



<p>the stack returns data to you in reverse order so i have to pop in the reverse register order to</p>



<p>un-reverse the reversal if that makes sense pop r13 pop r12 okay so i think 15 14 13 okay</p>



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



<p>started because we have our register here that we&#8217;re going to mess with let me just type rsp</p>



<p>real fast so this is the stack pointer register rsp this helps all programs know where where</p>



<p>they&#8217;re looking at in the stack all of your functions have to be really really careful about</p>



<p>messing with the stack pointer if you do it wrong you will crash the entire program because not only</p>



<p>will your local function not really know where its local variables end and begin it probably also</p>



<p>It&#8217;s return address when you try to return from the function because that is also sitting on the stack</p>



<p>And even if you were lucky enough to be able to jump back correctly to whoever called you</p>



<p>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>So the whole program is ruined. So we&#8217;ll start off by trying to remember where the stack pointer was</p>



<p>We&#8217;ll move</p>



<p>The stack pointer into the base pointer</p>



<p>didn&#8217;t do here that I that I want to do we should since we&#8217;re messing with the</p>



<p>base pointer and other programs sorry other functions or modules might also</p>



<p>rely on the base pointer and it&#8217;s considered callie saved we probably also</p>



<p>want to preserve that too so I&#8217;m going to do push RBP to basically say I would</p>



<p>like to restore I would like to preserve the base pointer so I don&#8217;t mess it up</p>



<p>for my callers so that means I have to restore it with the pop so RBP the base</p>



<p>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>So we have RBP at the front and the back there.</p>



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



<p>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>Then we&#8217;re restoring its value at the bottom.</p>



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



<p>let me show you real fast what happens what happens is nothing the program is</p>



<p>still okay because we we restored it so now we&#8217;re using the base pointer to</p>



<p>remember where the stack was now we&#8217;ve got to sort of calculate how much room</p>



<p>we want to make on the stack let me show you what I&#8217;m talking about here</p>



<p>remember all of our local variables are going to be on the stack and before we</p>



<p>drew this thing where it was like well we&#8217;ve got like a stack sitting here and</p>



<p>let&#8217;s just pretend that there&#8217;s some kind of data sitting on the stack data</p>



<p>data data right if the stack pointer dude green green there we go oh I erased my</p>



<p>green RSP if the stack pointer is currently pointing to this frame then</p>



<p>in order for us to make room on the stack to hold our array well if the</p>



<p>whole array is going to be sitting on the stack that just basically means</p>



<p>have five integers suppose five suppose that we want to make five integers on the stack that just</p>



<p>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>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>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>imagine these are all 64-bit integers and so they take eight bytes on the stack even though in our</p>



<p>previous example we were using just regular ints which are 32 we&#8217;ll just say we&#8217;re going to make</p>



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



<p>eight bytes and not just four bytes and it&#8217;s definitely not one byte so we make five of those</p>



<p>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>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>know further out and how do we get that number we&#8217;re just going to multiply the size of one</p>



<p>integer so you know the size of one integer here we know it&#8217;s going to be eight bytes we just</p>



<p>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>increase uh sorry decrease the stack pointers value by 40 because remember again when the</p>



<p>vertically, it&#8217;s actually growing downward in memory. So we&#8217;re going to decrease by 40 there,</p>



<p>at least in this drawing example. And that gives us a bunch of junk data, you know, because there</p>



<p>is always going to be some kind of a value sitting at every memory location in your computer.</p>



<p>It&#8217;s impossible that there is literally nothing at some memory location,</p>



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



<p>you know, you&#8217;ve done that and it won&#8217;t just give you back nothing if you try to read</p>



<p>nothing if you try to read.</p>



<p>So there&#8217;s going to be junk data sitting on there and then we&#8217;ll loop</p>



<p>through all those slots on the stack and we&#8217;ll just modify the data one by one</p>



<p>so that we can control what it is instead of just printing whatever junk data we end</p>



<p>up with.</p>



<p>So really, we&#8217;re just moving the stack pointer, just making room and then just</p>



<p>remembering where our array is.</p>



<p>We could put, you know, another frame on top if we wanted to make just like one</p>



<p>integer as a local variable.</p>



<p>You just got to remember where it is.</p>



<p>You know, what is it?</p>



<p>What is its offset?</p>



<p>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>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>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>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>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>not responsible for preserving it and so i&#8217;m going to say r10 is just going to be the number of</p>



<p>integers that we want to create if you recall at the top of our program here num integers is just</p>



<p>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>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>to be eight because we&#8217;re using quad words for our integers so we&#8217;re really just going to take</p>



<p>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>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>instruction just multiplies two numbers.</p>



<p>If you use the three operand format,</p>



<p>then the last two operands get multiplied</p>



<p>and the results stored in the first operand.</p>



<p>But if we use the two operand format like I&#8217;ve done here,</p>



<p>then both of those operands get multiplied</p>



<p>and then the result gets stored in the first operand.</p>



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



<p>R10 should hold the number of memory locations</p>



<p>that we should move the stack pointer</p>



<p>in order to make room for all those integers</p>



<p>like I showed you a second ago.</p>



<p>showed you a second ago. So then we&#8217;re going to move the stack pointer.</p>



<p>And maybe I&#8217;ll leave a little comment here. Remember, the stack grows downward</p>



<p>in memory. And so all I&#8217;m doing is subtracting the stack pointer. Remember the stack pointer</p>



<p>register, it just holds a number, which is a memory location. So if you subtract some numbers</p>



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



<p>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>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>had the three operand version then the last two operands would have one subtracted from the other</p>



<p>and the result will be stored in the first one but since I&#8217;m using the two operand version</p>



<p>basically it&#8217;s taking RSP minus R10 and then storing it in RSP so this this instruction just</p>



<p>this instruction just says let&#8217;s move the stack pointer downward in memory</p>



<p>enough times that we have room for all of our integers okay no problem next thing that we&#8217;re</p>



<p>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>word in memory right less okay so the first integer you know it&#8217;s up to you</p>



<p>how you style this because once we do the subtraction then RSP is actually</p>



<p>going to be pointing towards an integer you could consider that to be the first</p>



<p>integer or the last integer because all we have is an array of integers so you</p>



<p>you know rsp wherever it&#8217;s sitting when we&#8217;re finished we could say that&#8217;s pointing to the</p>



<p>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>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>a little comment here first integer it just makes it a little bit easier for me to write our loop</p>



<p>You could start by pointing to the one that RSP is pointing to,</p>



<p>or you could start by pointing to the other one that was like the first one that you added onto the stack.</p>



<p>You could call either one of those the first integer, as long as you remember where you started.</p>



<p>So you can increase or decrease the memory location to get to the next integer.</p>



<p>So I&#8217;m just going to do it in this style.</p>



<p>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>So let&#8217;s remember where is RSP.</p>



<p>So that&#8217;s like the top of the stack.</p>



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



<p>pointer.</p>



<p>I think that&#8217;s the way I&#8217;m going to use it.</p>



<p>Let me just double check my solution.</p>



<p>Yeah.</p>



<p>Okay.</p>



<p>a calculation where we remember where the last integer is so we know where the first integer</p>



<p>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>this is just the style i happen to be using so here we&#8217;re going to say that the first integer</p>



<p>is wherever rsp is pointing and then in r13 we&#8217;re going to remember where the other side of the</p>



<p>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>And then we add to it a memory location.</p>



<p>So remember we said before that the stack grows downward in memory.</p>



<p>So if we consider the top of the stack to be the first integer,</p>



<p>then that means previous items, maybe I should draw this,</p>



<p>are going to be increased memory locations.</p>



<p>So that&#8217;s kind of like the backwards of what you imagine a stack is doing,</p>



<p>but it&#8217;s kind of the way that I like it sometimes.</p>



<p>So imagine just in a very simple stack,</p>



<p>let&#8217;s pretend that the stack has one byte values,</p>



<p>one byte of values, which it doesn&#8217;t, but let&#8217;s pretend if you, if you do it,</p>



<p>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>that that address on the thing sitting at the bottom of the stack is five. So then that means</p>



<p>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>like a few more just to make it a little bit more interesting.</p>



<p>Let&#8217;s do a total of five.</p>



<p>So we&#8217;ll say four, three, two, one.</p>



<p>Maybe I should just add some numbers in front of those values.</p>



<p>So it doesn&#8217;t feel like we&#8217;re hitting zero.</p>



<p>But so just pretend we have a memory location of 15.</p>



<p>That&#8217;s not going to be the case in real life.</p>



<p>Pretend that we have one byte integers on the stack, which is not going to be the case.</p>



<p>We&#8217;re going to have quad words.</p>



<p>But you can see the memory locations go downward, right?</p>



<p>to the stack pointer is pointing to the topmost location we have to remember that anything up here</p>



<p>might exist in system ram but it&#8217;s not considered valid data because we didn&#8217;t</p>



<p>you know make it part of the stack by by growing the stack pointer so that means uh these</p>



<p>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>check and a check and a check and a check these items are okay to use so if I have</p>



<p>RSP or in this case we just remembered where RSP was by storing r12 if we have</p>



<p>that memory location 11 and we want to get an additional integer like somewhere</p>



<p>else like the next integer well that would be you know this one down here we</p>



<p>wouldn&#8217;t go in the other direction we wouldn&#8217;t go you know up we would just go</p>



<p>know up we would just go down in the stack but down visually is actually growing upwards in memory</p>



<p>because remember when we grow upwards visually we&#8217;re growing downward in memory so and you can</p>



<p>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>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>fix this yeah so that&#8217;s why here we&#8217;re adding a little formula instead of subtracting because</p>



<p>the rsp started there we&#8217;re saying that the top of the stack is the first integer</p>



<p>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>the number of integers minus one and then so that&#8217;s that&#8217;s the number of slots that</p>



<p>size. So if you imagine, you know, if we had 10 integers, then, you know, 10 minus one is nine</p>



<p>slots. So if you imagine that zero is the first integer, let&#8217;s say the memory location again,</p>



<p>then that means one, two, three, four, five, six, seven, eight, nine, 10 minus one is nine. So if we</p>



<p>added the number of integers minus one from the start, which we&#8217;ll consider zero here, that means</p>



<p>directly so that&#8217;s just like a little math because sometimes when you when you</p>



<p>think of adding two numbers together or taking the difference or including the</p>



<p>first number or not including the first number it&#8217;s a little confusing right so</p>



<p>keep in mind for this particular calculation we are adding to it the</p>



<p>number of integers minus one so that will be sitting on the last integer</p>



<p>rather than going past it so start with the first integers memory location and</p>



<p>the number of integers minus one or size minus one times the integer size,</p>



<p>because remember, every integer is going to be eight bytes.</p>



<p>And that will give us the memory location of the last integer in R13.</p>



<p>And then I&#8217;ve said this in other videos,</p>



<p>but basically you&#8217;re only allowed to make these sorts of calculations in Yasm</p>



<p>when you have the calculation inside of brackets,</p>



<p>but brackets will automatically dereference the value on the inside.</p>



<p>It&#8217;ll consider it as a pointer that needs to be dereferenced,</p>



<p>but we don&#8217;t want to dereference anything.</p>



<p>anything we don&#8217;t want to go to a memory location and take a value the memory location is the value</p>



<p>so these dereferencing brackets which are required for the formula are kind of bad so that&#8217;s why we</p>



<p>use the lea instruction instead of the move instruction if i put move there it would</p>



<p>definitely dereference the memory location and give me a value in r13 so r13 wouldn&#8217;t actually</p>



<p>be a pointer it would just be the value of the junk data of the last integer keep that in mind</p>



<p>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>I&#8217;m gonna start off with a label called demo loop init so for me personally when</p>



<p>I&#8217;m looping you know I like to make my labels inside of my loops start with a</p>



<p>prefix that matches the function so it&#8217;s like demo and then everything else is</p>



<p>gonna be demo underscore something so uh init loop init I&#8217;m gonna make a loop</p>



<p>to make a loop where i initialize the values of the array so i&#8217;m calling this loop the init loop</p>



<p>and then the last part is just like this is the initialization part of the loop this is where we</p>



<p>sort of like set up the initial values to loop so we have r12 and 13 that point to the first</p>



<p>and last integers now we&#8217;re going to set up r14 and 15 where r14 is the running pointer i think</p>



<p>before i might have accidentally said that r13 is the running pointer it just points to the last</p>



<p>it just points to the last integer but if you look back up at my comments r14 is the running pointer</p>



<p>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>that just keeps increasing so we can look at different data values so i&#8217;m going to start it</p>



<p>by looking at the first integer so now r14 is pointing to the first integer and then r15 is</p>



<p>going to be the value that i want to put into that position in the array so like the first integer</p>



<p>integer I want to put some kind of a starting value into it you can put the</p>



<p>number zero or whatever you want I just wanted to have a start value so that it</p>



<p>sort of looks more like I&#8217;m putting data and less like I have a loop counter so</p>



<p>remember the integer start value up here is just seven so I&#8217;m just going to start</p>



<p>at the number seven and now I&#8217;m done initializing my loop then the next thing</p>



<p>I&#8217;m going to add is the top of my loop so you can imagine this as the top of a</p>



<p>top of a while loop where you start comparing some sort of an expression maybe i&#8217;ll say uh</p>



<p>you know expr to say that we&#8217;re comparing some sort of an expression</p>



<p>and if that expression evaluates to true the loop continues if it evaluates to false</p>



<p>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>and 13 remember R14 is the running pointer and R13 is the last integer so basically I&#8217;m trying to</p>



<p>figure out am I looking at or like let&#8217;s compare the running pointer with the pointer of the last</p>



<p>integer then I&#8217;m going to say if the running pointer has a greater memory location than than</p>



<p>the last integer that means I&#8217;ve gone beyond the last integer and again the way I arranged the first</p>



<p>last integers just makes it easier for me to think of them as having increasing</p>



<p>memory locations so I&#8217;m going to jump if the running pointer has already</p>



<p>surpassed the last integer by saying let&#8217;s jump if it&#8217;s greater than so you</p>



<p>can imagine maybe in the expression here I should probably say while not</p>



<p>r14 is greater than r13 not a great expression but it&#8217;ll do so that&#8217;s what</p>



<p>up there at the top let me just put that into my notes too so that my notes match</p>



<p>the video okay so we are comparing and then we&#8217;re jumping to the end of the</p>



<p>loop if we end up you know going beyond the last integer so that labels not</p>



<p>created yet I&#8217;ll create that in a moment but pretty much that&#8217;s a label that&#8217;s</p>



<p>just going to be below the loop just to say like we&#8217;re finished with the loop</p>



<p>the loop body I like to put comments here to help myself remember oh this is</p>



<p>the part of the while loop that I&#8217;m currently inside of just makes things a</p>



<p>little bit easier to understand you know you put a block comment up top of every</p>



<p>label or every you know chunk of instructions just to let you know the</p>



<p>general idea and then you know sometimes you put comments also on the right side</p>



<p>to help you remember what each instruction is actually doing so then</p>



<p>what am I going to do here remember R14 is the running pointer if I D ref R14</p>



<p>ref r14 that means i want to move an actual value into that memory location rather than changing the</p>



<p>memory location that r14 points to so r15 is going to be the uh the integer that we want to</p>



<p>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>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>seven right now then I&#8217;m going to increase our 15 so that means every time</p>



<p>we iterate the loop we should see that the value increases so the first integer</p>



<p>should be seven the second one should be eight next one should be nine and so</p>



<p>forth so just a simple loop where I&#8217;m just writing data into my array so now</p>



<p>that we&#8217;ve ended the loop body let&#8217;s write the bottom of the loop which is</p>



<p>just going to increase the running pointer and jump back up to the top and</p>



<p>And this is not necessarily the only style for translating while loops.</p>



<p>I&#8217;m just doing it.</p>



<p>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>But this video is just really about local variables on the stack.</p>



<p>So I&#8217;m not going to go over all the different ways you can do it.</p>



<p>Anyway, so R14 is the running pointer.</p>



<p>So I&#8217;m going to just make the running pointer jump to the next integer.</p>



<p>And we can do that by increasing its memory location by the size of one integer.</p>



<p>Again, this is another benefit of the first and last pointers that I chose at the beginning.</p>



<p>I can just increase to go to the next integer.</p>



<p>So we&#8217;re going to increase by 8 bytes to just go to the next integer.</p>



<p>If you increase by 1 byte, you&#8217;ll probably have a huge corrupted mess because you&#8217;re</p>



<p>messing with 8 byte integers but you&#8217;re only increasing by 1 byte.</p>



<p>And then after we increase, we&#8217;re just going to jump to the top of the loop.</p>



<p>So notice how I have a jump statement here.</p>



<p>It&#8217;s going to go just to loop top.</p>



<p>loop top. So now this part is here is basically just going to execute over and over and over again</p>



<p>until we finally scan through all of the integers in our array. So that&#8217;s the bottom of the loop.</p>



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



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



<p>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>we&#8217;re done we jump to the loop done label and therefore there&#8217;s no more looping of that</p>



<p>initialization loop okay so we got that done let me just run the program real fast to make sure</p>



<p>that i haven&#8217;t screwed it up we actually should not see anything right now oh what did i do</p>



<p>must have done something naughty maybe if i finish</p>



<p>uh this program then everything will be okay oh my gosh what did i even do</p>



<p>well i&#8217;ve got a working solution in the other window so hopefully when i paste all the extra</p>



<p>steps everything will be fine you never know subtract the stack pointer oh did i forget to</p>



<p>oh did i forget to restore something at the very end print body move the base pointer into the</p>



<p>RSP stack pointer oh yeah okay that&#8217;s definitely what you got that&#8217;s why I</p>



<p>crashed okay so um remember I said you got to be very careful about the stack</p>



<p>this is a great lesson so I did preserve the base pointer but I didn&#8217;t actually</p>



<p>preserve the stack pointer notice how right here I subtracted from the stack</p>



<p>pointer but I did not restore the stack pointer anywhere so that means I</p>



<p>corrupted the stack for anyone that called me and also for my return address</p>



<p>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>just going to copy paste something else to make sure that we can actually run this. So I&#8217;m going</p>



<p>to copy paste into the epilog a restoration of the stack pointer. And that&#8217;s why we saved the</p>



<p>stack pointer in the base pointer, just to remember where it was when we originally started our</p>



<p>function. So now on line 128, it should be restored and the program should work. Let me just double</p>



<p>yeah it worked okay nothing happened that we can see but it did write values</p>



<p>into the array now let&#8217;s do another loop where we just print the array so let&#8217;s</p>



<p>see loop in it done looping it bottom and looping it done okay so now there&#8217;s</p>



<p>like gonna be another loop here we&#8217;re gonna call this the print loop and so</p>



<p>the print loop is gonna be kind of the same pattern we&#8217;re just gonna loop</p>



<p>going to loop through all the integers in the array but instead of modifying them we&#8217;re just</p>



<p>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>inside of r14 in order to start the loop at the first integer what was r14 again that was the</p>



<p>running pointer remember let&#8217;s see where is that yeah it was the running pointer so now we&#8217;re</p>



<p>resetting the r14 running pointer to the very beginning of the array and we know where the</p>



<p>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>going to copy paste again this stuff right here</p>



<p>right there okay so the top of the loop we&#8217;re asking ourselves you know we&#8217;re</p>



<p>gonna compare I&#8217;m not gonna put all the extra while stuff that I put in the</p>



<p>previous loop because hopefully by now you understand loops a little bit better</p>



<p>and if we&#8217;re beyond the last integer because the running point of r14 is beyond the memory</p>



<p>location of r13 that means we&#8217;re totally done so we should jump if it&#8217;s greater than</p>



<p>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>basically that you know if uh keep going as long as r14 is not greater than r13 so if it is</p>



<p>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>whatever value is sitting in that particular integer so how do we do that i&#8217;m just going to</p>



<p>use the printf statement or sorry the printf function which is provided by the c libraries</p>



<p>that&#8217;s why we&#8217;re doing you know a modular or a hybrid program with multiple modules and c linking</p>



<p>very quickly there is a function called printf which I can call it it takes multiple arguments</p>



<p>but the first two arguments that I can give it are the string that represents the formatting</p>



<p>that I want to print like I could do like a regular string message I could do tokens</p>



<p>to format some data inside of them and then the second argument is going to be the piece of data</p>



<p>that I actually want to use let me see if I can just type that up for you real fast</p>



<p>So, you know, the printf instruction, or sorry, this is not an instruction.</p>



<p>This is a function in C.</p>



<p>We would typically, you know, give it some kind of string.</p>



<p>The string should be null terminated, and it should have a new line at the very end of it</p>



<p>to make sure that printf actually flushes.</p>



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



<p>But I guess it&#8217;s more performant if you have a way to delay the flushing,</p>



<p>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>flush every time and then every argument after that is some sort of you know data that we can</p>



<p>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>so that means we would give that long as the next argument the rsi argument and then for the string</p>



<p>what I&#8217;m using right here %lu so you can imagine instead of this string it is this</p>



<p>string right here whoops too many too many quotes it&#8217;s just this string right</p>



<p>here and then instead of a 10 13 that&#8217;s the same thing as just doing an or if</p>



<p>you want to be you know more of a windows windows person slash r slash n</p>



<p>it&#8217;s all good and the zero is not needed because the string if you put a string</p>



<p>literal it&#8217;s automatically going to be null terminated which means there&#8217;s just</p>



<p>null terminated which means there&#8217;s just a zero at the end of the string in memory so this is</p>



<p>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>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>as an argument and then the first argument is going to say let&#8217;s just print this as a unsigned</p>



<p>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>I&#8217;m saying first argument is this the format that I want to be printed second argument is the actual</p>



<p>is the actual value and then I&#8217;m gonna make a call to printf why do I have this</p>



<p>weird push and pop pair sitting around printf so this is not a video about</p>



<p>stack alignment in GCC but basically the GCC libraries expect that your stack is</p>



<p>aligned to I think 16 bytes but since we use 8 byte integers every single time we</p>



<p>address to the stack which is eight bytes and then every time we do one single push or pop</p>



<p>we&#8217;re modifying the alignment of the stack by eight bytes so if you think about it when we&#8217;re</p>



<p>programming in assembly for the most part the stack is going in and out of alignment because</p>



<p>every time we modify it by eight bytes it it might line up with a 16 byte alignment or it might not</p>



<p>it&#8217;s just kind of like oscillating right so when I first wrote this solution I wasn&#8217;t doing the</p>



<p>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>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>prints one number certainly and if it&#8217;s an okay assembly program just to print one number is it</p>



<p>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>that for stack alignment but i guess i&#8217;ll just finish the loop so demo print loop done so we</p>



<p>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>into program here i&#8217;ll just say nada because we&#8217;re not really doing anything and then at the bottom</p>



<p>you know that&#8217;s the epilogue that&#8217;s separate from the the other label so basically now let me finish</p>



<p>know we already know the loops but I&#8217;ll just I&#8217;ll just say it at the bottom of</p>



<p>the loop we do the same thing that we did with the initialization loop we just</p>



<p>increase the running pointer you know we move it along to the next integer and</p>



<p>then we jump to the top of the loop that&#8217;s it and then the done label we</p>



<p>don&#8217;t really do anything we&#8217;re just letting execution drop through down to</p>



<p>that point so that the loop doesn&#8217;t continue so now we should be able to run</p>



<p>the program don&#8217;t get excited oh actually you know what get excited I</p>



<p>was gonna say don&#8217;t get excited because it was gonna totally work now but now</p>



<p>because it was going to totally work now,</p>



<p>but now I think we can just say that it&#8217;s going to crash.</p>



<p>So if I run it, notice how there&#8217;s a segpult.</p>



<p>So the GCC libraries, many functions expect your stack</p>



<p>to be aligned to 16 bytes.</p>



<p>So if you see mysterious crashes</p>



<p>and you are absolutely sure that you&#8217;re not ruining</p>



<p>the stack pointer or ruining something else,</p>



<p>you&#8217;re doing everything correctly,</p>



<p>but the program still crashes, it might be stack alignment.</p>



<p>So one way to get around stack alignment</p>



<p>is just to move the stack pointer.</p>



<p>move the stack pointer like at the top here we could have said oh we&#8217;ve got one two three four</p>



<p>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>addresses the stack might be out of alignment somehow so we could add an extra push up here</p>



<p>and then add a corresponding pop down at the bottom like we could easily do this let me just</p>



<p>show you real fast we could push r15 twice for no reason I acknowledge and then at the bottom</p>



<p>and then at the bottom we pop R15 twice,</p>



<p>that would change the alignment because that&#8217;s one more 8 byte push.</p>



<p>But in my case, and actually that would be a little bit smarter</p>



<p>because if you have our loop where it&#8217;s constantly calling on printf,</p>



<p>this is a lot of hits to memory, right?</p>



<p>This is like 100 hits to memory because every single time we do a push-pop pair</p>



<p>around a call to printf, we&#8217;re like touching memory.</p>



<p>Whereas if I did it at the beginning and the end,</p>



<p>Maybe I should just do it this way.</p>



<p>I want to do it both ways so you understand, but it&#8217;s more efficient, I think, if we do</p>



<p>it this way.</p>



<p>Anyway, so we&#8217;ll do pop twice at the bottom and then push twice at the top.</p>



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



<p>I think I haven&#8217;t tested this.</p>



<p>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>crash.</p>



<p>Yeah, so now see how the program works.</p>



<p>So I&#8217;m going to do it the other way now, which is the less efficient way, because here</p>



<p>now which is the less efficient way because here we just have one extra push</p>



<p>pop pair but if we do it the other way it&#8217;ll still work but we&#8217;ll be hitting</p>



<p>memory much more often so I&#8217;m just gonna do it this way just to show you you can</p>



<p>surround any call because sometimes in your programs you might have the stack</p>



<p>like you know modified throughout the function many different times so it</p>



<p>wouldn&#8217;t make too much sense for you to add an extra push pop pair in the prologue</p>



<p>and epilogue because that might not solve it for all of your calls to all of</p>



<p>to all of your seed library functions.</p>



<p>So in that case,</p>



<p>where you can&#8217;t really predict the stack well enough,</p>



<p>you can just surround your call with a push-pop pair.</p>



<p>It hits memory more, but it&#8217;ll work.</p>



<p>So this is basically, you can imagine,</p>



<p>by the time we get to line 139,</p>



<p>the stack is out of alignment.</p>



<p>So I just do a push that puts it into alignment.</p>



<p>And then after the call comes back,</p>



<p>I just pop it so it&#8217;s back out of alignment again,</p>



<p>but I don&#8217;t have extra erroneous data sitting on the stack.</p>



<p>Because if I only had push and not pop,</p>



<p>then it&#8217;s going to push it more and more out of alignment.</p>



<p>It&#8217;s going to push it in and out and in and out,</p>



<p>but it&#8217;s going to add a bunch of junk data to the stack</p>



<p>that I&#8217;ll never recover from.</p>



<p>Or actually, I guess I will recover at the very end</p>



<p>when I restore the stack pointer, but it&#8217;s pointless.</p>



<p>It&#8217;s going to consume too much memory.</p>



<p>What if I was writing like a billion</p>



<p>or like a million items on the stack, right?</p>



<p>a billion iterations of the loop would probably be a bad idea to start adding onto the stack</p>



<p>we&#8217;ll probably end up stack overflowing probably with far less than a billion items so anyway i&#8217;m</p>



<p>going to surround the call with the push pop pair and then at the bottom we just do the same thing</p>



<p>you know increase the integer and then go to the top and so now we you know you already can see it</p>



<p>it starts off printing well it starts off initializing the array but then the thing that we</p>



<p>can see starts off printing the first integer and then the next iteration of the loop prints the</p>



<p>second integer and the third and the fourth and so forth until we get all the way down just to</p>



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



<p>Oh no, that&#8217;s the print loop. Let&#8217;s do the init loop.</p>



<p>Init loop bottom.</p>



<p>Oh, right there, when we&#8217;re in the body.</p>



<p>So I&#8217;m just going to increase R15 twice.</p>



<p>Just to show you, you know, we can kind of control what&#8217;s going inside of the local array.</p>



<p>So see how it goes from 7 to 9 to 11 instead of 7, 8, 9.</p>



<p>So I&#8217;m going to take that out and then just show you that we can control</p>



<p>how many integers we have in our local array with just the number of integers.</p>



<p>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>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>alignment no I I have a push put pair there number 50 is not hard-coded anywhere anywhere</p>



<p>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>video. I wonder if some other value would work for that. There&#8217;s a number of things that could</p>



<p>be the problem. It could be like stack alignment somehow. It could also be, let&#8217;s see, am I making</p>



<p>any calls here inside of my function demo? I&#8217;m saying sub and LEA and then start and then jump</p>



<p>and start and then jump and increase and jump and then RDI.</p>



<p>Not really doing anything else.</p>



<p>So I don&#8217;t think it&#8217;s stack alignment.</p>



<p>I must have miscalculated somewhere somehow for changing that.</p>



<p>All right.</p>



<p>Well, if I figured it out, then I will release another video.</p>



<p>But basically, this is the idea.</p>



<p>100 works, 50 works.</p>



<p>What about 99?</p>



<p>Would that work?</p>



<p>Save faults on 99.</p>



<p>faults on 99 and then 98. 98 works so like every two seems to work but that is</p>



<p>two quad words or 16 oh right so like a hundred integers if we assume that</p>



<p>that&#8217;s in alignment then 99 would be eight bytes less that we&#8217;re moving the</p>



<p>98, notice how it is okay.</p>



<p>And then 97, it&#8217;s going to segfault again.</p>



<p>Watch.</p>



<p>This is a great stack alignment video, I guess.</p>



<p>96, it won&#8217;t segfault.</p>



<p>So it&#8217;s going in and out of alignment.</p>



<p>I think I figured it out.</p>



<p>So if we use the number 100, it&#8217;s in alignment.</p>



<p>Sorry, it&#8217;s out of alignment.</p>



<p>If we use the number 100, the program works because the number 100 throws it out of alignment.</p>



<p>number 100 throws it out of alignment but then we have this push pop pair here around the call to</p>



<p>printf which puts it back in alignment so if I change the number of integers I&#8217;m actually</p>



<p>changing the number of memory locations that I modify the stack pointer so I have to do it by</p>



<p>twos if I wanted to do 99 here then that means the stack is in alignment by the time I call</p>



<p>by the time I&#8217;m getting ready to call printf which means the push pop pair around it throws</p>



<p>push pop pair around it throws the stack out of alignment so just watch here I&#8217;ll</p>



<p>prove it okay so it&#8217;s sake false I comment that out now it won&#8217;t throw it</p>



<p>out of alignment okay should have known that before I recorded the video it&#8217;s</p>



<p>fun to guess sometimes though you know I get a little nervous oh no my program</p>



<p>broke on camera can I debug it live well I guess I can but kind of slowly</p>



<p>anyway so we I think we&#8217;ve gone over every single part that I wanted to show you</p>



<p>we know how to create a local array on the stack and and therefore you also know how to create like</p>



<p>any other data type on the stack if you want you want to create a long a 64-bit integer</p>



<p>just move it by eight bytes instead of moving it by eight times however many integers we were doing</p>



<p>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>Just, you know, move it by one memory location instead of eight memory locations.</p>



<p>So, you know, one byte instead of eight bytes.</p>



<p>You want to store a short, you know, a two byte integer?</p>



<p>Well, just move it by two bytes instead of eight bytes, right?</p>



<p>So you can do this as many times as you want.</p>



<p>You want to have several local variables?</p>



<p>Just move it one time for every local variable.</p>



<p>Same thing for accessing.</p>



<p>You just have to remember where everything is.</p>



<p>offset of the of the first variable local variable and then the offset of the second</p>



<p>local variable you can store those in globals or store those in registers if you can you just</p>



<p>got to remember somehow where everything starts but it&#8217;s all sitting on the stack if it&#8217;s a local</p>



<p>variable okay I guess that&#8217;s everything that I really have to say I hope you enjoyed this video</p>



<p>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>have a good one</p>



<p>you</p>



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



<p>I do hope you did learn something and have some fun</p>



<p>If you could do me a please a small little favor</p>



<p>Could you please subscribe and follow this channel or these videos or whatever it is?</p>



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



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



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



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



<p>the night and I just wake up because I know somebody subscribed or followed. It just wakes</p>



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



<p>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>the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



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



<p>which I think is also named somewhere at the bottom of this video and</p>



<p>It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p>I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



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



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



<p>I get I wake up in a cold sweat. I&#8217;m like</p>



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



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/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>x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Feb 2026 02:49:29 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[asm tutorial 2025]]></category>
		<category><![CDATA[assembly for loop]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly while loop]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional jump assembly]]></category>
		<category><![CDATA[implementing for loop in assembly]]></category>
		<category><![CDATA[inc instruction assembly]]></category>
		<category><![CDATA[learning assembly language]]></category>
		<category><![CDATA[loop in assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[reverse engineering basics]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86 assembly loops]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<category><![CDATA[yasm x86-64]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=295</guid>

					<description><![CDATA[<p>This tutorial explains how to implement a C-style for loop in x86-64 assembly using YASM by converting it to while-loop logic with initialization, conditional jump (jl), increment (inc), and unconditional jump back to the loop top. Demonstrates register usage (r12/r13), loop structure, and printing numbers inside the body.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/">x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly For Loops Explained - YASM Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/pftzv5lIPso?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>Quick practical guide showing how to implement a classic for loop (i=0; i&lt;n; i++) in x86-64 assembly with YASM. We break the for loop into init/check/update parts, convert it to while-style logic, then build it with compares, conditional jumps, inc, and unconditional jumps back to the top. Includes real working code, user input, and printed output.</p>



<p>Great for students learning assembly after C/C++, OSdev hobbyists, or reverse engineering beginners.</p>



<p>Introduction to For Loops in Assembly 00:00:00<br>For Loop Structure in High-Level Languages 00:00:28<br>Breaking Down For Loop Parts: Init, Check, Update 00:01:04<br>Converting For Loop to While Loop 00:01:52<br>Why While Loop Style Helps in Assembly 00:03:16<br>Program Overview and Setup 00:04:12<br>Data Section &#8211; Strings and Messages 00:04:40<br>External Functions and Hybrid Program 00:06:11<br>Entry Point &#8211; Looper Function 00:07:12<br>For Test Function and Register Usage 00:07:42<br>Preserving Callee-Saved Registers 00:08:05<br>Printing Welcome Message 00:09:00<br>Prompting User for Number 00:09:48<br>Getting User Input 00:10:25<br>For Loop Structure in Assembly &#8211; Comments 00:11:12<br>Initialization &#8211; Setting Counter to Zero 00:12:50<br>Loop Top Label and Condition Check 00:14:28<br>Conditional Jump Setup (jl) 00:15:07<br>Handling Jump Distances 00:15:34<br>Loop Body &#8211; Printing Current Number 00:16:58<br>Update Part &#8211; Increment Counter 00:18:40<br>Unconditional Jump Back to Loop Top 00:19:14<br>Loop Done Label and Exit 00:19:55<br>Printing Goodbye Message 00:20:23<br>Testing the Program 00:22:08<br>Final Results and Demo 00:22:24<br>Outro and Call to Action 00:23:25<br>Thanks and Subscribe Request 00:23:45<br>Website and QR Code Mention 00:24:38<br>Closing Thanks 00:25:16</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>Hey there! In this video I&#8217;m going to teach you how to implement for loops in YASM x86-64 assembly.</p>



<p>Although if you&#8217;re using a different type of machine or a different assembler,</p>



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



<p>Okay, so for starters here, what am I even talking about? Let&#8217;s do…</p>



<p>are you even talking about dude imagine in a higher level language we have a for loop here</p>



<p>and so we have like four size type i equals zero i is less than 99 i plus plus right probably most</p>



<p>programmers looking up assembly uh on the internet probably are interested in uh or probably already</p>



<p>know how to implement for loops in c plus plus so let&#8217;s just pretend that you do and so inside of</p>



<p>your for loop we&#8217;ll do stuff and what happens here is uh well you know this for loop is going to run</p>



<p>Well, you know, this for loop is going to run 99 times.</p>



<p>It&#8217;s going to start at zero.</p>



<p>And, you know, so we have like an initialization part right here.</p>



<p>Maybe I should copy paste this and kind of break down the parts.</p>



<p>We have an init part here for the for loop.</p>



<p>We&#8217;ll say init part.</p>



<p>And then here we have a check part.</p>



<p>We&#8217;ll continue to loop as long as this expression is true.</p>



<p>You can make a big complicated expression if you want to,</p>



<p>but we&#8217;ll just keep it simple for this video.</p>



<p>So I&#8217;ll call this the check part.</p>



<p>And then we have sort of an update or a maintenance part.</p>



<p>an update or a maintenance part. So I&#8217;m going to call this the update part where we increment our</p>



<p>counter variable, move pointers, even call functions, do whatever it is you think you need</p>



<p>to do to update the loop so that it runs it, you know, so that the loops don&#8217;t actually have to be</p>



<p>integers. But in this case, I think it&#8217;s better, it&#8217;s easier to understand what the loop is doing</p>



<p>if we just kind of break down these parts. Anyway, so what is really a while loop? Sorry,</p>



<p>but a while loop that breaks and has break has like a nit logic and breaking logic and checking logic just in specific places.</p>



<p>Imagine this. What if I said before the loop starts, I&#8217;ll let&#8217;s let&#8217;s say we&#8217;re going to do a while loop before the loop starts.</p>



<p>I&#8217;m going to initialize a variable size type I equals zero, right?</p>



<p>That&#8217;s the initialization part that&#8217;ll only happen one time.</p>



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



<p>the check part of the for loop.</p>



<p>So I&#8217;ll just say while i is less than 99.</p>



<p>And then for the update part, we just have to stick that inside of the while loop somewhere.</p>



<p>Depending on your logic, you might want to put it at the top or the middle or the bottom.</p>



<p>I&#8217;m going to put it at the bottom because I&#8217;m going to expect that it happens right</p>



<p>before we do the next check.</p>



<p>So I&#8217;m going to maybe do a comment here and call it the update part and I&#8217;m just going</p>



<p>So maybe if I update this code to print something, it&#8217;ll seem a little bit more clear.</p>



<p>Value of I is, and then I&#8217;ll just say I end L, and then I&#8217;ll copy paste that to here.</p>



<p>And then I can also put this inside of the while loop.</p>



<p>And now we have transformed our for loop into a while loop.</p>



<p>transform your for loop into a while loop it&#8217;s pretty easy to implement in assembly if you</p>



<p>already know how to implement while loops so i should again say that there are lots of parts</p>



<p>i don&#8217;t know if i said that during this video but there are lots of things in this video that i&#8217;m</p>



<p>not going to that i&#8217;m not going to explain specifically because i&#8217;ve already explained</p>



<p>them in other videos so for example implementing a while loop is in another video so if you don&#8217;t</p>



<p>already know how to implement while loops if you don&#8217;t know how to use conditional branching or</p>



<p>If you don&#8217;t know the basics of assembly, hybrid programs, make files, all the basic stuff that I&#8217;m going to skim over in this video,</p>



<p>then you should probably check out my other videos first.</p>



<p>But for now, I&#8217;m just going to assume that you know how to implement a while loop.</p>



<p>And really the lesson is, hey, just take your for loop and convert it into a while loop.</p>



<p>And then you can implement the while loop pretty easily.</p>



<p>That&#8217;s the secret as far as I&#8217;m concerned.</p>



<p>So imagine this now. Let&#8217;s do some assembly code.</p>



<p>I have a source code file here called looper.asm for assembly.</p>



<p>And you can imagine that I have a hybrid program running under the hood.</p>



<p>I&#8217;m not going to show you all the code involved.</p>



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



<p>We&#8217;re just going to write the assembly here.</p>



<p>So, you know, if you&#8217;re writing pure assembly at home to practice, that&#8217;s fine.</p>



<p>Just keep in mind, there&#8217;s like a few things under the hood that I&#8217;m not showing in this specific video.</p>



<p>So first off, I&#8217;m going to copy paste my data section.</p>



<p>paste my data section of my assembly program. The first thing to note is we have a bunch of</p>



<p>C strings here. We&#8217;re basically going to be telling the user, hey, we&#8217;re going to begin</p>



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



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



<p>the number minus one, which is like the typical four loop that you usually write in the most</p>



<p>and then we&#8217;re going to have a little prefix we&#8217;re going to say you know like an arrow and then we&#8217;re</p>



<p>going to print the number back to the user that we&#8217;re looping through so if the user enters a</p>



<p>10 it&#8217;s going to print the arrow 0 and then arrow 1 arrow 2 all the way up to arrow 9</p>



<p>and then when we&#8217;re done we&#8217;re going to print an ending message so this is nothing new if you know</p>



<p>assembly already just c strings that will print with system calls see my other videos if you don&#8217;t</p>



<p>And then a CRLF string, just basically doing a new line, a carriage return new line feed</p>



<p>on the system.</p>



<p>And then we&#8217;re going to output using system call code one.</p>



<p>And we&#8217;re going to use file descriptor one so we can print to standard output.</p>



<p>Okay.</p>



<p>So now the real fun begins.</p>



<p>Let&#8217;s start our text section, which is where the instructions of our assembly program go.</p>



<p>So I&#8217;m going to do text section, section text right there.</p>



<p>And I&#8217;m going to use two external functions to just help me input and output numbers to the user.</p>



<p>If you wanted to, you could use a system call that just inputted a character.</p>



<p>And then you could just kind of like loop printing various characters.</p>



<p>And like, let&#8217;s say if the user typed A, maybe you could imagine doing a loop that increases the character that they typed all the way until it hits Z.</p>



<p>Or if they hit, you know, F, it&#8217;ll just print F all the way to Z.</p>



<p>to z you could do that without using an external library for printing integers this video is not</p>



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



<p>hard code the start and end points when you&#8217;re practicing um you could you could use a different</p>



<p>library or a different function call to get the inputs you could use a system call just to input</p>



<p>one character um or you could you know use a hybrid program to to utilize printf and scanf</p>



<p>f. Either way I&#8217;m just going to be using these two functions just to like get input and output.</p>



<p>It&#8217;s not really part of the idea of looping. So now let&#8217;s start our entry point.</p>



<p>Our function is called looper. Since this is a hybrid program you can imagine there&#8217;s a C++</p>



<p>module elsewhere calling on the looper function and so that&#8217;s why I mark it as global so it can</p>



<p>be called upon. And then I have another function called for test. I don&#8217;t really know why I chose</p>



<p>it this way but i wanted to make another function that was called upon by our entry point here</p>



<p>so the looper function really doesn&#8217;t do anything except recall the for test function</p>



<p>so now let&#8217;s start the for test function actually maybe this is where the fun begins</p>



<p>so i&#8217;m gonna put it down here and so you can see the signature it doesn&#8217;t take any arguments it</p>



<p>doesn&#8217;t return anything it just does stuff and then i have a note to myself this is how we&#8217;re</p>



<p>going to use the registers we&#8217;re going to use r12 for the user&#8217;s number and then we&#8217;re going to use</p>



<p>And then we&#8217;re going to use R13 to keep track of where we&#8217;re going.</p>



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



<p>I&#8217;m going to start by saying let&#8217;s preserve R12 and R13</p>



<p>because you have to respect the application binary interface, the ABI,</p>



<p>and that designates R12 and R13 as Kali saved registers.</p>



<p>If I don&#8217;t preserve those and my program is even a little bit complicated,</p>



<p>I&#8217;m probably going to be debugging forever</p>



<p>debugging forever or I&#8217;m going to just crash my program for no reason. So I&#8217;m going to just</p>



<p>do a push pop pair. Notice how the pops are in reverse order. This is not a push pop video,</p>



<p>but just so you know, I guess while I&#8217;m here, I&#8217;m going to copy paste my crlf function,</p>



<p>which really does nothing. It just prints out the crlf string with a system call. That&#8217;s all</p>



<p>Okay, so do I even need CRLF in this program?</p>



<p>I think I just modified this.</p>



<p>Maybe I don&#8217;t even need it anymore.</p>



<p>No, I guess I do.</p>



<p>The first thing we&#8217;ll do is we&#8217;ll print an introduction message to the user.</p>



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



<p>message to the user.</p>



<p>And then I&#8217;m going to call CRLF, which will just give us a new line.</p>



<p>And yeah, you can hard code, you know, the 13, 10 at the end of these strings, but I</p>



<p>don&#8217;t really like doing that.</p>



<p>doing that. Okay, so we should have a working program at this point. Let me see if it actually</p>



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



<p>driver code, but that&#8217;s what it&#8217;s doing. And then the for test prints the welcome message that we</p>



<p>just added. And then the driver lets us know it&#8217;s retained control. And then it&#8217;s responsible for</p>



<p>returning to the operating system for us. Okay, so then the next thing we&#8217;re going to do is we&#8217;re</p>



<p>we&#8217;re going to ask the user for a number so that we know how many times to loop.</p>



<p>And again, you could hard code this number if you don&#8217;t want to do IO right now.</p>



<p>You could even print a character a certain number of times</p>



<p>if you don&#8217;t want to even deal with printing an integer.</p>



<p>But I&#8217;m going to ask the user for a number.</p>



<p>That&#8217;s going to be the prompt string.</p>



<p>And if we run it again, now you should see it asks the user for a number.</p>



<p>So it&#8217;s going to say, please enter a number.</p>



<p>The loop will print from zero to N minus one.</p>



<p>print from 0 to n minus 1. It doesn&#8217;t actually ask for the number though. It doesn&#8217;t actually</p>



<p>I guess take the number so that&#8217;s going to be my external library that this video is not about</p>



<p>where I just call a function called input assigned 64 integer and I&#8217;m going to receive that back in</p>



<p>rax and I&#8217;m just going to save rax into r12. So r12 is now going to be the number that the user</p>



<p>inputted and that&#8217;s why up here I have it designated as the user&#8217;s number. So really not a big deal but</p>



<p>a big deal but uh you know if we run it again it&#8217;ll ask for a number and then it won&#8217;t do</p>



<p>anything else it&#8217;ll just kind of quit okay so now we can implement our for loop this is going to be</p>



<p>a little tricky so we&#8217;re going to start off with the initialization part remember we had several</p>



<p>parts here if i just kind of drag this off to the side maybe pin it up to the top so we can see it</p>



<p>aren&#8217;t we? If I move it a little bit to the side. So remember that for loop, it&#8217;s got an init part</p>



<p>and a check part and an update part. And I&#8217;ve kind of added that as a comment just to remind myself</p>



<p>of the way my mind is supposed to be wrapped around this concept. And so I made another comment</p>



<p>here with four and then empty parentheses just to denote that some comments that come below</p>



<p>I guess the top of the for loop</p>



<p>So now I&#8217;m going to make a label here and what I like to do with my labels is I like to</p>



<p>prefix them with the name of the function that I&#8217;m currently in and then an underscore and that kind of helps me keep track of</p>



<p>My symbols a little bit more easily, especially if I have a large module the symbols are less likely to overlap if I prefix them with the</p>



<p>The function names</p>



<p>to be Fortest underscore something and since I&#8217;m only doing one thing inside of the Fortest function</p>



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



<p>parts inside of your function and it started getting a little cluttered you might want to have</p>



<p>you know another label you know that just sort of another I guess like part to your label that names</p>



<p>the part of your function you&#8217;re in although this is assembly it gets really hard really fast so</p>



<p>really hard really fast so if your function is even a little bit complicated you should probably</p>



<p>consider breaking it up into multiple functions if you can. We can&#8217;t really do that at this point</p>



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



<p>the first thing we had to do to initialize was you know setting i to zero or you know whatever</p>



<p>it is that we&#8217;re going to set up in the init part so I&#8217;m just going to do that here. I&#8217;m going to say</p>



<p>That&#8217;s the first part, the update part where we set size type i equals zero.</p>



<p>In fact, maybe I could do another copy paste of this.</p>



<p>Where instead of using a size type, we&#8217;ll just say that the register r13 equals zero.</p>



<p>I know that&#8217;s not going to make sense in a higher level language right now,</p>



<p>but just so that the for loop looks a little bit more like assembly.</p>



<p>We&#8217;ll keep going as long as r13 is less than r12.</p>



<p>and then we increase R13.</p>



<p>So I&#8217;m just going to put ink R13</p>



<p>so it looks more assembly-like,</p>



<p>even though this completely and totally</p>



<p>would not compile in C++.</p>



<p>I hope that this helps your understanding a little bit.</p>



<p>So yeah, we can do everything</p>



<p>except for just the increase part at the very top.</p>



<p>I&#8217;m going to choose to increase it at the bottom.</p>



<p>If you wanted to, I guess you could start off R13</p>



<p>as a negative number and then increase it at the top.</p>



<p>But I personally don&#8217;t feel that&#8217;s like very clean.</p>



<p>feel that&#8217;s like very clean it also forces you to use signed integers maybe you wanted to use an</p>



<p>unsigned integer so you could get like a gigantic um maximum number that you looped up to i don&#8217;t</p>



<p>know so we&#8217;ll just initialize here and we&#8217;ll say r13 is equal to zero the init part should not be</p>



<p>part of the actual like looping like every time you loop up to the top of the for loop you should</p>



<p>not repeat that part again it should only happen once so the next label that i have is called loop</p>



<p>have is called loop top and that&#8217;s just going to be the top of the loop that I</p>



<p>continue to go back up to every time I want to see if we&#8217;re supposed to</p>



<p>continue looping and then go into the loops body so maybe I should say that&#8217;s</p>



<p>why this is named to underscore loop top and I don&#8217;t know you don&#8217;t have to do</p>



<p>camel casing and your labels you know you could just do loop in it or whatever</p>



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



<p>Remember that was the check part, right?</p>



<p>So we&#8217;re going to check to see that R13 is still less than R12.</p>



<p>If it is, we&#8217;ll continue with the for loop.</p>



<p>If it&#8217;s not, then we jump out of the for loop.</p>



<p>So that means probably the true case where R13 is indeed less than R12,</p>



<p>that&#8217;s going to be a short jump just into the loop&#8217;s body.</p>



<p>And the false case where R13 is not less than R12,</p>



<p>that should probably end up being a much longer jump.</p>



<p>on how big your your loop is maybe that jump is too long too far away for a conditional branching</p>



<p>instruction to reach if you&#8217;ve watched my previous videos you should know already that</p>



<p>the conditional branching instructions like jl like jump less than they can only reach about 128</p>



<p>bytes away if you try to go further than that the assembler will actually stop and block you from</p>



<p>finishing your compilation it&#8217;ll say i think it&#8217;s like jump out of range or something like that</p>



<p>So you want to try to keep the short jump points with your conditional branches.</p>



<p>And then in the false case, where the conditional branch doesn&#8217;t actually do anything,</p>



<p>then it falls through to the next instruction where you will have an unconditional jump.</p>



<p>And remember, the unconditional jumps, they don&#8217;t have a limitation of 128 bytes.</p>



<p>They can jump like all over the place.</p>



<p>Like they can jump anywhere, basically.</p>



<p>that means if you think about it, we come in to the loop top right here.</p>



<p>We immediately do a compare instruction and a conditional branch.</p>



<p>So if R13 is less than R12, meaning we should continue to for loop,</p>



<p>then we&#8217;ll just do a short jump into the loop&#8217;s body and actually execute its body.</p>



<p>And if not, we will end up falling through to line 92,</p>



<p>where there&#8217;s just an unconditional jump instruction that just says,</p>



<p>all right, let&#8217;s jump all the way down to being done.</p>



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



<p>Okay, so we&#8217;ve done that.</p>



<p>Now let&#8217;s implement the loops body because the first thing that we did is we wanted to</p>



<p>jump into the loop body to actually execute our instructions, which in this case, we&#8217;ll</p>



<p>just be, you know, printing a number every time we loop and then increasing that number.</p>



<p>So I&#8217;m going to do a little like new line there.</p>



<p>I&#8217;m going to paste the loop body.</p>



<p>little comments to help remind myself that this is actually the loop&#8217;s body. So for test loop body</p>



<p>that takes care of the branching instruction, hitting on that and actually going into the body.</p>



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



<p>we&#8217;ll just say the body. What does it do? It just prints a little message to the user.</p>



<p>If you look at the message for current number string, if I go up real fast,</p>



<p>message for current number, it&#8217;s just an arrow. So the user&#8217;s going to see an arrow and then</p>



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



<p>current number.</p>



<p>And then every time it loops, it&#8217;s just going to continue printing that arrow with a number</p>



<p>on it.</p>



<p>So we&#8217;re doing that.</p>



<p>And then we&#8217;re going to say R13, which is the current counter variable, which started</p>



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



<p>the first integer argument for a function call.</p>



<p>So I&#8217;m just going to call this other function here, which is not part of the video to just</p>



<p>which is not part of the video to just say hey please print this number for me so the first</p>



<p>time this iterates it&#8217;s going to print zero because it&#8217;s going to print r13 and then it&#8217;s</p>



<p>going to print a new line so that the cursor goes to the next you know line of the terminal</p>



<p>so that&#8217;s all the body does it just kind of like prints the current number with an arrow and does</p>



<p>a new line and then at the very bottom of the loop body we just sort of maintain the for loop this is</p>



<p>for loop this is going to be the update part so like let&#8217;s see maybe scooch this up a little bit</p>



<p>the update part where we have let&#8217;s see on the very right side of the for loop which is like i</p>



<p>plus plus or in more assembly speak increasing the counter variable so i&#8217;m just going to increase r13</p>



<p>and then that way the loop can progress you know we&#8217;re always looking at r13</p>



<p>to decide if we need to stop or not you know we&#8217;re comparing r13 to r12</p>



<p>So we&#8217;re just increasing at the very bottom of the loop and then we unconditionally jump back up to the top.</p>



<p>Probably a better idea to unconditionally jump back up to the top because maybe the jump to the top is very very far and if it&#8217;s greater than 128 bytes it won&#8217;t work.</p>



<p>So the regular jump instruction doesn&#8217;t have that limitation. So now we&#8217;re jumping up to the loop top.</p>



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



<p>We&#8217;re just jumping up to the top here where we immediately ask you know are we done?</p>



<p>If we&#8217;re not done, we jump into the loop body and print another number.</p>



<p>And then at the very bottom of the body, we say, all right, increase the counter and then jump back up to the top.</p>



<p>So this is a simple for loop, but you can see what it&#8217;s doing, right?</p>



<p>It&#8217;s just going to be printing a number over and over again as the number increases.</p>



<p>And then eventually it&#8217;ll stop when it hits the correct number.</p>



<p>The last thing we need is the loop done label.</p>



<p>If you look back up at the top here on line 92, if R13 was not less than R12,</p>



<p>less than r12 then execution would fall through to line 92 and there is our unconditional jump</p>



<p>instruction basically saying if r13 is not less than r12 then we&#8217;ll jump to the loop done label</p>



<p>which means we&#8217;re just totally finished with this loop so we have to make that real fast</p>



<p>and all it&#8217;s going to do is just basically say goodbye it&#8217;s just going to print a message to</p>



<p>to the restoration functions.</p>



<p>Oh, did I ruin my return somewhere?</p>



<p>Uh-oh, what did I do wrong?</p>



<p>I lost my return statement.</p>



<p>Did I accidentally delete that somehow?</p>



<p>That&#8217;s a bad program.</p>



<p>Or did I not even…</p>



<p>Hmm, I wonder if it like…</p>



<p>I wonder if I didn&#8217;t have my return statement</p>



<p>and it fell through into the CRLF function</p>



<p>and then the CRLF function returned to the caller</p>



<p>to the caller on behalf of the for test function I don&#8217;t even know only all only</p>



<p>the spirits know at this point I don&#8217;t know leave a comment or something if</p>



<p>you know what happened but every function has to have its own return for</p>



<p>sure so you know the loop done label is usually where you want to jump to when</p>



<p>you know the for loop is finished or the while loop is finished when it comes to</p>



<p>saying goodbye it probably would be a little bit more clear of me to add an</p>



<p>you know for test you know say goodbye just so that I that I remember that this</p>



<p>is this is the place where we&#8217;re done and this other place this is just</p>



<p>something else that&#8217;s happening maybe I&#8217;m doing more instructions or more</p>



<p>operations or calling a function or whatever so obviously if I did it this</p>



<p>way then the loop done label would just end up falling through to the goodbye</p>



<p>label and it would be fine but it&#8217;s just more visually clear I&#8217;m gonna take it</p>



<p>Anyway, so at this point we might have a program that actually works.</p>



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



<p>What else do I need to add?</p>



<p>No, I think we&#8217;re done with that.</p>



<p>Okay, let&#8217;s try it.</p>



<p>So we&#8217;ll do a make run and we&#8217;ll enter the number five and let&#8217;s see if it works or it</p>



<p>crashes.</p>



<p>It worked on the first try.</p>



<p>So I had a solution though.</p>



<p>Not fair, but I could have typoed.</p>



<p>Anyway, so it says we entered a five, the loop will print from zero to N minus one.</p>



<p>print from zero to n minus one so we should see from zero to four and so then every iteration of</p>



<p>the loop it&#8217;s just that little message we&#8217;re just printing a zero printing one printing a two you</p>



<p>know the number increases because we did did that little inc instruction the increase instruction</p>



<p>we unconditionally jump to the top of the loop where we decide if we&#8217;re supposed to uh finish</p>



<p>you know be done with the loop by jumping to the done label eventually after we uh let&#8217;s see</p>



<p>it prints this four here it&#8217;ll do the increase instruction at the bottom of the loop&#8217;s body</p>



<p>and then it&#8217;ll jump to the loop&#8217;s top then the loop&#8217;s top will see that it&#8217;s a five because we</p>



<p>just increased the four it will see that five is definitely not less than five so that&#8217;s a false</p>



<p>which means execution will you know fall through where the heck is that it&#8217;ll fall through to the</p>



<p>line 92 jump instruction which is just the loop being done so that&#8217;s here where it says goodbye</p>



<p>says goodbye and then we have successfully implemented a basic for loop</p>



<p>all right so thank you so much for watching this video I hope you learned</p>



<p>a little bit and had a little bit of fun I will see you in the next video happy</p>



<p>coding and happy studying hey everybody thanks for watching this video again</p>



<p>from the bottom of my heart I really appreciate it I do hope you did learn</p>



<p>hope you did learn something and have some fun. If you could do me a please, a small little favor,</p>



<p>could you please subscribe and follow this channel or these videos or whatever it is you do on the</p>



<p>current social media website that you&#8217;re looking at right now. It would really mean the world to</p>



<p>me and it&#8217;ll help make more videos and grow this community. So we&#8217;ll be able to do more videos,</p>



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



<p>and subscribe. You know sometimes I&#8217;m sleeping in the middle of the night and I just wake up because</p>



<p>I know somebody subscribed or followed. It just wakes me up and I get filled with joy. That&#8217;s</p>



<p>exactly what happens every single time. So you could do it as a nice favor to me or you could</p>



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



<p>and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also if you look at the middle of the</p>



<p>screen right now you should see a QR code which you can scan in order to go to the website which</p>



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



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



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



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



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



<p>I wake up in a cold sweat and I&#8217;m like,</p>



<p>it would really mean the world to me.</p>



<p>I would really appreciate it.</p>



<p>So again, thank you so much for watching this video</p>



<p>and enjoy the cool music as I fade into the darkness,</p>



<p>which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/">x86-64 Assembly For Loops Explained &#8211; YASM Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-for-loops-explained-yasm-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 25 Jan 2026 01:02:57 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly control flow]]></category>
		<category><![CDATA[assembly language loops]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly while loop example]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional jump assembly]]></category>
		<category><![CDATA[learn assembly language]]></category>
		<category><![CDATA[low level loops]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM vs YASM]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[unconditional jump]]></category>
		<category><![CDATA[while loop assembly]]></category>
		<category><![CDATA[x86 assembly loops]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 linux assembly]]></category>
		<category><![CDATA[x86-64 programming]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=291</guid>

					<description><![CDATA[<p>This video teaches how to implement a classic while loop structure in x86-64 assembly language using YASM. We cover the conceptual mapping from C-style while(condition) { body } to assembly labels (while_top, while_body, while_done), conditional jumps (jne, je, etc.), and the unconditional jump back to the top of the loop. A complete runnable example repeatedly reads integers from the user and echoes them until 99 is entered.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/">x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly While Loops Explained Step by Step in YASM - From Concept to Working Program" width="1380" height="776" src="https://www.youtube.com/embed/JnkyFMw09Us?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>In this detailed tutorial I walk you through exactly how to implement while loops in x86-64 assembly language using YASM. We start with the basic concept of a while loop as it exists in higher-level languages like C/C++, then break it down into labels, conditional jumps, and unconditional jumps so you can see exactly how the control flow works at the assembly level.</p>



<p>I explain why we usually put the condition check at the top, how to handle the jump distances (especially the ~128-byte limit of conditional branches), why unconditional jumps are preferred for the loop-back, and how to structure init / top / body / done sections clearly.</p>



<p>We then build a complete, runnable hybrid program (assembly + tiny C++ driver) that:</p>



<ul class="wp-block-list">
<li>prints an intro message</li>



<li>repeatedly asks the user to enter numbers</li>



<li>echoes each number back</li>



<li>continues until the user enters 99</li>



<li>prints a goodbye message when finished</li>
</ul>



<p>Lots of practical tips about label naming conventions, register preservation (R12 in this case), and debugging flow are included along the way.</p>



<p>Assumed knowledge: basic x86-64 assembly, how to use a makefile, simple system calls, and calling external functions. If you&#8217;re new to those topics, check my earlier videos first.</p>



<p>Hope this helps someone finally &#8220;get&#8221; while loops in assembly!<br>Thanks for watching &#8211; subscribe if these kinds of low-level explanations are useful to you.</p>



<p>Introduction to While Loops in x86-64 YASM 00:00:00<br>While Loop Concept in High-Level Languages 00:00:56<br>Breaking Down While Loop Structure 00:02:00<br>Labeling Key Sections Top Body Done 00:02:40<br>Conditional and Unconditional Jumps Explained 00:03:26<br>Why Prefer Shorter Conditional Jumps 00:04:40<br>Diagram of While Loop Flow 00:06:52<br>Alternative While True with Internal Break 00:08:08<br>Do-While vs Regular While Difference 00:09:32<br>Program Setup and Data Section Overview 00:09:53<br>Hybrid Program Structure and External Functions 00:10:56<br>Main Function and While Test Call 00:12:56<br>While Test Function Prologue 00:13:21<br>CRLF Helper Function 00:14:00<br>Intro Message and Loop Initialization 00:14:50<br>While Top Comparison with 99 00:16:57<br>Entering the Loop Body 00:19:08<br>User Input and Echo Output 00:19:38<br>Jump Back to While Top 00:20:29<br>Exit to While Done Section 00:21:39<br>Goodbye Message and Function Epilogue 00:22:14<br>Live Demo Running the Program 00:22:30<br>Summary and Closing Remarks 00:23:12<br>Call to Subscribe and Website Mention 00:23:46</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>All right, hello there. In this video, I&#8217;m going to teach you how to implement while loops in YASM</p>



<p>x86-64 assembly. Although you probably don&#8217;t need to be using YASM assembly to understand</p>



<p>this video or to benefit from it. So what am I talking about? First off, okay, so we&#8217;re going</p>



<p>to do while loops. There&#8217;s a lot of assumed knowledge in this video. If you don&#8217;t know how</p>



<p>assembly program, if you don&#8217;t know how to make a make file, if you don&#8217;t know a lot</p>



<p>of the basics that I&#8217;m just skimming over in this video, you&#8217;ll need to see my other</p>



<p>videos because I explain all of those things at length.</p>



<p>For this video, I&#8217;m just going to be talking about while loops only.</p>



<p>So for starters, let&#8217;s see here and open up a little notepad here and just type a while</p>



<p>loop and try to explain a little bit about it conceptually.</p>



<p>And then after that, we&#8217;re going to, we&#8217;re going to write a program in Yasm to show that</p>



<p>we can get this to work.</p>



<p>So what is a while loop in a higher level language?</p>



<p>We&#8217;ll just suppose C or C++.</p>



<p>You can imagine whatever language you want, but typically in a while loop,</p>



<p>we&#8217;re not talking about a do while loop, although that would be really easy</p>



<p>to implement after watching this video.</p>



<p>But in a regular while loop, you have the keyword while usually,</p>



<p>and then you have some sort of an expression like does a equal B or whatever.</p>



<p>So maybe I&#8217;ll just say does a equal B?</p>



<p>Expression evaluates to true then the body of the while loop will execute and then when it&#8217;s done executing</p>



<p>Execution will go back up to look at the expression again and make sure that it&#8217;s still true</p>



<p>So the loop will run forever until that expression evaluates to false</p>



<p>Maybe that&#8217;s what you want. Maybe that&#8217;s not what you want, but that&#8217;s what it&#8217;ll do</p>



<p>And that means the body runs an endless number of times</p>



<p>Also, if the expression evaluates to false on the very first run</p>



<p>then the body of the while loop will just never run at all.</p>



<p>So that&#8217;s, you know, a basic idea of a while loop in a higher level language.</p>



<p>Hopefully you kind of already know that.</p>



<p>So let&#8217;s look at the parts right here.</p>



<p>If we imagine this as assembly,</p>



<p>then probably the first thing we should do is label the part</p>



<p>where we look at the expression and decide whether we&#8217;re going to continue or not, right?</p>



<p>So I&#8217;m going to put a little label here</p>



<p>and maybe we&#8217;ll call it a while top or something.</p>



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



<p>just to label the part of the code</p>



<p>So that we know when we write assembly</p>



<p>We&#8217;ll be able to put the appropriate labels on the different parts of the while loop and we&#8217;ll be able to imagine</p>



<p>The while loop that we&#8217;re implementing in assembly</p>



<p>So I&#8217;m gonna say this is the top or you can call it the eval part whatever you want</p>



<p>Then we&#8217;ll make another label down here. We&#8217;ll call it</p>



<p>You know while body something like that and so basically if we want to jump up to the top of the while loop to evaluate the</p>



<p>expression, we just have to basically jump to that label called while top.</p>



<p>And if we want to jump to the body to actually execute the code,</p>



<p>then we just have to jump to that other label instead. We should also have a label at the</p>



<p>very bottom after the whole while loop is over and we&#8217;ll call it, you know, while finished</p>



<p>or while, whoops, finished or while done or while over something like that. So I&#8217;m just going to</p>



<p>Now you can imagine that when we&#8217;re at the top of the while loop and we see that the</p>



<p>expression is false, then we can just jump down to the done label, thereby exiting the</p>



<p>entire while loop.</p>



<p>Then of course we probably want to have some jump statements in here.</p>



<p>So for example, the expression part here, it&#8217;s probably going to jump either to the</p>



<p>body or either to the done area based on whether or not the expression evaluated to true or</p>



<p>false.</p>



<p>label while top you know which contains a conditional branch and then like an unconditional</p>



<p>jump so if a condition is true maybe we can jump into the body if the condition is false we&#8217;ll fall</p>



<p>through to the next statement and do an unconditional jump to the done label if you don&#8217;t</p>



<p>know conditional branching this is another one of the topics that i&#8217;ve covered in previous videos</p>



<p>as we implement the while loop.</p>



<p>So just keep in mind, there are other videos that I have</p>



<p>that you should watch first</p>



<p>if you don&#8217;t understand conditional branching or jumps.</p>



<p>The reason that I&#8217;m going to kind of design</p>



<p>the jumps like this where I&#8217;m going to say,</p>



<p>let&#8217;s do a conditional branch.</p>



<p>And basically if the loop evaluates to true,</p>



<p>we&#8217;ll jump into the body of the loop.</p>



<p>And if it doesn&#8217;t evaluate to true,</p>



<p>then the conditional branch will allow execution</p>



<p>to fall through down to the next statement,</p>



<p>which will simply contain a jump to the done portion.</p>



<p>to the done portion so basically as soon as the while loop is ready to break it gets uh implemented</p>



<p>by an unconditional jump which was fallen through to by the conditional branch that would only jump</p>



<p>into the body if uh expression was true i&#8217;m doing it this way because it&#8217;s usually a shorter jump</p>



<p>uh to jump to the true area you know the the body of the while loop and remember conditional</p>



<p>branching instructions have a maximum you know jump reach of about 128 bytes so if you try to</p>



<p>128 bytes. So if you try to jump too far, like for example, if you wanted to, you could reverse</p>



<p>the logic to where you could say, if an expression is true, let&#8217;s jump to the done portion and say,</p>



<p>we&#8217;re going to break the loop. If the expression was false, we&#8217;ll jump into the body. You could</p>



<p>do that. But then if the body of your while loop was too long, maybe it was so long that the last</p>



<p>instruction was greater than 128 bytes away from the top. Then by the time you wanted to</p>



<p>let&#8217;s see yeah if you have if you have too long of while loops you can&#8217;t jump more than 128</p>



<p>instructions away so that&#8217;s a limitation of contentional branching so I just I want to put</p>



<p>the biggest jump on an unconditional jump instruction because it doesn&#8217;t have a limitation</p>



<p>like that I was having a hard time imagining what I was about to say just now for the</p>



<p>the reversal I guess I&#8217;m just going to ignore the reversal scenario and we&#8217;ll just do it the</p>



<p>and we&#8217;ll just do it the regular way.</p>



<p>Anyway, so we want to have a conditional jump that either jumps into the body or to the done area.</p>



<p>And then at the very bottom, we want to have an unconditional jump to the top,</p>



<p>which basically means every time we get to the bottom of the while loop body,</p>



<p>we&#8217;re just going to jump up to the while top.</p>



<p>Actually, I&#8217;ll just say to while top.</p>



<p>Remember again, the regular jump instructions,</p>



<p>maybe I&#8217;ll take out the U here.</p>



<p>The regular jump instructions don&#8217;t have a limitation</p>



<p>on how far they can jump.</p>



<p>So that should be fine.</p>



<p>And that&#8217;s the basic idea of implementing a while loop.</p>



<p>Maybe I should draw just a quick diagram</p>



<p>before we start looking at the code,</p>



<p>just to make sure everybody of different learning styles</p>



<p>understands what I&#8217;m talking about.</p>



<p>So we&#8217;ll, let&#8217;s see, eval.</p>



<p>I&#8217;m still adjusting my pen and you know what?</p>



<p>You know what? It looked a lot better before I hit record.</p>



<p>Eval the expression.</p>



<p>So I&#8217;m going to first evaluate the expression.</p>



<p>If it evaluates to true, we&#8217;ll put a green arrow here and I&#8217;ll put like a T for true.</p>



<p>Then this will be the body of the while loop.</p>



<p>I&#8217;ll say like the body.</p>



<p>If it evaluates to false, let&#8217;s see.</p>



<p>I&#8217;ll just put that in red and I&#8217;ll put an F here then this is just going to be the done area</p>



<p>right so you can imagine we evaluate an expression using the compare instruction in assembly and</p>



<p>based on the results of comparing something whatever it is that your condition is then we</p>



<p>you know we we either branch to the done area or we branch to the to the body area</p>



<p>want to have a more complicated while loop and you don&#8217;t want to put a huge</p>



<p>amount of expressions or if you don&#8217;t want to logically concatenate a bunch of</p>



<p>different components into the expression you know that&#8217;s fair I usually write my</p>



<p>while loops in real life as just while true and then I break when certain</p>



<p>conditions are met you can do that too we&#8217;re not going to talk about that in</p>



<p>this video but you could just have a while true so you always jump to the top</p>



<p>and there&#8217;s no branching that goes to while done but then throughout the loop</p>



<p>loop you can just sort of check to see if certain conditions are met and if they are then you&#8217;ll do</p>



<p>a branch to the done area and if they&#8217;re not then by the time you make it down to the bottom of the</p>



<p>loop then it just automatically jumps to the top although you got to make sure with the the reach</p>



<p>of conditional branching you probably want to test to see if your condition is true or false or</p>



<p>whatever if it means we&#8217;re going to continue the loop then you probably just want to do a conditional</p>



<p>then you probably just want to do a conditional branch that jumps down a little bit</p>



<p>so that it can continue the body of the loop.</p>



<p>And then the part that it skipped over,</p>



<p>just a little part should be an unconditional jump that jumps out of the loop.</p>



<p>That way, it doesn&#8217;t matter how big your while loop body is,</p>



<p>you can always jump out of the loop.</p>



<p>But we&#8217;re not going to talk about that in this video.</p>



<p>Anyway, so we evaluate the expression.</p>



<p>If it&#8217;s true, we go to the body.</p>



<p>If it&#8217;s false, we go to done.</p>



<p>After the body is done executing,</p>



<p>then we just jump back up to the top where we evaluate the expression.</p>



<p>where we evaluate the expression.</p>



<p>I&#8217;ll put while here just to make it more clear that we&#8217;re talking about the basic</p>



<p>idea of a while loop.</p>



<p>Not too hard, you know.</p>



<p>And then if you wanted to implement a do while loop, just make sure that you always</p>



<p>evaluate the body at least once.</p>



<p>That&#8217;s really the only difference.</p>



<p>But that&#8217;s up to you.</p>



<p>This is just a regular while loop video.</p>



<p>Okay.</p>



<p>So we&#8217;ve kind of talked about it a little bit.</p>



<p>We&#8217;ve sort of, you know, drawn it out in a diagram and put some regular code in.</p>



<p>and put some regular code in let&#8217;s set up an actual assembly program that will do this so for starters</p>



<p>again i&#8217;m not going to show you my make file because i&#8217;ve already made other videos where i</p>



<p>explained how to make a make file from scratch same thing goes for hybrid programs i&#8217;m going</p>



<p>to have a driver which is a c plus plus module that just sort of calls on this assembly module</p>



<p>if you want to know how to make hybrid programs or you know drivers or you know whatever see my</p>



<p>to be talking about looping only. Okay, so let me get my solution up here. My source code is called</p>



<p>looper. And I&#8217;m just going to copy paste the data section for my program real fast, just so you can</p>



<p>see it. Okay, so here&#8217;s the data section. Again, this is not a basics for assembly video. If you</p>



<p>don&#8217;t know how to do a data section in Yasm, see my other videos. But for now, I&#8217;m just going to say</p>



<p>like the actual array of bytes and then a length and all I&#8217;m doing is printing out messages you</p>



<p>know begin the wild test your numbers will be printed back to you until you decide to quit so</p>



<p>that&#8217;s going to be printed to the user right away then every time the program wants a number from</p>



<p>the user it&#8217;ll just ask you know please enter a number or 99 to quit and then it&#8217;ll prefix</p>



<p>the echo back it&#8217;ll say you entered and then it will actually print the number and then when you</p>



<p>we&#8217;re done and i&#8217;m going to use an external uh function that i have available to just kind of</p>



<p>like input and output numbers this video is not about uh input and outputting and external libraries</p>



<p>if you&#8217;re interested you could probably just link a hybrid program and use printf and scan</p>



<p>f to very easily do input and output but that&#8217;s not what this video is about see my other videos</p>



<p>then i have crlf which is just a carriage return in line feed uh you know select the cursor goes</p>



<p>You know select the cursor goes to the next line and then I have like a system call code to just print and</p>



<p>Then a file descriptor just to print a standard output again. That&#8217;s explained in other videos</p>



<p>So now I&#8217;m ready to start copy pasting the main portion of my program</p>



<p>So first off I&#8217;m going to start the text section, which is where the instructions go in the ASM</p>



<p>So there it is and then I&#8217;m going to copy paste</p>



<p>of external symbols. So I just have a little library that I&#8217;m using that makes it easier for</p>



<p>me to input and output integers. So you can do this any way you want, or you can hard code a</p>



<p>number in the globals area. If you don&#8217;t want to deal with input and output while you&#8217;re learning</p>



<p>how to do loops, that&#8217;s totally fine. But then the entry point is going to be a function called</p>



<p>looper. So this is my function. And again, this is a hybrid program. So the main function or the</p>



<p>be present in my assembly module the driver is going to be a c plus plus module that just calls</p>



<p>on a function named looper so that means this function is going to get called from another</p>



<p>module and that&#8217;s why i have to mark it as global and then it is a function so i&#8217;m going to call</p>



<p>return at the end of it to say we&#8217;re done and then within this function i&#8217;m just going to call two</p>



<p>other functions that i&#8217;m about to create one is called while test which is going to actually do</p>



<p>the while loop and the other is called crlf which just print which just prints a new line for me i</p>



<p>which just prints a new line for me.</p>



<p>I don&#8217;t know why I do it that way, but I want to.</p>



<p>Sorry, not sorry.</p>



<p>So this is the real meat of the function here.</p>



<p>Let&#8217;s start, well, the real meat of the code or the video.</p>



<p>Let&#8217;s start a function called while test.</p>



<p>You can see it&#8217;s got a void signature with no arguments,</p>



<p>so it doesn&#8217;t really, you know, take anything or return anything.</p>



<p>I&#8217;m going to use register R12 to hold the user&#8217;s input</p>



<p>so that I can, you know, print it out and stuff.</p>



<p>print it out and stuff so that means I have to do a preservation of R12 because it&#8217;s a</p>



<p>callee saved for the ABI which you should respect so I&#8217;m going to push it at the beginning</p>



<p>and then I&#8217;m going to pop it at the end and then this is a function so I have to return</p>



<p>at the end of the function let me just double check that there&#8217;s nothing else weird at the</p>



<p>bottom of that nope okay so we&#8217;ve got the prologue and epilogue the wild test function</p>



<p>maybe I should copy paste my crlf real fast my crlf function it&#8217;s just sad but</p>



<p>it&#8217;s also kind of cute right it does nothing except just print out a new line</p>



<p>that&#8217;s all it does this video is not about that so at this point I should</p>



<p>have a program that probably works let&#8217;s see if it does I&#8217;m gonna do clear and</p>



<p>make run again if you want to know how to use make files or compile or link or</p>



<p>anything like that see my other videos so I&#8217;m gonna run it and it just says</p>



<p>which is some code that we&#8217;re not looking at.</p>



<p>And then it says it&#8217;s regain control.</p>



<p>And this line in the middle, which is just an empty new line,</p>



<p>that&#8217;s definitely from the assembly program.</p>



<p>Because at the top here we have CRLF.</p>



<p>If I call it multiple times CRLF,</p>



<p>then you&#8217;ll see there are multiple blank lines.</p>



<p>All right, I&#8217;m gonna take that out.</p>



<p>Now let&#8217;s continue with while test.</p>



<p>So what should we do here?</p>



<p>The first thing that we should do is print an intro message</p>



<p>message just to let the user know that we&#8217;re about to you know begin our while</p>



<p>tests and if we run the program one more time we should see that message now</p>



<p>begin the while test your numbers will be printed back to you system calls and</p>



<p>simple printing is covered in other videos so now uh you know for me I kind</p>



<p>of like to init all of my loops even if it&#8217;s a while loop and not just a for</p>



<p>loop or anything so I always have an extra label that I like to call init</p>



<p>or before or something like that.</p>



<p>So I have a label now called while test underscore init,</p>



<p>and it&#8217;s just where I&#8217;m gonna initialize</p>



<p>whatever it is that I think I need to initialize</p>



<p>so that the loop will actually work.</p>



<p>If you look here, all I&#8217;m really doing is setting R12 to zero</p>



<p>because what I&#8217;m gonna do is stop the loop.</p>



<p>I&#8217;m gonna break the loop whenever the user enters a 99.</p>



<p>So I don&#8217;t know what&#8217;s inside of R12</p>



<p>when we first start this function.</p>



<p>And I just wanna make sure that it&#8217;s not 99,</p>



<p>the stopping number at the very start so I&#8217;m just going to set it to zero. So</p>



<p>another note about my labels you don&#8217;t have to do it this way but I love to</p>



<p>write labels where the first part of the label is always the function that the</p>



<p>label is inside of so notice how the function is named while test and so my</p>



<p>label is always while test underscore something and I&#8217;m putting init here</p>



<p>just to say we&#8217;re initializing the loop but if you had a function that had a</p>



<p>had a lot of stuff going on in it for starters you should probably be breaking up that function</p>



<p>into multiple functions but but assuming you didn&#8217;t uh you should probably do another underscore</p>



<p>and then like another component and then another underscore based on what giant chunk of your</p>



<p>function you&#8217;re inside of so if there was like an if part a while part an input part an output part</p>



<p>you probably want to you know stick that into your labels your labels will get huge but for me</p>



<p>infused in assembly and this makes it easier.</p>



<p>So we&#8217;re going to initialize so that we can run our while loop and then the next thing</p>



<p>we&#8217;re going to do is implement the top of the while loop.</p>



<p>So remember if we looked at this code up here, maybe if I drag this over to the side and</p>



<p>pin it to the top for a little while, you can see that the top of the while loop is</p>



<p>where we kind of evaluate the expression to see if we need to keep going or not.</p>



<p>jump into the ending area, the done area. So the top, whoops, the top is always for that.</p>



<p>We&#8217;ll say first, I&#8217;m going to ask, are we done? So, you know, how do we know if we&#8217;re done?</p>



<p>In this particular while loop, we want to compare the user&#8217;s input, which is R12 to the number 99.</p>



<p>And if it&#8217;s equal, then we will quit, which means also if it&#8217;s not equal, we will jump into the</p>



<p>So by the way, you&#8217;re probably wondering how did R12 get the user&#8217;s input?</p>



<p>Well, we&#8217;re going to do that as the next step.</p>



<p>And of course, your design pattern may vary a little bit.</p>



<p>No, that&#8217;s okay.</p>



<p>I just like to implement it this way.</p>



<p>So first thing we&#8217;re going to do is compare R12 with 99.</p>



<p>And that&#8217;s why I&#8217;ve written this comment here, just like the blank while comparison part.</p>



<p>And I guess I could have put this R12 not equal to 99 up at the top, but then it kind</p>



<p>of feels like I&#8217;m leaving these other two instructions.</p>



<p>So I moved it down one.</p>



<p>these other two instructions so I moved it down one so basically as long as you know we compare</p>



<p>R12 and 99 compare and conditional branching is covered in other videos but we compare those two</p>



<p>values and then we say if R12 is not equal to 99 then jump to the body and we expect that the body</p>



<p>will be like a short jump which will be within the range of a conditional branch</p>



<p>So anyway, if the not equal branch didn&#8217;t happen, that means R12 is equal to 99.</p>



<p>At that point, execution falls through to line 84, and we&#8217;ll just unconditionally jump out of the while loop.</p>



<p>So basically we&#8217;re saying, if it&#8217;s true, we continue looping.</p>



<p>If it&#8217;s not true, we just jump outside of the loop.</p>



<p>We&#8217;re just totally done with the while loop.</p>



<p>Okay.</p>



<p>So then we need a body, because obviously we&#8217;re going to jump into the body here.</p>



<p>that means I&#8217;m going to just copy paste another little set of code here.</p>



<p>Right after that unconditional jump.</p>



<p>So now we got the body.</p>



<p>Notice how I put a little comment here that has a brace just to indicate to you,</p>



<p>hey, this is the beginning of the actual while loop body,</p>



<p>just to make it a little bit more clear.</p>



<p>And what are we going to do inside of the body?</p>



<p>We&#8217;re just going to ask the user for some input.</p>



<p>So I&#8217;m printing a simple message here,</p>



<p>and then I&#8217;m calling on my helper function</p>



<p>to just actually input a number from the user.</p>



<p>I&#8217;m going to store that number into R12.</p>



<p>So that&#8217;s how R12 gets the numbers, gets the user&#8217;s input.</p>



<p>And the way I&#8217;ve written this, if the user enters a 99,</p>



<p>it&#8217;ll echo it back to the user and then it&#8217;ll break the loop afterwards.</p>



<p>So, you know, if you wanted to rearrange things like I talked about before,</p>



<p>where you input before you check to see if you&#8217;re going to keep going, you could do that.</p>



<p>But it would be a little harder to echo the user&#8217;s input back to them before you break.</p>



<p>I don&#8217;t know. It&#8217;s up to you.</p>



<p>I don&#8217;t know it&#8217;s up to you anyway so we grab input from the user and then we print another</p>



<p>message basically saying here&#8217;s the thing that you inputted no problem and then again we use</p>



<p>one of my helper functions to actually spit the number back out at them so this is not</p>



<p>a very complicated body it&#8217;s just asking for a number and then printing the number back to them</p>



<p>and then after that I&#8217;m going to do another label and I&#8217;m going to call it the body bottom</p>



<p>we don&#8217;t really have to do this label but for clarity I think it&#8217;s probably a good idea</p>



<p>I think it&#8217;s probably a good idea.</p>



<p>So the very bottom of the while loop&#8217;s body is usually where you don&#8217;t do any more instructions</p>



<p>that are part of the work of the while loop&#8217;s body,</p>



<p>but just sort of the place where you jump back up to the top</p>



<p>so you can evaluate and decide to continue or not again.</p>



<p>So notice how I&#8217;m using an unconditional jump here.</p>



<p>That&#8217;s a good idea because again, if you have like a huge while loop body,</p>



<p>you might end up surpassing the threshold of 128 bytes</p>



<p>of 128 bytes and then you&#8217;ll get a assembler error that says</p>



<p>I can never remember this. It&#8217;s like a</p>



<p>jump out of range error or something like that. Basically, if you do a conditional branch to jump up the top</p>



<p>because some people like to check to see</p>



<p>if they should continue the loop, they like to check for that at the bottom. I&#8217;ve done that before.</p>



<p>And then if true, then we&#8217;ll jump to the top of the loop. But if the loop is too big, that won&#8217;t work. So</p>



<p>I just like to take a long jump to the top of the loop and then decide if I&#8217;m going to keep going at the very top.</p>



<p>if I&#8217;m going to keep going at the very top. And then there&#8217;s a comment saying, hey, that&#8217;s the</p>



<p>end of the body. Okay, no problem. Now let&#8217;s do the done label, which is basically where we jump</p>



<p>if the loop is actually finished. So remember, if this expression right here evaluates to false,</p>



<p>then execution is going to fall through to line 84, where we jump to this while test done label.</p>



<p>And so I&#8217;m just going to put the while test done label right here. So we&#8217;re done. So the done is</p>



<p>So we&#8217;re done. So the done is not part of the loop. It comes after the loop. That&#8217;s this right</p>



<p>here on line nine of the little notepad. And we can just kind of do whatever we want. At that</p>



<p>point, we can return to a caller, we can just do other stuff, we can, you know, do a different</p>



<p>loop or, you know, whatever, we&#8217;re just done with the original loop. For me, I&#8217;m just going to say</p>



<p>goodbye with this little print to just sort of like, you know, print an exit message. And then</p>



<p>we&#8217;ll do the epilogue where we restore R12. And then we just return to the caller,</p>



<p>the driver worry about exiting the program okay so if we&#8217;ve done this correctly we should now have</p>



<p>a working program let&#8217;s see let&#8217;s see okay let&#8217;s enter a number let&#8217;s do 22 and it says you enter</p>



<p>22 and let&#8217;s do 55 and we just we can enter any numbers we want and as long as we&#8217;re not entering</p>



<p>99 the program will just continue forever so this is a while loop if i want to quit i do 99</p>



<p>breaks at the top it breaks by jumping down to the done area where we print our goodbye message</p>



<p>which just is end while test and then if we run this again if i do 99 from the start then it just</p>



<p>immediately breaks you know it prints out what you entered but then it immediately breaks</p>



<p>and that&#8217;s it that&#8217;s uh the basics for how to write a while loop you just use basically</p>



<p>layered on top of an abstract concept of what you think a while loop is,</p>



<p>or what I guess the world thinks a while loop is.</p>



<p>Okay, thank you so much for watching this video.</p>



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



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



<p>Hey everybody.</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>if you could do me a please a small little favor could you please subscribe and follow this channel</p>



<p>or these videos or whatever it is you do on the current social media website that you&#8217;re looking</p>



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



<p>community so we&#8217;ll be able to do more videos longer videos better videos or just i&#8217;ll be able</p>



<p>to keep making videos in general so please do do me a kindness and uh and subscribe you know</p>



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



<p>It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time.</p>



<p>So you could do it as a nice favor to me or you could troll me if you want to just wake me up in the middle of the night.</p>



<p>Just subscribe and then I&#8217;ll just wake up.</p>



<p>I promise that&#8217;s what will happen.</p>



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



<p>which I think is also named somewhere at the bottom of this video.</p>



<p>and it&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



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



<p>if you have a suggestion for uh uh clarifications or errata or just future videos that you want to</p>



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



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



<p>it would really mean the world to me. I would really appreciate it.</p>



<p>So again, thank you so much for watching this video and</p>



<p>enjoy the cool music as I fade into</p>



<p>the darkness which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/">x86-64 Assembly While Loops Explained Step by Step in YASM &#8211; From Concept to Working Program</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-while-loops-explained-step-by-step-in-yasm-from-concept-to-working-program/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &#038; More</title>
		<link>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/</link>
					<comments>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 18 Jan 2026 00:49:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[cmp instruction]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[conditional branching]]></category>
		<category><![CDATA[conditional jumps]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[JE JNE]]></category>
		<category><![CDATA[JL JG]]></category>
		<category><![CDATA[jump out of range]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[NASM tutorial]]></category>
		<category><![CDATA[operating systems programming]]></category>
		<category><![CDATA[reverse engineering]]></category>
		<category><![CDATA[signed unsigned jumps]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=287</guid>

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



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



<p>We cover:</p>



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



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



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



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



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



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



<p>Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.</p>



<p>Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p>Enjoy learning assembly!</p>



<p>Intro to Conditional Branching in Assembly 00:00:00<br>Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23<br>Unconditional vs Conditional Jumps 00:01:39<br>How Conditional Branching Works 00:02:06<br>The CMP Instruction Explained 00:03:21<br>Overview of Conditional Jump Instructions 00:04:50<br>Signed vs Unsigned Jump Instructions 00:06:10<br>Jump Equal &amp; Jump Not Equal (JE/JNE) 00:07:00<br>Main Conditional Jumps Summary 00:08:14<br>Starting the Code Example 00:09:09<br>Program Structure &amp; Printing Setup 00:10:35<br>Function Prologue &amp; Getting User Input 00:13:21<br>First Branch: Is Number Greater Than 0? 00:15:17<br>Live Demo: Testing Greater Than Zero 00:21:53<br>Second Branch: Is Number Less Than 10? 00:22:52<br>Live Demo: Testing Range 0-10 00:26:06<br>Third Branch: Is Number == 5? 00:26:52<br>Live Demo: Testing Equal to 5 00:28:00<br>Jump Out of Range Error &amp; Workaround 00:29:00<br>Final Summary &amp; Closing 00:32:17<br>Thanks &amp; Call to Subscribe 00:32:57</p>



<p>=-=-=-=-=-=-=-=-=</p>



<p>Thanks for watching!</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>up this book i&#8217;m going to assume that maybe you&#8217;ll get a copy or you&#8217;re just going to follow along</p>



<p>with what i&#8217;m doing on the screen but i&#8217;m going to open this book and i&#8217;m going to go to section 7</p>



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



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



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



<p>So that&#8217;s a lot of subsections, isn&#8217;t it? I love a book with a ton of subsections. It makes things so much easier to find</p>



<p>If you or someone you know is going to write a book, especially if it&#8217;s technical, please use lots and lots of subsections</p>



<p>Anyway, so conditional control instructions in my last video</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>So if we want to compare, I don&#8217;t know, let&#8217;s say R12 with R13, that&#8217;s how you would do it.</p>



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



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



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



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



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



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



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



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



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



<p>order to decide whether or not it&#8217;s going to jump or not okay so let&#8217;s see there are tons</p>



<p>there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



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



<p>by the way i&#8217;m going to write a sample program just to show you how it is in practice but so the</p>



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



<p>here we&#8217;ll say sets up r flags and then we&#8217;ll decide like what we&#8217;re going to do are we going</p>



<p>to jump if they&#8217;re equal to some label i&#8217;ll write some label here so that means uh the je instruction</p>



<p>will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p>did then we&#8217;ll jump if not execution will fall down to the next line so i&#8217;ll put a nope instruction</p>



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



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



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



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



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



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



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



<p>well I&#8217;ve got my pen it&#8217;s working oh I erased it okay okay it&#8217;s working anyway so notice how jump</p>



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



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



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



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



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



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



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



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



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



<p>bits are the same then you just say they&#8217;re equal you don&#8217;t even care actually if it&#8217;s a signed or</p>



<p>unsigned integer if it&#8217;s a float if it&#8217;s a whatever you just you just go are all the bits equal or are</p>



<p>at least one of the bits unequal then we&#8217;ll say it&#8217;s equal or not equal that&#8217;s it so keep that in</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>go to the code now. So here&#8217;s a little empty code, source code, a page. I&#8217;m going to stick</p>



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



<p>you don&#8217;t know how to write assembly at all you should see my other videos because i&#8217;m not going</p>



<p>to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p>or assemble or like do all the basics that i&#8217;m not talking about in this video i&#8217;ve covered them all</p>



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



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



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



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



<p>i&#8217;m going to announce that the branching test is going to begin and then i&#8217;m going to ask the user</p>



<p>for some input and then i&#8217;m going to based on whatever the user inputs i&#8217;m going to just print</p>



<p>out a message that sort of you know understands what the user inputted so if the user inputted</p>



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



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



<p>print that and then it&#8217;s going to say we&#8217;ve ended the branching tests then i have like a crlf again</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>if everything&#8217;s within the same piece of source code it&#8217;s totally fine so then I&#8217;m going to make</p>



<p>a function call to a function called branch test so very quickly before I actually add branch test</p>



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



<p>story short all it does is just it prints a new line in the terminal and that&#8217;s it like it&#8217;s just</p>



<p>it&#8217;s a convenience function I like to be able to call crlf and have a new line okay so let&#8217;s start</p>



<p>our branch test function so here&#8217;s the signature it is just branch test with a</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>out a message.</p>



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



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



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



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



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



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



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



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



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



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



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



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



<p>this is how I&#8217;m going to do it in my program. In your program it might be different or you might</p>



<p>have a hard-coded number or something like that. So for now just trust me when I say that I&#8217;m going</p>



<p>to print a prompt basically saying could you please enter a number and then the next two lines</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>instance uh right now i&#8217;m asking is r12 greater than zero so i can do a jump less than or equal</p>



<p>to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p>in a different order, but I&#8217;m just choosing to say, let&#8217;s compare the two and then let&#8217;s jump</p>



<p>if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



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



<p>and then I&#8217;m going to draw out what the code is kind of doing. So let me make a label here so</p>



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



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



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



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



<p>in the middle. So think about this. This is kind of the idea. Let&#8217;s see if I can remember how to</p>



<p>idea let&#8217;s see if I can remember how to draw this right now off the top of my head suppose we have</p>



<p>like some sort of an input I&#8217;ll call this in put and then maybe the input is going to be you know</p>



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



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



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



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



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



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



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



<p>um less than or equal to whoops maybe i&#8217;ll do r12 uh i need to somehow adjust the pressure on this</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>kind of you know come as a pair so both of these combined mean let&#8217;s look at r12 versus zero and</p>



<p>if r12 is less than or equal to zero meaning if it will jump if we&#8217;re less than or equal to the</p>



<p>the right operand r12 less than equal to the right operand then we go down this path on the right</p>



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



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



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



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



<p>just show you that real fast oh man i&#8217;m having a hard time with this okay so let&#8217;s pretend that r12</p>



<p>less than or equal to. So that means instructions are coming, you know,</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>Okay.</p>



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



<p>So okay.</p>



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



<p>Okay.</p>



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



<p>how to to check for jump less than or equal to um we&#8217;re going to say goodbye right there so now the</p>



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



<p>it&#8217;s going to be here so again the same thing like we can just reuse r12 because we&#8217;re not really</p>



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



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



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



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



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



<p>so now at this point we&#8217;re saying compare r12 with 10 if it&#8217;s greater than or equal to 10 then</p>



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



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



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



<p>want me to write it out for you I don&#8217;t know I think once is probably enough but let me just do</p>



<p>it again we&#8217;ll do input is coming in and it&#8217;s just going to be you know comparing and then</p>



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



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



<p>we&#8217;ll do r12 and 10 and so that&#8217;s just sort of what we&#8217;re comparing with the compare instruction</p>



<p>and then the jump instruction and so if we go on the left i&#8217;m going to say that&#8217;s the fall through</p>



<p>path again and if we go to the right it&#8217;s going to be um r12 greater than or equal to 10 is going</p>



<p>and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



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



<p>here that&#8217;s just like print p for how about p for print p for print it&#8217;s going to fall through and</p>



<p>then it&#8217;s going to print and then it&#8217;s going to go to the after label so you can see that</p>



<p>the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



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



<p>10.</p>



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



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



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



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



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



<p>Okay.</p>



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



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



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



<p>Make run.</p>



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



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



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



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



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



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



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



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



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



<p>So I&#8217;m going to go to the next condition where we&#8217;re going to compare R12 to the number five.</p>



<p>And we&#8217;re going to ask, you know, is R12 equal to five?</p>



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



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



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



<p>I just think there are less jumps if I do it this way so uh here we&#8217;re just going to print you know</p>



<p>your number was equal to five and so basically if it was not equal we should have another label here</p>



<p>that allows us to skip that so all right so same thing I&#8217;m not going to draw the diagram this time</p>



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



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



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



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



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



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



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



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



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



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



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



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



<p>do a 4 we&#8217;ll get both of those greater than 0 and also less than 10 let&#8217;s do a</p>



<p>5 now we should get all those messages your number was greater than 0 your</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>to an instruction that is 100 is more than 128 bytes away, it won&#8217;t work, you&#8217;ll get that error.</p>



<p>So how do you solve that problem? Well, maybe I&#8217;ll just write this down real fast. I&#8217;m not going to</p>



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



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



<p>so if your assembler says hey you can&#8217;t jump to some label because it&#8217;s too far away it&#8217;s a</p>



<p>jump out of range assembler error then basically you can just make some other labels here you can</p>



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



<p>label and basically say if it is um well false yeah okay i guess this is the way i&#8217;ll do it this</p>



<p>is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p>can just jump to the true label so i&#8217;m going to say jump uh to the short jump true label otherwise</p>



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



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



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



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



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



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



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



<p>I&#8217;m just trying to show you that if you make a short jump to a label that will only have an unconditional jump,</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now?</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p>get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p>appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 11 Jan 2026 01:54:51 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly control flow]]></category>
		<category><![CDATA[assembly jump instructions]]></category>
		<category><![CDATA[assembly language basics]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[ed jorgensen textbook]]></category>
		<category><![CDATA[jmp instruction]]></category>
		<category><![CDATA[learning assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[unconditional jump]]></category>
		<category><![CDATA[x86 assembly tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 programming]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=278</guid>

					<description><![CDATA[<p>Learn how unconditional jump instructions (JMP) work in x86-64 assembly language using Yasm. This tutorial explains labels, unlimited jump range, and demonstrates skipping code sections with practical examples. Essential knowledge before studying conditional branching in modern assembly programming.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/">x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm" width="1380" height="776" src="https://www.youtube.com/embed/jkc7ydqAp7M?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>In this beginner-to-intermediate assembly language tutorial, we dive deep into unconditional jump instructions (JMP) in x86-64 assembly using Yasm syntax.</p>



<p>We cover:</p>



<ul class="wp-block-list">
<li>What unconditional jumps really are (basically a &#8220;go to&#8221; for assembly)</li>



<li>How labels work and how to create them</li>



<li>Why JMP has unlimited range (unlike conditional jumps)</li>



<li>Practical demo showing how to skip code sections using jumps</li>



<li>Comparison between jumping over code vs letting it execute</li>



<li>Quick look at why this matters before learning conditional branching</li>
</ul>



<p>We also reference the excellent free open-source textbook by Professor Ed Jorgensen (May 2024 version) which is highly recommended for anyone serious about learning x86-64 assembly.</p>



<p>Whether you&#8217;re preparing for university courses, reverse engineering, operating systems development, or just love low-level programming, this video will give you a clear understanding of how unconditional control flow works in modern x86-64 assembly.</p>



<p>Next video will cover conditional jumps (je, jne, jg, jl, etc.) and their limitations.</p>



<p>Enjoy the video and happy coding at the machine level!</p>



<p>Introduction to Jump Instructions 00:00:00<br>Recommended Free Assembly Textbook 00:00:23<br>What Unconditional Jumps Actually Do 00:01:27<br>Labels Explained with Examples 00:02:40<br>Unlimited Jump Range Advantage 00:04:43<br>Overview of the Demonstration Program 00:06:56<br>Building and Running the Jump Test 00:09:21<br>Live Jump Test Demonstration 00:10:53<br>Effect of Removing the Jump Instruction 00:13:50<br>Jumping in Different Directions Example 00:14:58<br>Summary and Next Video Teaser 00:17:28<br>Closing Remarks and Call to Action 00:17:44</p>



<p>Thanks for watching!</p>



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



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



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p>Hello there.</p>



<p>In this video, we&#8217;re going to be talking about jump instructions in assembly.</p>



<p>This video is going to be about x86-64 Yasm assembly,</p>



<p>but I think probably anyone who&#8217;s interested in jump instructions</p>



<p>will benefit from this video because the concept is usually the same</p>



<p>throughout any system that you might use.</p>



<p>So for starters, I want to direct your attention to a textbook</p>



<p>that I think is wonderful.</p>



<p>This is an open source free textbook that will help you become an expert in assembly.</p>



<p>It&#8217;s not mine, I didn&#8217;t write it.</p>



<p>The author is Professor Ed Jorgensen, PhD.</p>



<p>He releases this textbook for free.</p>



<p>It&#8217;s under a copyleft license so you can literally just go to his website and download it and</p>



<p>send it to your friends and everything and it&#8217;s wonderful.</p>



<p>This book will take you from zero to hero when it comes to Yasm Assembly.</p>



<p>It&#8217;s wonderful and amazing.</p>



<p>This is the book and I just want to show you the section on jump instructions real fast</p>



<p>and then I&#8217;ll talk about them a little bit and then I&#8217;ll show you a sample program that</p>



<p>uses jump instructions.</p>



<p>So this version of the book that I&#8217;m working with right now is May 2024 version 1.1.56.</p>



<p>I&#8217;m going to go down to let&#8217;s see section 7 instruction set overview.</p>



<p>Inside of there there&#8217;s another subsection called where the heck is it control instructions</p>



<p>instructions 7.7 within that there&#8217;s a another subsection called 7.7.2 that&#8217;s</p>



<p>why I look this other not one of the many reasons that I love this book it</p>



<p>has so many subsections there&#8217;s just so many yummy subsections to organize</p>



<p>everything in a way that you can find it all so fast and okay so we&#8217;re looking</p>



<p>at unconditional control instructions in other words a jump instruction if</p>



<p>you&#8217;re an old-school programmer especially if you use some sort of like</p>



<p>if you use some sort of like a basic language or a language with go-to&#8217;s you might recognize jumps</p>



<p>as just being a go-to meaning we&#8217;re not actually going to call a function and then return from it</p>



<p>which is what the modern programs tend to do we&#8217;re just going to say let&#8217;s write a jump instruction</p>



<p>and we will literally just change execution to to jump to some other location just kind of go there</p>



<p>forever maybe we come back but if we do it&#8217;s going to be because there was a different jump instruction</p>



<p>instruction that told us to jump back.</p>



<p>So we&#8217;re not calling and returning.</p>



<p>We&#8217;re just going somewhere and that&#8217;s it.</p>



<p>Obviously it&#8217;s a little bit more convenient to be able to call functions,</p>



<p>but that&#8217;s sort of like an abstraction that has to be implemented after we</p>



<p>understand how to jump. So anyway, the jump instruction is pretty simple.</p>



<p>You just put JMP in Yasm anyway, and then follow it with a label.</p>



<p>So, you know, just as a quick little recap here, what&#8217;s a label?</p>



<p>imagine we have an assembly program here and maybe here&#8217;s our text section and we put some</p>



<p>instructions maybe there&#8217;s like an entry point right here I&#8217;ll say a global entry point and</p>



<p>literally just taking the word entry point and putting a colon after it now makes that a label</p>



<p>so if there are any instructions underneath I&#8217;m gonna put a bunch of nopes then if someone somewhere</p>



<p>to say jump entry point they should be able to go right here to instruction 8 and then start</p>



<p>executing downward. I guess maybe I didn&#8217;t need to put the global keyword global just means let&#8217;s</p>



<p>make this label available to other modules within the same program so if you have a multi-source</p>



<p>program or a hybrid program with multiple different languages then you know you should do this but if</p>



<p>it&#8217;s just a pure assembly program and there&#8217;s only one source code filed you don&#8217;t need to mark a</p>



<p>Just as a quick example here, entry points, I&#8217;ll just put hello as a label and I&#8217;ll say like do exit stuff.</p>



<p>So imagine on line 16, you add some instructions just to kind of exit.</p>



<p>If I wanted to skip all these nope instructions for some reason, I could just do this.</p>



<p>I could say jump hello.</p>



<p>And what would happen is execution.</p>



<p>Oh, I can use my pen.</p>



<p>Execution would just sort of, you know, it would come into the text section.</p>



<p>you know, it&#8217;d come into the text section.</p>



<p>It would go down through the label and it would execute this first jump</p>



<p>instruction and then execution would jump over the nopes into the hello label.</p>



<p>And then, you know, if there was other stuff here, then it would get executed.</p>



<p>So by jumping over the nopes,</p>



<p>I&#8217;m essentially saying that the nopes should not actually end up being</p>



<p>executed. They&#8217;ll be there in the program, but they won&#8217;t actually execute.</p>



<p>So that&#8217;s the basics of a jump instruction. Okay.</p>



<p>So what else do I need to tell you real fast?</p>



<p>What else do I need to tell you real fast?</p>



<p>Oh, one thing that&#8217;s really good about jump instructions is they have unlimited jump range.</p>



<p>So you can jump from a place at the very, very beginning of your assembly program and</p>



<p>jump to a place that is at the very, very, very end of your assembly program.</p>



<p>There&#8217;s not going to be a limitation on how far you can jump.</p>



<p>I mean, in theory, there&#8217;s a limit, but practically speaking, there&#8217;s not a limit.</p>



<p>Why would you care that there&#8217;s not a limit?</p>



<p>not a limit well because in a future video that i&#8217;m going to release we&#8217;re going to talk about</p>



<p>conditional branching which is sort of a jump that only jumps if a certain condition is true</p>



<p>and those have limited ranges where they can jump so there&#8217;s going to be a bunch of different</p>



<p>instructions but one of the conditional branching instructions is jne and another one is jge and</p>



<p>there&#8217;s another one that&#8217;s je basically you know jump if something is equal jump if something is</p>



<p>can only jump about 128 bytes away.</p>



<p>So after your assembler assembles and compiles</p>



<p>down to object code,</p>



<p>and then after your linker links your final executable,</p>



<p>wherever it is that the instructions happen to end up</p>



<p>inside of your program,</p>



<p>the conditional jumps,</p>



<p>the conditional branching instructions,</p>



<p>they can&#8217;t jump more than 128 bytes away</p>



<p>to some other instruction.</p>



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



<p>Even if later on you graduate</p>



<p>to making decisions in your program,</p>



<p>like I&#8217;m going to do in the next video,</p>



<p>in your program like i&#8217;m going to do in the next video you can only jump so far and if you have to</p>



<p>jump too far you actually might not be able to jump at all unless you jump a very short jump</p>



<p>to a regular jump instruction and then that jump instruction jumps very very far away that&#8217;s kind</p>



<p>of the workaround for it i&#8217;m not going to talk about that in this video though this is not a</p>



<p>video for uh conditional branching i just wanted you to be aware of one of the benefits of regular</p>



<p>Okay, so we&#8217;re looking at the book here.</p>



<p>There&#8217;s not really a whole lot to the jump instruction, just jump and then a label.</p>



<p>We talked about its benefit over conditional branch instructions,</p>



<p>but we also talked about its, I guess, its shortcoming,</p>



<p>meaning it can&#8217;t actually make a decision.</p>



<p>It will always jump to a label no matter what.</p>



<p>There&#8217;s no condition.</p>



<p>So there&#8217;s the book there, and now I&#8217;m going to make a sample program</p>



<p>and show you how to run it.</p>



<p>I&#8217;m just going to run it.</p>



<p>I&#8217;m just gonna run it I&#8217;m show you what it does in order to implement conditional branches so for</p>



<p>starters I want you to know that there&#8217;s a make file that I&#8217;ve generated under the hood and we&#8217;re</p>



<p>not going to be talking about that in this video this is also a hybrid program so there&#8217;s a C++</p>



<p>entry point a driver module under the hood of this we&#8217;re not going to talk about that if you</p>



<p>want to know how to make hybrid programs you want to generate make files you want to learn the basics</p>



<p>videos for now we&#8217;re only going to be talking about jump instructions so I&#8217;m</p>



<p>going to skip a lot of information okay so for starters I&#8217;m going to make a</p>



<p>little data section here and again this is explained in other videos but for now</p>



<p>we&#8217;ll just trust that we can make a data section that contains strings C strings</p>



<p>and other values so pretty much I&#8217;m just going to make a string called begin jump</p>



<p>test just to announce to the user that we&#8217;re we&#8217;re going to start doing this</p>



<p>We&#8217;re going to start doing this and then I&#8217;m going to make a string called this message</p>



<p>should not appear.</p>



<p>So in the code, I&#8217;m going to try to print that message, but then I&#8217;m going to jump over</p>



<p>the call to print it just to prove to you that there are instructions that would print</p>



<p>that message, but we&#8217;re jumping over them with the jump instruction.</p>



<p>And then there&#8217;s like an exit message.</p>



<p>And then there&#8217;s a CRLF, which is just a carriage return line feed.</p>



<p>Again, all of this stuff is in other videos already.</p>



<p>So we&#8217;re going to use system call one to print.</p>



<p>We&#8217;re going to print a file descriptor one, which is just standard output for your program.</p>



<p>Then we&#8217;re going to start the text section where the actual code lives.</p>



<p>So this text section is here and it&#8217;s supposed to be at line 37 already.</p>



<p>I think I missed a bunch of lines.</p>



<p>Oh no, I think I missed some comments.</p>



<p>Anyway, so we have a text section here and an entry point and I&#8217;m calling it cool.</p>



<p>calling it cool and I am marking it as global because in this particular program that I&#8217;m</p>



<p>building it&#8217;s a hybrid program there&#8217;s going to be a C++ module that will call on our cool</p>



<p>function so cool has to be global and then I&#8217;m just going to call on a method called jump test</p>



<p>I don&#8217;t know I have the words load there I&#8217;m just going to get rid of that real fast locally and in</p>



<p>my solution up above and so we&#8217;re going to call a function called jump test and then when we&#8217;re</p>



<p>finished we&#8217;re going to return to the caller which is going to be the driver and that&#8217;ll</p>



<p>pretty much be it.</p>



<p>So if I comment this out real fast, let&#8217;s see,</p>



<p>this might actually work.</p>



<p>Let&#8217;s see if I can get it to run in the terminal.</p>



<p>But there&#8217;s a bunch more code that we have to add, so I&#8217;m not really sure.</p>



<p>So let&#8217;s do clear and make run.</p>



<p>And it seems to not have a shared object directory.</p>



<p>Let me pause the video while I copy paste one of my stupid libraries into the</p>



<p>program. You don&#8217;t need this library.</p>



<p>It just helps me print things.</p>



<p>okay so now I have copy pasted my shared object which allows me to do extra printing stuffs</p>



<p>just for just to make this demo easier for me but you don&#8217;t need to know it or you don&#8217;t need to have</p>



<p>it to to learn jump instructions anyway so I&#8217;m going to do that again and now it actually prints</p>



<p>something okay so hello from the main CPP driver and then it says the driver has regained control</p>



<p>make a call to jump test here and then let&#8217;s start the actual jump test function. So I&#8217;m going to do</p>



<p>well I guess this thing is kind of short I could copy paste the whole thing all at once.</p>



<p>So let&#8217;s do yeah let&#8217;s just jump let&#8217;s just call the whole thing. Okay I&#8217;m going to copy paste the</p>



<p>whole thing then I&#8217;ll explain it a little bit to you. So there is a function that I have in here</p>



<p>It&#8217;s just a convenience function that I made so I can print a carriage return line feed.</p>



<p>The real interesting thing here is the jump test function.</p>



<p>So we were just making a call to jump test.</p>



<p>Now we&#8217;re making the actual jump test function.</p>



<p>It&#8217;s got a signature of just void with no arguments.</p>



<p>So it&#8217;s not super interesting from the caller&#8217;s perspective, but it does some stuff.</p>



<p>So for starters, it has an intro message.</p>



<p>So this will print, you know, hello, welcome to the jump test.</p>



<p>jump test. In fact, if I do a return call here,</p>



<p>it should actually just print that and do nothing else. Right. Okay.</p>



<p>Notice how it printed, begin the jump test.</p>



<p>And then right after that,</p>



<p>there&#8217;s a jump instruction just proving to you that we can jump over other</p>



<p>instructions. So look at this,</p>



<p>this piece of code should never actually be called because we&#8217;re going to jump</p>



<p>over it. What it is, is it&#8217;s printing that jump shouldn&#8217;t happen message.</p>



<p>at the top here jumps shouldn&#8217;t happen so it&#8217;s trying to print out this message should not appear</p>



<p>but we&#8217;re going to jump over that by using this jump instruction here on line 66.</p>



<p>Again note that the jump instruction is just jmp followed by a label the label specified has to be</p>



<p>where you want to jump it&#8217;s never going to return from that place unless you specifically jump back</p>



<p>somehow later on like i guess if we wanted to we could put a label on line 67 call it the return</p>



<p>call it the return point and then jump back from it after the jump point in fact maybe that would</p>



<p>be kind of interesting to do at the end of this video but otherwise we&#8217;re gonna you know just</p>



<p>let&#8217;s see we&#8217;re gonna end up jumping over so let me reduce the front size just for a second here</p>



<p>so imagine execution uh comes into this program you know we&#8217;re executing uh instructions we&#8217;re</p>



<p>calling crlf we&#8217;re just executing executing as soon as we hit this jump instruction then execution</p>



<p>then execution jumps over into the label that I specified.</p>



<p>So this whole code section here just never even gets called.</p>



<p>So that&#8217;s why we will not see that message.</p>



<p>And then at the very end, all I&#8217;m doing is I&#8217;m just properly,</p>



<p>you know, I&#8217;m printing the exit message.</p>



<p>So I&#8217;m just printing another string saying the exit or the jump test is done.</p>



<p>I return to the caller execution goes all the way back up to just you know right here right after</p>



<p>call jump test was executed and then the cool function will return to the caller and that&#8217;s</p>



<p>just a c++ main function that does nothing so at this point we should see the whole entire point of</p>



<p>the program and then I&#8217;ll start tweaking it so you can kind of see the difference with the jump</p>



<p>instruction uh there and not there so let&#8217;s run one more time and notice how it says begin the</p>



<p>says begin the jump test and then end jump test and then it goes back to the driver that is</p>



<p>regain control it never says this message should not be printed so this whole section was just</p>



<p>skipped let&#8217;s comment out line 66 so that we don&#8217;t actually jump over that code and then now you&#8217;ll</p>



<p>see that that message does get printed so notice how it says this message should not appear okay</p>



<p>and then run the program one more time.</p>



<p>Now that message does not appear.</p>



<p>Pretty cool.</p>



<p>Now let&#8217;s do that double jumping thing just to show you.</p>



<p>I mean, this is not something that you actually want to do.</p>



<p>You probably want to write functions and function calls,</p>



<p>but if you wanted to, you could do something like this.</p>



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



<p>And maybe right after this, let&#8217;s make another label.</p>



<p>Let&#8217;s do, oh gosh, what am I going to do?</p>



<p>what am I going to do? Because if I jump after the exiting label and I jump back up to some label</p>



<p>up here, it&#8217;s just going to be an infinite loop. So maybe, um, I don&#8217;t know, let&#8217;s make a, I mean,</p>



<p>if I make another label down at the bottom, you&#8217;ll kind of think it&#8217;s a function just without</p>



<p>a return statement. So let&#8217;s actually jump within the same function. Let&#8217;s do, um,</p>



<p>over the never area.</p>



<p>So I&#8217;m going to say jump test and I&#8217;m going to write never.</p>



<p>So now we have a label that tells us where the never printed message actually starts.</p>



<p>So if we jump over it to the exiting, then we&#8217;re good.</p>



<p>But then if I up here, if I say jump instruction that subverts</p>



<p>never message so I&#8217;m just I&#8217;m just leaving a comment not code I could then</p>



<p>say let&#8217;s jump to jump test never and what will happen now is we&#8217;ll still see</p>



<p>the never message because what will happen is execution comes down you know</p>



<p>through here all these instructions are executing and then we see a jump that</p>



<p>tells us to go to the the never label so we actually jump over this exiting jump</p>



<p>over this exiting jump or this like the skipping jump,</p>



<p>the jump that skips the message.</p>



<p>And then we actually do print the never message</p>



<p>and we just keep going down and down and down</p>



<p>until we&#8217;re finished with that.</p>



<p>And we end up just sort of exiting normally.</p>



<p>So that means the only code that doesn&#8217;t get executed</p>



<p>in this case is the one right here</p>



<p>that skips over the never message.</p>



<p>Hopefully that makes sense.</p>



<p>Let&#8217;s run the program just to prove it real fast.</p>



<p>So I&#8217;m going to do this again.</p>



<p>And now you see the message should not appear.</p>



<p>This message should not appear.</p>



<p>You see that message.</p>



<p>So again, if we comment out that jump that subverts the skip, then execution will fall</p>



<p>through and we&#8217;ll end up executing line 69, the skipping instruction.</p>



<p>again now that message does not appear.</p>



<p>We could jump back and forth if we wanted to.</p>



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



<p>Should I do a back and forth?</p>



<p>I don&#8217;t really want to.</p>



<p>I think at this point you understand we can jump anywhere we want, right?</p>



<p>I could take a bunch of time in this video to rewrite this program and have it say,</p>



<p>let&#8217;s jump downward and then let&#8217;s jump upward again and let&#8217;s let it fall through</p>



<p>and then let&#8217;s jump over something and whatever.</p>



<p>let&#8217;s jump over something and whatever. I mean, just wherever you want to jump,</p>



<p>just make a label and then jump to it. Then you have to figure out what your execution path is</p>



<p>actually going to be. And maybe that&#8217;ll be complicated, but I hope I&#8217;ve made my point by</p>



<p>now. Anyway, so that&#8217;s the basics of just jumping around. It&#8217;s not super useful. Conditional</p>



<p>branching is a lot better. So see my next video. And I thank you for watching this and I hope you</p>



<p>learned a little bit and had a little fun. See you soon.</p>



<p>See you soon.</p>



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



<p>So please do me a kindness and subscribe. You know sometimes I&#8217;m sleeping in the</p>



<p>middle of the night and I just wake up because I know somebody subscribed or</p>



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



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



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



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



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



<p>which I think is also named somewhere at the bottom of this video and</p>



<p>It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p>I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Clarifications or errata or just future videos that you want to see</p>



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



<p>You know, just send me a comment, whatever. I also wake up for those in the middle of the night.</p>



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



<p>I would really appreciate it. So again, thank you so much for watching this video and, um,</p>



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/">x86-64 Assembly Jump Instructions Explained: Unconditional JMP with Full Example in Yasm</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-jump-instructions-explained-unconditional-jmp-with-full-example-in-yasm/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &#038; Rotates Tutorial</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 29 Dec 2025 08:55:41 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[AND OR XOR]]></category>
		<category><![CDATA[arithmetic shift]]></category>
		<category><![CDATA[assembly language tutorial]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[bit manipulation]]></category>
		<category><![CDATA[bit rotation]]></category>
		<category><![CDATA[bit shifting]]></category>
		<category><![CDATA[bitwise operations]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[gdb debugging]]></category>
		<category><![CDATA[logical shift]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[two's complement]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=272</guid>

					<description><![CDATA[<p>Learn bitwise operations in x86-64 assembly language including AND, OR, XOR, NOT, logical and arithmetic shifts, and bit rotations with practical examples and GDB debugging.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/">x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/N8Nciq6l5IU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>In this hands-on tutorial, we dive into bit manipulation in x86-64 assembly using Yasm on Ubuntu. We cover the basics of logical operations like AND, OR, XOR, and NOT, then move into shifting (logical vs arithmetic), rotating bits, and why signed vs unsigned matters for shifts.</p>



<p>I explain each concept on paper first, then show real code examples running in GDB to prove how the bits change. We look at patterns in registers, see what happens with negative numbers in two&#8217;s complement, and even touch on how shifts can multiply or divide by powers of 2.</p>



<p>Perfect if you&#8217;re learning low-level programming, reverse engineering, or just curious about how bits work under the hood. No prior assembly knowledge needed, but it&#8217;ll help if you&#8217;ve seen my other videos.</p>



<p>Like and subscribe for more assembly tutorials!</p>



<p>Thanks for watching!</p>



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



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



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p>Hey everybody.</p>



<p>In this video, we&#8217;re going to talk all about manipulating bits in x86-64 Yasm assembly within</p>



<p>Ubuntu.</p>



<p>But if you&#8217;re not using Yasm or you&#8217;re not on Ubuntu, this video is probably going to</p>



<p>be pretty useful to you anyway, because I&#8217;m going to talk about the basic concept of manipulating</p>



<p>bits in various ways.</p>



<p>We&#8217;re going to talk about some basic logical operators like ANDing, ORing, XORing, and</p>



<p>XORing, and then we&#8217;re going to talk about shifting the bits to the left and the right,</p>



<p>and then we&#8217;re going to talk about the difference between shifting without any respect for whether</p>



<p>the number inside of the bits is an actual integer, or whether we just consider them</p>



<p>to just be bits by themselves, or whether we&#8217;re actually respecting the bits as numbers,</p>



<p>and also we&#8217;re going to look at the rotation instruction.</p>



<p>So first I&#8217;m going to explain this, I&#8217;m going to write it down in a pad, and then I&#8217;m going</p>



<p>show you a program that actually does it just so we can prove that it all works.</p>



<p>Okay, so for starters, let me open up a little notepad here.</p>



<p>What the heck am I even talking about? Let me write this down because I forgot this on my</p>



<p>first take I spent like five minutes, blew right past the explanation of and and or and xor because</p>



<p>I forgot that that was supposed to be part of this video. We&#8217;re going to look at and or and xor</p>



<p>at least for now and then we&#8217;re going to look at shifting and then we&#8217;re going to look at rotating.</p>



<p>at rotating. Okay so first off what is anding? I&#8217;ve explained that in another video but long</p>



<p>story short and just requires that two bits that you&#8217;re comparing against each other are both a one</p>



<p>in order to receive a one and so any other pair like a zero versus a one or one versus a zero or</p>



<p>two zeros are just going to be a zero. So and means both bits that you&#8217;re comparing have to be a one</p>



<p>The OR is pretty easy.</p>



<p>If either of the bits is a 1, then the result is going to be 1.</p>



<p>So if I say 1 versus 1, the result is going to be 1.</p>



<p>If I say 0 versus 1, the result is going to be a 1.</p>



<p>Or if I say 1 versus 0, the result is going to be a 1.</p>



<p>In the only case where you end up with a 0, it&#8217;s just going to be 0 versus 0 results in a 0.</p>



<p>To upgrade to exclusive OR, basically exactly 1 bit needs to be a 1.</p>



<p>bit needs to be a one and if two or zero are a one then the result is a zero which means here when</p>



<p>we have the two ones at the top the answer is going to be a zero these are the two stay the</p>



<p>same and this one stays the same so that&#8217;s xor again i have another video where i explain this</p>



<p>a little bit better but i&#8217;m just doing like a quick overview shifting is pretty interesting</p>



<p>because let me do a pattern let&#8217;s say zero one one zero suppose we just have four bits</p>



<p>to the left or to the right. So imagine left, if I shift them to the left, then what I&#8217;m really</p>



<p>doing is I&#8217;m just taking off that zero on the end and padding on the right with a zero. And then if</p>



<p>I&#8217;m shifting to the right, then well, it&#8217;s to the right. I&#8217;m just going to, you know, take off,</p>



<p>let&#8217;s see the zero on the right side and pad with the zero on the left. Be careful though, because</p>



<p>or logical shifting. I haven&#8217;t actually shifted with respect to the original number. Think</p>



<p>about this. That number right there on line three, if we just consider that an unsigned</p>



<p>integer or just bits, then what I just did with the shifting is fine. But what if that</p>



<p>was a two&#8217;s complement number that can represent a signed integer? If it&#8217;s two&#8217;s complement</p>



<p>or if it&#8217;s just like a signed integer, then the leftmost bit being a zero tells me that</p>



<p>result of my left shift, there&#8217;s a one now in that first position, which tells me that&#8217;s</p>



<p>probably a negative number.</p>



<p>We could face a similar problem with shifting to the right.</p>



<p>I mean, we didn&#8217;t this time.</p>



<p>It just looks like it actually shifted and the answer is going to end up being right.</p>



<p>But what if we shifted something else right?</p>



<p>Let&#8217;s see, what am I going to do here?</p>



<p>One, one, zero, zero.</p>



<p>What if I shifted that number one to the right?</p>



<p>The answer would be zero, you know, one, one, zero, right?</p>



<p>you know 110 right but then that&#8217;s wrong because the original number was negative because there&#8217;s</p>



<p>a one in the first position but the result is a zero so if i&#8217;m not careful about what instructions</p>



<p>i&#8217;m using and how to respect whether the number i&#8217;m shifting is actually considered an integer</p>



<p>assigned integer or if it&#8217;s just bits if i don&#8217;t respect that then i could end up ruining the</p>



<p>number so keep that in mind we&#8217;re going to do several examples of that i&#8217;m not going to show</p>



<p>shift by hand I&#8217;m just going to show you what the instructions in the assembler</p>



<p>will do so then the other thing that we should talk about is rotating so imagine</p>



<p>that I rotate this number one to the right well nothing really happens</p>



<p>visually but what you&#8217;re supposed to do if we rotate to the right you take the</p>



<p>number on the very right side and you just pop it onto the front so it sort of</p>



<p>it doesn&#8217;t fall off and get replaced by a zero or get replaced by a one depending</p>



<p>moves to the other side so if I shift this again maybe let&#8217;s shift it one more</p>



<p>time to the right I&#8217;m gonna take that one and pop it off the right side and</p>



<p>put it on the very front so see do you see how there it&#8217;s kind of rotating I do</p>



<p>this one more time actually let me copy paste this I&#8217;ll take that number on the</p>



<p>right side pop it off put it back on the front then if I do it again take that</p>



<p>zero pop it off put it back on the front notice how it eventually gets back to</p>



<p>the original pattern and I would have to rotate you know exactly four times to</p>



<p>to get that to happen same same thing applies for rotating to the left i&#8217;m not going to show you</p>



<p>because uh it&#8217;s it&#8217;s the same thing as rotating to the right just in a different direction</p>



<p>one thing that i forgot to say a moment ago when i was talking about shifting is uh think about this</p>



<p>if we shift to the right pretend that uh we don&#8217;t really care that much let&#8217;s let me put some extra</p>



<p>zeros here so that we don&#8217;t have to carry too much we don&#8217;t have to care too much about what&#8217;s</p>



<p>happening with positive or negative. If you know how to convert to binary, which I&#8217;ve explained in</p>



<p>other videos, then you&#8217;ll know this number is, let&#8217;s see, there&#8217;s a one and a two, and then these</p>



<p>positions are four and eight. So this should be a 12, right? If I shift this to the right,</p>



<p>I should be dividing this by two because the base of binary is two. So every time you kind of like</p>



<p>move to the left in terms of what digit you&#8217;re looking at, you&#8217;re increasing the power of that</p>



<p>every time you move to the right you&#8217;re decreasing the power of the digit by two</p>



<p>so that means if I shift the entire thing to the right I should be dividing the whole thing by two</p>



<p>right not just one bit so if I just take a zero off of there and put it on there I&#8217;ve now shifted</p>



<p>all the bits shifted all the bits one to the right I should end up with the six let&#8217;s just</p>



<p>compute what it is there&#8217;s a one there and there&#8217;s a two there and there&#8217;s a four there what&#8217;s four</p>



<p>plus two that&#8217;s six so this is actually six in binary or the original number divided by two</p>



<p>because we shifted to the right on the other hand if I take this number one more time and</p>



<p>just kind of shift it to the left I should multiply it by two so that means I should</p>



<p>expect to see a 24 let me just remove one of those zeros and put it on the right side there</p>



<p>this is not a rotation I&#8217;m just kind of like filling in zeros I&#8217;m keeping it simple with</p>



<p>the shifted number this is a 1, a 2, and a 4 again if you don&#8217;t know how to convert</p>



<p>from binary to decimal or back see my other videos so then we basically have</p>



<p>an 8 here and a 16 what&#8217;s 8 plus 16 that&#8217;s 24 24 is twice of 12 so we have</p>



<p>multiplied by 2 so you could multiply by 4 by shifting twice multiply by 8 by</p>



<p>shifting 3 times you know whatever you want to do but shifting is is like a</p>



<p>is like a shortcut besides just to move the bits it&#8217;s also a shortcut to multiply and divide by two</p>



<p>okay so that&#8217;s the basic idea let me start working on a little program here for you before I continue</p>



<p>I just want you to know that my program it&#8217;s got stuff in it already we&#8217;ve got a make file here</p>



<p>if I uh I edit the make file I just have a make file set up to build the program so again this</p>



<p>is specific to just you know bits we&#8217;re not going to talk about make files see my other videos if</p>



<p>you want to know how to make your own make file same thing for this gdb.txt file I&#8217;ve just put</p>



<p>commands that I&#8217;m going to send to gdb my debugger of choice this is not a gdb video I haven&#8217;t</p>



<p>actually made my gdb video yet so look out for that in this in the somewhat near future</p>



<p>so we&#8217;re just going to focus on the actual program for now anyway so I&#8217;ve got a solution</p>



<p>Anyway, so I&#8217;ve got a solution already set up here.</p>



<p>Let me start copy pasting some stuff in there.</p>



<p>Going to copy paste my data section.</p>



<p>So first off,</p>



<p>my sample program is just gonna have codes for writing,</p>



<p>you know, with the system call</p>



<p>and exiting with the system call.</p>



<p>And then I&#8217;m gonna output to standard output</p>



<p>and I&#8217;m gonna do a zero return code.</p>



<p>If you don&#8217;t know that,</p>



<p>see my other videos that I&#8217;ve already put out.</p>



<p>I&#8217;m gonna make a sample string where I just say,</p>



<p>I just say hello my name is Gibson Montgomery Gibson that&#8217;s not my name but I love those names</p>



<p>I&#8217;m just going to say hello basically when the program starts then I have some bit strings here</p>



<p>that will help me with the you know help me demonstrate some of the bitwise operations</p>



<p>the ending and the oring and the shifting and whatever and then I&#8217;m going to put a negative</p>



<p>number here as a variable just to prove to you that sometimes the bitwise or logical shifting</p>



<p>break a number if the number is supposed to actually be assigned integer and and use that</p>



<p>as evidence for why you should use arithmetic shifting so now I&#8217;m going to start my program</p>



<p>here let&#8217;s see my entry point should start at line 30 I got to be super careful about this</p>



<p>because if I don&#8217;t know 26 if I don&#8217;t then all of my debug statements are going to have to be</p>



<p>it lined up correctly so i&#8217;m basically just going to print the hello message and then maybe you know</p>



<p>what at the very end i&#8217;m going to exit from the system with a system call so basically if we run</p>



<p>this program again system calls and everything like that are not part of this video see my other</p>



<p>videos but uh if this works we should see just like a simple message okay so that&#8217;s it my name</p>



<p>Conmary Gibson. Okay.</p>



<p>So then the next thing that I&#8217;m going to do is I&#8217;m going to show you a bitwise</p>



<p>and operation. So I&#8217;m going to copy paste that should start on line 42.</p>



<p>And if I get this wrong, the debugging is going to be awful.</p>



<p>I was trying to make this a pure assembly program without a bunch of extra work.</p>



<p>I guess I could have linked GCC so I could just print results to the terminal,</p>



<p>but whatever too late now. So it starts on 42.</p>



<p>and ends on 51 okay so I think that&#8217;s good so the instruction for ending doing</p>



<p>a logical and between two registers which are full of bits and then what</p>



<p>will happen is every bit gets anded against its own course or I guess the</p>



<p>corresponding bit in the other other register is just and so what I&#8217;m doing</p>



<p>first is I&#8217;m gonna set up the registers so I&#8217;m gonna move the a sequence which</p>



<p>sequence, which if you look up is just, you know, I put a bunch of bits. So like in hex,</p>



<p>I like to type hex more than binary when I&#8217;m just kind of putting stuff in there.</p>



<p>So, you know, each F is basically four ones. So it&#8217;s going to be like, I guess, 16 ones,</p>



<p>and then a bunch of zeros, and then, you know, a few ones, and then some zeros and ones.</p>



<p>So I&#8217;m just making a little pattern for bits A. Don&#8217;t think that they mean anything right now.</p>



<p>And I&#8217;m going to make a different pattern with bits B. And when we look into the debugger,</p>



<p>And when we look into the debugger, we should see the before and after patterns so we can kind of see what the AND instruction did.</p>



<p>So I&#8217;m going to move bits A and bits B into R10 and R11.</p>



<p>Then I&#8217;m going to move, so these are going to be like the original bits that we can inspect later.</p>



<p>this AND instruction against R11 which is the B pattern. So I&#8217;m basically taking the A pattern</p>



<p>and I&#8217;m ANDing it against the B pattern but I&#8217;m also loading up the registers so we can see</p>



<p>the A pattern then the B pattern and then the result of ANDing them together.</p>



<p>Finally on line 50 here I&#8217;m using a NOPE which is just a non-operation so that I can easily</p>



<p>break the debugger so we can sort of inspect the program and see what&#8217;s going on inside of there.</p>



<p>program and see what&#8217;s going on inside of there. You know what, let me, uh, let me open this up</p>



<p>again here so that I can control where I&#8217;m breaking. So 48 is where the nope is. That&#8217;s</p>



<p>totally wrong. I&#8217;ve already ruined my program somehow. It&#8217;s going to be 50, I guess. All right,</p>



<ol start="50" class="wp-block-list">
<li>And what I&#8217;m doing inside of this, and again, this is not a GDB video. I&#8217;m just saying,</li>
</ol>



<p>let&#8217;s break at line 50 so I can kind of poke around. And I&#8217;m going to comment all these</p>



<p>And I&#8217;m going to comment all these things out for now.</p>



<p>That&#8217;s going to happen at the very end.</p>



<p>It&#8217;s going to be majestic.</p>



<p>How many different things we can look at.</p>



<p>So I&#8217;m telling GDB not to execute any of these instructions, except for the, uh,</p>



<p>the break point instructions.</p>



<p>So we can just break on line 50.</p>



<p>Okay.</p>



<p>Hopefully this works.</p>



<p>I&#8217;m going to now use make debug, which under the hood is just going to be compiling and</p>



<p>then telling GDB to launch with the name of my program inside of it.</p>



<p>And then to execute those things.</p>



<p>and then to execute those things.</p>



<p>So it hits breakpoint number 50.</p>



<p>There&#8217;s a bunch of crap that gets printed.</p>



<p>And I can now inspect my registers.</p>



<p>I&#8217;m going to go info R for just info registers.</p>



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



<p>So we had R10, 11, and 12.</p>



<p>So if we look at the terminal, you can see that R10, the hex version,</p>



<p>has the original pattern of the A sequence,</p>



<p>and then R11 has the original pattern of the B sequence.</p>



<p>original pattern of the b sequence if we do a logical and and put the result into r12 then</p>



<p>you should see let me just double check that that&#8217;s definitely r12 yeah then you should see</p>



<p>the result of ending all of the f&#8217;s together remember that an f is just four ones so i&#8217;m kind</p>



<p>of simplifying it a little bit you could make yours more complicated with lots of randomness</p>



<p>in your ones and zeros just to prove it to yourself but notice how r12 only has an f</p>



<p>So like FF means F, F0 means 0, F0 means 0, you know, and so forth.</p>



<p>0F means 0.</p>



<p>So only where you see two Fs will you see an F in the final target pattern.</p>



<p>So that&#8217;s a logical AND, and that&#8217;s how you do it in Yasm Assembly.</p>



<p>You just load up a register with some bits, R12.</p>



<p>We loaded them up with the first pattern.</p>



<p>And then we take the second pattern and say that&#8217;s what I want to AND it against.</p>



<p>that&#8217;s what I want to end it against. And you can have that in a register or I think an immediate</p>



<p>or a memory location. So we&#8217;re just ending these registers together. And you know that they&#8217;re</p>



<p>loaded up with patterns A and B. So nothing too hard at this point. Let&#8217;s do a, let&#8217;s look at</p>



<p>bitwise or this should start on line 52 or I&#8217;m screwing myself. 52. Okay. 52. So same thing,</p>



<p>up R10 and R11 with the bit patterns and then we&#8217;re going to move the first bit pattern</p>



<p>into R12 so that we can or it against the second pattern and so basically R10 and 11</p>



<p>will have the original bit patterns R12 will have the result of oring them.</p>



<p>Then we&#8217;ll do a nope on line 60 so I want to break here on line 60.</p>



<p>Yep I ruined all my line numbers. What did I do wrong? I think I just have it offset by 2 now</p>



<p>two now for some reason so if we just kind of run the program now in the debugger again i&#8217;m going to</p>



<p>quit and then yes if we clear and make debug probably clear was unnecessary we&#8217;re broken at</p>



<p>line 60 if i say info registers the registers in question are oh shoot um 10 11 and 12 right so</p>



<p>we look at 10 and 11 they have the same pattern as before and 12 has a lot more f&#8217;s because if</p>



<p>has a lot more F&#8217;s because if either of the patterns had an F,</p>



<p>then the resulting pattern would have an F.</p>



<p>So this is a logical OR.</p>



<p>Same idea as AND in terms of the way you format the instruction and the OPS,</p>



<p>but it ORs instead of ANDs.</p>



<p>Okay, no problem.</p>



<p>Let&#8217;s move on.</p>



<p>Next thing I want to show you is the XOR, which is pretty cool.</p>



<p>By the way, so far we&#8217;re just operating on bits.</p>



<p>We don&#8217;t consider these bits to actually be numbers.</p>



<p>maybe they are but we should probably be careful about that anyway so I&#8217;m going to say let&#8217;s check</p>



<p>out our bitwise xor which is exclusive or which means exactly one bit in any pair needs to be a</p>



<p>one for the resulting bit to be a one and of course per usual all bits will be checked against each</p>



<p>other in the pair of of bit patterns it&#8217;ll be one bit at a time from each pattern that gets checked</p>



<p>Okay, so it starts on 62 and then we got a nope there.</p>



<p>Okay.</p>



<p>So same thing that we did before.</p>



<p>We&#8217;re just loading up two registers with the bit patterns and then R12 is going to be the</p>



<p>result of exclusive oring or X oring the two bit patterns.</p>



<p>So that means we&#8217;re going to break on line 70.</p>



<p>Now, let me just get rid of my other break point, change that 68 to a 70.</p>



<p>And now I should quit.</p>



<p>bug we&#8217;re at 70 good info registers so I think we said it was 10 and 11 had the original bit patterns</p>



<p>and notice the resulting bit pattern it only has an f in a position where there was exactly</p>



<p>one f between the two input patterns so</p>



<p>wait did I just do something wrong here what did I just do wrong</p>



<p>hang on a second I&#8217;m seeing something bad x or r12 am I looking at r12</p>



<p>Yeah.</p>



<p>R10 and 11.</p>



<p>Did I just do something wrong?</p>



<p>10 and 11 and then we load 10 up into 12 and then we XOR it by 11 and then it stores it</p>



<p>in 12.</p>



<p>Something&#8217;s a little fishy here.</p>



<p>Exactly one.</p>



<p>XOR.</p>



<p>I don&#8217;t like that.</p>



<p>Oh, I understand now.</p>



<p>Okay.</p>



<p>So that was a little scary.</p>



<p>This has shifted because the printout is not great.</p>



<p>Because the printout is not great. See how there&#8217;s like a blank space here?</p>



<p>That threw me off. So you have to imagine that all of the bit patterns are kind of shifted.</p>



<p>Maybe I should just copy paste this.</p>



<p>I was worried that I suddenly didn&#8217;t know how to do an XOR in assembly.</p>



<p>So let me get rid of these integers here.</p>



<p>Notice how there&#8217;s just a blank space that shifted everything and threw it off in terms of the display.</p>



<p>So I&#8217;m going to do this.</p>



<p>And what should really happen now is that there should be a zero here.</p>



<p>A zero at the front.</p>



<p>a zero at the front now everything would make more sense sometimes the display likes to just</p>



<p>kind of truncate things and just assume that everything will be read correctly but it wasn&#8217;t</p>



<p>so notice uh in the first position both of the uh both the input patterns are are an f so the</p>



<p>output pattern will be a zero you can imagine under the hood there was a bunch of ones and</p>



<p>then a bunch of ones and that resulted in a bunch of zeros uh here we&#8217;ve got like an f and a zero so</p>



<p>the result is going to be an f and then f and a zero and after zero zero and an f is an f</p>



<p>f um and then where you see zeros that means uh there was not exactly one uh one in each bit</p>



<p>position so for the let&#8217;s say the first one that&#8217;s a bunch of zeros for both input patterns so that&#8217;s</p>



<p>why there&#8217;s a zero there then for the next two each of them had all ones or just the two f&#8217;s</p>



<p>uh so that&#8217;s why x or says it&#8217;s a zero and so forth right so like at the end two f&#8217;s making</p>



<p>Okay, that was a little scary.</p>



<p>I&#8217;m glad I figured that out quickly instead of taking a million years.</p>



<p>Okay.</p>



<p>So now let&#8217;s do another example.</p>



<p>We&#8217;re done with XOR.</p>



<p>Now let&#8217;s look at bitwise knot.</p>



<p>I forgot to explain knot.</p>



<p>I&#8217;m not going to redo this video.</p>



<p>But let me just say that a knot is just inverting the bit.</p>



<p>So if we have a zero, it becomes a one.</p>



<p>And if we have a one, it becomes a zero.</p>



<p>If you knot against a register that has lots of bits or a memory location that has lots</p>



<p>of bits, you can do that and just every single bit in the sequence will just be flipped from</p>



<p>a zero to a one or vice versa.</p>



<p>So let&#8217;s see, we just did bitwise XOR.</p>



<p>Now we&#8217;re going to do a knot starting on line 72, I guess.</p>



<p>And then we&#8217;re going to do another nope there.</p>



<p>Okay.</p>



<p>All right.</p>



<p>So now let&#8217;s get rid of the break point at the line 70 and put it at line 78 instead.</p>



<p>78.</p>



<p>Oh man, this video is going to take forever.</p>



<p>Okay.</p>



<p>So now let&#8217;s check out bitwise knot.</p>



<p>We&#8217;re going to have R10.</p>



<p>Oh, I guess I should improve this because what I&#8217;m doing is I&#8217;m moving everything into</p>



<p>everything into R10 and then I&#8217;m just nodding R10 so we can&#8217;t really compare R10 against itself.</p>



<p>That&#8217;s not fun. Let me instead move R10 into R11 so that I can nod R11 so that we can</p>



<p>see two different registers at least. Okay, so let me just update my solution real fast.</p>



<p>move r11 r10 okay so now it stops at 75 and that one stops at 75 okay cool</p>



<p>now we got our breakpoint should be at line 79 not 78 now we&#8217;re ready to run it and see what the</p>



<p>does clear and make debug info registers so we should have r10 r11 again the formatting is a</p>



<p>so I&#8217;m just going to copy paste it up here. I&#8217;m going to do this, get rid of the numbers,</p>



<p>pad on the left for the missing zeros, and then now you can see clearly that all of the</p>



<p>F&#8217;s were flipped to zero. Again, that means, you know, each F had four ones inside of it,</p>



<p>so all those ones were flipped to a zero, so that&#8217;s why it&#8217;s a zero. Okay, so nodding, pretty easy,</p>



<p>this and show the next example which was gonna be let&#8217;s see seven let&#8217;s check out bitwise rotation</p>



<p>i&#8217;m going to copy paste all the way up to that nope there should start on line 81</p>



<p>i paused before i took a drink of water hopefully i reminded myself later to edit the drink of water</p>



<p>out i don&#8217;t know maybe you&#8217;ll be wondering why my mustache is wet right now if i did not edit it out</p>



<p>I did not edit it out.</p>



<p>Okay, so let&#8217;s check out bitwise rotation.</p>



<p>So I&#8217;m gonna set up R10 and 11 and 12 and 13 and 14,</p>



<p>all with the A pattern.</p>



<p>And then I&#8217;m gonna start rotating things.</p>



<p>So first I&#8217;m gonna rotate to the left R11 and then R12.</p>



<p>I&#8217;m gonna rotate by one position on R11</p>



<p>and then two positions on R12.</p>



<p>And then I&#8217;m going to rotate</p>



<p>rotate by one position for R13 and then two positions for R14 so we should see</p>



<p>some interesting patterns. The NOPE is now on line 94 so I&#8217;m gonna break there</p>



<p>instead whoops so I&#8217;m gonna change that and I&#8217;m now off by three it&#8217;s getting</p>



<p>precarious and then I&#8217;m gonna uncomment all of my little print statements to</p>



<p>show you the value of the registers after I rotate it so we should see like</p>



<p>printed in the terminal just to prove to you what&#8217;s going to happen okay so man I got to</p>



<p>remember what&#8217;s happening now first thing that I did was I printed r10 to show you one type of</p>



<p>rotation and then I printed r10 again to show you another type of rotation so we should see the</p>



<p>original and then the two rotated values and then the original again and then the other two rotated</p>



<p>values so here&#8217;s the original we&#8217;re printing it in binary with the special print command in gdb</p>



<p>in GDB, which is just like P slash T and then the register. So this is the original pattern</p>



<p>right here. And if you just kind of squint at it for a while, you can realize that the dollar</p>



<p>sign two pattern has been, uh, shifted once to the left. Actually, let me just make sure that</p>



<p>I&#8217;m doing a shift and not a rotate. I&#8217;m accidentally saying the word, uh, rotate, I think. And, uh,</p>



<p>that&#8217;s, oh no, no, it is rotate. Yeah. We&#8217;re going to shift later, I guess.</p>



<p>I guess. Okay, so we are definitely rotating. So we should see some wrap around. I guess I used</p>



<p>bad bit patterns for the wrap around. But just imagine that, you know, in this pattern right here,</p>



<p>this one, if we&#8217;re rotating to the left, this one fell off and it got placed on the right side of</p>



<p>of the next pattern it&#8217;s going to be the same thing basically so but this is rotation so we</p>



<p>wouldn&#8217;t be reusing the bits that fell off but anyway you can see that if I rotated to the left</p>



<p>by one position that whole thing is is kind of shifted to the left I shouldn&#8217;t use the word shift</p>



<p>and then for the second pattern we did a rotation by two positions so all the bits are just kind of</p>



<p>moved to the left compared to the very first pattern okay so no problem then the next thing</p>



<p>that we&#8217;re looking at is just you know rotating to the right and you can tell that everything is</p>



<p>offset by one here and it&#8217;s offset by two here so rotating to the left by one position by two</p>



<p>positions rotating to the right by one position or by two positions that&#8217;s rotation in a nutshell</p>



<p>okay so then the next thing that we&#8217;re going to do we&#8217;re on 94 now</p>



<p>um let&#8217;s see logical shifting nope nope nope I&#8217;m gonna grab some code that will shift both</p>



<p>logically and arithmetically so we can kind of see the difference</p>



<p>okay so show maybe I should change this to show some some shifting</p>



<p>because uh if I just say it&#8217;s logical then yeah sure uh the shift function uh the shift</p>



<p>those are logical shifts but then down here when it&#8217;s a sal and sar those are arithmetic shifts</p>



<p>I don&#8217;t know maybe maybe I can do that maybe I can just</p>



<p>duplicate the comment maybe to make it more more intuitive so show arithmetic shifts</p>



<p>Okay, I think that looks pretty good. Let me paste that into 105 on my solution so I don&#8217;t get out of sync here.</p>



<p>So what&#8217;s going to happen here? We&#8217;re basically going to load up R10 with a negative number.</p>



<p>I&#8217;ve chosen the number negative 12 because I kind of feel like it at least shows you a little bit for dividing and stuff like that.</p>



<p>So we&#8217;re loading up R10 with a negative number, and then R11 is going to get R10, but then we&#8217;re going to shift it left by two positions.</p>



<p>positions so that should be multiplying it by four but this is well a bitwise</p>



<p>shifting so it it&#8217;s not going to necessarily respect the fact that the</p>



<p>number is in twos complement so this is not a good idea if you consider r tend</p>



<p>to be a number because sometimes the result might be right and sometimes the</p>



<p>result might be wrong here it says math won&#8217;t work in my comment but sometimes</p>



<p>it will accidentally work but you still want to consider it to be non-working</p>



<p>It&#8217;s only good for bit shifting.</p>



<p>Then we&#8217;re going to put R12, we&#8217;re going to give it the same value as R10 and then we&#8217;re</p>



<p>just going to shift it to the right by two positions.</p>



<p>This will definitely break because when you shift to the right, the sine bit is going</p>



<p>to get padded with a zero rather than what the actual sine bit was originally.</p>



<p>So that will almost certainly break it almost every time.</p>



<p>Then we&#8217;re going to use arithmetic shifts which just respect the fact that the original</p>



<p>number is two&#8217;s complement.</p>



<p>two&#8217;s complement so you know like we have this negative 12 number it&#8217;s going to end up being</p>



<p>bits but the uh the shl and the shr instructions are not going to respect the fact that it&#8217;s</p>



<p>negative 12 they&#8217;re just going to consider them to be bits but the sal shifting to the left</p>



<p>arithmetically and the sar shifting to the right arithmetically are going to respect that respect</p>



<p>the fact that it&#8217;s two&#8217;s complement so let&#8217;s see we&#8217;ve got all this stuff and then we&#8217;ve got a</p>



<p>We&#8217;ve got all this stuff and then we&#8217;ve got a non-operation here.</p>



<p>You know, again, like per usual, when we&#8217;re shifting, when we&#8217;re rotating, when we&#8217;re</p>



<p>oaring, anding, whatever, it&#8217;s just the instruction name followed by both the source and the</p>



<p>destination operand.</p>



<p>And then the second operand is only going to be source.</p>



<p>So if we have a shifting left R11, then the result is going to be stored in R11, but it&#8217;s</p>



<p>going to take as the source R11 and shift it to the left by two positions.</p>



<p>to the left by two positions and so forth.</p>



<p>Okay.</p>



<p>So we have all of that.</p>



<p>We&#8217;re going to nope on line one of what is it?</p>



<p>One, one 14.</p>



<p>So that means I&#8217;m going to stop breaking on 94 and I&#8217;m going to break on one 14.</p>



<p>I thought it was four lines off and now I&#8217;m only two lines off.</p>



<p>I think I just did something else wrong here between my source.</p>



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



<p>Is the nope on one 14 up there?</p>



<p>Yeah, it is.</p>



<p>Okay.</p>



<p>Guess I didn&#8217;t do that much wrong.</p>



<p>Okay.</p>



<p>Okay, so let&#8217;s run this, but this code up here was just going to print bit patterns</p>



<p>related to our rotations.</p>



<p>I don&#8217;t really want that anymore.</p>



<p>So I&#8217;m going to uncomment all of these other prints that relate to our shifts.</p>



<p>And this is going to be a nightmare to read and interpret, but I&#8217;m going to try.</p>



<p>I really should have written a more complicated program that can just print them in the terminal</p>



<p>so I don&#8217;t have to like read the debugger.</p>



<p>Whatever, it&#8217;s brain exercise.</p>



<p>size. And I think this actually is the end of the program. 119 is the last line. Yeah. Okay.</p>



<p>So we just need to run this and see what happens. So really we&#8217;re interested in these, uh, you know,</p>



<p>shifting to the left twice, shifting to the right twice bit wise, and then shifting to the left</p>



<p>twice, arithmetically shifting to the right twice, arithmetically. Okay. So we&#8217;ll go back to the</p>



<p>terminal here and quit. And then I&#8217;m going to do make debug. And now there&#8217;s a bunch of junk.</p>



<p>here&#8217;s the original number and then we&#8217;re gonna bitwise shift left binary</p>



<p>notice how it just sort of you know it shifted everything to the left and you</p>



<p>know we&#8217;re not using we&#8217;re not printing hex anymore because I want you to see</p>



<p>the binary and then you know like the patterns are just shifted to to the left</p>



<p>like see here&#8217;s the first one if we&#8217;re looking at the right side and then here</p>



<p>is the first one if we&#8217;re looking at the right side for the shifted version</p>



<p>A similar deal for shift right except we lost a couple of numbers there because of the display</p>



<p>so I&#8217;m going to copy paste both of these real fast.</p>



<p>Per usual we lost numbers due to the display so I&#8217;m going to say notice how we have two</p>



<p>numbers missing here if I added zeros on the right side that&#8217;s going to change the actual</p>



<p>value so I&#8217;m going to have to add them to the left and now we can clearly see that the</p>



<p>shifted four positions I think to the left wait did I do that where did I where did I even just</p>



<p>copy paste from original shift left and shift right okay so shift right is what it&#8217;s supposed</p>



<p>to be doing let me just double check here shift right is printing r12 and what does r12 have it</p>



<p>shifting right by two. Okay.</p>



<p>Oh, I&#8217;m supposed to compare it to the very first one.</p>



<p>Let me just double check that I have this correct now.</p>



<p>Why is it</p>



<p>shifting to the right? Oh gosh, I think I just lost track of myself. Let me just,</p>



<p>give me a second here. Or 14.</p>



<p>I&#8217;m forgetting where I copy and paste. Okay. Bitwise shift right is</p>



<p>bitwise shift right is R12, not R14. Oh gosh. Did I do it wrong? Were you cringing at home?</p>



<p>Maybe this will work. And the two, that&#8217;s kind of the same thing that I did before. No, no,</p>



<p>it&#8217;s different. Okay. So I had it wrong before. Now I finally have it right. So you can see that</p>



<p>So you can see that the bit pattern is shifted twice to the right and then on the left two zeros show up which would have destroyed a number.</p>



<p>Okay.</p>



<p>So we shift it to the right and now we&#8217;re ready to look at arithmetic shift to the left and right.</p>



<p>And let&#8217;s see do we have enough bits to actually print everything?</p>



<p>This one should look like it&#8217;s shifted to the right just because of the way it prints.</p>



<p>So that means this one is totally lined up.</p>



<p>Okay so let me grab the original pattern here.</p>



<p>grab the original pattern here so this is the original pattern and then if I go shift left</p>



<p>in binary I&#8217;ll just put an L here and then I&#8217;ll put shift right in binary</p>



<p>there we have enough bits I think in both cases so we don&#8217;t have to pad</p>



<p>so now if we look at the original pattern and then we shift left arithmetically twice</p>



<p>twice notice how it does kind of look like it survived right like this one moved over to the</p>



<p>left twice because the sine bits are just kind of falling off but there&#8217;s enough of them that it&#8217;s</p>



<p>not going to change the sine bit thank you two&#8217;s complement but when we shift to the right</p>



<p>arithmetically normally we would have padded on the left with zeros remember we had zeros there</p>



<p>on the left before but now that we&#8217;re respecting the arithmetic uh the the padding is correct now</p>



<p>is correct now for two&#8217;s complement so that&#8217;s why there are ones there instead of zeros</p>



<p>and then if you kind of look at it it&#8217;s like well we shifted twice to the right let me get</p>



<p>rid of the left because we already understand that now it seems to be that the pattern is uh</p>



<p>every bit is shifted over to the right the correct number of times the zeros dropped off the side</p>



<p>and um the sine bits were padded on the left so everything seems to be correct and then just to</p>



<p>to kind of prove to you that what we&#8217;re seeing is real, I just have the same things printed</p>



<p>all over again from scratch in decimal. So originally we had our R10 as negative 12.</p>



<p>And then if we do a bitwise shift to the left, it multiplies it by four. And so we&#8217;re accidentally</p>



<p>correct here. Remember, this is a bitwise shift, so it doesn&#8217;t respect two&#8217;s complement. So just,</p>



<p>we happen to have a starting number that would end up looking correct after we shifted it to the left.</p>



<p>And then when we do a bitwise shift to the right, again, this could be disaster because we&#8217;re not</p>



<p>respecting two&#8217;s complement or the sine bit. And that&#8217;s exactly what happened. You know, definitely</p>



<p>if we&#8217;re shifting to the right, that should be 12 divided by four equals three or negative three.</p>



<p>But instead we get this gigantic huge number because the sine bit got killed with the zeros</p>



<p>killed with the zeros that were padded on the left. And then we finally try to shift left</p>



<p>arithmetically. We end up still with negative 48, which is the correct number. If we&#8217;re going to</p>



<p>shift left twice, that&#8217;s multiplying 12 by 4 and that&#8217;s 48 or negative 48. And then when we shift</p>



<p>to the right, that should divide it by 2 twice because we&#8217;re shifting to the right by two</p>



<p>positions. So that should be 12 divided by 4, which is negative 3 or sorry, negative 12 divided</p>



<p>which is negative three so everything seems to be working except for a couple of moments of</p>



<p>stumbling on my part which i hope you laughed at um and i think this is the end of the program so</p>



<p>now hopefully you&#8217;re an expert in uh ending oring x oring nodding shifting left and right uh bitwise</p>



<p>rotating. We&#8217;ve done it all in this one video. How long did this video take? 40 minutes?</p>



<p>Anyway, thank you so much for watching this video. I hope you learned a little bit of stuff</p>



<p>and you had a little bit of fun. I&#8217;ll see you in the next video.</p>



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



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



<p>and follow this channel or these videos or whatever it is you do on the current social</p>



<p>media website that you&#8217;re looking at right now. It would really mean the world to me and it&#8217;ll</p>



<p>help make more videos and grow this community. So we&#8217;ll be able to do more videos, longer videos,</p>



<p>better videos, or just I&#8217;ll be able to keep making videos in general. So please do me a kindness</p>



<p>and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night and I just wake up</p>



<p>because I know somebody subscribed or followed. It just wakes me up and I get filled with joy.</p>



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



<p>single time. So you could do it as a nice favor to me or you could you could</p>



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



<p>and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also if you look</p>



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



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



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



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



<p>the services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Clarifications or errata or just future videos that you want to see</p>



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



<p>You know, just send me a comment, whatever. I also wake up for those in the middle of the night</p>



<p>I get I wake up in a cold sweat. I&#8217;m like</p>



<p>It would really it really mean the world to me. I would really appreciate it. So</p>



<p>Again, thank you so much for watching this video and enjoy the cool music as I fade into the darkness, which is coming for us all.</p>



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/">x86-64 Assembly Bit Manipulation: AND, OR, XOR, NOT, Shifts &amp; Rotates Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-bit-manipulation-and-or-xor-not-shifts-rotates-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Signed Integer Division in x86-64 Assembly with IDIV and CQO</title>
		<link>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/</link>
					<comments>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 14 Dec 2025 09:12:38 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[64-bit assembly]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly language programming]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[cqo instruction]]></category>
		<category><![CDATA[idiv instruction]]></category>
		<category><![CDATA[integer division assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[rax rdx division]]></category>
		<category><![CDATA[signed division]]></category>
		<category><![CDATA[ubuntu assembly programming]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=266</guid>

					<description><![CDATA[<p>Step-by-step tutorial on performing signed integer division in x86-64 assembly language using the IDIV instruction and CQO for sign extension. Includes complete working Yasm code on Ubuntu/Linux.</p>
<p>The post <a href="https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/">Signed Integer Division in x86-64 Assembly with IDIV and CQO</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Signed Integer Division in x86-64 Assembly with IDIV and CQO" width="1380" height="776" src="https://www.youtube.com/embed/axasPyjJFAA?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p>In this video, I walk through how to perform signed integer division in x86-64 assembly language using Yasm on Ubuntu/Linux.</p>



<p>We focus on 64-bit signed integers and the IDIV instruction. I explain the setup: loading the dividend into RAX, using CQO for proper sign extension into RDX, loading the divisor, and executing IDIV. After division, the quotient ends up in RAX and the remainder in RDX.</p>



<p>I reference the excellent free book &#8220;x86-64 Assembly Language Programming with Ubuntu&#8221; and show practical code examples, including a complete working program that prints both the quotient and remainder.</p>



<p>We test with small numbers (256 / 233) and larger ones to see it in action. I also touch on the difference between signed (IDIV) and unsigned (DIV) division, and why CQO is needed for correct sign handling.</p>



<p>Perfect for anyone learning low-level programming, assembly language, or wanting to understand how integer division really works under the hood.</p>



<p>Introduction to Signed Division 00:00:00<br>Referencing the Assembly Book 00:00:28<br>IDIV Instruction Overview 00:01:16<br>Dividend and Divisor Setup 00:01:24<br>Sign Extension and CQO 00:04:18<br>Quotient and Remainder Results 00:04:40<br>Setting Up the Program 00:05:13<br>Data Section and Strings 00:06:22<br>Function Prologue and Preservation 00:09:20<br>Loading the Dividend into RAX 00:10:00<br>Sign Extension with CQO 00:11:24<br>Loading the Divisor 00:12:36<br>Performing IDIV 00:14:19<br>Saving Quotient and Remainder 00:16:48<br>Printing the Results 00:17:20<br>Running and Testing the Program 00:18:51<br>Testing Larger Numbers 00:19:48<br>Returning the Remainder 00:21:42<br>Signed vs Unsigned Division 00:23:48<br>Conclusion and Thanks 00:25:02</p>



<p>Thanks for watching!</p>



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



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



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p>Hello there.</p>



<p>In this video, I&#8217;d like to talk to you about dividing signed integers in an x86-64 Yasm</p>



<p>assembly program.</p>



<p>I&#8217;m going to be focusing on 64-bit integers and I&#8217;m going to use an instruction called</p>



<p>iDiv.</p>



<p>Okay, so for starters, I just want to give you an overview of the process.</p>



<p>I&#8217;m going to use an awesome book that I love to reference.</p>



<p>awesome book that i love to reference i did not write this book the book is titled x86 64 assembly</p>



<p>language programming with ubuntu i mention it in a lot of other videos the author here</p>



<p>is a genius and this book is open source and freely available so go look it up get yourself a</p>



<p>copy and become an assembly expert too anyway so what i&#8217;m going to do is go into this book</p>



<p>instruction set overview and then inside of that i&#8217;m going to click on 7.5 integer arithmetic</p>



<p>instructions or arithmetic i don&#8217;t know whatever you prefer to say it as but and then i&#8217;m going to</p>



<p>go inside of there and i&#8217;m going to click on 754 integer division so you know the basic idea for</p>



<p>dividing integers in a yasm assembly program on x86 64 is just to load up your dividend you know</p>



<p>top portion I guess that is the numerator and then the divisor which</p>



<p>would be the denominator with separate registers so load up the dividend with a</p>



<p>register or put the dividend into a register and then put the divisor into</p>



<p>another register they&#8217;re going to be special registers we&#8217;re going to be</p>



<p>using the A and D registers you can kind of see that at the bottom already and</p>



<p>then once that&#8217;s set up properly you just sort of use the IDIV function or</p>



<p>instruction and there&#8217;s another instruction we can use to make things a</p>



<p>little bit more simple because if you kind of notice what what this is saying</p>



<p>here it&#8217;s saying that the the dividend usually needs to be stacked between two</p>



<p>other registers at the same time and that&#8217;s a little bit confusing</p>



<p>especially if these registers right here notice how a DX colon ax that means it</p>



<p>16-bit versions of two different registers just sort of like pushed up against each other so</p>



<p>Obviously if you know how to manipulate bits which I&#8217;m gonna do other videos for in the future</p>



<p>Then you could do that with any size, but just I&#8217;m gonna make the easiest example that I can here. I</p>



<p>Love this line. I just found it today</p>



<p>As always division by zero will crash the program and damage the space-time continuum. So try not to divide by zero. I love it</p>



<p>nifty little table inside of the book that sort of shows you how you&#8217;re supposed to set up the</p>



<p>numerator and denominator or the divisor what was the other word that they used dividend and divisor</p>



<p>i usually say numerator and denominator anyway so remember in in if you watched my previous videos</p>



<p>then you&#8217;ll know that we have smaller versions of our registers like for example uh this eax right</p>



<p>here that&#8217;s actually the rax register but it&#8217;s only about half of it uh half of those bytes are</p>



<p>half of those bytes are being ignored so all of these different versions that you&#8217;re seeing are</p>



<p>just different sizes of the a and d registers so i&#8217;m going to go to the simplest example here and</p>



<p>just kind of show you that basically what they&#8217;re trying to do is force you to have a numerator that</p>



<p>is bigger than the denominator that way it&#8217;s guaranteed you&#8217;ll have some kind of a quotient</p>



<p>or you know more likely that you&#8217;ll have some kind of a quotient and and a remainder that kind of</p>



<p>that kind of makes sense because if you divide a number that&#8217;s much smaller than</p>



<p>the denominator then you&#8217;ll just end up with zero remainder the original number</p>



<p>pretty much in integer division this video is not about float division float</p>



<p>division is more precise so in other words if I want to divide something by a</p>



<p>64-bit integer then I need to first set up a 128-bit integer and that means I</p>



<p>and that means I have to take two 64-bit integers, the D register and the A register.</p>



<p>I have to load both of those registers up with 64 bits each so that when you imagine them being combined,</p>



<p>it&#8217;s a 128-bit register or 128-bit value.</p>



<p>Once I have something that is sufficiently large, then I can divide it by something else.</p>



<p>Then the result will show up in the A and D registers.</p>



<p>the actual quotient, you know, the actual result of dividing without a fractional part at the end,</p>



<p>just, you know, truncated data. Then the remainder will be in the D register, the RDX register. So at</p>



<p>least for this form, I just want you to know you can do it with all these other forms if you want</p>



<p>to. But in this example, I&#8217;m just going to take 128 bits, divide it by 64 bits, and then I&#8217;ll get</p>



<p>So that&#8217;s the basic idea. Here&#8217;s some sample code in the book. I love it. It&#8217;s wonderful.</p>



<p>But I&#8217;m going to go ahead and start writing my own program now.</p>



<p>Okay, so I got a solution up here. I&#8217;m going to just copy paste my data section and explain it briefly.</p>



<p>Okay, so here is, oh, maybe I should first say, here&#8217;s a sample program that I&#8217;m using.</p>



<p>You don&#8217;t have to worry too much about what&#8217;s actually inside of it.</p>



<p>The C module and the make file, they&#8217;re standard stuff that I usually use in all of my videos.</p>



<p>all of my videos this is not a make file video see my other videos if you want to learn how to</p>



<p>make your own make files but long story short i&#8217;m just going to type you know make run in order to</p>



<p>just get this program to run and then for the c program i&#8217;m compiling a hybrid program so i mean</p>



<p>it&#8217;s just easier for me to just kind of get started this way so i have my main entry point sitting</p>



<p>inside of a c source code file that gets compiled and linked into my main program and it just calls</p>



<p>a function called math right here which is just going to be you know this source code right here</p>



<p>so if you want to make hybrid programs you want to learn the basics of assembly you want to learn</p>



<p>anything that i&#8217;m not really talking about in this video see my other videos</p>



<p>for today or for this video it&#8217;s just going to be integer division anyway i&#8217;m going to paste in</p>



<p>some starter code here for my math module i&#8217;ve got a data section and i&#8217;ve got some strings</p>



<p>strings or anything but just keep in mind I&#8217;m going to eventually print the division result</p>



<p>quotient is and then I&#8217;m going to actually print it and then I&#8217;m going to print the division</p>



<p>remainder is and then actually print it so I&#8217;m just I&#8217;m just setting up strings to make my</p>



<p>results look a little bit prettier that&#8217;s all I&#8217;m really doing crlf that&#8217;s just new line stuff</p>



<p>system calls here&#8217;s the system service code for writing to something writing to a file or standard</p>



<p>standard output again that&#8217;s in another video file descriptor one another video and then return</p>



<p>value I&#8217;m just choosing to return the number seven from this module I don&#8217;t know why I just</p>



<p>I like to reuse code and I&#8217;m not going to change it and then we&#8217;re going to take two different</p>



<p>integers here and uh 56 and oh I think in my starter code I actually hard-coded those numbers</p>



<p>after everything is working so um i&#8217;m going to start my text section here by just copy pasting</p>



<p>a little thing that says okay here&#8217;s where my code is going to go now all my instructions</p>



<p>and then uh here&#8217;s my entry point which is just my function called math you want to write functions</p>



<p>see my other videos but this is basically it you just kind of have something enter and then return</p>



<p>from it when you&#8217;re done and if you mess up any registers you have to preserve them with push and</p>



<p>you have to preserve them with push and pop pairs.</p>



<p>Another thing that I&#8217;m going to add that I&#8217;m not going to explain in this video</p>



<p>is I&#8217;m going to do calls to one of my own personal libraries.</p>



<p>And basically I&#8217;m just going to mark two functions as external.</p>



<p>They&#8217;re sitting in a shared object that I made.</p>



<p>Don&#8217;t worry about that. This video is not about printing.</p>



<p>It&#8217;s just about dividing. But I want to be able to easily print.</p>



<p>So I&#8217;m just going to use that library.</p>



<p>So don&#8217;t worry about that.</p>



<p>for this video is my function called divide test because notice how in the entry point of math</p>



<p>remember the c module the driver it calls on this math function the math function eventually or</p>



<p>immediately calls on divide test and so here&#8217;s the prototype basically for my divide test function</p>



<p>I&#8217;m just going to say it&#8217;s void takes no arguments that way it can just do everything by itself</p>



<p>as a little demo and it&#8217;s a little bit easier so that means I don&#8217;t really need to preserve any</p>



<p>I am using them and if you see here well I guess if it was going to take arguments you don&#8217;t need</p>



<p>to preserve the argument register so I should have just said yeah we are going to use some</p>



<p>registers and therefore we&#8217;re going to preserve them so our 12 14 and 15 oh why am I not using</p>



<p>13 I must have erased that at an earlier date whatever we&#8217;re going to use these three registers</p>



<p>make this an official function and because I know I&#8217;m going to use r12 14 and 15 I&#8217;m just going to</p>



<p>preserve them with a push pop pair so I&#8217;m going to say this is the prologue whoops and I&#8217;m going to</p>



<p>push r12 push r14 push r15 respect the abi if you don&#8217;t other pro other parts of the program</p>



<p>will probably crash epilogue is just popping all those in reverse order so that I can basically</p>



<p>basically preserve them for the caller.</p>



<p>Okay, so now we&#8217;re good with all our registers.</p>



<p>Next thing I want to do is I want to load up, let&#8217;s see,</p>



<p>I want to load up RAX with a number.</p>



<p>Now maybe I&#8217;ll fix this right now.</p>



<p>What was 256?</p>



<p>It was the my integer B.</p>



<p>So I&#8217;m going to hit main memory,</p>



<p>which is a little slower than using an immediate,</p>



<p>but hey, it&#8217;s more flexible.</p>



<p>but hey it&#8217;s more flexible and so basically i&#8217;m going to set up rax which is the numerator if we</p>



<p>go back to the book real fast if our intention is to divide a 128 bit uh uh like combined register</p>



<p>by a 64 bit something then uh well rax is the lower 64 bit so that&#8217;s why i&#8217;m loading that up</p>



<p>first i&#8217;m just saying well i&#8217;ve got 64 bits worth of stuff a quad word worth of stuff sitting in</p>



<p>so i&#8217;m just going to load that into rax then i just have to somehow take care of rdx so obviously</p>



<p>if you wanted to you could just you know take rdx and just set a bunch of zeros to it but that</p>



<p>would probably only work if you were guaranteed that rax was a positive number so that its sign</p>



<p>bit was zero at the very left if it was a negative number its sign bit would be a one at the very</p>



<p>And that wouldn&#8217;t be very good if RDX plus RAX, you know, lined up are supposed to be a negative number because you would want ones all the way through to RDX.</p>



<p>So there&#8217;s a special instruction we can use called CQO.</p>



<p>I&#8217;m just going to paste that in here just to make things a little bit easier because I&#8217;m really trying to divide a 64-bit integer by something.</p>



<p>The system wants a 128-bit integer.</p>



<p>basically this instruction right here CQO whoops what happened how come it&#8217;s not lined up</p>



<p>CQO basically just takes RAX whatever is in there and stretches it onto the 128 bit combination of</p>



<p>RDX colon RAX so remember when you see this kind of thing remember when you see RDX colon RAX or</p>



<p>just like a register colon another register that&#8217;s usually telling you that the system will think of</p>



<p>in a combined way as one longer register so that&#8217;s really what it is cqo is going to say</p>



<p>let&#8217;s look at rex and let&#8217;s uh you know fix it up and then also fix up uh rdx so that when they&#8217;re</p>



<p>combined they make sense as just one number um and you know that that would work pretty easily</p>



<p>like i said before if it was a positive number but not necessarily in all cases so we&#8217;ll just use</p>



<p>is, let&#8217;s see, set up the denominator. So let&#8217;s see, here, I&#8217;m going to say set up denominator.</p>



<p>And you know, we can hard code the 233 there, but I think I&#8217;ve already decided,</p>



<p>let&#8217;s just use variables. So the A, I think has 233. Where the heck&#8217;s my A? There we go.</p>



<p>So I&#8217;m going to say my integer A. And here I&#8217;m going to put the pointer to the A variable,</p>



<p>to the a variable and then i&#8217;m going to dereference it and um oh one thing that we have to remember is</p>



<p>when the system is using your registers like rax here it knows rax is the 64-bit form of the a</p>



<p>register so if you use different forms of your register then it knows how many bits you want</p>



<p>like for example here we just kind of go back up uh this eax that&#8217;s still the a register but the e</p>



<p>that it&#8217;s a you know 32 bits instead of 64 bits but there&#8217;s no there&#8217;s no way for</p>



<p>the system to really know that that memory location is 64 bits how does it</p>



<p>how does it know if I&#8217;m if I&#8217;m trying to load from memory a one byte integer or</p>



<p>two byte integer or four bytes or eight bytes it doesn&#8217;t really know so I kind</p>



<p>of have to specify what the data size is of the integer at that place in memory</p>



<p>this probably wouldn&#8217;t even compile. But if it did compile, you probably should be nervous that</p>



<p>it would compile and do something wrong. So I&#8217;m just going to specify exactly what the data size</p>



<p>is for that. So now we&#8217;ve set up our numerator and also set up our denominator. And the next step is</p>



<p>to just actually divide. Let me copy paste that real fast. So now we actually divide. Why is my</p>



<p>Oh, I know what&#8217;s going on.</p>



<p>I have the tab size set up differently between my two editors.</p>



<p>I got to fix that.</p>



<p>Or how about never fix it so I can always complain and you can laugh at me.</p>



<p>That&#8217;s okay.</p>



<p>So on line 85, we&#8217;re going to divide the numerator by the denominator.</p>



<p>And we&#8217;ll say the numerator is, you know, RAX with RDX in front of it.</p>



<p>the denominator is just one register which is uh what have I done wrong here oh no no I&#8217;m okay I&#8217;m</p>



<p>okay r12 so if you look back at the I got a little confused here because uh the book you know it</p>



<p>wants uh the d and the a registers as the numerator but then it says op64 as the denominator you can</p>



<p>just kind of use a bunch of different stuff for that but I&#8217;m choosing to divide by another register</p>



<p>So basically I&#8217;m taking the denominator and loading it up into R12.</p>



<p>And then when I call my idiv instruction, I&#8217;m just saying integer division.</p>



<p>And I want you to divide whatever&#8217;s in the RDX, RAX set as I want you to divide it by R12.</p>



<p>At that point, RAX and RDX will be overwritten.</p>



<p>So that data is now destroyed, but they contain the answer.</p>



<p>So RAX now contains the answer.</p>



<p>I&#8217;m just gonna say quotient and the RAX register contains the remainder so for</p>



<p>example if you are familiar with the let me pull this up real fast is that the</p>



<p>calculator no there is if you&#8217;re familiar with the modulo instruction like if we</p>



<p>said let&#8217;s see what was it again 233 okay so let&#8217;s do 256 divided by 233 this</p>



<p>this gives us the number one and then some sort of like a fractional remainder</p>



<p>with integer division we&#8217;re not going to get the fractional remainder it&#8217;s just literally going to</p>



<p>be the number one sitting in rax and then in rdx it&#8217;s going to give us the result of using the</p>



<p>modulo operator which is going to be 23 so it&#8217;s going to be one remainder 23</p>



<p>is what we should see unless i&#8217;m totally wrong so then right away</p>



<p>of our division which is a good practice you know like the rex and rdx those are registers that</p>



<p>could be quickly destroyed especially if you called another function or a system call or</p>



<p>something so i&#8217;m just going to save them right away so that&#8217;s why we saved 14 and 15</p>



<p>in addition to r12 so r12 we were using for the denominator and then we&#8217;re using 14 and 15 to</p>



<p>save them real fast and now I&#8217;m just going to do easy stuff that I&#8217;ve talked about in other videos</p>



<p>just to sort of print out an answer so I&#8217;m just going to I&#8217;m just going to write this up real</p>



<p>fast copy paste this and then just kind of briefly talk about oh look I&#8217;m using a system call</p>



<p>explained in another video to print out that little prefix message you know the result of your</p>



<p>division here&#8217;s the quotient and then a colon and then I can actually print the number</p>



<p>personal library to print a 64-bit integer and so that way it&#8217;s easy for me and notice how I&#8217;m</p>



<p>printing r14 I&#8217;m giving it to the call to the function as the first argument which is rdi</p>



<p>what is r14 again that&#8217;s just the quotient so here I&#8217;m printing the quotient and then I do the</p>



<p>same thing for the remainder and then we&#8217;re going to do a paste here and I&#8217;m just going to print my</p>



<p>prefix of the remainder you know here&#8217;s your remainder and then I&#8217;m going to actually print</p>



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



<p>And the remainder was in R15.</p>



<p>So 14 and 15 had the answer.</p>



<p>And then the other register just kind of had, or the R12 just kind of had the denominator.</p>



<p>So I think that&#8217;s pretty much all I need.</p>



<p>Oh, there&#8217;s one more function that I forgot to put in there.</p>



<p>I&#8217;m fond of writing a little CRLF function.</p>



<p>This is not part of the video, really.</p>



<p>But it&#8217;s just a special function that I can call on to do a carriage return line feed.</p>



<p>And I&#8217;m just, I just can never stop using this.</p>



<p>stop using this. I know I could just prefix or suffix my strings, you know, with like a CRLF,</p>



<p>but I like to be able to call every time I want to see our lab. Okay. So we have the program now.</p>



<p>Let&#8217;s see if it actually works. Is that my terminal? Yeah. Okay. I&#8217;m going to go clear</p>



<p>and make, hopefully it actually works. So, oh yeah, there we are. Okay. So everything seemed</p>



<p>It printed hello from the driver.</p>



<p>And then when we went inside of the assembly module,</p>



<p>this is really what we&#8217;re interested in.</p>



<p>It said the division result, the quotient is the number one</p>



<p>and the division remainder is the number 23.</p>



<p>That&#8217;s what we said before, right?</p>



<p>So it&#8217;s going to be 23 is the remainder.</p>



<p>If you just do the modular operator or if you&#8217;re dividing,</p>



<p>whoops, if you&#8217;re dividing, then you&#8217;ll just get a one.</p>



<p>Remember like the quotient is just going to, you know,</p>



<p>truncate or remove or throw away all the extra data.</p>



<p>all the extra data it&#8217;s not going to round up or down or anything it&#8217;s just going to say</p>



<p>anything past the decimal point is gone and that&#8217;s why the uh the remainder is pretty important</p>



<p>then of course you know just for fun we could i don&#8217;t know put like some other numbers in here</p>



<p>if we wanted to maybe i&#8217;ll copy paste this just put some other numbers let&#8217;s put like a giant</p>



<p>number and see what happens because these are 64-bit numbers we can do it um i probably should</p>



<p>have switched a and b right because doesn&#8217;t it kind of feel like a&#8217;s on top so it should be the</p>



<p>top so it should be the numerator or whatever so I&#8217;m just gonna hit a bunch of stuff here and then</p>



<p>divide that by some other stuff see if this comes up with a more fun answer so one more time clear</p>



<p>and make and it&#8217;s telling me that uh whoa that&#8217;s the result I guess probably should have printed</p>



<p>out the original numbers but let me just put that into the calculator just for fun we&#8217;ll say this</p>



<p>just sort of go back to the calculator it enters so uh oh the squiggly lines</p>



<p>that means that&#8217;s not exactly the result i got to figure out how to change the mode on</p>



<p>this calculator so i can get precise let&#8217;s see can i do that right here</p>



<p>exact no can i try it again with exact all right this is not a tutorial on the</p>



<p>it as is maybe i&#8217;ll do can i do a slightly smaller number yeah there we go okay i&#8217;ll take</p>



<p>off three digits so that the calculator will at least show me something okay run it one more time</p>



<p>and then so now we have smaller stuff the answer should have been 38677662 38677662 yeah that looks</p>



<p>the decimal point so the point six is not on there and then we run the modulo</p>



<p>to see what the remainder is and it should say one two eight six three yeah</p>



<p>okay and then the seven is getting returned I don&#8217;t know do you want to</p>



<p>return the remainder for some reason we could do it let&#8217;s just return the</p>



<p>remainder might as well make this video a little bit more fun so recall that in</p>



<p>order to return something if the return value is an integer and not a float or</p>



<p>float or if it&#8217;s a pointer then you&#8217;ll just use RAX as the return register so I&#8217;m just going to do</p>



<p>move return the remainder to the color okay I&#8217;m going to move something into RAX it&#8217;s going to be</p>



<p>R15 nice now this is a function with a return type I&#8217;m going to say along here and then</p>



<p>up here, I guess, RAX is already going to be pre-filled now after that call.</p>



<p>But you could imagine if you were doing more stuff between the call and the return,</p>



<p>you would probably want to save the return value from RAX in some other register like R12.</p>



<p>And then that means you got to add a push pop pair to preserve it.</p>



<p>And then we&#8217;ll definitely just, well, maybe I&#8217;ll say move RAX into RAX</p>



<p>totally useless. That&#8217;s why I&#8217;m commenting it out. So we&#8217;ll do one less instruction.</p>



<p>So basically after we get back from divide test, RAX is going to have the</p>



<p>return value and math, this function would also use RAX for its return value. So if we just sort</p>



<p>of don&#8217;t do anything, we should now see that the driver receives the remainder as its return value.</p>



<p>Let me open that up again real fast just to make sure we understand what&#8217;s going on with</p>



<p>the driver.</p>



<p>Even though this is not a driver video, it says external long math.</p>



<p>It names that as a long function.</p>



<p>And then when we call the function, we just kind of grab a long result, 64 bit int, and</p>



<p>then we print it with printf because this is a C program, not C++.</p>



<p>integers, assigned integers. Let&#8217;s see in assembly. Let&#8217;s see. Where&#8217;s that? Yeah, there we go.</p>



<p>So just keep in mind what I&#8217;ve been showing you is idiv, which is a signed integer, which is what</p>



<p>you probably want, which means the integer can be positive or negative. And, you know, if you know</p>



<p>for sure that you&#8217;re going to be dividing two positive numbers, and there&#8217;s no chance that</p>



<p>that the numerator or denominator or the quotient or you know any of those things are going to be</p>



<p>negative then you could you could divide unsigned integers and then you can use larger values</p>



<p>because remember when we have signed values we basically lose half of the I guess maximum</p>



<p>possible value that we can represent because we lose one bit to the fact that the integer is signed</p>



<p>is for signed sorry i div is for signed and regular div is for unsigned and then</p>



<p>yeah i guess that&#8217;s basically all i really needed to show you there&#8217;s a bunch of nice code in this</p>



<p>book i&#8217;m not gonna i&#8217;m not gonna look at it right now so okay thank you so much for watching this</p>



<p>video i hope you learned a little bit of stuff and had a little bit of fun i&#8217;ll see you in the next</p>



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



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



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



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



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



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



<p>or just I&#8217;ll be able to keep making videos in general.</p>



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



<p>You know, sometimes I&#8217;m sleeping in the middle of the night</p>



<p>and I just wake up because I know somebody subscribed or followed.</p>



<p>It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time.</p>



<p>So you could do it as a nice favor to me</p>



<p>or you could troll me if you want to just wake me up in the middle of the night.</p>



<p>Just subscribe and then I&#8217;ll just wake up.</p>



<p>I promise that&#8217;s what will happen.</p>



<p>Also, if you look at the middle of the screen right now,</p>



<p>the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p>which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p>main website where you can just kind of like see all the videos i published and the services and</p>



<p>tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p>clarifications or errata or just future videos that you want to see please leave a comment or</p>



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



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



<p>to me I would really appreciate it so again thank you so much for watching this video and</p>



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



<p>Thank you.</p>



<p>you</p>
<p>The post <a href="https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/">Signed Integer Division in x86-64 Assembly with IDIV and CQO</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/signed-integer-division-in-x86-64-assembly-with-idiv-and-cqo/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How Your CPU Works: Fetch, Decode, Execute Explained</title>
		<link>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/</link>
					<comments>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 10 Aug 2025 06:23:48 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ALU]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[code cache]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[CPU clock]]></category>
		<category><![CDATA[data cache]]></category>
		<category><![CDATA[fetch decode execute]]></category>
		<category><![CDATA[instruction pointer]]></category>
		<category><![CDATA[processor basics]]></category>
		<category><![CDATA[RIP register]]></category>
		<category><![CDATA[tech tutorials]]></category>
		<category><![CDATA[x86 processors]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=199</guid>

					<description><![CDATA[<p>Discover how your CPU fetches, decodes, and executes instructions in this beginner-friendly guide. Learn about the clock, RIP register, and ALU in a simple breakdown of computer architecture.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/">How Your CPU Works: Fetch, Decode, Execute Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="How Your CPU Works: Fetch, Decode, Execute Explained" width="1380" height="776" src="https://www.youtube.com/embed/UXMDPnMfcAM?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>Get a clear, beginner-friendly dive into how your CPU works! This video explains the fetch-decode-execute cycle, the role of the clock, instruction pointer (RIP), and units like the ALU and cache. Learn why timing matters and how your CPU handles instructions step-by-step. Perfect for gamers, coders, or anyone curious about what makes computers tick. Inspired by Kip Irvine’s Assembly Language for x86 Processors, this is your gateway to understanding computer architecture. Subscribe for more tech insights and hit that QR code to explore more tutorials! #CPU #ComputerScience #TechTutorials</p>



<p>Introduction to CPU Operations 00:00:00<br>CPU Clock Explanation 00:00:17<br>Clock Cycle and Stability 00:01:07<br>Instruction Fetch from RAM 00:03:24<br>Instruction Pointer (RIP) Role 00:03:48<br>Sequential and Conditional Execution 00:04:49<br>Code Cache Functionality 00:06:24<br>Instruction Decoding Process 00:07:51<br>Control Unit Operations 00:08:33<br>Arithmetic Logic Unit (ALU) 00:08:52<br>Floating Point Unit 00:11:03<br>Data Cache Usage 00:12:08<br>Summary of CPU Process 00:14:36<br>Closing and Subscription Call 00:15:07</p>



<p>Thanks for watching!</p>



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



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



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p>CPU #ComputerScience #TechTutorials #ComputerArchitecture #AssemblyLanguage #TechExplained #CPUBasics #LearnTech #x86Processors #Coding</p>



<p>Hello there. Let&#8217;s talk about how your CPU fetches, decodes and executes instructions.</p>



<p>We&#8217;re going to just do a quick surface level overview, but I hope you like it.</p>



<p>Okay. So for starters, your CPU has something called the clock.</p>



<p>keep my pen onto one monitor here let me just double check that real fast</p>



<p>okay is it working yeah okay so</p>



<p>okay so notice how we have this line that goes up and down and up and down it kind of looks like a</p>



<p>little binary line uh it actually is uh sometimes your cpu is outputting a zero to this special</p>



<p>and it just oscillates between zero and one constantly it goes zero one zero one zero one</p>



<p>it does so very very very very fast and the reason it does that is because something needs to control</p>



<p>the timing of your cpu imagine this we kind of take this for granted but when you apply electricity</p>



<p>somewhere as a human you kind of feel like the electricity goes to the other place like on the</p>



<p>other side of your house or your room or whatever you kind of feel like it goes there instantaneously</p>



<p>really fast but in reality electricity doesn&#8217;t go faster than the speed of light so there is a limit</p>



<p>which means if you have a cpu that is executing instructions really really really really fast</p>



<p>you know fetching instructions from your system ram really really fast</p>



<p>there&#8217;s there&#8217;s there&#8217;s the problem of trying to um to know for sure that the electricity actually</p>



<p>made it uh to the other place in time uh so like what if we said let&#8217;s load some instructions from</p>



<p>load some instructions from memory, they come down the wire to your CPU.</p>



<p>How do we know when it actually finished coming down the wire to the CPU?</p>



<p>We just take it for granted.</p>



<p>But in reality, physics has a limit.</p>



<p>And so the electricity needs time to get there and it needs time to stabilize.</p>



<p>So the clock cycle helps us with that timing.</p>



<p>So, you know, if we make the clock go too fast, your CPU becomes unstable.</p>



<p>Your gamers, you gamers probably know that.</p>



<p>If we make it too slow, then it&#8217;ll work.</p>



<p>it&#8217;ll work, but, um, you know, we&#8217;re not getting the fastest, you know, CPU, uh, that, that</p>



<p>we, that we can, we&#8217;re not getting all the performance out of it that we can.</p>



<p>So we make it, we make the clock cycle go as fast as we can, uh, without making the</p>



<p>CPU, uh, instable.</p>



<p>And so just keep that in mind.</p>



<p>That&#8217;s one component of the CPU.</p>



<p>Okay.</p>



<p>So then the next thing I wanted to show you, oh, and by the way, let me give credit to</p>



<p>the authors of this book.</p>



<p>I&#8217;m just looking at a diagram, but, um, this book is written by someone named Kip</p>



<p>This book is written by someone named Kip Irvine</p>



<p>and it&#8217;s titled Assembly Language for X86 Processors.</p>



<p>It&#8217;s written by Pearson or it&#8217;s published by Pearson</p>



<p>and the year is 2014.</p>



<p>So get a copy of this book.</p>



<p>Anyway, so this is a simplified CPU block diagram.</p>



<p>Let me get my notes here real fast</p>



<p>so I don&#8217;t forget what I was supposed to say.</p>



<p>Clock ticks and alu.</p>



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



<p>Imagine now that we have a little RAM stick or some place where we can get instructions for the CPU.</p>



<p>So this is like system RAM, we&#8217;ll say.</p>



<p>And we&#8217;ll just pretend for the sake of argument that we already have existing circuitry</p>



<p>that will easily transfer data back and forth from the RAM stick to the CPU.</p>



<p>So anytime the CPU wants to grab some kind of data or like an instruction or something off the RAM stick,</p>



<p>it can just do it. And that&#8217;s the arrow right there.</p>



<p>keep in mind after that is that there is a special register in the CPU called the instruction pointer</p>



<p>RIP in x86-64 assembly the RIP register and the RIP register it basically just holds a memory</p>



<p>location telling the CPU where is the next where&#8217;s the address of the next instruction that we want</p>



<p>to execute so you know imagine you have like some kind of memory address 0x I don&#8217;t know 55 and then</p>



<p>an instruction on the very next actually let&#8217;s do five zero so I can increase this by eight without</p>



<p>doing hex in my head we&#8217;ll say this is like the address five zero in hexadecimal so that means</p>



<p>that the next time the CPU wants to execute an instruction it&#8217;s just going to ask system ram</p>



<p>for that address system ram is going to give it whatever ones and zeros it has and then it can be</p>



<p>decoded and executed after that and then of course we have to execute a program you know step by step</p>



<p>you know step by step we have to execute more instructions and we can&#8217;t just do one instruction</p>



<p>so typically in a program you execute instructions one by one by one by one by one</p>



<p>and then every once in a while there might be some decision logic like you have an if else</p>



<p>branch like let&#8217;s say uh you know for the for the high level people i&#8217;ll put a question mark here</p>



<p>you have like an if statement and you say like if that&#8217;s true then execute this i&#8217;ll put maybe i&#8217;ll</p>



<p>then we&#8217;ll just execute this other logic down here. So you know very often we execute instructions</p>



<p>sequentially but every once in a while we have to jump right because if if you executed this</p>



<p>if statement and you realized that this condition was not true then you could not execute the body</p>



<p>that came right after the comparison instructions you would have to jump down to wherever the else</p>



<p>block is so there is circuitry inside of the CPU that allows you to either increase the instruction</p>



<p>instruction pointer by 8 bytes, 8 bytes because 64 bits per instruction because that&#8217;s your CPU.</p>



<p>So you just kind of go sequentially or to jump somewhere because you made a decision or you&#8217;re</p>



<p>like making a function call or whatever. So just keep in mind that under the hood, under this</p>



<p>diagram, there is a lot more logic even than the more detailed diagrams that you can find elsewhere.</p>



<p>There&#8217;s just like a lot more detail. So anyway, the RIP register just kind of increases by 8</p>



<p>every time we execute an instruction,</p>



<p>sometimes it&#8217;ll drastically change its address</p>



<p>so we can jump execution somewhere else</p>



<p>in order to make a decision.</p>



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



<p>So suppose that we just fetched an instruction from memory.</p>



<p>You know, it comes in here</p>



<p>and we kind of send it down the line.</p>



<p>We first give it to the code cache unit.</p>



<p>Your CPU is just not a solid block of stuff.</p>



<p>It&#8217;s got subunits inside of it.</p>



<p>And one of these units is the code cache unit.</p>



<p>it&#8217;s a lot more complicated than what you&#8217;re seeing here but basically</p>



<p>system ram is very slow dealing with the cpu internally is very very very fast in comparison</p>



<p>so if we have like a register or some unit that we&#8217;re accessing that&#8217;s inside of the cpu it&#8217;s</p>



<p>lightning fast compared to accessing the memory so that means cpus have caches which just i&#8217;m not</p>



<p>going to go too much into caches for this video but basically every time we get a new instruction</p>



<p>we might put it into the cache, which is like incredibly fast because it&#8217;s hardware that&#8217;s part</p>



<p>of the CPU. Then maybe sometime in the future, we might want to execute the same exact instruction</p>



<p>at the same exact address. Again, that&#8217;s possible. Remember we have loops. We can execute the same</p>



<p>instructions a bunch of times in a row. So if we come across an instruction that we have executed</p>



<p>in the past and it&#8217;s already in the code cache, then we don&#8217;t actually need to wait for memory</p>



<p>to fetch the instruction for us, we can just go directly to the code cache and it&#8217;s a dramatic</p>



<p>speed up.</p>



<p>Remember, when you access memory, execution has to stall for quite a long time usually</p>



<p>or sometimes.</p>



<p>Okay, so that&#8217;s what the code cache does.</p>



<p>Once the memory and the code cache have figured out, okay, we got the instruction and everything&#8217;s</p>



<p>good, then we just sort of decode the instruction.</p>



<p>We decide, you know, what is in the instruction?</p>



<p>it all like data or is it all like like is it half and half and part instruction</p>



<p>part data part whatever there&#8217;s lots of different formats for instructions so</p>



<p>the instruction decoder just sort of like you know decides how to separate</p>



<p>it and it sends data into the control unit notice how there&#8217;s a little arrow</p>



<p>pointing from the instruction decoder to the instruction pointer so we could use</p>



<p>that information to decide how to modify the RIP register like if we&#8217;re gonna go</p>



<p>or if we&#8217;re going to jump somewhere.</p>



<p>And then the, let&#8217;s see,</p>



<p>when we send the instruction to the control unit,</p>



<p>the control unit is not necessarily the brain</p>



<p>because the whole CPU is the brain of the computer,</p>



<p>but the control unit is kind of like the decider of the CPU.</p>



<p>At least that&#8217;s the way I see it.</p>



<p>It decides what to do.</p>



<p>There&#8217;s a lot of other subunits in the CPU.</p>



<p>Notice how down here we have, you know, CPU registers</p>



<p>and we also have the all you.</p>



<p>the all you the all you is basically the arithmetic logical unit so it&#8217;s it&#8217;s a little subunit that</p>



<p>will execute mathematical instructions or logical instructions like it&#8217;ll take some bits and it&#8217;ll</p>



<p>it&#8217;ll move them to the left or the right or it&#8217;ll flip the bits or whatever or it&#8217;ll add two numbers</p>



<p>together the registers you know they&#8217;re very very very fast they&#8217;re like you know the variables of</p>



<p>the cpu um so the control unit will examine uh whatever it&#8217;s getting and it&#8217;ll decide all right</p>



<p>want us to do it wants us to add two integers together or it wants us to</p>



<p>write something to memory or it wants us to do whatever right so when</p>



<p>the control unit receives input remember this is all just like ones and zeros</p>



<p>right so imagine if we had like a bunch of ones and zeros coming in and they&#8217;re</p>



<p>just wired directly into the control unit right so then the control unit has</p>



<p>its own internal circuitry that that makes it so well if I receive this</p>



<p>going to tell everybody else to add numbers together and if I see some other number I&#8217;m</p>



<p>going to tell everybody else to multiply numbers together so the control unit is really responsible</p>



<p>for deciding what the other units need to do in order to accomplish the current instruction</p>



<p>so gosh I really need to fix this I just reconfigured this thing and now it&#8217;s just like</p>



<p>drawing even when I don&#8217;t want it to draw so the control unit let&#8217;s say that we do that we have</p>



<p>that we have like an addition instruction we want to add two registers together the control unit will</p>



<p>send this down the wire to let the all you unit know that it needs to perform addition the all</p>



<p>you unit also has inputs and based on what it receives it will decide to do something it&#8217;ll</p>



<p>decide to add or subtract or multiply or just whatever so the control unit sends the right</p>



<p>input into the all you unit so that it knows to add or do whatever we wanted it to do</p>



<p>operands like it might receive data that just came from memory it might receive registers it might</p>



<p>receive just like a raw immediate number that&#8217;s embedded into the instruction on the other hand</p>



<p>we might also send something into the floating point unit so the control unit it&#8217;s always sending</p>



<p>all of these signals to all of its you know connected units these wires it&#8217;s not like we</p>



<p>can remove these wires so there&#8217;s always something even if it&#8217;s a zero going down the line</p>



<p>so the control unit will basically send the right signals down to the all you to tell it you&#8217;re</p>



<p>going to add and it&#8217;ll send the right signals down to the floating point unit to say you&#8217;re</p>



<p>not going to do anything or just we&#8217;re not we&#8217;re going to ignore the result there&#8217;s like all these</p>



<p>logical gates that we&#8217;re not showing on this diagram where we can just like ignore the output</p>



<p>result of some unit that we&#8217;re not going to use so it&#8217;s sending all these signals to these units</p>



<p>Just like a quick note here, the all you only deals with integer math and, you know, bitwise</p>



<p>logical operations.</p>



<p>Floating point numbers in the machine are stored totally differently than integers.</p>



<p>So we need a totally different unit in order to process them.</p>



<p>So that&#8217;s why there are two units there.</p>



<p>Well, I mean, I guess depending on, you know, what CPU we&#8217;re talking about, but like as</p>



<p>far as the ones that I&#8217;m aware of and that I use.</p>



<p>So then, you know, the data cache, kind of the same thing.</p>



<p>kind of the same thing um you know there&#8217;s like a code cache up here and a data cache down there</p>



<p>so sometimes we might want to fetch data that we&#8217;ve already fetched before and we don&#8217;t want</p>



<p>to hit system ram so you know we don&#8217;t want that slow down so we&#8217;ll just take it from the cache</p>



<p>and um we will also populate the cache when we have a result so when this like all you is done</p>



<p>adding whatever it&#8217;s going to add it&#8217;ll send its result data to the data cache and then um</p>



<p>two or three or whatever then we can reuse the result in another instruction or send it back to</p>



<p>memory or do whatever we want to so this is like a basic idea right like you you start off by you</p>



<p>know grabbing something from memory possibly if it&#8217;s already in the cache then you wouldn&#8217;t</p>



<p>actually grab it and then the instruction decoder decides you know are we gonna we&#8217;re gonna jump</p>



<p>are we gonna go to the next instruction after this is over so it&#8217;ll have the next address</p>



<p>notice how there&#8217;s kind of like a little cycle that goes here between code cache instruction</p>



<p>decoder and instruction pointer and so you know we&#8217;re always kind of deciding what&#8217;s the next</p>



<p>instruction that we&#8217;re going to execute and then we like you know send the decoded instruction to</p>



<p>the control unit the control unit decides you know are you going to do some addition are we</p>



<p>going to use registers or not and then you know like are you not going to do anything or are maybe</p>



<p>you are going to do something in this case I&#8217;m just saying the floating point won&#8217;t do anything</p>



<p>integers. And then once that is all processed, you know, it comes to the data cache and, um,</p>



<p>well, we can, we can basically just, uh, you know, write it back to memory. I think this</p>



<p>diagram is not showing an arrow that will write it, write it back to memory. Um,</p>



<p>yeah, the code cache is definitely not going to feed directly into memory. That&#8217;s just a cache,</p>



<p>but you know, so, so imagine like there&#8217;s a, there&#8217;s like another wire here that goes</p>



<p>so when we have a result we can just write it into system ram</p>



<p>let me see if i forgot to say anything rip</p>



<p>yeah that&#8217;s basically all i wanted to say okay so now you have a fairly good idea of at least</p>



<p>one level deeper of what&#8217;s going on inside of your computer how the cpu in a general way loads</p>



<p>and all these little subunits getting told what to do</p>



<p>and the results getting piped back to the instruction pointer</p>



<p>and also to system RAM and so forth.</p>



<p>And it just keeps going like this again and again and again.</p>



<p>And also just remember that at certain points</p>



<p>during even one cycle, we wait for a clock tick.</p>



<p>I&#8217;m not gonna say exactly when we wait</p>



<p>because it&#8217;s kind of specific to what&#8217;s going on</p>



<p>to be on the scope of this video.</p>



<p>But yeah, the clock speed controls</p>



<p>how fast these cycles go.</p>



<p>And there&#8217;s much more advanced stuff</p>



<p>And there&#8217;s much more advanced stuff that you could learn later,</p>



<p>like pipelining and such to increase efficiency.</p>



<p>But this is a basic idea of how your computer works.</p>



<p>I hope you enjoyed this video.</p>



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



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



<p>Hey, everybody.</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>If you could do me a please, a small little favor,</p>



<p>Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media</p>



<p>Website that you&#8217;re looking at right now</p>



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



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



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



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



<p>subscribed or followed it just wakes me up and I get filled with joy that&#8217;s</p>



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



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



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



<p>happen also if you look at the middle of the screen right now you should see a QR</p>



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



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



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



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



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



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



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



<p>get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p>really appreciate it. So again, thank you so much for watching this video and enjoy the cool music</p>



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/">How Your CPU Works: Fetch, Decode, Execute Explained</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-your-cpu-works-fetch-decode-execute-explained/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Demystifying x86-64 Program Segments: Stack, Heap, and More</title>
		<link>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/</link>
					<comments>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 04 Aug 2025 01:16:47 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[BSS section]]></category>
		<category><![CDATA[C++ programming]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[data section]]></category>
		<category><![CDATA[heap memory]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[program segments]]></category>
		<category><![CDATA[segmentation fault]]></category>
		<category><![CDATA[stack overflow]]></category>
		<category><![CDATA[virtual memory]]></category>
		<category><![CDATA[x86-64]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=195</guid>

					<description><![CDATA[<p>Understand x86-64 program segments like stack, heap, data, and BSS. Learn to avoid segmentation faults and stack overflows in this beginner-friendly coding tutorial.</p>
<p>The post <a href="https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/">Demystifying x86-64 Program Segments: Stack, Heap, and More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Demystifying x86-64 Program Segments: Stack, Heap, and More" width="1380" height="776" src="https://www.youtube.com/embed/GUicAuRPgPA?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>Join us for a clear, engaging look at x86-64 program segments! We break down the text, data, and BSS sections, explore stack vs. heap memory, and show a real stack overflow in C++. Learn why segmentation faults happen, how virtual memory works, and tips to avoid crashes. Whether you&#8217;re new to assembly or leveling up your coding skills, this video is packed with insights to boost your understanding of low-level programming. Subscribe for more coding deep dives!</p>



<p>Introduction to x86-64 program segments 00:00:01<br>Understanding segmentation faults 00:00:12<br>Importance of segments in assembly 00:00:32<br>Overview of assembly program structure 00:01:09<br>Defining segments in Yasm assembly 00:01:49<br>Data section: Initialized global variables 00:02:20<br>BSS section: Uninitialized variables 00:03:23<br>Text section: Code and functions 00:03:59<br>Global and extern function declarations 00:04:41<br>Stack: Local variables and return addresses 00:07:11<br>Stack overflow example in C++ 00:08:48<br>Heap: Dynamic memory allocation 00:11:31<br>Stack vs heap memory growth 00:11:56<br>Virtual memory and memory allocation 00:14:00<br>Demonstrating memory overflow with heap allocation 00:16:59<br>Summary of segments and their purposes 00:17:59<br>Closing remarks and call to subscribe 00:18:47</p>



<p>Thanks for watching!</p>



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



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



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p>Hi there!</p>



<p>Let&#8217;s talk about x86-64 programs and their segments and the purpose of the segments.</p>



<p>Probably when you&#8217;ve been programming in the past you&#8217;ve seen segmentation fault errors.</p>



<p>If you&#8217;ve accidentally crossed the bounds of some appropriate memory location, if you</p>



<p>have an array and you try to index it way outside of bounds or something, you might</p>



<p>have seen a segfault.</p>



<p>SegFaults basically come from the idea that you have crossed a segment boundary.</p>



<p>Segments used to be really, really important.</p>



<p>They&#8217;re a little bit less important now that we have virtual memory.</p>



<p>I&#8217;ll just talk a little bit about them.</p>



<p>I&#8217;m going to be discussing this from the perspective of an assembly program,</p>



<p>just because even in higher level languages,</p>



<p>your languages will typically be compiled down to assembly</p>



<p>first before they actually hit machine code and your actual executable binary.</p>



<p>So if you&#8217;re not interested in Yasm Assembly,</p>



<p>Yasm assembly I think this could still be worth it for you to watch because</p>



<p>you&#8217;ll be able to see the different segments and just kind of get a better</p>



<p>understanding of what they are okay so I&#8217;m gonna actually write a very very</p>



<p>very quick program in assembly it&#8217;s not gonna really do anything I just want you</p>



<p>to show I just want you to see the segments that are involved so for</p>



<p>starters let&#8217;s just pretend that I&#8217;m writing a an assembly program here we&#8217;ll</p>



<p>This is a comment just so you know what I&#8217;m about to type.</p>



<p>Perhaps actually this is not the best editor.</p>



<p>I don&#8217;t know why I pulled this up.</p>



<p>Let me get Gene here.</p>



<p>It&#8217;s a little bit better.</p>



<p>Gene.</p>



<p>There we go.</p>



<p>Maybe I&#8217;ll save it somewhere.</p>



<p>I&#8217;ll say segments assembly.</p>



<p>And then now the comment gets highlighted.</p>



<p>Okay.</p>



<p>So in assembly in the ASM assembly,</p>



<p>we denote various segments by using the section keywords.</p>



<p>keywords so we&#8217;ll say section and then dot something indicating the type of segment that we&#8217;re</p>



<p>defining so section text and then up here we&#8217;ll have another one section dot bss and you know if</p>



<p>you already program assembly you know that somewhere in here is where the code goes right</p>



<p>between these sections so just a quick recap of what the data section is it just is where you</p>



<p>can name and initialize at the same time at the very top.</p>



<p>Globals are not great, but sometimes you want to do that in assembly, depending.</p>



<p>It&#8217;s better if you stick all your variables on the stack.</p>



<p>Like if they&#8217;re a local variable, it&#8217;s a little bit better.</p>



<p>Think of higher level languages.</p>



<p>You typically want to avoid global variables if you can possibly avoid them and make</p>



<p>everything else a local variable or a member variable.</p>



<p>So that means somewhere else other than data, probably the stack, but or the heap.</p>



<p>Anyway, you know, this is like the basic pattern.</p>



<p>We&#8217;ll say, you know, my variable and then we&#8217;ll decide to say what data size it is.</p>



<p>In this case, it&#8217;s DB for one byte of data.</p>



<p>You could put a, I think DD in there or a DW for a word.</p>



<p>Sorry, sorry, DW would be for a word.</p>



<p>And then I think DD would be a double and then DQ would definitely be a quad word.</p>



<p>So here quad word is eight bytes.</p>



<p>And then I can sort of initialize it to some number.</p>



<p>some number and now I have a global variable so not like a huge deal the BSS section is basically</p>



<p>where you have uninitialized variables but there you can make huge amounts of variables you can</p>



<p>make an array of variables so if I say my array something like that I could put resource queue</p>



<p>to say I want the type of one item to be a quad word so you know eight bytes per item and then I</p>



<p>could just say I want you know a hundred thousand items or something and I&#8217;ll</p>



<p>double check that after I stop recording this video and correct it if I&#8217;m wrong</p>



<p>it&#8217;s been a while since I actually typed BSS here and then in the text section</p>



<p>this is where you put code and functions and things you typically want to have at</p>



<p>least one entry point somewhere you know if you want to make like if you want to</p>



<p>make this the entry point of your entire program you&#8217;ll probably do global main</p>



<p>main and then this assumes that you use the GCC libraries and just sort of return from the label</p>



<p>sort of makes it a function assuming you didn&#8217;t ruin any of the other data.</p>



<p>So those are the three sections that we can start off looking at the data section,</p>



<p>the BSS section, and the tech section. It&#8217;s important to note that besides marking functions</p>



<p>as global so that they&#8217;re accessible to other segments or other modules, we also mark</p>



<p>We also mark functions and labels that we would like to access that are in other modules.</p>



<p>So if we&#8217;re using the GCC libraries, we could say something like global printf,</p>



<p>and then right away we&#8217;re able to call the printf function.</p>



<p>Or say if you have another function in another module,</p>



<p>like a different source code file that&#8217;s compiled somewhere else,</p>



<p>we could say, you know, my other function.</p>



<p>Something like that.</p>



<p>Just mark it as global.</p>



<p>Or sorry, not global.</p>



<p>Extern.</p>



<p>Xtern. Global is when you have the function in the current module and you want it to be available to other modules.</p>



<p>Xtern is when the function or label is in another module and it&#8217;s already been made available to you,</p>



<p>but you want your current module to know that it can access it for the purposes of assembling the program.</p>



<p>So that means inside of here we could do something like call.</p>



<p>We could say, let&#8217;s call my other function.</p>



<p>my other function that will work if the other function is actually a function</p>



<p>with a return statement and it respects the ABI you know it pushes registers that</p>



<p>it&#8217;s going to modify if they&#8217;re callie saved if it&#8217;s not another function maybe</p>



<p>we&#8217;ll say extern my other jump point so like not a proper function but just</p>



<p>something that you might want to jump to then instead of using call we would use</p>



<p>jump, you know, and so forth. So those are the basic sections. Let&#8217;s see, for the BSS,</p>



<p>I definitely talked about token size and count. And then there&#8217;s resource B, resource W, resource</p>



<p>D, resource Q for the different sizes. So basically just take all the different sizes you could put</p>



<p>in the regular data section and assume that that could be popped on to the end of RES inside of</p>



<p>the BSS section. If you&#8217;re interested, this is a segments video, not an assembly video.</p>



<p>assembly video um another segment uh another segment that we&#8217;re not showing here in this</p>



<p>source code is the actual stack probably i guess the best way that i could show it is</p>



<p>i don&#8217;t know by messing around with the stack pointer increase uh</p>



<p>oh rsp why did i think it was the instruction pointer we could increase the stack pointer and</p>



<p>stack pointer and then decrease the stack pointer.</p>



<p>It doesn&#8217;t really help a whole lot.</p>



<p>But in another video, I&#8217;m gonna talk more in depth</p>



<p>about what the stack is and how to use it and such.</p>



<p>But just keep in mind that the stack</p>



<p>is where local variables are stored</p>



<p>and you can use it in assembly and C++</p>



<p>and other higher level languages.</p>



<p>You can even make virtual stacks</p>



<p>in any type of program almost</p>



<p>to just sort of have a stack like structure.</p>



<p>But in C++, if you make any local variable,</p>



<p>any local variable, then it&#8217;s usually going on the stack.</p>



<p>And the stack tends to have a fixed size.</p>



<p>And so if you sort of put too much data on the stack,</p>



<p>you risk a stack overflow, which will crash your program.</p>



<p>Besides local variables, the stack will hold other information that the current scope needs,</p>



<p>like its return address.</p>



<p>So when we make a function and then let&#8217;s say we call another function inside of it,</p>



<p>actually just up here even when we call another function on line 22 the stack will receive the</p>



<p>return address of where we currently are so that later when we return from the other function you</p>



<p>know the program knows where to jump back and forth it&#8217;s actually under the hood just jump</p>



<p>instructions when you do call it&#8217;s it&#8217;s not it&#8217;s not like a special thing it&#8217;s just jumping to</p>



<p>another location and then pushing the return address on the call stack so that&#8217;s other stuff</p>



<p>other stuff that the stack holds, which means if you have like an endless or like a infinite</p>



<p>recursion happening in your function where just a function calls itself over and over</p>



<p>and over again, or you have some complicated call graph and you end up calling too many</p>



<p>functions without ever returning, you could overflow the stack.</p>



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



<p>Let me show you a quick example that I&#8217;m going to make for you real fast.</p>



<p>Oh shoot.</p>



<p>Did I actually make this?</p>



<p>Where is the, it&#8217;s on the desktop.</p>



<p>Okay, I guess I&#8217;m going to make more on the desktop.</p>



<p>Let&#8217;s do a stack overflow.</p>



<p>Let me do desktop here.</p>



<p>Let&#8217;s do stack overflow.</p>



<p>That&#8217;s CBP.</p>



<p>I&#8217;m just going to make a quick program.</p>



<p>In C++, that doesn&#8217;t really do anything except overflow the stack.</p>



<p>So I just want you to know the stack can overflow.</p>



<p>I&#8217;ll make a function called F.</p>



<p>And F just calls itself.</p>



<p>calls itself. So that&#8217;s going to be an infinite recursion.</p>



<p>And that means, you know, every single time F calls itself,</p>



<p>the return address of like line five is going to get pushed onto the stack again.</p>



<p>So it&#8217;s just going to get pushed and pushed and pushed and pushed.</p>



<p>The stack will overflow faster if I had local variables because every scope that</p>



<p>gets called, even if it&#8217;s the same function name, it&#8217;ll have its own allocation on</p>



<p>the stack. So if I just sort of like call this from main and then I&#8217;ll just need</p>



<p>and then I&#8217;ll just need to make a little uh a little make file that just kind of compiles</p>



<p>I don&#8217;t really even need to make a make file I could do a build script but I&#8217;m going to do it</p>



<p>anyway we&#8217;ll say g plus plus standard equals c plus plus 23 show all warnings show pedantic</p>



<p>the input file will be stack overflow cpp and then the output file will be main</p>



<p>main and then assuming that succeeds I&#8217;ll just run main so this is like not a make</p>



<p>file video I have other videos for make files let&#8217;s see let me get to the</p>



<p>desktop if I run the make file with just the word make you should see a stack</p>



<p>overflow we should also see a warning about the fact that there&#8217;s infinite</p>



<p>That&#8217;s funny, I must have done something wrong or different because on a different computer</p>



<p>not too long ago I did the same thing and I saw a warning.</p>



<p>Okay, well thanks for not warning me.</p>



<p>But anyway, you can see that we have a segmentation fault here because the stack overflowed.</p>



<p>We just made a stack that was too huge.</p>



<p>This is a reminder that the stack is not necessarily as dynamic as the heap.</p>



<p>You can definitely overflow it.</p>



<p>It&#8217;s not really meant to store gigantic amounts of data.</p>



<p>What am I talking about when I say the heap though?</p>



<p>So just keep in mind that whenever you allocate dynamic data,</p>



<p>well, maybe, well, what do you think?</p>



<p>Maybe, maybe inside of, instead of calling F, let&#8217;s call G.</p>



<p>The heap is where you allocate dynamic data.</p>



<p>So anytime you use the malloc operator in C or the new allocator in C++,</p>



<p>you&#8217;re creating like a dynamic allocation of data.</p>



<p>I&#8217;ll do a new integer and then I&#8217;ll do like a thousand integers so this is</p>



<p>basically going to allocate a thousand new integers in the heap which is a</p>



<p>segment that is right up against the stack I should mention also that the</p>



<p>stack it&#8217;s important the stack its memory locations go down as the stack</p>



<p>grows keep that in mind because typically when you when you imagine a</p>



<p>stack I&#8217;ll make more videos in the future so you can visualize the stack better but most of you who</p>



<p>know what a stack is already you imagine that the stack grows upward visually right but in your</p>



<p>computer the memory locations of the stack grow downward so if I add something to the stack then</p>



<p>the new stack head pointer is actually going to be a lesser address and the reason for that is the</p>



<p>stack and the heap they grow in the same direction towards the same unallocated space we&#8217;ll call that</p>



<p>We&#8217;ll call that space the unallocated memory space or the unmapped memory space.</p>



<p>And it&#8217;s just, you know, a segment stack in the heap.</p>



<p>And then there&#8217;s the unallocated space that they grow towards each other.</p>



<p>If they ever meet, then your program is out of memory and it&#8217;ll crash.</p>



<p>But the heap actually can be dynamically allocated to store tons of RAM.</p>



<p>I&#8217;m probably about to crash this computer.</p>



<p>So maybe this video won&#8217;t finish recording.</p>



<p>recording so maybe I&#8217;ll try to crash this at the very end I just wanted you to</p>



<p>see let me let me do this I&#8217;ll say while true we&#8217;ll just allocate a new integer</p>



<p>this is kind of bad form because when you allocate you know a new variable</p>



<p>somewhere you want to have access to it after the allocation you would usually</p>



<p>send that into a pointer you&#8217;d say like you know integer pointer P equals new</p>



<p>in something like that right but in this case I don&#8217;t really care I&#8217;m not gonna</p>



<p>that memory i just want you to see how fast the memory can grow when we did the stack overflow</p>



<p>seemed like the program died pretty quickly but here we&#8217;re going to be able to see the memory</p>



<p>boom pretty pretty big before the whole computer just starts to you know fall to its knees</p>



<p>so i&#8217;m going to do a call to g here and then i won&#8217;t actually run this until</p>



<p>i&#8217;m ready to crash this whole video let&#8217;s see what else can i say</p>



<p>Oh, the reason that the heap can grow so much larger than the stack is our programs now in the modern era, they use virtual memory.</p>



<p>So it&#8217;s not like when we allocate, let&#8217;s say we&#8217;re accessing a memory location via a pointer inside of our program.</p>



<p>It&#8217;s not that the pointer has the absolute memory location through the whole system.</p>



<p>Like it doesn&#8217;t, it&#8217;s not able to name locations outside of the current program.</p>



<p>Every program sees its own start memory location as just zero.</p>



<p>And then under the hood, there are some extra tricks so that when you try to access a memory location,</p>



<p>let&#8217;s say memory location 100, because you&#8217;ve made, I don&#8217;t know, this many integers or whatever.</p>



<p>Then under the hood, the computer will know what is the start offset of the program&#8217;s memory.</p>



<p>So maybe the start offset is 1000.</p>



<p>It&#8217;s like totally unrealistic, but just pretend.</p>



<p>The start offset is 1000.</p>



<p>1000 then that means if it&#8217;s trying to access a pointer with memory location 100 it just gets added to the offset the virtual offset</p>



<p>So it&#8217;ll be a thousand and 100 is the real you know physical memory location or at least at that point</p>



<p>It&#8217;ll probably go to the paging system, but that&#8217;s that&#8217;s another video</p>



<p>So just know that it&#8217;s virtual which means the program thinks it has the full range of of the 64 bit memory address space</p>



<p>Even if you don&#8217;t even have that much memory on your computer, which there&#8217;s no way there&#8217;s no way you have so much memory</p>



<p>there&#8217;s no way you have so much memory that it&#8217;s going to overflow or even meet</p>



<p>64 bits at all but the program thinks it can so it&#8217;s just going to grow and grow</p>



<p>and grow until you actually run out of physical memory and then eventually</p>



<p>probably your program is going to start going to the page file or to the swap</p>



<p>file and then when you run out of that then your whole computer probably is</p>



<p>going to start crashing to be honest it&#8217;ll probably start crashing as soon as</p>



<p>it hits the swap file because at that point none of the other programs really</p>



<p>None of the other programs really have enough leftover memory to to breathe to do extra</p>



<p>allocations and things so everything&#8217;s going to start slowing down it&#8217;s going to be pretty bad</p>



<p>and well i think that&#8217;s all i really need to say this is not supposed to be a really long video</p>



<p>let me uh let me show you this crasher i&#8217;m going to say goodbye now thank you so much for watching</p>



<p>the video i hope you learned a little bit and had some fun too and and that way i&#8217;ve already said</p>



<p>if it actually crashes the computer. So I&#8217;m going to open up this and then I&#8217;m going to do maybe</p>



<p>HTOP so that I can see memory. So if you kind of like look at HTOP real fast,</p>



<p>this is just a Linux command line tool that you can install. It&#8217;s pretty cool.</p>



<p>What the heck&#8217;s going on here? Oh, that&#8217;s my recorder. You can see here&#8217;s the memory allocation.</p>



<p>So I&#8217;m using two gigs of eight gigs on this virtual machine and then I&#8217;m not using any swap</p>



<p>space which means I have not exhausted all my memory and and I have not been forced to go to</p>



<p>the disk for like additional memory but in a second we&#8217;ll see that will probably change</p>



<p>so I&#8217;m going to do make just to run that it looks like it&#8217;s stuck but if you keep looking at the</p>



<p>memory allocation oh it already hit eight gigs now it&#8217;s into the swap already probably</p>



<p>The out of memory killer, the OOM, successfully killed the program.</p>



<p>Thank goodness.</p>



<p>Or the whole computer would have been down.</p>



<p>I always forget that that&#8217;s going to happen lately.</p>



<p>But you can see it spiked, right?</p>



<p>It went right up to the full physical memory allocation of 7.75 gigs.</p>



<p>And then the swap file went all the way up to 2 gigs.</p>



<p>You can see that the swap file still has about 700 megs inside of it.</p>



<p>That just means that there&#8217;s memory that isn&#8217;t really being accessed.</p>



<p>the operating system doesn&#8217;t feel like it&#8217;s necessary to pull it back out of swap and put</p>



<p>it into RAM but I wonder if the video skipped when I did that or stuttered or anything I&#8217;m</p>



<p>interested to find out now okay so that&#8217;s the end of this video I hope you have a decent basic</p>



<p>understanding of segments and then you know like the segments and their purpose you know the heap</p>



<p>and the stack they grow together the heap can dynamically allocate to grow bigger notice how it</p>



<p>Whereas the stack will crash a lot faster.</p>



<p>If the stack and the heap meet, then you&#8217;ve ran out of memory.</p>



<p>At that point, you know, you probably want to like resize the heap or the stack just crashes.</p>



<p>And then we got the text section of the program and the data section and the BSS section of the program.</p>



<p>And these sections are a little bit less relevant in the modern era because we have, you know, virtual memory and stuff.</p>



<p>That definitely means you have tried to access outside of your segment and it&#8217;s a no go.</p>



<p>Okay.</p>



<p>Thank you for watching this video.</p>



<p>I will see you at a later date and time.</p>



<p>Hey everybody.</p>



<p>Thanks for watching this video again from the bottom of my heart.</p>



<p>I really appreciate it.</p>



<p>I do hope you did learn something and have some fun.</p>



<p>If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p>as you do on the current social media website</p>



<p>that you&#8217;re looking at right now,</p>



<p>it would really mean the world to me</p>



<p>and it&#8217;ll help make more videos</p>



<p>and grow this community.</p>



<p>So we&#8217;ll be able to do more videos,</p>



<p>longer videos, better videos,</p>



<p>or just I&#8217;ll be able to keep making videos in general.</p>



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



<p>You know, sometimes I&#8217;m sleeping</p>



<p>in the middle of the night</p>



<p>and I just wake up</p>



<p>because I know somebody subscribed or followed.</p>



<p>It just wakes me up and I get filled with joy.</p>



<p>That&#8217;s exactly what happens every single time.</p>



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



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



<p>Also, if you look at the middle of the screen right now, you should see a QR code which you can scan</p>



<p>in order to go to the website which I think is also named somewhere at the bottom of this video</p>



<p>and it&#8217;ll take you to my main website where you can just kind of like see</p>



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



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



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



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



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



<p>get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p>would really appreciate it so again thank you so much for watching this video and um enjoy the cool</p>



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/">Demystifying x86-64 Program Segments: Stack, Heap, and More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</title>
		<link>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/</link>
					<comments>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 27 Jul 2025 00:12:09 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[ABI]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[callee saved registers]]></category>
		<category><![CDATA[coding optimization]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[CPU registers]]></category>
		<category><![CDATA[general purpose registers]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[RAX register]]></category>
		<category><![CDATA[RBX register]]></category>
		<category><![CDATA[stack pointer]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=190</guid>

					<description><![CDATA[<p>Learn x86-64 assembly with this beginner’s guide to CPU registers. Master RAX, RBX, ABI rules, and optimize code. Perfect for low-level programming enthusiasts.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Mastering x86-64 Assembly: A Beginner&#039;s Guide to CPU Registers" width="1380" height="776" src="https://www.youtube.com/embed/jPysL_y4rRA?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>Ready to dive into x86-64 assembly? This beginner-friendly video breaks down CPU registers like RAX, RBX, and more in Yasm assembly. Learn how to use general-purpose registers, respect the ABI to avoid debugging nightmares, and optimize code by minimizing RAM hits. We’ll cover callee-saved registers, function arguments, and why you shouldn’t mess with the stack pointer. Perfect for coders looking to master low-level programming. Grab the free book mentioned and subscribe for more assembly tips! #AssemblyProgramming #x86</p>



<p>Introduction to Registers 00:00:00<br>x86-64 Yasm Assembly 00:00:04<br>Recommended Book 00:00:09<br>Move Instruction Example 00:01:04<br>64-bit Registers Overview 00:01:52<br>General Purpose Registers 00:02:24<br>Two&#8217;s Complement for Integers 00:03:37<br>Floating Point Registers Introduction 00:03:54<br>ABI Importance 00:04:55<br>Function Return Values 00:08:01<br>C++ to Assembly Translation 00:08:43<br>Avoiding System RAM 00:11:45<br>Optimizing with Registers 00:13:04<br>Callee Saved Registers 00:14:16<br>Preserving Registers with Push/Pop 00:16:40<br>Stack and Local Variables 00:22:00<br>Function Arguments in Registers 00:25:12<br>Stack Pointer and Base Pointer 00:31:29<br>Instruction Pointer (RIP) 00:36:30<br>Temporary Registers (R10, R11) 00:36:49<br>Accessing 32-bit Register Portions 00:38:29<br>Floating Point Registers Details 00:42:53<br>Conclusion and Call to Subscribe 00:45:35</p>



<p>Thanks for watching!</p>



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



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



<p>Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p>Hello there.</p>



<p>Let&#8217;s talk about registers in assembly.</p>



<p>More specifically, x8664 Yasm assembly.</p>



<p>Okay, so first off, check out this book.</p>



<p>It&#8217;s a wonderful book.</p>



<p>The author is a genius.</p>



<p>It&#8217;s called x8664 assembly language programming with Ubuntu.</p>



<p>It&#8217;s free.</p>



<p>website and you know grab your copy it&#8217;s wonderful it&#8217;s released under a copy left license</p>



<p>author&#8217;s a genius whenever i&#8217;m trying to remember things about cpu registers for assembly</p>



<p>i always go to this section i say callie saved i search for callie saved i already had it there</p>



<p>callie saved and it ends up being section 12.8.2 register usage so before we do that let me just</p>



<p>that let me just give you a little quick example so you kind of know what I&#8217;m</p>



<p>talking about here hopefully everybody&#8217;s seen this instruction by now this is a</p>



<p>move instruction in Yasm x86 64 assembly it just moves data from one place to</p>



<p>another it takes two operands so you know we have like operand one and</p>



<p>operand two basically operand one is the destination operand so we could put</p>



<p>destination there if we wanted to operand two is the source we could put</p>



<p>that actually won&#8217;t, you know, compile or assemble,</p>



<p>but I&#8217;m just saying that&#8217;s what those positions are for.</p>



<p>So here&#8217;s an example of a register receiving the value 10.</p>



<p>We use the move instruction and we say,</p>



<p>we would like to move something into the REX register.</p>



<p>We would like to move the value 10 into the REX register.</p>



<p>And there you have it. There&#8217;s a move instruction. Okay.</p>



<p>So REX is a register.</p>



<p>that it is a 64-bit register in our 64-bit CPUs every single register or</p>



<p>sorry every single general purpose register and the plot registers they</p>



<p>have 64 bits available for us to use 64 bits like you know ones and zeros it&#8217;s</p>



<p>like a very very very big number a long time ago we had 32 bit registers and so</p>



<p>these are like you know twice as mathematically robust or whatever you</p>



<p>allows the registers to address, you know, the 64 bit memory addressing space,</p>



<p>which helps us go beyond four gigabytes of RAM in our modern computers,</p>



<p>helps us go far beyond what we can even achieve right now.</p>



<p>So we have a register called RAX and there&#8217;s a list of other registers on this</p>



<p>page here. So basically, you know,</p>



<p>we have the RAX register and the RBX register and the RCX register and so</p>



<p>forth. I have a hard time remembering all these names,</p>



<p>remembering all these names so I go to this book when I forget but otherwise</p>



<p>you know you can kind of think of like ABCD and then just surround them with</p>



<p>RNX you know our kind of for register and X for extra big maybe so we&#8217;ve got</p>



<p>the RAX RBX RCX and RDX registers and then we have a bunch of other ones</p>



<p>through R15 but first before we do that let me just emphasize that these are</p>



<p>general purpose registers to be used for integers or data you can put</p>



<p>characters in there for strings you can put just like you know whatever data you</p>



<p>want inside the machine we use twos complement to represent integers so it&#8217;s</p>



<p>it&#8217;s fine the instructions that you will do for addition and other mathematical</p>



<p>but don&#8217;t put floating point numbers in these registers because the computer the</p>



<p>system the machine won&#8217;t know how to operate on that data the thing is</p>



<p>integers are stored as twos complement I&#8217;m going to talk about that more in</p>



<p>another video and floats are stored in something called I triple E seven five</p>



<p>four standard which is just like a crazy different way of storing floating point</p>



<p>numbers that makes more sense for floating point numbers but because the</p>



<p>differently in the machine you can&#8217;t really use normal instructions like you</p>



<p>can&#8217;t add two floats together the same with the same instruction that you would</p>



<p>add two integers together you would have to use special floating point registers</p>



<p>and special floating point instructions I&#8217;ll talk about floats at the end of</p>



<p>this video I hope but basically just keep in mind these are general purpose</p>



<p>registers to be used for integers in data only okay so what am I talking</p>



<p>Okay, so what am I talking about?</p>



<p>What are we seeing here on the side here?</p>



<p>Why does it say return value?</p>



<p>What is going on with Kali saved and fourth argument and third argument and so forth?</p>



<p>So there&#8217;s a standard that you&#8217;re supposed to use when you program.</p>



<p>You can get yourself into a lot of hot water or you can dramatically increase your debugging</p>



<p>time if you don&#8217;t do this.</p>



<p>standard called the ABI which I think is short for application binary interface but basically I just</p>



<p>like to say the ABI respect the ABI there&#8217;s a plan that people came up with that says this is how you</p>



<p>should use these registers don&#8217;t use them in a different way and that way if everybody is writing</p>



<p>you know modules and they&#8217;re all going to interact together like I write a module you write a module</p>



<p>the abi then it&#8217;s it&#8217;s pretty much guaranteed that we can we can expect certain things like</p>



<p>our registers won&#8217;t become corrupted after a function call or that we&#8217;re not going to corrupt</p>



<p>someone else&#8217;s uh function or you know whatever right so you can also use this to benefit yourself</p>



<p>even if you&#8217;re the only person who is writing your code and you&#8217;re not calling on anyone else&#8217;s</p>



<p>function um then you know like how are you going to remember like what was i using this register</p>



<p>Was I supposed to save that one?</p>



<p>Was I supposed to preserve it?</p>



<p>How was I going to return data from that function?</p>



<p>Was I going to use this register or the other register?</p>



<p>It&#8217;s hard to remember what you,</p>



<p>what you yourself even did like a month ago.</p>



<p>So if you respect this plan,</p>



<p>then your functions will work not only</p>



<p>with other people&#8217;s functions,</p>



<p>but with your own functions from a month ago.</p>



<p>The other thing too, is we have system calls, right?</p>



<p>So you have like a system call.</p>



<p>Let&#8217;s say we want to move something into REX.</p>



<p>something into REX, maybe I want to exit the system or something.</p>



<p>Just forget about this.</p>



<p>This is not a system call video.</p>



<p>Suppose I want to do a system call, right?</p>



<p>The system call itself is also going to respect the ABI.</p>



<p>So if you start calling other</p>



<p>services in the system call instruction, like if you want to open a file,</p>



<p>close a file, read a file, write a file, whatever.</p>



<p>If you don&#8217;t respect the ABI and the system call is respecting the ABI,</p>



<p>might have some of your data corrupted so it&#8217;s a really really good idea to respect the abi</p>



<p>in fact think about it this way what if uh what if you wrote a bunch of functions for a long long</p>



<p>time and then like a year later you couldn&#8217;t you couldn&#8217;t remember what you were doing you know</p>



<p>last year oh what was the return register uh and it costs you a bunch of extra time debugging</p>



<p>eventually you&#8217;ll probably decide to write your write your own standard okay from now on i&#8217;m always</p>



<p>going to use this register for return values and i&#8217;m going to use this register for the first</p>



<p>and i&#8217;m going to use this register for the first argument and i&#8217;m going to write you probably</p>



<p>come up with a plan right that&#8217;s basically the abi the abi covers more than just how to use</p>



<p>registers but um register usage is covered in the abi so why would you put yourself through</p>



<p>a bunch of heartache and then eventually come up with your own plan when you could have just</p>



<p>been following the correct plan in the first place right so everybody should respect the abi</p>



<p>I wanted to talk next about the return value.</p>



<p>Okay, so let me write up a little function here.</p>



<p>Let me just say that we have a C++ function.</p>



<p>And we&#8217;ll just say it&#8217;s like void f.</p>



<p>And we&#8217;ll say it calls on g.</p>



<p>And then we&#8217;ll have another function called g.</p>



<p>And actually, maybe it&#8217;s not void.</p>



<p>Maybe it actually does return a long.</p>



<p>returns the number five or something. Okay.</p>



<p>Hopefully you&#8217;ve seen some kind of a higher level language before like C++</p>



<p>so that you can understand what&#8217;s going on here.</p>



<p>We&#8217;re just making two functions and one is calling the other.</p>



<p>And the second one is just returning a value to the first one.</p>



<p>So in assembly, the equivalent of this would be,</p>



<p>let&#8217;s make a label with the name of the function and then a little colon,</p>



<p>and then just put a return instruction at the end of it. Wham,</p>



<p>it, wham, you&#8217;ve got a function. It&#8217;s not going to work very well, but you do have a function at</p>



<p>this point. So F, uh, I&#8217;m going to try to copy the C++, uh, function. So it&#8217;s a really good idea.</p>



<p>Whenever you&#8217;re writing a function in assembly, try to imagine what the prototype would be for</p>



<p>C++ and just put it as a comment at the top. So I know how the F function is going to behave in</p>



<p>my assembly module because I put a comment up there just kind of reminding me what the prototype</p>



<p>for C++. It doesn&#8217;t really do anything. It just sort of runs. So that&#8217;s okay. And then I&#8217;ll make</p>



<p>another function down here. I&#8217;ll do the long G again in a comment, and then I&#8217;ll do the actual</p>



<p>assembly version. I&#8217;ll say G colon, and then I&#8217;ll return. So just by putting that comment up,</p>



<p>I kind of can remind myself now that the G function is supposed to return some kind of a value.</p>



<p>up here, you know, do how about let&#8217;s say, I don&#8217;t want to start adding a bunch of local</p>



<p>variables right now.</p>



<p>Pretend that we&#8217;re going to print the return value of the call to G somehow we&#8217;re going</p>



<p>to use C out or whatever.</p>



<p>We&#8217;re going to send it to a variable, whatever.</p>



<p>I&#8217;m just going to try to keep the assembly as simple as possible.</p>



<p>But anyway, the point is G returns something.</p>



<p>you know, a value in your assembly function.</p>



<p>Well, remember that higher level languages,</p>



<p>part of why they&#8217;re so awesome</p>



<p>is they do a lot of extra work for us under the hood</p>



<p>and they actually provide illusions to us</p>



<p>that make programming easier.</p>



<p>For example, there are no functions</p>



<p>going on inside the actual machine.</p>



<p>I mean, I guess from a certain point of view there are,</p>



<p>but basically the machine is just sort of like moving data</p>



<p>and jumping to instructions</p>



<p>and jumping back from somewhere.</p>



<p>And, you know, it&#8217;s just sort of like jumping around</p>



<p>and executing instructions.</p>



<p>and executing instructions there&#8217;s no actual function it&#8217;s so it&#8217;s like if you want to pretend</p>



<p>that you have a function in assembly well there is a return instruction that will help you jump</p>



<p>back to wherever you came from most recently but we have to we have to implement more is what i&#8217;m</p>



<p>saying so how do we return a value in an assembly function we just have to load up the return value</p>



<p>it as return value that just means we have to load rax up with something so for example we wanted to</p>



<p>return the number five so i&#8217;m just going to load it up with the number five and then return</p>



<p>now we&#8217;ve pretty much translated a c plus plus function into uh an assembly function i mean</p>



<p>that&#8217;s really what&#8217;s happening under the hood when you compile your c plus plus program</p>



<p>it&#8217;s um it&#8217;s just you know translating all the c plus plus into assembly</p>



<p>Another thing that I should probably point out is that you should use registers as often as possible</p>



<p>and you should try your best not to touch memory.</p>



<p>Of course at some point you have to touch memory when you want to save your final result or send</p>



<p>it off somewhere or whatever but you know imagine that you have an assembly function and you&#8217;re</p>



<p>doing lots and lots of calculations you&#8217;re performing an algorithm or something your</p>



<p>program will be so much more efficient by like a factor of a hundred or probably more if you don&#8217;t</p>



<p>if you don&#8217;t hit system ram because every time you hit system ram like a global variable or the</p>



<p>stack or something then um your cpu you know typically in the uncashed uh scenario your cpu</p>



<p>has to go talk to the system bus on your motherboard and then you know send a message</p>



<p>to system ram and then wait for the system ram to figure out what it&#8217;s doing and then get a response</p>



<p>back so when your program is executing on the cpu you can encounter a stall which is like your</p>



<p>like a hundred clock cycles or more like it&#8217;s a long time at least in the most basic case</p>



<p>and how do you avoid that just use registers only when you&#8217;re doing lots of calculations the</p>



<p>registers are built into the cpu they&#8217;re part of the cpu&#8217;s hardware so they are lightning fast</p>



<p>compared to system ram or your disk or whatever</p>



<p>i probably say this a lot but you know part of the reason you would even want to code an assembly</p>



<p>Maybe you have a big program that does a lot of stuff and eventually you profile the program</p>



<p>and you realize, Hey, this one part of the program is really slow because it gets called</p>



<p>constantly and it&#8217;s not super efficient.</p>



<p>That might be a good use case for writing a hybrid program where you have multiple modules.</p>



<p>Some of your modules are in a higher level language and some of your modules are in assembly.</p>



<p>So you take your most important function that slows down your program that gets called all</p>



<p>like a big loop or something and you rewrite it to assembly so that you can</p>



<p>have more control over how often you touch system RAM to try to make the</p>



<p>whole thing more efficient and you know reduce the number of instructions and</p>



<p>just like whatever and you can improve the product of the efficiency of your</p>



<p>program so anyway you want to try to avoid hitting system RAM your registers</p>



<p>are built in the CPU there&#8217;s 64 bits that gives us 64 bits of address space</p>



<p>can reach you know back in the day we had 32 bit systems that means you could only have a memory</p>



<p>stick that was about four gigabytes so now we can go much further so um yeah that&#8217;s what&#8217;s going on</p>



<p>with our registers on the cpu back to the return value okay so we have return value here goes into</p>



<p>the rax register and then the next thing here is it&#8217;s saying that the rbx register is something</p>



<p>remember we said that uh we have to respect the abi right like you have to respect this convention</p>



<p>uh because if you don&#8217;t you&#8217;re going to cost yourself debug time and your</p>



<p>functions probably won&#8217;t be interoperable interoperable with other people&#8217;s functions</p>



<p>or the system call uh instruction well so one of the things about the abi is some of the registers</p>



<p>are designated as the function that is being called has to be the thing that saves the register&#8217;s</p>



<p>registers value so that it doesn&#8217;t corrupt for the caller. Imagine this if I had a function</p>



<p>called f I&#8217;m going to do like f and g here suppose the function f is going to move something into</p>



<p>let&#8217;s say r12 or actually let&#8217;s do rbx move the value 10 into rbx and then it&#8217;ll call g for some</p>



<p>reason maybe g doesn&#8217;t take any arguments maybe g is just kind of doing stuff let&#8217;s just say that</p>



<p>g just moves a different value into rbx so the thing is when the call comes back like after we</p>



<p>get to this next line let&#8217;s say we&#8217;re going to do something with do something do something with</p>



<p>rbx by the time we get to this line dude can i get the line numbers on this oh yeah i forgot okay</p>



<p>so by the time we get to line seven uh the register rbx is ruined these registers are not</p>



<p>local variables they&#8217;re not like uh you know tied to any scope or function call these registers are</p>



<p>just basically global variables that are sitting on the cpu for lightning fast performance but that</p>



<p>means the rbx i use inside of f is the same rbx i use inside of g so that means if g messes up the</p>



<p>value of rbx and then if f calls g then f now has a bad value for rbx this is a broken program</p>



<p>broken program because we did not respect the ABI.</p>



<p>The ABI says that RBX is callee saved,</p>



<p>which means if I use RBX in the G,</p>



<p>then I have to preserve the value so that by the time I return,</p>



<p>the value is the same as it was when the call first came in.</p>



<p>So how do we do that?</p>



<p>We&#8217;ll do that with a push pop pair.</p>



<p>I&#8217;ll probably make more videos in the future about pushing and popping,</p>



<p>but I&#8217;m going to try to keep it simple for now.</p>



<p>We just say, let&#8217;s push RBX onto the stack.</p>



<p>at the beginning and then we&#8217;ll pop it off the stack at the very end.</p>



<p>This basically means we&#8217;re going to take the value of RBX.</p>



<p>We&#8217;re going to send it onto the stack.</p>



<p>So, so yeah, we are hitting system RAM.</p>



<p>It&#8217;s a little slower at that point.</p>



<p>The ABI will save us a little time for the other registers.</p>



<p>I&#8217;ll try to explain that in a second, but basically we&#8217;re preserving the value here</p>



<p>with push and then we&#8217;re popping it back off.</p>



<p>So we&#8217;re restoring it.</p>



<p>So that means even though we ruined the value at line 14, when F does its line</p>



<p>When F does its line 7, it&#8217;s going to have the correct value of RBX.</p>



<p>It&#8217;s going to have the value 10.</p>



<p>The book and I like to call this the prologue, just meaning this is a little section of code</p>



<p>where we&#8217;re going to set things up.</p>



<p>We&#8217;re going to start getting ready to do more instructions.</p>



<p>And then this at the bottom is going to be called the epilogue.</p>



<p>This is just like the finale.</p>



<p>We&#8217;re like, we&#8217;re kind of cleaning up.</p>



<p>We&#8217;re finishing up right before we return.</p>



<p>If you wanted to return a value from G, then you could put, you know, the move inside of</p>



<p>RAX somewhere else.</p>



<p>Like you could put it like below the epilogue or just above the epilogue.</p>



<p>It&#8217;s fine.</p>



<p>As long as you&#8217;re sure that RAX is not getting trampled upon.</p>



<p>So it&#8217;s important to note also that if I do a, let&#8217;s say a system call right after that,</p>



<p>let&#8217;s say I call G and then I do a system call with, you know, like, you know, some</p>



<p>you know some other number as the call code i want to open a file i want to close a file</p>



<p>i want to read or write a file whatever i want to ask the system to do something for me the</p>



<p>system is also going to respect the abi which means i&#8217;m guaranteed that rbx is not modified</p>



<p>when the system call comes back if if the system call you know if the syscall instruction didn&#8217;t</p>



<p>respect the abi then my rbx could be ruined i&#8217;d have to do a bunch of stuff to preserve it so this</p>



<p>some of these registers let me go down a little bit here like this R10 the</p>



<p>temporary register and also the RDI if you decided to use that in the body of</p>



<p>your program which you&#8217;re allowed to these are not designated as callee saved</p>



<p>which means system call could actually ruin the value of those registers so</p>



<p>suppose for the sake of argument I really decided that I needed to use I</p>



<p>10 because that&#8217;s marked as a temporary I&#8217;ll put a value in our 10 and then I&#8217;ll say you know</p>



<p>do something</p>



<p>With our 10 I&#8217;m sorry our 10</p>



<p>Then that means by the time I&#8217;m done with my call to G</p>



<p>I should assume that our 10 is ruined because even though you&#8217;re you can see right here that that G doesn&#8217;t actually do anything</p>



<p>To our 10 we should assume that we have to respect the ABI which means the other function could have ruined it</p>



<p>Ruined it. This is especially true if you call someone else&#8217;s module or system call or whatever same thing for the system call</p>



<p>so if I call system call</p>



<p>and I use</p>



<p>R10 at some point before and then after then I have to assume R10 is destroyed by the time I get back in system call</p>



<p>So that&#8217;s no good</p>



<p>The cure to that when we&#8217;re talking about a temporary or something that is not designated as callee saved is that the caller has to save</p>



<p>hit a system RAM just to make a call.</p>



<p>Anything that we think we need when we&#8217;re finished.</p>



<p>So I&#8217;m going to do a push.</p>



<p>This is not the only way to do it, but I&#8217;m going to do a push R10 here.</p>



<p>And then afterwards, I&#8217;m going to do pop R10.</p>



<p>So that sucks.</p>



<p>And then I have to do the same thing for the system call.</p>



<p>I can go push R10 and then pop R10.</p>



<p>And of course, if you were clever, you probably would, you know, put the call to G inside</p>



<p>of that system call push pop pair.</p>



<p>So you could have one less push pop pair.</p>



<p>push pop pair but I&#8217;m just saying there&#8217;s no guarantee r10 is going to survive so you always</p>



<p>have to preserve it if you ever want to use it again so this is like another way that the abi</p>



<p>kind of can help you save time notice how here in the uh in the g function which I probably should</p>



<p>have prototyped let me just say it looks like to me it&#8217;s a void and it doesn&#8217;t take any arguments</p>



<p>to prototype your functions in comments.</p>



<p>Maybe let me do the same thing to F up here.</p>



<p>So F looks like it&#8217;s not returning anything and it&#8217;s not taking anything.</p>



<p>Okay. Whoops.</p>



<p>Those are C++ comments that would not compile.</p>



<p>All right. So notice how I&#8217;m using RBX.</p>



<p>So I decided to preserve RBX, but there&#8217;s also other registers that are marked as</p>



<p>marked as Kali saved like R12 through 15 and RBP and whatever,</p>



<p>but I&#8217;m not preserving those.</p>



<p>The reason I don&#8217;t have to preserve those is because I&#8217;m not using them.</p>



<p>So the, uh, the, uh, the ABI can save you a little time here.</p>



<p>Like for example, I&#8217;m preserving R10 because I&#8217;m supposed to assume that I,</p>



<p>that I, that they could be destroyed by the time the function comes back.</p>



<p>What if instead of using R10, I used R12.</p>



<p>have to surround any calls with a push pop pair because I can trust that whoever is going to</p>



<p>modify R12 will preserve it for me. But then notice down here G is not actually using R12.</p>



<p>So G doesn&#8217;t even do push pop on R12. So we save hits to memory. We don&#8217;t even have to touch memory</p>



<p>to preserve R12 because the ABI is helping us sort of stay a little bit more efficient.</p>



<p>So this is great, right? F is a broken function at this point though, because I&#8217;m using</p>



<p>at this point though because I&#8217;m using two registers that are callee saved I</p>



<p>can&#8217;t assume that whoever called F is gonna know that I&#8217;m modifying RBX and</p>



<p>R12 so I should do a prolog and an epilog up here I&#8217;m gonna say push RBX</p>



<p>and then I&#8217;m gonna push R12 and then down here at the bottom I&#8217;m gonna do an</p>



<p>epilog epilog and I&#8217;m gonna pop RBX and I&#8217;m gonna pop R12</p>



<p>but I&#8217;ve done something wrong.</p>



<p>The thing is, I&#8217;m not going to talk about the stack too much in this video,</p>



<p>but the stack is a particular type of data structure</p>



<p>that will return to you data in a reverse order than you sent into it.</p>



<p>This is great for helping us keep track of function calls and return addresses,</p>



<p>as I&#8217;ll probably try to explain in this video.</p>



<p>But basically, the data comes out backwards.</p>



<p>The data comes out backwards. So if I do it like this</p>



<p>Then actually by the time the return statement instruction gets executed</p>



<p>RBX will have the value that was intended for R12 and R12 will have the value that was intended for RBX</p>



<p>It&#8217;ll be backwards. So you have to do your push pops in the reverse order to notice how</p>



<p>It&#8217;s kind of like a shell that goes outwards. It&#8217;s like R12 is first on the inside and then RBX is next on the outside</p>



<p>That&#8217;s just what you have to do to preserve everything.</p>



<p>So the F function works now.</p>



<p>The G function, I think it works now.</p>



<p>We have a prologue and epilogue.</p>



<p>Okay, so we talked about the ABI.</p>



<p>We talked about calls.</p>



<p>We talked about the registers.</p>



<p>Talked about callee saved.</p>



<p>We talked about the fact that we don&#8217;t have to save the other ones.</p>



<p>I think I can talk about…</p>



<p>um so uh one thing that i should make sure to mention is that these are general purpose registers</p>



<p>they&#8217;re to be used for integers integer data or like characters or just like regular data</p>



<p>they use certain instructions that are not to be used with floating point data so you actually</p>



<p>should not store floating point numbers inside of your general purpose registers you should only</p>



<p>store floating points in special registers which i&#8217;ll talk about at the end of this video</p>



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



<p>Floating point numbers are stored differently in the system.</p>



<p>They&#8217;re stored with a scheme called IEEE 754 floating point.</p>



<p>So like the numbers wouldn&#8217;t make sense.</p>



<p>Integers and floats are stored differently.</p>



<p>So the hardware is wired differently to operate on two different types of data.</p>



<p>So anyway, I just want to say that this is only for integers and general purpose data.</p>



<p>So now let&#8217;s look at the other types of registers.</p>



<p>rc x and rd x and rsi and rdi are designated as arguments first second third and fourth</p>



<p>okay so let me uh let me write like a quick c plus function here and this is going to be uh let&#8217;s</p>



<p>say we have like a function that returns some value we&#8217;ll call it uh we&#8217;ll call it f and we&#8217;ll</p>



<p>say that f takes in some arguments let&#8217;s say that it takes in three arguments um again uh</p>



<p>integers right now because we&#8217;re only using integer registers.</p>



<p>We can&#8217;t use floating point arguments or return values.</p>



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



<p>So I&#8217;m just going to do like three arguments long A and B and C.</p>



<p>OK, so we can take in three arguments and maybe we&#8217;re just going to return,</p>



<p>you know, a plus B plus C.</p>



<p>OK, simple C++ function.</p>



<p>Let&#8217;s do this in assembly.</p>



<p>We&#8217;ll make a label and we&#8217;ll do a little comment that just reminds us.</p>



<p>of sorry of the</p>



<p>Function that we&#8217;re trying to implement we stick a little return instruction at the end of it so that</p>



<p>it will jump back to the caller and</p>



<p>then we&#8217;ll say</p>



<p>Well when the caller of this function</p>



<p>Called us and gave us a and B and C. How do we get a and B and C?</p>



<p>Well a is the first integer argument, so it&#8217;s just going to be the RDI register</p>



<p>So we&#8217;ll literally just use a move instruction</p>



<p>just use a move instruction.</p>



<p>Let&#8217;s say we want to, for whatever reason, use the R12 register</p>



<p>and we&#8217;ll do something with it later.</p>



<p>We&#8217;ll print it, we&#8217;ll save it, we&#8217;ll do whatever.</p>



<p>Let&#8217;s just grab the incoming argument.</p>



<p>So we&#8217;ll just grab RDI.</p>



<p>So I&#8217;m going to put a little comment up above here, you know, grab</p>



<p>a and do something</p>



<p>with it and I&#8217;ll just write down here more instructions just to just to denote</p>



<p>Just to denote that we&#8217;re doing something with the a value, but I&#8217;m not going to write it down here because I don&#8217;t want this to get huge.</p>



<p>So we&#8217;ll do the same thing with the b.</p>



<p>Maybe I should remember that programming is case sensitive.</p>



<p>We&#8217;ll do something with the b.</p>



<p>It&#8217;s not RDI.</p>



<p>It&#8217;s the second argument.</p>



<p>So that&#8217;s actually RSI.</p>



<p>And then we&#8217;ll do something else.</p>



<p>We could have used R13 or other registers if we wanted to.</p>



<p>I&#8217;m just showing you how to grab incoming arguments.</p>



<p>So then we&#8217;ll grab the c and do something with it also.</p>



<p>C and do something with it also it&#8217;s not going to be RDI it&#8217;s going to be RDX and you can do this</p>



<p>up to like I think six arguments let me just double check here yeah so like R9 ends up being</p>



<p>the sixth argument and if you want more than that then the caller will have to put stuff on the stack</p>



<p>and in fact if you if you kind of like understand what&#8217;s happening here let me just do like a main</p>



<p>Let&#8217;s say we do cout whatever f returns.</p>



<p>You know, this is like a typical function call, right?</p>



<p>So we&#8217;ll call f.</p>



<p>f will jump in there.</p>



<p>It&#8217;ll jump into its body.</p>



<p>It&#8217;ll do some sort of a manipulation with the incoming arguments.</p>



<p>It&#8217;ll return a final value.</p>



<p>Then that final value gets printed out.</p>



<p>So what&#8217;s 5 plus 6 plus 7?</p>



<p>That&#8217;s 11 plus 7.</p>



<p>What is that, like 18?</p>



<p>I&#8217;m bad at math.</p>



<p>So let&#8217;s just say that&#8217;s 18 gets printed.</p>



<p>So what happens is, um, we return, Oh, I forgot to return.</p>



<p>I&#8217;m going to say compute the, uh,</p>



<p>compute the sum of the things.</p>



<p>Uh, and I&#8217;ll just say like block cause I don&#8217;t want to put a bunch of addition</p>



<p>instructions here. Uh, and we&#8217;ll just like store in, uh,</p>



<p>in R13, let&#8217;s say. So then when we have finally computed our result, I&#8217;ll just move R13 into the</p>



<p>return value. And what I&#8217;m really trying to say here is if you imagine a function that behaves</p>



<p>this way in C++, then what I&#8217;ve written down below is really what&#8217;s actually happening in</p>



<p>assembly. Your compiler compiles the code down to assembly language first, and then it assembles it</p>



<p>down to machine code. So really this is what&#8217;s happening under the hood. It&#8217;s not like a special</p>



<p>under the hood it&#8217;s not like a special trick when you call a function in C++</p>



<p>literally the A B and C have been loaded up just before the the jump</p>



<p>instruction to go to that you know line 9 they&#8217;ve been loaded up with the</p>



<p>appropriate values so that&#8217;s part of like you know the magic of what higher</p>



<p>level languages give us it&#8217;s just makes things a little bit easier okay so I&#8217;ve</p>



<p>If I had only used RDI and RSI and RDX, those are not designated as callee saved.</p>



<p>So I wouldn&#8217;t have had to preserve those, but because I chose to use R12 and R13, I</p>



<p>have to preserve those for the caller, even if the caller is not going to use them.</p>



<p>So I&#8217;m going to do, I&#8217;m going to do push R12 and I&#8217;m going to do push R13.</p>



<p>And then at the bottom, let me do like a prologue here.</p>



<p>is kind of helpful because it kind of helps you remember like, oh, did I forget the prologue?</p>



<p>Did I forget the epilogue? They got a match. And then I guess the fact that you can see the words</p>



<p>prologue and epilogue also helps remind you that everything is supposed to be in reverse order</p>



<p>in the epilogue. So again, notice how R13 is on the inside and R12 is on the outside</p>



<p>of the push pop pair. So now my program is good. And we&#8217;ve, you know, successfully translated</p>



<p>We&#8217;ve, you know, successfully translated a nice function call there.</p>



<p>And we&#8217;ve done a bunch of nonsense in the middle that I&#8217;m not writing down at this point.</p>



<p>We are respecting the ABI.</p>



<p>So if somebody else uses our function in their module, then they can be pretty confident</p>



<p>that we&#8217;re not going to ruin their registers and so forth.</p>



<p>We talked about the return value.</p>



<p>We talked about that these general purpose registers are not for floats.</p>



<p>We talked about pushes and pops.</p>



<p>and pops. We&#8217;ve talked about function arguments,</p>



<p>which right now we can only do integer arguments and return values.</p>



<p>We&#8217;ve talked about, uh, well, he talked about the stack a little bit.</p>



<p>So let me, let me just point out that, um,</p>



<p>suppose you have a function. I just want to mention this briefly.</p>



<p>Suppose you have a function F let&#8217;s say it&#8217;s, it&#8217;s void.</p>



<p>So it doesn&#8217;t return anything and it has an integer a equals five.</p>



<p>I think the reason that I wanted to do this is to show you how dangerous it is to mess</p>



<p>with the wrong register like the stack register.</p>



<p>So suppose we have two variables and then we&#8217;ll do something with a and b.</p>



<p>So now suppose we have two threads.</p>



<p>We have an execution thread one and an execution thread two.</p>



<p>If you&#8217;re not familiar with threads, that just basically means your computer is literally</p>



<p>executing your function or sorry, it&#8217;s executing twice at the exact same time.</p>



<p>Maybe not the exact same time if you only have a single core on your CPU, but you could</p>



<p>imagine that that&#8217;s possible, especially if you have many cores, that&#8217;s an operating systems</p>



<p>video.</p>



<p>But basically pretend that we have an execution thread.</p>



<p>So it&#8217;s like going through your program, executing one instruction at a time.</p>



<p>Then you launch another execution thread that also executes your program one instruction</p>



<p>at a time.</p>



<p>at a time so at some point it&#8217;s possible that um thread one and thread two could be executing f</p>



<p>at the exact same time right so i&#8217;m not going to talk too much about this because this is not an</p>



<p>os video but basically this could create a race condition where thread one and thread two kind</p>



<p>of step on each other&#8217;s toes and they both try to modify the value of a uh at the wrong time and</p>



<p>because the other thread modified it.</p>



<p>So this is why local variables in a function actually live on the stack.</p>



<p>A and B are not global variables.</p>



<p>They&#8217;re actually temporary local variables that are sitting on the stack.</p>



<p>And the way the stack works, I&#8217;m not going to talk about it too much in this video,</p>



<p>is that, well, every function kind of has its own area of the stack.</p>



<p>So when the function comes in, on the stack sits the return address</p>



<p>return address so that the function knows where to jump back to when it returns and any local</p>



<p>variable that the function makes. So if you think about it, when thread one calls F, we actually</p>



<p>end up with a different version of A and a different version of B. We could imagine them</p>



<p>as being, you know, A sub one and B sub one. But when thread two comes in and, you know,</p>



<p>creates those local variables, we can imagine that we get two different versions of A and B also.</p>



<p>of a and b also so this is how the computer prevents multiple threads from stepping on each</p>



<p>other&#8217;s toes if you&#8217;re using local variables local variables are just local to the function</p>



<p>this also helps if a function calls itself a bunch of times maybe one call or another might want to</p>



<p>see a different version of a or b and it would be just incredibly difficult to keep track of that</p>



<p>if you were using globals but when you use locals well they&#8217;re just basically different variables</p>



<p>trying to lead up to is the stack is pretty important if you corrupt the</p>



<p>stack then you probably are going to crash the entire program you might make</p>



<p>it so that a function doesn&#8217;t know where to return from like when you do the</p>



<p>return statement it might jump to some other part of the code that doesn&#8217;t even</p>



<p>make sense and then everything crashes or you might mess up some data in a local</p>



<p>variable like if you mess up if you mess up the stack you might be messing up the</p>



<p>that the caller is depending on and so then the caller continues to execute and</p>



<p>it sees the wrong value and it just crashes or you might even mess up your</p>



<p>own local variables so don&#8217;t miss with the stack the reason I&#8217;m saying that now</p>



<p>is because this very this register right here the RBP sorry that&#8217;s not it the</p>



<p>RSP register that&#8217;s the stack pointer eventually you can learn how to</p>



<p>manipulate the stack pointer in order to create your own local variables that&#8217;s</p>



<p>variables that&#8217;s okay once you know what you&#8217;re doing but unless you know exactly what you&#8217;re</p>



<p>doing you probably shouldn&#8217;t touch the stack pointer especially you shouldn&#8217;t mess with it</p>



<p>right before you call a function or right before you return from your own function be very careful</p>



<p>with that the rbp uh register is similarly dangerous uh it won&#8217;t like automatically destroy</p>



<p>your program but it is uh it&#8217;s usually i mean it&#8217;s quite often used as sort of like a bookmark for</p>



<p>mark for where the stack pointer was pointing in other modules and other</p>



<p>functions.</p>



<p>So if you fail to preserve the base pointer,</p>



<p>the RBP and then you returned from your function,</p>



<p>then you might have actually messed up the stack pointer for the caller or some</p>



<p>other caller somewhere in like the ancestry of your call graph.</p>



<p>So be very careful. I mean,</p>



<p>I guess all callee preserved variables or registers you should be very careful,</p>



<p>careful about, but these are like kind of the two worst ones to forget about.</p>



<p>first ones to forget about.</p>



<p>There&#8217;s also another register that is not listed on this page called the</p>



<p>instruction pointer. It&#8217;s RIP.</p>



<p>It basically is a register that holds</p>



<p>the memory location of the next instruction to be executed.</p>



<p>So if you modify that, then you just told your program to go execute</p>



<p>in some random crazy place and probably everything is going to crash.</p>



<p>R10 and R11.</p>



<p>I can&#8217;t remember already if I explain this, but I&#8217;ll just say it again.</p>



<p>But I&#8217;ll just say it again, R10 and R11 are just temporary registers.</p>



<p>You don&#8217;t have to preserve them.</p>



<p>But the callee, if you call another function, they don&#8217;t have to preserve the registers either,</p>



<p>which means it can be really fast to use R10 and R12 if you&#8217;re kind of like already running out of registers.</p>



<p>If you don&#8217;t have to make a function call anywhere, so you know no one is going to ruin your R10 and R11,</p>



<p>then you can also use R10 and R11 to sort of like, you know,</p>



<p>reduce your hits to system RAM to speed up your program.</p>



<p>So R10 and R11, they should probably be used last.</p>



<p>R12, 13, 14, 15, I usually use those first.</p>



<p>Once I start running out of those, Kali saved.</p>



<p>Then I&#8217;ll start eating into the arguments.</p>



<p>I&#8217;ll say, you know, I&#8217;m going to start using RBX</p>



<p>and then I&#8217;m going to use RDI and RSI and whatever.</p>



<p>It&#8217;s okay if you use, you know, the argument registers.</p>



<p>as long as you&#8217;ve already done something with your incoming arguments or you don&#8217;t have any.</p>



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



<p>You&#8217;re allowed to use them.</p>



<p>I mean, this is all just a standard.</p>



<p>As long as you obey what the rule is, then you&#8217;ll be all right.</p>



<p>The rule is you got to callee save those.</p>



<p>The rule is that one&#8217;s just temporary.</p>



<p>The rule is this one&#8217;s an argument and so forth.</p>



<p>Okay.</p>



<p>locals and how we use these registers. Let me try to just show you one more</p>



<p>thing real fast. So I said before that the size of the REX register is 64 bits</p>



<p>because it takes up the whole available register bits in the CPU but there are</p>



<p>other versions of this register that we could use. What if you only wanted to use</p>



<p>32 bits in your register? What if you wanted to pack like two different 32</p>



<p>32-bit numbers in the same 64-bit register.</p>



<p>You could do that.</p>



<p>There are also older modules and instructions</p>



<p>that will operate only on 32-bits.</p>



<p>So how do you reference only 32-bits of a register?</p>



<p>Well, if I search for, oh shoot, what was it?</p>



<p>Is it EDI?</p>



<p>No, no.</p>



<p>Shoot.</p>



<p>It&#8217;s in here somewhere.</p>



<p>This is like a good lesson in actually preparing before you record a video.</p>



<p>So I&#8217;m going to do architecture overview, CPU registers.</p>



<p>Oh, it was at the beginning of this section.</p>



<p>I always skip down to 2.4, 12 point something, and I should have gone to 2.3.1.1.</p>



<p>So obviously I&#8217;m not going to remember all of this, but basically notice at the top,</p>



<p>we have the REX register, all 64 bits of it.</p>



<p>But if you wanted to access the lowest six, sorry, the lowest 32 bits, then in your code,</p>



<p>instead of putting REX, you put EAX.</p>



<p>So for example, you know, move REX, whoops, REX, let&#8217;s say the number five, but over</p>



<p>here we&#8217;re going to move EAX, the number five.</p>



<p>five uh here it&#8217;s going to set the whole 64 bits to just like a bunch of zeros and then there&#8217;s</p>



<p>going to be a few bits that help represent five the lowest bits so it&#8217;s going to erase like the</p>



<p>whole thing all 64 bits but if you use the eax instruction then only the lowest 32 bits will</p>



<p>actually be modified which means whatever data you had in the higher 60 the higher 32 bits of the</p>



<p>of the register itself they&#8217;re just going to stay there it&#8217;s going to be considered</p>



<p>point so sometimes if you&#8217;re going to work with something that&#8217;s going to be using only 32 bits</p>



<p>of your register you better make sure that the other bits are uh are cleared out if you intend</p>



<p>to use rax right after that and and vice versa so keep in mind that we have other forms of the same</p>



<p>register a designation but just to let the cpu know that we only want to touch 32 bits worth we</p>



<p>And this, this kind of table covers all of the registers,</p>



<p>REX, RBX, RCX, and so forth.</p>



<p>I think it doesn&#8217;t cover the instruction pointer</p>



<p>and something else in there, but you know, most of them.</p>



<p>However, you know, for my purposes,</p>



<p>I usually just use 64 bits</p>



<p>because I&#8217;m not like super advanced</p>



<p>unless I have to divide or do something.</p>



<p>Okay, so that&#8217;s the last thing I wanted to mention</p>



<p>about the general purpose registers.</p>



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



<p>Callie saved.</p>



<p>So the not sure if I said this before already, but basically the.</p>



<p>So just to emphasize the, uh, the RBP register is the base pointer register.</p>



<p>You&#8217;re probably going to mess up, uh, the caller because the caller often uses RBP as</p>



<p>a bookmark for the stack pointer.</p>



<p>mark for the stack pointer you definitely don&#8217;t want to mess with a stack pointer unless you</p>



<p>actually know what you&#8217;re doing in order to create local variables um there&#8217;s another variable</p>



<p>another register in here called rip which is the instruction pointer register so if you</p>



<p>if you mess that one up then the program is going to start executing in some crazy place that doesn&#8217;t</p>



<p>even make sense okay so now that we&#8217;ve talked about general purpose registers by the way i</p>



<p>called special purpose registers, not general purpose.</p>



<p>General purpose is like RAX, RBX, all these ones that you can just randomly use as long</p>



<p>as you respect the ABI.</p>



<p>But the base pointer and the stack pointer, those are kind of special.</p>



<p>And the RIP, the instruction pointer, that&#8217;s a special register.</p>



<p>You shouldn&#8217;t mess with those unless you really know what you&#8217;re doing or you&#8217;ll get yourself</p>



<p>in trouble.</p>



<p>But now that we&#8217;ve talked about those, let&#8217;s talk about the floating point registers just</p>



<p>real fast.</p>



<p>about using floats until a lot later but basically</p>



<p>i at least want to mention that we do have registers that will work with floating point</p>



<p>operations let me see there we go okay so it&#8217;s 18.2 floating point registers um essentially</p>



<p>we use xmm0 through xmm13 for floating point numbers there&#8217;s not going to be any</p>



<p>not going to be any crazy letters like RDI, RSI that you have to memorize. You can just use zero</p>



<p>through 15 and it&#8217;s totally fine. When you want to return a floating point number from a function,</p>



<p>you will load up XMM0 as the return value. You won&#8217;t even touch RAX. The arguments are the same.</p>



<p>They&#8217;re just kind of ordered. It&#8217;s like the first argument is going to be XMM0. The second argument</p>



<p>is going to be XMM1 and so forth all the way up to XMM15. If you need more than 15 arguments,</p>



<p>some sorcery with the stack or with some kind of system like the global variable or something</p>



<p>and also for floating point registers we use different instructions so what I really want</p>



<p>you to know because this is mostly about the regular registers is that if you have floating</p>



<p>point numbers and you want to multiply them or do some other operation on them the normal</p>



<p>instructions for regular registers won&#8217;t actually work you&#8217;ll have to look up a different set of</p>



<p>and i&#8217;m going to do this in another video some other time uh if you want to move uh data with</p>



<p>a floating point register you can&#8217;t just use the regular move instruction you got to use either</p>



<p>move ss or move sd um and and both operands have to be floats or actually i think the one on the</p>



<p>right can be memory but um move ss basically means a single piece of data single precision</p>



<p>So you can move like multiple pieces of data at a time.</p>



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



<p>Um, but you can move single precision floating points, which means there are 32 bit floating</p>



<p>points or you can move double precision floating points, which means they&#8217;re a full 64 bit</p>



<p>floating point number.</p>



<p>There&#8217;s also bigger registers, but I&#8217;m not going to talk about that.</p>



<p>Yeah.</p>



<p>Well, yeah.</p>



<p>Later processors 256.</p>



<p>I think mine has that, but I&#8217;m not going to talk about that.</p>



<p>I think mine are like YMM or something.</p>



<p>So yeah, with that, those are the basics of registers and some stuff to keep in mind.</p>



<p>In future videos, I&#8217;m going to talk more in depth about making functions and calling functions</p>



<p>and all that stuff, respecting the ABI.</p>



<p>But for now, here is your primer.</p>



<p>I hope it was useful on CPU registers.</p>



<p>Thank you for watching this video.</p>



<p>I&#8217;ll see you in the future.</p>



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



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



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



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



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



<p>mean the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able</p>



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



<p>So please do me a kindness and subscribe. You know sometimes I&#8217;m sleeping in the</p>



<p>middle of the night and I just wake up because I know somebody subscribed or</p>



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



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



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



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



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



<p>Which I think is also named somewhere at the bottom of this video</p>



<p>and it&#8217;ll take you to my main website where you can just kind of like see all the videos I published and</p>



<p>services and tutorials and things that I offer and all that good stuff and</p>



<p>If you have a suggestion for</p>



<p>Clarifications or errata or just future videos that you want to see</p>



<p>please leave a comment or if you just want to say, Hey, what&#8217;s up, what&#8217;s going on? You know,</p>



<p>just send me a comment, whatever. I also wake up for those in the middle of the night. I get,</p>



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



<p>really appreciate it. So again, thank you so much for watching this video and, um, enjoy the cool</p>



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



<p>Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/">Mastering x86-64 Assembly: A Beginner&#8217;s Guide to CPU Registers</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mastering-x86-64-assembly-a-beginners-guide-to-cpu-registers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
