<?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>x86 assembly Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/x86-assembly/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/x86-assembly/</link>
	<description></description>
	<lastBuildDate>Sun, 01 Mar 2026 02:14:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.3</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>x86 assembly Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/x86-assembly/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Mixed Function Arguments in x86-64 Assembly &#8211; Integers &#038; Floats Explained</title>
		<link>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/</link>
					<comments>https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 01 Mar 2026 02:14:39 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[abi calling convention]]></category>
		<category><![CDATA[assembly language]]></category>
		<category><![CDATA[Ed Jorgensen]]></category>
		<category><![CDATA[floating point registers]]></category>
		<category><![CDATA[function calling convention]]></category>
		<category><![CDATA[integer float arguments]]></category>
		<category><![CDATA[linux assembly]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[mixed arguments]]></category>
		<category><![CDATA[nasm]]></category>
		<category><![CDATA[rax return]]></category>
		<category><![CDATA[rdi rsi xmm0]]></category>
		<category><![CDATA[system v abi]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[x86 assembly]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[xmm registers]]></category>
		<category><![CDATA[xmm0 return]]></category>
		<category><![CDATA[yasm]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=310</guid>

					<description><![CDATA[<p>In the System V x86-64 ABI, integer and floating-point arguments use separate register assignment sequences. Integer arguments go into rdi, rsi, rdx, rcx, r8, r9 (then stack), while floating-point arguments use xmm0 through xmm7 independently. This means the first integer you see — even if it appears after several doubles — always goes into rdi, and the second float always goes into xmm1 regardless of how many integers came before it.</p>
<p>The post <a href="https://www.NeuralLantern.com/mixed-function-arguments-in-x86-64-assembly-integers-floats-explained/">Mixed Function Arguments in x86-64 Assembly &#8211; Integers &amp; Floats 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="Mixed Function Arguments in x86-64 Assembly - Integers &amp; Floats Explained" width="1380" height="776" src="https://www.youtube.com/embed/ZAY3srLRx8c?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Quick guide to passing mixed int + float arguments in x86-64 assembly (System V ABI). See why rdi can still be the first integer after several doubles, why xmm registers count separately, and how return values switch between rax and xmm0. Perfect for YASM/NASM programmers interfacing with C/C++.</p>



<p class="wp-block-paragraph">Intro to Mixed Arguments 00:00<br>Simple Integer-Only Functions 00:46<br>Adding Arguments and Return Values 01:01<br>Integer Arguments in RDI and RSI 01:30<br>Pointers Treated as Integers 02:06<br>Introducing Floating-Point Returns 02:31<br>Returning Double in XMM0 03:06<br>First Float Argument in XMM0 03:36<br>Float Registers Count Separately 04:03<br>Integer Register Order Explained 04:16<br>Separate Counting for Integers and Floats 05:21<br>RSI as First Integer After Float 05:38<br>Reference to Ed Jorgensen&#8217;s Book 07:00<br>Callee-Saved Registers Overview 07:56<br>Complex Mixed Argument Examples 09:48<br>Inserting Integer Among Floats 10:57<br>Skipping Float Registers on Integer 11:18<br>Calling C from Assembly Notes 11:56<br>Name Mangling Reminder 12:21<br>Closing Remarks and Thanks 12:30<br>Call to Subscribe and Support 13:04<br>Website and QR Code Mention 13:38<br>Final Thanks and Outro Music 14:19</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to talk to you a little bit about using functions with</p>



<p class="wp-block-paragraph">mixed arguments in a YASM x86-64 assembly language program. Although this</p>



<p class="wp-block-paragraph">video will probably still be useful to you if you&#8217;re using NASM or MASM because the</p>



<p class="wp-block-paragraph">mixed arguments and their order and how they work is governed by the ABI which is a system</p>



<p class="wp-block-paragraph">so what the heck am i talking about okay so the first thing that i want to show you is uh</p>



<p class="wp-block-paragraph">imagine we have a function here let&#8217;s say void f this function doesn&#8217;t take any arguments and it</p>



<p class="wp-block-paragraph">doesn&#8217;t return anything congratulations you&#8217;re done that was the easiest thing that you&#8217;ve ever</p>



<p class="wp-block-paragraph">done in your entire life but as soon as you start adding arguments to it you have to uh</p>



<p class="wp-block-paragraph">start understanding how to uh how to call registers and stuff so imagine we have like a</p>



<p class="wp-block-paragraph">a long a and a long b so we give it two arguments and maybe we want it to return a long</p>



<p class="wp-block-paragraph">this is kind of like level one of writing functions in assembly right we just have</p>



<p class="wp-block-paragraph">a bunch of integers and we realize okay um this long the return value is going to come to the</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do a comment up here, we&#8217;ll do rax, we&#8217;ll say the long is actually coming</p>



<p class="wp-block-paragraph">back to the user in rax and the long is coming into the function with rdi, the long a and</p>



<p class="wp-block-paragraph">the long b is coming into the function with rsi.</p>



<p class="wp-block-paragraph">So if you understand this then you can write a function in assembly where basically you</p>



<p class="wp-block-paragraph">two incoming integer arguments and then you return to the caller a long value using rax</p>



<p class="wp-block-paragraph">keep in mind also that if this second argument was a pointer long pointer then it would be the</p>



<p class="wp-block-paragraph">same thing rsi because pointers are integers they&#8217;re 64-bit unsigned integers even if the</p>



<p class="wp-block-paragraph">pointer was not to a long even if the pointer was to a double or a character or whatever it would</p>



<p class="wp-block-paragraph">pointers are integers. It starts getting a little bit more complicated when you want to mix between</p>



<p class="wp-block-paragraph">floats and numeric arguments. So what I&#8217;ve been saying so far, it&#8217;s all just integers.</p>



<p class="wp-block-paragraph">Even if we&#8217;re mixing pointers with regular longs, it&#8217;s just, you know, they&#8217;re all longs basically,</p>



<p class="wp-block-paragraph">right? So, but as soon as we start introducing floats, then things get a little bit more</p>



<p class="wp-block-paragraph">complicated. So for starters, what if we did a function that was, that returned a double?</p>



<p class="wp-block-paragraph">that returned a double. So let&#8217;s say we have a function G and it returns a double. Maybe it</p>



<p class="wp-block-paragraph">still takes in a long and a double pointer. If it returns a double, let&#8217;s see, is this still</p>



<p class="wp-block-paragraph">lining up? I don&#8217;t think it&#8217;s lining up anymore. There we go. If it&#8217;s returning a double, then</p>



<p class="wp-block-paragraph">that means we have to return to the caller with the XMM0 register. So XMM0 is always designated as</p>



<p class="wp-block-paragraph">the well the first float argument but also the float return value so keep in</p>



<p class="wp-block-paragraph">mind if I&#8217;m returning a double to the caller I&#8217;m not going to use our a X at</p>



<p class="wp-block-paragraph">all I&#8217;m just going to use XMM zero load it up with the return value and then</p>



<p class="wp-block-paragraph">return to the caller and that&#8217;s it if I wanted to do another function let&#8217;s say</p>



<p class="wp-block-paragraph">hmm how about H and then we&#8217;ll say I don&#8217;t know maybe the first argument is a</p>



<p class="wp-block-paragraph">argument is a float we call it a double a that means rdi is not going to be the argument that</p>



<p class="wp-block-paragraph">or the register that we look at to see our incoming double it has to be a float register</p>



<p class="wp-block-paragraph">xmm0 is the float register that we look at for the first argument the float registers are really</p>



<p class="wp-block-paragraph">easy you just kind of go in order like xmm0 is the first argument xmm1 is the next argument xmm2</p>



<p class="wp-block-paragraph">the general purpose uh integer registers they&#8217;re a little more complicated you have to remember</p>



<p class="wp-block-paragraph">their their labels uh you know like the letters have an ordering to it uh suppose for the sake</p>



<p class="wp-block-paragraph">of argument that we&#8217;re going to return a double from this also uh in that case let&#8217;s see did i</p>



<p class="wp-block-paragraph">just mess the formatting up again i think that&#8217;s okay um again we&#8217;re going to return xmm0</p>



<p class="wp-block-paragraph">notice how we&#8217;re returning xmm0 oh that&#8217;s why i spaced it forward notice how we are</p>



<p class="wp-block-paragraph">forward notice how we are returning xmm0 and we&#8217;re also taking xmm0 these float registers tend to be</p>



<p class="wp-block-paragraph">reused all over the place none of them are designated as callee saved within the abi</p>



<p class="wp-block-paragraph">so you always have to stash their values somewhere if you&#8217;re ever going to make a function call or</p>



<p class="wp-block-paragraph">anything or a system call the other thing to keep in mind is notice how rsi i still have rsi there</p>



<p class="wp-block-paragraph">for the second incoming argument you would imagine that rsi is supposed to be the second argument</p>



<p class="wp-block-paragraph">supposed to be the second argument even if it uh you know comes after a double but that&#8217;s not</p>



<p class="wp-block-paragraph">actually true because the the order i guess like the the ordering of the uh of the registers they</p>



<p class="wp-block-paragraph">only count against their own class like the float registers the float arguments they only count</p>



<p class="wp-block-paragraph">against the position of the float and the uh of the float registers and and and the general purpose</p>



<p class="wp-block-paragraph">they only count against themselves.</p>



<p class="wp-block-paragraph">What I&#8217;m trying to say is that RSI is the second integer argument,</p>



<p class="wp-block-paragraph">but if you look at the signature, we only have one integer argument, right?</p>



<p class="wp-block-paragraph">So RSI is not the second integer argument.</p>



<p class="wp-block-paragraph">It&#8217;s actually the first integer argument.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put an RDI there.</p>



<p class="wp-block-paragraph">You probably can infer that the second float argument should be XMM1 at this point, I hope.</p>



<p class="wp-block-paragraph">and that&#8217;s true if we just say that there&#8217;s going to be another double</p>



<p class="wp-block-paragraph">that this function takes then it&#8217;s going to be xmm1</p>



<p class="wp-block-paragraph">not xmm2 because if you&#8217;re just counting the arguments</p>



<p class="wp-block-paragraph">and you&#8217;re sort of grouping the integers with the floats together</p>



<p class="wp-block-paragraph">you might think well first argument is xmm0 second argument would have been xmm1</p>



<p class="wp-block-paragraph">third argument would have been xmm2 therefore if I see</p>



<p class="wp-block-paragraph">a float coming into the third argument position it&#8217;ll be xmm2</p>



<p class="wp-block-paragraph">No, the integers don&#8217;t count against the floats and the floats don&#8217;t count against the integers.</p>



<p class="wp-block-paragraph">That&#8217;s why RDI is the first argument for integers, even though it&#8217;s in the second position,</p>



<p class="wp-block-paragraph">it&#8217;s still the first integer that we see.</p>



<p class="wp-block-paragraph">And XMM1 is the second float argument that we see.</p>



<p class="wp-block-paragraph">That&#8217;s why it&#8217;s XMM1 instead of XMM2, because we&#8217;re looking at it in the third position.</p>



<p class="wp-block-paragraph">So you&#8217;re not really looking at, whoops, you&#8217;re not looking at overall position.</p>



<p class="wp-block-paragraph">at position only counting that type of register let me pull open my favorite book to get a little</p>



<p class="wp-block-paragraph">bit more clarity on this um this book is written by a wonderful professor dr ed jorgensen phd</p>



<p class="wp-block-paragraph">this book i did not write it ed jorgensen wrote this book it&#8217;s called x86 64 assembly language</p>



<p class="wp-block-paragraph">programming with ubuntu this book can turn you into an expert with yasm assembly so uh</p>



<p class="wp-block-paragraph">I recommend everybody grab a copy. It&#8217;s also free and it&#8217;s got a copy left license.</p>



<p class="wp-block-paragraph">You can see like the license here. But anyway, I wanted to open up this book real fast</p>



<p class="wp-block-paragraph">just to show you the registers. Okay. I&#8217;m going to search for Kali saved so I don&#8217;t have to fumble</p>



<p class="wp-block-paragraph">around the book too long. Kali saved and that brings us to section 12.8.2 register usage.</p>



<p class="wp-block-paragraph">let&#8217;s see callee saved you&#8217;ll see rbx is callee saved rbp is callee saved probably shouldn&#8217;t</p>



<p class="wp-block-paragraph">mess with that one too much r12 13 14 15 all callee saved there&#8217;s a couple temporary registers</p>



<p class="wp-block-paragraph">but if you look carefully rdi is designated as the first argument but it should say really</p>



<p class="wp-block-paragraph">the first integer argument so rdi is the first integer argument that we see that&#8217;s why</p>



<p class="wp-block-paragraph">argument that we see that&#8217;s why in all of these examples the first integer argument that we</p>



<p class="wp-block-paragraph">actually see is rdi so like for function f long a that was the first integer argument</p>



<p class="wp-block-paragraph">and then in the function g it&#8217;s also the first integer argument and then in function h rdi is</p>



<p class="wp-block-paragraph">the double pointer b because that is the first integer argument that we actually see</p>



<p class="wp-block-paragraph">so keep that in mind you would just repeat that pattern you know the second integer argument you</p>



<p class="wp-block-paragraph">second integer argument you see that&#8217;s RSI. That&#8217;s why RSI is the pointer here because</p>



<p class="wp-block-paragraph">pointers are integers and the pointer here and we don&#8217;t have anything else but if I guess if we</p>



<p class="wp-block-paragraph">added another you know let&#8217;s say a long D then it would be RSI right there. Hopefully that makes</p>



<p class="wp-block-paragraph">sense and you can you can carry this logic forward for the first argument the second argument the</p>



<p class="wp-block-paragraph">and then R8 for the fifth argument R9 for the sixth argument and then you can</p>



<p class="wp-block-paragraph">look for further arguments on the stack if you need more than that but basically</p>



<p class="wp-block-paragraph">that&#8217;s how you count them up and then the float registers are just a lot</p>



<p class="wp-block-paragraph">easier they always start at zero and they go all the way up to 15 and so I</p>



<p class="wp-block-paragraph">don&#8217;t know maybe I should just copy paste well maybe I&#8217;ll start it from</p>



<p class="wp-block-paragraph">scratch we&#8217;ll say dub no let&#8217;s let&#8217;s mix it up a little bit more we&#8217;ll say long</p>



<p class="wp-block-paragraph">And the long is going to be returned with the RAX.</p>



<p class="wp-block-paragraph">And I guess I have to space this forward a little bit.</p>



<p class="wp-block-paragraph">And then if we have a bunch of floats, say double A.</p>



<p class="wp-block-paragraph">Can I maybe just copy paste this a little bit so I don&#8217;t have to do so much typing?</p>



<p class="wp-block-paragraph">Okay, so we&#8217;ll do double A, double B, double C, double D, double E.</p>



<p class="wp-block-paragraph">and then this is going to be xmm zero why did i put the extra comment there i don&#8217;t know</p>



<p class="wp-block-paragraph">and then we&#8217;re going to have xmm one and then we&#8217;re going to have xmm two xmm three and so forth</p>



<p class="wp-block-paragraph">and just to make sure you&#8217;re paying attention ask yourself this real fast if i were to insert</p>



<p class="wp-block-paragraph">z what register would we use for that would it be the one two three four five the fifth argument</p>



<p class="wp-block-paragraph">would it be uh would it be r8 or would it be something else hopefully you understand now</p>



<p class="wp-block-paragraph">let&#8217;s say we do a character pointer that it would be rdi because that&#8217;s the first integer argument</p>



<p class="wp-block-paragraph">that we actually see all right and then you know to return we would return in rax and then the</p>



<p class="wp-block-paragraph">return in RAX and then the floats just keep counting notice how it skipped from XMM2 or it</p>



<p class="wp-block-paragraph">went from XMM2 to XMM3 even though we crossed over an integer argument in between because the</p>



<p class="wp-block-paragraph">float arguments don&#8217;t count against the integers and the ints don&#8217;t count against the floats</p>



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



<p class="wp-block-paragraph">well if you want you can look at section 18.2 which just sort of talks about the floating</p>



<p class="wp-block-paragraph">which just sort of talks about the floating point registers we have 0 through 15 but I don&#8217;t think</p>



<p class="wp-block-paragraph">we really need to do that here I think we pretty much have everything we need to know to to call</p>



<p class="wp-block-paragraph">on functions with mixed arguments and to have a function within assembly that supports mixed</p>



<p class="wp-block-paragraph">arguments so again if your C or C++ modules are expecting some kind of signature like this now you</p>



<p class="wp-block-paragraph">know how to calculate what registers they&#8217;re actually going to populate by the time execution</p>



<p class="wp-block-paragraph">execution jumps into your assembly function.</p>



<p class="wp-block-paragraph">And if you want to call a C or a C++ function from assembly,</p>



<p class="wp-block-paragraph">now you know which registers to populate so that the the other function that</p>



<p class="wp-block-paragraph">you&#8217;re calling can receive the right data from you.</p>



<p class="wp-block-paragraph">Don&#8217;t forget about name mangling, which I talked about in another video.</p>



<p class="wp-block-paragraph">Okay, I think that&#8217;s all I really have to say.</p>



<p class="wp-block-paragraph">This video was pretty short.</p>



<p class="wp-block-paragraph">Thank you so much for watching.</p>



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



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">make more videos and grow this community so we&#8217;ll be able to do more videos longer videos better</p>



<p class="wp-block-paragraph">videos or just i&#8217;ll be able to keep making videos in general so please do do me a kindness and uh</p>



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



<p class="wp-block-paragraph">i know somebody subscribed or followed it just wakes me up and i get filled with joy that&#8217;s</p>



<p class="wp-block-paragraph">exactly what happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you could troll me if you want to just wake me up in the middle of the night just subscribe</p>



<p class="wp-block-paragraph">and then I&#8217;ll just wake up. I promise that&#8217;s what will happen. Also, if you look at the middle of</p>



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



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video. And it&#8217;ll take you to my</p>



<p class="wp-block-paragraph">main website where you can just kind of like see all the videos I published and the services and</p>



<p class="wp-block-paragraph">tutorials and things that I offer and all that good stuff. And if you have a suggestion for</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">if you just want to say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also</p>



<p class="wp-block-paragraph">wake up for those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would</p>



<p class="wp-block-paragraph">really it really mean the world to me i would really appreciate it so again thank you so much</p>



<p class="wp-block-paragraph">for watching this video and um enjoy the cool music as as i fade into the darkness which is</p>



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



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

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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.</p>



<p class="wp-block-paragraph">Highly recommended companion resource: Professor Ed Jorgensen&#8217;s free x86-64 Assembly Language Programming book (linked in comments).</p>



<p class="wp-block-paragraph">Enjoy learning assembly!</p>



<p class="wp-block-paragraph">Intro to Conditional Branching in Assembly 00:00:00<br>Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23<br>Unconditional vs Conditional Jumps 00:01:39<br>How Conditional Branching Works 00:02:06<br>The CMP Instruction Explained 00:03:21<br>Overview of Conditional Jump Instructions 00:04:50<br>Signed vs Unsigned Jump Instructions 00:06:10<br>Jump Equal &amp; Jump Not Equal (JE/JNE) 00:07:00<br>Main Conditional Jumps Summary 00:08:14<br>Starting the Code Example 00:09:09<br>Program Structure &amp; Printing Setup 00:10:35<br>Function Prologue &amp; Getting User Input 00:13:21<br>First Branch: Is Number Greater Than 0? 00:15:17<br>Live Demo: Testing Greater Than Zero 00:21:53<br>Second Branch: Is Number Less Than 10? 00:22:52<br>Live Demo: Testing Range 0-10 00:26:06<br>Third Branch: Is Number == 5? 00:26:52<br>Live Demo: Testing Equal to 5 00:28:00<br>Jump Out of Range Error &amp; Workaround 00:29:00<br>Final Summary &amp; Closing 00:32:17<br>Thanks &amp; Call to Subscribe 00:32:57</p>



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



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



<p class="wp-block-paragraph">Hey there. In this video,</p>



<p class="wp-block-paragraph">we&#8217;re going to talk about conditional branching in an assembly program.</p>



<p class="wp-block-paragraph">We&#8217;re going to be looking at YASM x86-64 assembly or AMD 64 assembly</p>



<p class="wp-block-paragraph">within Ubuntu, but this will probably be a useful video.</p>



<p class="wp-block-paragraph">If you&#8217;re just thinking about how to branch in assembly in general.</p>



<p class="wp-block-paragraph">So for starters, I&#8217;m going to promote one of my favorite books,</p>



<p class="wp-block-paragraph">as I often do. This is a book that is free and open source. You can get a copy yourself. I did</p>



<p class="wp-block-paragraph">not write this. The author of this book is a genius. His name is Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">He made this whole book and released it under an open source license. So you can literally just go</p>



<p class="wp-block-paragraph">to his website and get a copy of this for free. You can use this to make yourself into an assembly</p>



<p class="wp-block-paragraph">expert. So I highly recommend this to everybody. Anyway, so I&#8217;m going to open up this book. I&#8217;m</p>



<p class="wp-block-paragraph">up this book i&#8217;m going to assume that maybe you&#8217;ll get a copy or you&#8217;re just going to follow along</p>



<p class="wp-block-paragraph">with what i&#8217;m doing on the screen but i&#8217;m going to open this book and i&#8217;m going to go to section 7</p>



<p class="wp-block-paragraph">which is entitled instruction set overview i&#8217;m going to double click it so open out open the</p>



<p class="wp-block-paragraph">subsections and then i&#8217;m going to click on 7.7 control control instructions and then under that</p>



<p class="wp-block-paragraph">i&#8217;m going to click on 7.7.3 conditional control instructions so uh that&#8217;s a lot of subsections</p>



<p class="wp-block-paragraph">So that&#8217;s a lot of subsections, isn&#8217;t it? I love a book with a ton of subsections. It makes things so much easier to find</p>



<p class="wp-block-paragraph">If you or someone you know is going to write a book, especially if it&#8217;s technical, please use lots and lots of subsections</p>



<p class="wp-block-paragraph">Anyway, so conditional control instructions in my last video</p>



<p class="wp-block-paragraph">We talked about the jump instruction where we can just be sitting somewhere in our program and then just jump somewhere else</p>



<p class="wp-block-paragraph">Anywhere we want based on the label so conditional branching or conditional control instructions</p>



<p class="wp-block-paragraph">conditional control instructions are kind of the same thing except they will</p>



<p class="wp-block-paragraph">decide whether or not to actually jump they might fall through to the next</p>



<p class="wp-block-paragraph">instruction or they might jump away to somewhere else so conditional branching</p>



<p class="wp-block-paragraph">or conditional jumping it basically kind of goes like this step one you compare</p>



<p class="wp-block-paragraph">two values to each other they will probably be sitting in registers and then</p>



<p class="wp-block-paragraph">conditional branching instruction or a conditional jump instruction there&#8217;s</p>



<p class="wp-block-paragraph">lots of synonyms for these but basically in the regular jump instruction that I</p>



<p class="wp-block-paragraph">just talked about in the last video you just jumped and it was only one thing</p>



<p class="wp-block-paragraph">that you had to do but in this case we first have to compare two items and then</p>



<p class="wp-block-paragraph">we&#8217;ll issue the jumping instruction because the jumping instruction will</p>



<p class="wp-block-paragraph">have to decide whether to jump or not based on the result of your comparison</p>



<p class="wp-block-paragraph">which makes sense right like if you&#8217;re going to implement something that&#8217;s like</p>



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



<p class="wp-block-paragraph">before we jump either into the block of the if or if we jump into the block of the else is whether</p>



<p class="wp-block-paragraph">or not that expression is true right so that&#8217;s kind of what the compare instruction is doing</p>



<p class="wp-block-paragraph">sort of in higher level languages like c++ it can get a lot more complicated than that but you know</p>



<p class="wp-block-paragraph">Okay, so we&#8217;re going to do conditional branching instructions.</p>



<p class="wp-block-paragraph">Let me show you a few of those.</p>



<p class="wp-block-paragraph">Okay, so for starters, here&#8217;s the compare instruction right here on page 126.</p>



<p class="wp-block-paragraph">I think it&#8217;s actually 127 in the PDF, but basically, you know, 126.</p>



<p class="wp-block-paragraph">We do CMP followed by two operands.</p>



<p class="wp-block-paragraph">So if we want to compare, I don&#8217;t know, let&#8217;s say R12 with R13, that&#8217;s how you would do it.</p>



<p class="wp-block-paragraph">just a two operand instruction there&#8217;s no results that are going to get stored in one of these</p>



<p class="wp-block-paragraph">operands both of these operands are just for input the result actually gets stored in a special</p>



<p class="wp-block-paragraph">register called r flags which we hardly ever use or look at in in the most common assembly</p>



<p class="wp-block-paragraph">instructions and design patterns we just kind of like let the r flags do whatever it&#8217;s going to do</p>



<p class="wp-block-paragraph">and the compare instruction will fill that up so imagine that you know depending on whether r13 is</p>



<p class="wp-block-paragraph">is like greater than r12 or less than or they&#8217;re equal or something else is going on between the</p>



<p class="wp-block-paragraph">two of them the compare instruction just fills up all sorts of flags defining the relationship</p>



<p class="wp-block-paragraph">between those two registers or those two operands r12 and r13 and then after the r flags register</p>



<p class="wp-block-paragraph">is all set up then the conditional branching instruction will sort of rely on r flags in</p>



<p class="wp-block-paragraph">order to decide whether or not it&#8217;s going to jump or not okay so let&#8217;s see there are tons</p>



<p class="wp-block-paragraph">there are tons of conditional well not tons i guess like half a page worth but there are lots</p>



<p class="wp-block-paragraph">of conditional branching instructions we can use the first two i want to show you and after this</p>



<p class="wp-block-paragraph">by the way i&#8217;m going to write a sample program just to show you how it is in practice but so the</p>



<p class="wp-block-paragraph">first one is je which means let&#8217;s jump if the two operands were equal so again you know we compare</p>



<p class="wp-block-paragraph">here we&#8217;ll say sets up r flags and then we&#8217;ll decide like what we&#8217;re going to do are we going</p>



<p class="wp-block-paragraph">to jump if they&#8217;re equal to some label i&#8217;ll write some label here so that means uh the je instruction</p>



<p class="wp-block-paragraph">will look at the r flags register and decide did compare think that r12 and r13 were equal if it</p>



<p class="wp-block-paragraph">did then we&#8217;ll jump if not execution will fall down to the next line so i&#8217;ll put a nope instruction</p>



<p class="wp-block-paragraph">So if they were equal, then we&#8217;ll end up jumping to some label.</p>



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



<p class="wp-block-paragraph">And same thing for jump not equal and all these other ones.</p>



<p class="wp-block-paragraph">But there are actually two different versions of some of these instructions that kind of seem the same.</p>



<p class="wp-block-paragraph">Like for instance, this one right here, JL, that means jump if the first operand was less than the second operand.</p>



<p class="wp-block-paragraph">is a jump below that jumps also if the first operand was less than the second operand so</p>



<p class="wp-block-paragraph">what&#8217;s the difference between these two let me get my pen I love excuses to do my little pen now</p>



<p class="wp-block-paragraph">well I&#8217;ve got my pen it&#8217;s working oh I erased it okay okay it&#8217;s working anyway so notice how jump</p>



<p class="wp-block-paragraph">less than and jump below are basically doing the same thing but there&#8217;s one difference one of them</p>



<p class="wp-block-paragraph">One of them works on signed operands and one of them works on unsigned operands.</p>



<p class="wp-block-paragraph">So keep this in mind.</p>



<p class="wp-block-paragraph">If you&#8217;re comparing two signed integers, you want to use this group up here of instructions.</p>



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



<p class="wp-block-paragraph">And you definitely want to know what kind of data you have that you&#8217;re comparing.</p>



<p class="wp-block-paragraph">Notice though that jump equal and jump not equal, they don&#8217;t really care.</p>



<p class="wp-block-paragraph">not equal they don&#8217;t really care if the operands are signed or unsigned that&#8217;s because if you want</p>



<p class="wp-block-paragraph">to check to see if two things are equal you just look at all of their bits and if all of their</p>



<p class="wp-block-paragraph">bits are the same then you just say they&#8217;re equal you don&#8217;t even care actually if it&#8217;s a signed or</p>



<p class="wp-block-paragraph">unsigned integer if it&#8217;s a float if it&#8217;s a whatever you just you just go are all the bits equal or are</p>



<p class="wp-block-paragraph">at least one of the bits unequal then we&#8217;ll say it&#8217;s equal or not equal that&#8217;s it so keep that in</p>



<p class="wp-block-paragraph">mind for all of your jump instructions where you&#8217;re trying to see if things are equal or not</p>



<p class="wp-block-paragraph">or not equal you&#8217;re going to definitely be using these whether you&#8217;re using signed or unsigned</p>



<p class="wp-block-paragraph">integers or floats or anything else like that but when it comes to actually you know integers that</p>



<p class="wp-block-paragraph">are signed or unsigned these are the instructions you use for signed and these are the instructions</p>



<p class="wp-block-paragraph">whoops that you use for what have i done that you&#8217;ll use for unsigned i keep confusing the on</p>



<p class="wp-block-paragraph">off button for the red button is what i&#8217;m doing oh okay whatever i guess we&#8217;re done anyway</p>



<p class="wp-block-paragraph">I guess we&#8217;re done anyway.</p>



<p class="wp-block-paragraph">So keep that in mind.</p>



<p class="wp-block-paragraph">In this video, we&#8217;re just going to be using the signed integer instructions</p>



<p class="wp-block-paragraph">because that&#8217;s the example that I have set up.</p>



<p class="wp-block-paragraph">But just imagine you could very easily translate this to unsigned integers or floats.</p>



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



<p class="wp-block-paragraph">We&#8217;ve got jump less than, we&#8217;ve got jump less than or equal to,</p>



<p class="wp-block-paragraph">then we&#8217;ve got jump greater than, and then we&#8217;ve got jump greater than or equal to.</p>



<p class="wp-block-paragraph">And when you combine those with jump equal to or jump not equal to,</p>



<p class="wp-block-paragraph">jump not equal to that&#8217;s pretty much all the comparison that you need and if you want to do</p>



<p class="wp-block-paragraph">something much much more complex well then you can just you know start stacking logic on top of</p>



<p class="wp-block-paragraph">logic and and branching on top of branching to do something that&#8217;s like this is what i&#8217;m talking</p>



<p class="wp-block-paragraph">like if you want to do this expression and expression or expression and then maybe like a</p>



<p class="wp-block-paragraph">expression, right? If you want to do something super complicated, like we can do in a higher</p>



<p class="wp-block-paragraph">level language, you&#8217;re just going to have to make a bunch of mini jumps or find another</p>



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



<p class="wp-block-paragraph">comparison. All right. So I think that&#8217;s everything that I can show you inside of this book. Let&#8217;s</p>



<p class="wp-block-paragraph">go to the code now. So here&#8217;s a little empty code, source code, a page. I&#8217;m going to stick</p>



<p class="wp-block-paragraph">all my code inside of it. Another reminder that if you don&#8217;t know how to write assembly</p>



<p class="wp-block-paragraph">you don&#8217;t know how to write assembly at all you should see my other videos because i&#8217;m not going</p>



<p class="wp-block-paragraph">to cover the basics of assembly in this video if you want to learn how to make a make file or compile</p>



<p class="wp-block-paragraph">or assemble or like do all the basics that i&#8217;m not talking about in this video i&#8217;ve covered them all</p>



<p class="wp-block-paragraph">in my other videos up to this point so for now we&#8217;re just going to focus only on conditional</p>



<p class="wp-block-paragraph">branching conditional jumping inside of an assembly module so i&#8217;m going to start off by setting up my</p>



<p class="wp-block-paragraph">and i&#8217;m gonna say paste and again you know this is covered in other videos but long story short</p>



<p class="wp-block-paragraph">i&#8217;m just going to be printing out a bunch of strings while this program executes so</p>



<p class="wp-block-paragraph">i&#8217;m going to announce that the branching test is going to begin and then i&#8217;m going to ask the user</p>



<p class="wp-block-paragraph">for some input and then i&#8217;m going to based on whatever the user inputs i&#8217;m going to just print</p>



<p class="wp-block-paragraph">out a message that sort of you know understands what the user inputted so if the user inputted</p>



<p class="wp-block-paragraph">something greater than zero it&#8217;s going to output that if they entered something that was less than</p>



<p class="wp-block-paragraph">10 it&#8217;s going to print that if they entered something that was equal to five it&#8217;s going to</p>



<p class="wp-block-paragraph">print that and then it&#8217;s going to say we&#8217;ve ended the branching tests then i have like a crlf again</p>



<p class="wp-block-paragraph">all this stuff is explained in other videos but we&#8217;re just basically printing so i&#8217;m going to</p>



<p class="wp-block-paragraph">start the text section of my program which is where all the instructions are and i&#8217;m going to</p>



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



<p class="wp-block-paragraph">just going to show you how to branch. Just trust me when I say that this helps me easily print</p>



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



<p class="wp-block-paragraph">But for you, if you&#8217;re trying to run this at home, you can just start hard coding numbers just to</p>



<p class="wp-block-paragraph">make sure that you can branch correctly and then eventually work your way up to a library that can</p>



<p class="wp-block-paragraph">videos in the distant future. I don&#8217;t know years from now. I don&#8217;t know.</p>



<p class="wp-block-paragraph">I might release the library or some other stuff to the casual viewer.</p>



<p class="wp-block-paragraph">So now here&#8217;s our entry point. Again,</p>



<p class="wp-block-paragraph">I mentioned that there&#8217;s going to be other parts of this program that I&#8217;m not</p>



<p class="wp-block-paragraph">showing. So this is a hybrid program where a C++ driver actually will call on</p>



<p class="wp-block-paragraph">my cool function. So just imagine from somewhere else in the program,</p>



<p class="wp-block-paragraph">you&#8217;re calling on the cool function. How do we know it&#8217;s a function?</p>



<p class="wp-block-paragraph">a label called cool and then it has a return statement at the very end.</p>



<p class="wp-block-paragraph">That&#8217;s pretty much all we need to know right now.</p>



<p class="wp-block-paragraph">Let me get rid of that load.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I always have the word load in all of my things.</p>



<p class="wp-block-paragraph">I think I put it in there on an early example</p>



<p class="wp-block-paragraph">and then I just kept copy pasting to my heart&#8217;s content and never stopped.</p>



<p class="wp-block-paragraph">We mark the function cool as global so that other modules can call on it</p>



<p class="wp-block-paragraph">like the C++ module that I&#8217;m not showing you.</p>



<p class="wp-block-paragraph">But if you&#8217;re writing a pure assembly module,</p>



<p class="wp-block-paragraph">you don&#8217;t really need to mark your functions as global.</p>



<p class="wp-block-paragraph">if everything&#8217;s within the same piece of source code it&#8217;s totally fine so then I&#8217;m going to make</p>



<p class="wp-block-paragraph">a function call to a function called branch test so very quickly before I actually add branch test</p>



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



<p class="wp-block-paragraph">story short all it does is just it prints a new line in the terminal and that&#8217;s it like it&#8217;s just</p>



<p class="wp-block-paragraph">it&#8217;s a convenience function I like to be able to call crlf and have a new line okay so let&#8217;s start</p>



<p class="wp-block-paragraph">our branch test function so here&#8217;s the signature it is just branch test with a</p>



<p class="wp-block-paragraph">void in front of it I&#8217;m gonna put a ret to say that this is a function that we</p>



<p class="wp-block-paragraph">will return from and I left myself a little note here saying that I&#8217;m gonna</p>



<p class="wp-block-paragraph">use register r12 as the number that the user inputs so that means I&#8217;m gonna have</p>



<p class="wp-block-paragraph">to preserve that because you have to respect the ABI and the ABI says that</p>



<p class="wp-block-paragraph">The ABI says that R12 is a Kali saved register.</p>



<p class="wp-block-paragraph">Again, see my other videos if you don&#8217;t know what I&#8217;m talking about.</p>



<p class="wp-block-paragraph">But I&#8217;m just going to preserve it in a section that I call the prologue, or actually the</p>



<p class="wp-block-paragraph">book calls the prologue.</p>



<p class="wp-block-paragraph">I do that now though too.</p>



<p class="wp-block-paragraph">And, you know, restore it in a section called the epilogue.</p>



<p class="wp-block-paragraph">So we have prologue and epilogue.</p>



<p class="wp-block-paragraph">We can have a push-pop pair.</p>



<p class="wp-block-paragraph">And then in between, we can have, you know, the meat of our function.</p>



<p class="wp-block-paragraph">Okay, so for the introduction, I&#8217;m just going to write a few instructions that just print</p>



<p class="wp-block-paragraph">out a message.</p>



<p class="wp-block-paragraph">print out a message. So we&#8217;re going to print out that intro message and then call</p>



<p class="wp-block-paragraph">CRLF to print a new line and then return. So maybe actually at this point we could</p>



<p class="wp-block-paragraph">probably check to see if this program works by trying to run it.</p>



<p class="wp-block-paragraph">So let me say clear and make run.</p>



<p class="wp-block-paragraph">Okay. So now the driver just prints hello. The driver is</p>



<p class="wp-block-paragraph">elsewhere and we get that welcome message that says begin</p>



<p class="wp-block-paragraph">branching test and then the driver retains control after that. So</p>



<p class="wp-block-paragraph">let&#8217;s do let&#8217;s do a goodbye message real fast so we&#8217;ll do our goodbye message</p>



<p class="wp-block-paragraph">it&#8217;s just another string that I&#8217;m printing it&#8217;s no big deal right now but</p>



<p class="wp-block-paragraph">if I run the program again you can see that begin branching test and end</p>



<p class="wp-block-paragraph">branching test okay so far we&#8217;re not really doing branching yet the next thing</p>



<p class="wp-block-paragraph">we need to do is ask for a number so this is how I&#8217;m going to do it in my</p>



<p class="wp-block-paragraph">this is how I&#8217;m going to do it in my program. In your program it might be different or you might</p>



<p class="wp-block-paragraph">have a hard-coded number or something like that. So for now just trust me when I say that I&#8217;m going</p>



<p class="wp-block-paragraph">to print a prompt basically saying could you please enter a number and then the next two lines</p>



<p class="wp-block-paragraph">are going to be just calling on one of my custom functions that will input a number from the user</p>



<p class="wp-block-paragraph">so the user can type at the terminal and we will receive their number through REX which is the</p>



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



<p class="wp-block-paragraph">And we&#8217;re going to store it in R12.</p>



<p class="wp-block-paragraph">So that&#8217;s why I had to preserve R12,</p>



<p class="wp-block-paragraph">because I&#8217;m actually going to start,</p>



<p class="wp-block-paragraph">you know, messing with the value of R12 now.</p>



<p class="wp-block-paragraph">Okay, so we ask for a number,</p>



<p class="wp-block-paragraph">and then we sort of don&#8217;t do anything after that.</p>



<p class="wp-block-paragraph">If we run the program again,</p>



<p class="wp-block-paragraph">it&#8217;s just asking for a number,</p>



<p class="wp-block-paragraph">and I can just like type some stuff and hit enter,</p>



<p class="wp-block-paragraph">and then the program ends, nothing actually happens.</p>



<p class="wp-block-paragraph">We&#8217;re building, we&#8217;re building.</p>



<p class="wp-block-paragraph">So now let&#8217;s ask ourselves,</p>



<p class="wp-block-paragraph">is the number greater than zero?</p>



<p class="wp-block-paragraph">So let me copy paste some code here.</p>



<p class="wp-block-paragraph">some code here so right now we&#8217;ve just finished inputting a number from the user and we&#8217;ll ask</p>



<p class="wp-block-paragraph">you know is that number that they inputted greater than zero so again the first thing we do is we use</p>



<p class="wp-block-paragraph">a compare instruction cmp we give it two operands the order doesn&#8217;t necessarily matter um for</p>



<p class="wp-block-paragraph">instance uh right now i&#8217;m asking is r12 greater than zero so i can do a jump less than or equal</p>



<p class="wp-block-paragraph">to like a different branch or i can jump greater than equal to if i switch the operands or if i</p>



<p class="wp-block-paragraph">in a different order, but I&#8217;m just choosing to say, let&#8217;s compare the two and then let&#8217;s jump</p>



<p class="wp-block-paragraph">if R12 is less than or equal to, which basically means not greater than, this label right here,</p>



<p class="wp-block-paragraph">which is just branch test after greater test. So I&#8217;m going to do a print statement right after that</p>



<p class="wp-block-paragraph">and then I&#8217;m going to draw out what the code is kind of doing. So let me make a label here so</p>



<p class="wp-block-paragraph">actually will compile. Whoops. I&#8217;m in the wrong window. Okay. There we go. So what am I saying</p>



<p class="wp-block-paragraph">here? Okay. Do the comparison. And then if the condition seems to be satisfied, then we&#8217;ll jump</p>



<p class="wp-block-paragraph">to this, which means we&#8217;ll just say goodbye and then exit the program because we&#8217;ll return to the</p>



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



<p class="wp-block-paragraph">in the middle. So think about this. This is kind of the idea. Let&#8217;s see if I can remember how to</p>



<p class="wp-block-paragraph">idea let&#8217;s see if I can remember how to draw this right now off the top of my head suppose we have</p>



<p class="wp-block-paragraph">like some sort of an input I&#8217;ll call this in put and then maybe the input is going to be you know</p>



<p class="wp-block-paragraph">whether r12 and r0 are equal or greater than or whatever so I&#8217;m just gonna say r12 and 0</p>



<p class="wp-block-paragraph">god that&#8217;s awful I really need to practice this draw pad I spend like a whole week practicing</p>



<p class="wp-block-paragraph">whole week practicing with a different draw pad and then when I come back to</p>



<p class="wp-block-paragraph">this one I&#8217;m even worse than when I started so we we come in and we kind of</p>



<p class="wp-block-paragraph">look at our 12 versus zero and then we will jump in one direction if that&#8217;s not</p>



<p class="wp-block-paragraph">you know greater than or equal to or less than or equal to and we&#8217;ll jump in</p>



<p class="wp-block-paragraph">another direction otherwise so basically here we&#8217;ll say jump less than equal to</p>



<p class="wp-block-paragraph">um less than or equal to whoops maybe i&#8217;ll do r12 uh i need to somehow adjust the pressure on this</p>



<p class="wp-block-paragraph">thing r12 less than or equal to and so this is sort of uh has to do with you know this branching</p>



<p class="wp-block-paragraph">path that we take like if r12 was less than or equal to zero then we&#8217;ll take the right branching</p>



<p class="wp-block-paragraph">to after, I&#8217;ll just put the word after,</p>



<p class="wp-block-paragraph">we&#8217;ll jump to the after greater test sub label.</p>



<p class="wp-block-paragraph">And otherwise, if R12 is, sorry,</p>



<p class="wp-block-paragraph">I said less than or equal to, yeah,</p>



<p class="wp-block-paragraph">if R12 is greater than zero,</p>



<p class="wp-block-paragraph">then we jump to this other thing,</p>



<p class="wp-block-paragraph">which is gonna be the fall through.</p>



<p class="wp-block-paragraph">So it&#8217;s gonna be line 96, but I&#8217;ll just say,</p>



<p class="wp-block-paragraph">I&#8217;ll say fall for fall through.</p>



<p class="wp-block-paragraph">basically the jump won&#8217;t happen if it&#8217;s greater than or equal to so you could imagine if you</p>



<p class="wp-block-paragraph">wanted to reverse the logic so you can use a jump greater than instruction you could but I&#8217;m choosing</p>



<p class="wp-block-paragraph">to do it this way and then both of those will eventually reach let&#8217;s see will eventually</p>



<p class="wp-block-paragraph">actually yeah the fall through instructions those will eventually reach the after instruction I&#8217;ll</p>



<p class="wp-block-paragraph">here and then the after instruction will just kind of like finish the program</p>



<p class="wp-block-paragraph">and exit and all that stuff. So if you think about the control path here,</p>



<p class="wp-block-paragraph">we have an input we&#8217;re looking at R12 with the zero with the compare instruction</p>



<p class="wp-block-paragraph">and then we use a conditional branching instruction. So maybe I should write,</p>



<p class="wp-block-paragraph">what do you want to write here? Maybe I&#8217;ll just put JLE up here because usually the compare</p>



<p class="wp-block-paragraph">usually the compare instruction and the conditional jump or branching instructions</p>



<p class="wp-block-paragraph">kind of you know come as a pair so both of these combined mean let&#8217;s look at r12 versus zero and</p>



<p class="wp-block-paragraph">if r12 is less than or equal to zero meaning if it will jump if we&#8217;re less than or equal to the</p>



<p class="wp-block-paragraph">the right operand r12 less than equal to the right operand then we go down this path on the right</p>



<p class="wp-block-paragraph">to the after label if that is false meaning r12 was greater than zero then we&#8217;ll fall through</p>



<p class="wp-block-paragraph">this stuff right here and then whether or not we actually fall through will always end up at the</p>



<p class="wp-block-paragraph">at the after area because we&#8217;re either going to jump directly to the after area or we&#8217;re going</p>



<p class="wp-block-paragraph">to fall through and then the fall through falls through to the after area anyway as well let me</p>



<p class="wp-block-paragraph">just show you that real fast oh man i&#8217;m having a hard time with this okay so let&#8217;s pretend that r12</p>



<p class="wp-block-paragraph">less than or equal to. So that means instructions are coming, you know,</p>



<p class="wp-block-paragraph">instructions are getting executed. We&#8217;re going down and down and down and down and down.</p>



<p class="wp-block-paragraph">Once we see jump less than or equal to, if that&#8217;s true, then we jump to the after label,</p>



<p class="wp-block-paragraph">which means we just kind of jump around this message. And so we&#8217;re not going to exit,</p>



<p class="wp-block-paragraph">sorry, we&#8217;re not going to execute that code. So in the case where that is true,</p>



<p class="wp-block-paragraph">where it is less than or equal to, we&#8217;re not going to execute those instructions.</p>



<p class="wp-block-paragraph">So we&#8217;re not going to say their number was greater than zero. We&#8217;re just going to simply</p>



<p class="wp-block-paragraph">We&#8217;re just going to simply say goodbye.</p>



<p class="wp-block-paragraph">However, on the other hand, if that is not true, meaning if R12 was indeed greater than</p>



<p class="wp-block-paragraph">zero, then we&#8217;re going to fall through because this jump less than or equal to instruction,</p>



<p class="wp-block-paragraph">it will only jump if the R flags, if the comparison instruction thought that R12 was less than</p>



<p class="wp-block-paragraph">or equal to zero.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, if it&#8217;s greater than, then instructions, the control path is just</p>



<p class="wp-block-paragraph">the control path is just going to fall through.</p>



<p class="wp-block-paragraph">So that means we will actually execute these.</p>



<p class="wp-block-paragraph">And then when they&#8217;re done,</p>



<p class="wp-block-paragraph">execution will continue to fall through to the rest.</p>



<p class="wp-block-paragraph">So that means we&#8217;re either going to see a message saying</p>



<p class="wp-block-paragraph">their number was greater than zero or not,</p>



<p class="wp-block-paragraph">based on whether their number actually was greater than zero.</p>



<p class="wp-block-paragraph">Okay, hopefully I explained that somewhat clearly.</p>



<p class="wp-block-paragraph">Now let&#8217;s run the code and see if it works.</p>



<p class="wp-block-paragraph">Hopefully I didn&#8217;t forget anything.</p>



<p class="wp-block-paragraph">This is kind of a partial program at this point.</p>



<p class="wp-block-paragraph">program at this point I&#8217;m gonna add a bunch more stuff to it so hopefully</p>



<p class="wp-block-paragraph">this actually compiles enter an integer five notice how it says your number was</p>



<p class="wp-block-paragraph">greater than zero and if I run it again and I just type zero it does not say</p>



<p class="wp-block-paragraph">that it was greater than zero if I run it again I type like a negative five it</p>



<p class="wp-block-paragraph">also does not say that it was greater than zero you know a huge giant number</p>



<p class="wp-block-paragraph">here it&#8217;ll say your number was greater than zero you could probably type a</p>



<p class="wp-block-paragraph">number that was just absolutely huge and have it overflow I actually don&#8217;t know</p>



<p class="wp-block-paragraph">nines I need to type and I don&#8217;t even know what the behavior is going to be.</p>



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



<p class="wp-block-paragraph">to input a number which is happening in a different library.</p>



<p class="wp-block-paragraph">Let&#8217;s see if that even works greater than zero.</p>



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



<p class="wp-block-paragraph">It probably overflowed and went to a very, very low negative number.</p>



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



<p class="wp-block-paragraph">I guess at least I know my library will probably work sort of.</p>



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



<p class="wp-block-paragraph">So let&#8217;s go back to the code here and let&#8217;s upgrade it.</p>



<p class="wp-block-paragraph">how to to check for jump less than or equal to um we&#8217;re going to say goodbye right there so now the</p>



<p class="wp-block-paragraph">next thing we should do is check to see if the number was less than 10. so after the greater test</p>



<p class="wp-block-paragraph">it&#8217;s going to be here so again the same thing like we can just reuse r12 because we&#8217;re not really</p>



<p class="wp-block-paragraph">modifying it and we&#8217;re not calling uh well i guess even if we were calling a function r12 was supposed</p>



<p class="wp-block-paragraph">supposed to be Kali preserved so we&#8217;re just comparing it and then maybe</p>



<p class="wp-block-paragraph">printing a message if we want to and regardless we&#8217;re going to end up at 103</p>



<p class="wp-block-paragraph">this after greater test label so then we&#8217;ll compare again r12 with the number</p>



<p class="wp-block-paragraph">10 and we&#8217;ll say jump if it&#8217;s greater than or equal to branch test after less</p>



<p class="wp-block-paragraph">so now at this point we&#8217;re saying compare r12 with 10 if it&#8217;s greater than or equal to 10 then</p>



<p class="wp-block-paragraph">just skip over the message so if it&#8217;s greater than or equal to 10 then wait a minute wait wait</p>



<p class="wp-block-paragraph">yeah yeah if it&#8217;s greater than or equal to 10 then skip the message otherwise the message</p>



<p class="wp-block-paragraph">that is printed is going to say that their number is less than 10. so again you could imagine a</p>



<p class="wp-block-paragraph">want me to write it out for you I don&#8217;t know I think once is probably enough but let me just do</p>



<p class="wp-block-paragraph">it again we&#8217;ll do input is coming in and it&#8217;s just going to be you know comparing and then</p>



<p class="wp-block-paragraph">jumping greater than or equal to the input is going to be where we&#8217;re just looking to see if</p>



<p class="wp-block-paragraph">R12 how it compares to zero god the number one is awful my entire penmanship is awful okay one</p>



<p class="wp-block-paragraph">we&#8217;ll do r12 and 10 and so that&#8217;s just sort of what we&#8217;re comparing with the compare instruction</p>



<p class="wp-block-paragraph">and then the jump instruction and so if we go on the left i&#8217;m going to say that&#8217;s the fall through</p>



<p class="wp-block-paragraph">path again and if we go to the right it&#8217;s going to be um r12 greater than or equal to 10 is going</p>



<p class="wp-block-paragraph">and then here this is going to be after less I&#8217;ll say the after label the after less label</p>



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



<p class="wp-block-paragraph">here that&#8217;s just like print p for how about p for print p for print it&#8217;s going to fall through and</p>



<p class="wp-block-paragraph">then it&#8217;s going to print and then it&#8217;s going to go to the after label so you can see that</p>



<p class="wp-block-paragraph">the print instructions are only reached if r12 is not greater than equal to 10 which is the same</p>



<p class="wp-block-paragraph">to 10 which is the same thing as saying it&#8217;s going to fall through if r12 is less than</p>



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



<p class="wp-block-paragraph">And then after we finally get to the after label, then we&#8217;re just going to do, you know,</p>



<p class="wp-block-paragraph">the rest of the instructions.</p>



<p class="wp-block-paragraph">We&#8217;re going to exit the program.</p>



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



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



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



<p class="wp-block-paragraph">So say there never was less than 10.</p>



<p class="wp-block-paragraph">And then regardless, here&#8217;s like the ending label that we were conditionally jumping to.</p>



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



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



<p class="wp-block-paragraph">So we&#8217;ll enter five.</p>



<p class="wp-block-paragraph">we&#8217;ll enter five notice how it says your number was greater than zero and your</p>



<p class="wp-block-paragraph">number was less than ten let&#8217;s do an 11 I guess it says your number was greater</p>



<p class="wp-block-paragraph">than zero but it does not say that our number was less than ten because 11 is</p>



<p class="wp-block-paragraph">not less than ten if I do a negative three here it says that our number was</p>



<p class="wp-block-paragraph">less than ten but it doesn&#8217;t say that it&#8217;s greater than zero so we have like a</p>



<p class="wp-block-paragraph">lot of decisions that we could make right like imagine if instead of just</p>



<p class="wp-block-paragraph">based on something happening, you could call a full function or skip a bunch of extra code,</p>



<p class="wp-block-paragraph">you know, whatever you want it to do.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go to the next condition where we&#8217;re going to compare R12 to the number five.</p>



<p class="wp-block-paragraph">And we&#8217;re going to ask, you know, is R12 equal to five?</p>



<p class="wp-block-paragraph">So this is going to be the jump not equal branch.</p>



<p class="wp-block-paragraph">And again, you know, if you wanted to say jump, if it&#8217;s equal to the place where we</p>



<p class="wp-block-paragraph">and then maybe like right after that fall through and just do an unconditional jump you can</p>



<p class="wp-block-paragraph">I just think there are less jumps if I do it this way so uh here we&#8217;re just going to print you know</p>



<p class="wp-block-paragraph">your number was equal to five and so basically if it was not equal we should have another label here</p>



<p class="wp-block-paragraph">that allows us to skip that so all right so same thing I&#8217;m not going to draw the diagram this time</p>



<p class="wp-block-paragraph">R12 with 5 and if it&#8217;s not equal to 5 then jump to this label down here which just means</p>



<p class="wp-block-paragraph">let&#8217;s say goodbye and not actually say that their number was equal to 5.</p>



<p class="wp-block-paragraph">But if it was equal to 5 then this JNE conditional branch won&#8217;t actually jump anywhere.</p>



<p class="wp-block-paragraph">Execution will fall through to the next statements which is just printing the message that their</p>



<p class="wp-block-paragraph">number is equal to 5 and then when that&#8217;s done it&#8217;ll fall through to this other label</p>



<p class="wp-block-paragraph">and eventually fall through to the return statement.</p>



<p class="wp-block-paragraph">the whole thing already nice that was easy um let&#8217;s do another run and um let&#8217;s enter the</p>



<p class="wp-block-paragraph">integer one it says one is uh greater than zero and one is less than 10</p>



<p class="wp-block-paragraph">greater than zero and less than 10 is there any way i can get nothing to print out</p>



<p class="wp-block-paragraph">greater than zero and less than 10</p>



<p class="wp-block-paragraph">you don&#8217;t think i can do it let me try it nine no</p>



<p class="wp-block-paragraph">why did I even put 9 okay yeah it&#8217;s always gonna say 0 or 10 okay so let&#8217;s</p>



<p class="wp-block-paragraph">do a 4 we&#8217;ll get both of those greater than 0 and also less than 10 let&#8217;s do a</p>



<p class="wp-block-paragraph">5 now we should get all those messages your number was greater than 0 your</p>



<p class="wp-block-paragraph">number was less than 10 your number was equal to 5 and if I just do a 6 in there</p>



<p class="wp-block-paragraph">again it doesn&#8217;t print your number was equal to 5 and we have to enter an</p>



<p class="wp-block-paragraph">actual 5 for that so that&#8217;s the that&#8217;s the basics of</p>



<p class="wp-block-paragraph">conditional branching oh there&#8217;s one other thing that I wanted to make sure</p>



<p class="wp-block-paragraph">in my previous video where we only talked about jumps bear in mind that</p>



<p class="wp-block-paragraph">these conditional branching statements or instructions you know je j any all</p>



<p class="wp-block-paragraph">these things they have a limited range of jumps of where they can jump to so if</p>



<p class="wp-block-paragraph">you have a gigantic program and you&#8217;re trying to jump very very very far away</p>



<p class="wp-block-paragraph">based on a condition you might have an assembler error where your program won&#8217;t</p>



<p class="wp-block-paragraph">even compile won&#8217;t even assemble telling you i think the message is i wrote it</p>



<p class="wp-block-paragraph">I wrote it down somewhere jump out of range coming from the assembler that basically means that these</p>



<p class="wp-block-paragraph">instructions can only jump about 128 bytes away from whatever instruction they are at or whatever</p>



<p class="wp-block-paragraph">memory location they are at so you know after your assembler assembles your program and after</p>



<p class="wp-block-paragraph">your linker links your program and you just have like a binary you know if you were to inspect</p>



<p class="wp-block-paragraph">all the instructions inside of your executable your binary you would see that they have offsets</p>



<p class="wp-block-paragraph">offsets, right? You know, like one instruction is basically eight bytes away from the one that</p>



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



<p class="wp-block-paragraph">to an instruction that is 100 is more than 128 bytes away, it won&#8217;t work, you&#8217;ll get that error.</p>



<p class="wp-block-paragraph">So how do you solve that problem? Well, maybe I&#8217;ll just write this down real fast. I&#8217;m not going to</p>



<p class="wp-block-paragraph">make this part of the code example. But just imagine we have, you know, jump equal to some</p>



<p class="wp-block-paragraph">and maybe I&#8217;ll do a comment very very far away and here&#8217;s the label some label right</p>



<p class="wp-block-paragraph">so if your assembler says hey you can&#8217;t jump to some label because it&#8217;s too far away it&#8217;s a</p>



<p class="wp-block-paragraph">jump out of range assembler error then basically you can just make some other labels here you can</p>



<p class="wp-block-paragraph">say uh you can say short jump true and short jump false you know just make up some sort of a label</p>



<p class="wp-block-paragraph">label and basically say if it is um well false yeah okay i guess this is the way i&#8217;ll do it this</p>



<p class="wp-block-paragraph">is not super efficient but instead of jumping to the very far away label in the case of true you</p>



<p class="wp-block-paragraph">can just jump to the true label so i&#8217;m going to say jump uh to the short jump true label otherwise</p>



<p class="wp-block-paragraph">To the false label. So now you&#8217;re definitely jumping in either case and both of these labels are close by</p>



<p class="wp-block-paragraph">So then I can pretty much just have the close by true label do an unconditional</p>



<p class="wp-block-paragraph">Jump to some label and that overcomes the limitation of branching instructions</p>



<p class="wp-block-paragraph">So, you know JMP the regular jump instruction</p>



<p class="wp-block-paragraph">It has an unlimited jump that it can do it can go just to like very very very far away instructions</p>



<p class="wp-block-paragraph">And then for the false, you know, I don&#8217;t know, do whatever you want and then jump wherever you want.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really matter.</p>



<p class="wp-block-paragraph">I&#8217;m just trying to show you that if you make a short jump to a label that will only have an unconditional jump,</p>



<p class="wp-block-paragraph">so it&#8217;s sort of like two jumps, you know, like a short jump and then a long jump,</p>



<p class="wp-block-paragraph">then you can overcome that limitation.</p>



<p class="wp-block-paragraph">Again, bearing in mind that some label is supposed to be very, very far away and sort of unreachable.</p>



<p class="wp-block-paragraph">Okay, so let me just double check that I talked about everything that I wanted to.</p>



<p class="wp-block-paragraph">We looked at the branching instructions, the sign in this, I drew a diagram for you.</p>



<p class="wp-block-paragraph">We solved the jump out of range error.</p>



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



<p class="wp-block-paragraph">Hopefully you feel like an expert now at conditional branching and how to make decisions.</p>



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



<p class="wp-block-paragraph">and all that stuff.</p>



<p class="wp-block-paragraph">decisions in yasm x86-64 assembly and conceptually you can apply this to other assembly languages too</p>



<p class="wp-block-paragraph">so thanks for watching my video i hope you learned a little bit of stuff and had a little bit of fun</p>



<p class="wp-block-paragraph">i&#8217;ll see you in the next video</p>



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



<p class="wp-block-paragraph">i do hope you did learn something and have some fun uh if you could do me a please a small little</p>



<p class="wp-block-paragraph">Could you please subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you&#8217;re looking at right now?</p>



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



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



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



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



<p class="wp-block-paragraph">or followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



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



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a QR code which</p>



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



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



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the</p>



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



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



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



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



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">appreciate it so again thank you so much for watching this video and enjoy the cool music</p>



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/">Learn Conditional Branching in x86-64 Assembly (YASM/NASM) &#8211; CMP, JE, JNE, JL, JG &amp; More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/learn-conditional-branching-in-x86-64-assembly-yasm-nasm-cmp-je-jne-jl-jg-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 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 class="wp-block-paragraph">In this video, I walk through how to perform signed integer division in x86-64 assembly language using Yasm on Ubuntu/Linux.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Perfect for anyone learning low-level programming, assembly language, or wanting to understand how integer division really works under the hood.</p>



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



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



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



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



<p class="wp-block-paragraph">In this video, I&#8217;d like to talk to you about dividing signed integers in an x86-64 Yasm</p>



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



<p class="wp-block-paragraph">I&#8217;m going to be focusing on 64-bit integers and I&#8217;m going to use an instruction called</p>



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



<p class="wp-block-paragraph">Okay, so for starters, I just want to give you an overview of the process.</p>



<p class="wp-block-paragraph">I&#8217;m going to use an awesome book that I love to reference.</p>



<p class="wp-block-paragraph">awesome book that i love to reference i did not write this book the book is titled x86 64 assembly</p>



<p class="wp-block-paragraph">language programming with ubuntu i mention it in a lot of other videos the author here</p>



<p class="wp-block-paragraph">is a genius and this book is open source and freely available so go look it up get yourself a</p>



<p class="wp-block-paragraph">copy and become an assembly expert too anyway so what i&#8217;m going to do is go into this book</p>



<p class="wp-block-paragraph">instruction set overview and then inside of that i&#8217;m going to click on 7.5 integer arithmetic</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">go inside of there and i&#8217;m going to click on 754 integer division so you know the basic idea for</p>



<p class="wp-block-paragraph">dividing integers in a yasm assembly program on x86 64 is just to load up your dividend you know</p>



<p class="wp-block-paragraph">top portion I guess that is the numerator and then the divisor which</p>



<p class="wp-block-paragraph">would be the denominator with separate registers so load up the dividend with a</p>



<p class="wp-block-paragraph">register or put the dividend into a register and then put the divisor into</p>



<p class="wp-block-paragraph">another register they&#8217;re going to be special registers we&#8217;re going to be</p>



<p class="wp-block-paragraph">using the A and D registers you can kind of see that at the bottom already and</p>



<p class="wp-block-paragraph">then once that&#8217;s set up properly you just sort of use the IDIV function or</p>



<p class="wp-block-paragraph">instruction and there&#8217;s another instruction we can use to make things a</p>



<p class="wp-block-paragraph">little bit more simple because if you kind of notice what what this is saying</p>



<p class="wp-block-paragraph">here it&#8217;s saying that the the dividend usually needs to be stacked between two</p>



<p class="wp-block-paragraph">other registers at the same time and that&#8217;s a little bit confusing</p>



<p class="wp-block-paragraph">especially if these registers right here notice how a DX colon ax that means it</p>



<p class="wp-block-paragraph">16-bit versions of two different registers just sort of like pushed up against each other so</p>



<p class="wp-block-paragraph">Obviously if you know how to manipulate bits which I&#8217;m gonna do other videos for in the future</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Love this line. I just found it today</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">nifty little table inside of the book that sort of shows you how you&#8217;re supposed to set up the</p>



<p class="wp-block-paragraph">numerator and denominator or the divisor what was the other word that they used dividend and divisor</p>



<p class="wp-block-paragraph">i usually say numerator and denominator anyway so remember in in if you watched my previous videos</p>



<p class="wp-block-paragraph">then you&#8217;ll know that we have smaller versions of our registers like for example uh this eax right</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">half of those bytes are being ignored so all of these different versions that you&#8217;re seeing are</p>



<p class="wp-block-paragraph">just different sizes of the a and d registers so i&#8217;m going to go to the simplest example here and</p>



<p class="wp-block-paragraph">just kind of show you that basically what they&#8217;re trying to do is force you to have a numerator that</p>



<p class="wp-block-paragraph">is bigger than the denominator that way it&#8217;s guaranteed you&#8217;ll have some kind of a quotient</p>



<p class="wp-block-paragraph">or you know more likely that you&#8217;ll have some kind of a quotient and and a remainder that kind of</p>



<p class="wp-block-paragraph">that kind of makes sense because if you divide a number that&#8217;s much smaller than</p>



<p class="wp-block-paragraph">the denominator then you&#8217;ll just end up with zero remainder the original number</p>



<p class="wp-block-paragraph">pretty much in integer division this video is not about float division float</p>



<p class="wp-block-paragraph">division is more precise so in other words if I want to divide something by a</p>



<p class="wp-block-paragraph">64-bit integer then I need to first set up a 128-bit integer and that means I</p>



<p class="wp-block-paragraph">and that means I have to take two 64-bit integers, the D register and the A register.</p>



<p class="wp-block-paragraph">I have to load both of those registers up with 64 bits each so that when you imagine them being combined,</p>



<p class="wp-block-paragraph">it&#8217;s a 128-bit register or 128-bit value.</p>



<p class="wp-block-paragraph">Once I have something that is sufficiently large, then I can divide it by something else.</p>



<p class="wp-block-paragraph">Then the result will show up in the A and D registers.</p>



<p class="wp-block-paragraph">the actual quotient, you know, the actual result of dividing without a fractional part at the end,</p>



<p class="wp-block-paragraph">just, you know, truncated data. Then the remainder will be in the D register, the RDX register. So at</p>



<p class="wp-block-paragraph">least for this form, I just want you to know you can do it with all these other forms if you want</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">But I&#8217;m going to go ahead and start writing my own program now.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Okay, so here is, oh, maybe I should first say, here&#8217;s a sample program that I&#8217;m using.</p>



<p class="wp-block-paragraph">You don&#8217;t have to worry too much about what&#8217;s actually inside of it.</p>



<p class="wp-block-paragraph">The C module and the make file, they&#8217;re standard stuff that I usually use in all of my videos.</p>



<p class="wp-block-paragraph">all of my videos this is not a make file video see my other videos if you want to learn how to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">just get this program to run and then for the c program i&#8217;m compiling a hybrid program so i mean</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">inside of a c source code file that gets compiled and linked into my main program and it just calls</p>



<p class="wp-block-paragraph">a function called math right here which is just going to be you know this source code right here</p>



<p class="wp-block-paragraph">so if you want to make hybrid programs you want to learn the basics of assembly you want to learn</p>



<p class="wp-block-paragraph">anything that i&#8217;m not really talking about in this video see my other videos</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">some starter code here for my math module i&#8217;ve got a data section and i&#8217;ve got some strings</p>



<p class="wp-block-paragraph">strings or anything but just keep in mind I&#8217;m going to eventually print the division result</p>



<p class="wp-block-paragraph">quotient is and then I&#8217;m going to actually print it and then I&#8217;m going to print the division</p>



<p class="wp-block-paragraph">remainder is and then actually print it so I&#8217;m just I&#8217;m just setting up strings to make my</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">system calls here&#8217;s the system service code for writing to something writing to a file or standard</p>



<p class="wp-block-paragraph">standard output again that&#8217;s in another video file descriptor one another video and then return</p>



<p class="wp-block-paragraph">value I&#8217;m just choosing to return the number seven from this module I don&#8217;t know why I just</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">integers here and uh 56 and oh I think in my starter code I actually hard-coded those numbers</p>



<p class="wp-block-paragraph">after everything is working so um i&#8217;m going to start my text section here by just copy pasting</p>



<p class="wp-block-paragraph">a little thing that says okay here&#8217;s where my code is going to go now all my instructions</p>



<p class="wp-block-paragraph">and then uh here&#8217;s my entry point which is just my function called math you want to write functions</p>



<p class="wp-block-paragraph">see my other videos but this is basically it you just kind of have something enter and then return</p>



<p class="wp-block-paragraph">from it when you&#8217;re done and if you mess up any registers you have to preserve them with push and</p>



<p class="wp-block-paragraph">you have to preserve them with push and pop pairs.</p>



<p class="wp-block-paragraph">Another thing that I&#8217;m going to add that I&#8217;m not going to explain in this video</p>



<p class="wp-block-paragraph">is I&#8217;m going to do calls to one of my own personal libraries.</p>



<p class="wp-block-paragraph">And basically I&#8217;m just going to mark two functions as external.</p>



<p class="wp-block-paragraph">They&#8217;re sitting in a shared object that I made.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about that. This video is not about printing.</p>



<p class="wp-block-paragraph">It&#8217;s just about dividing. But I want to be able to easily print.</p>



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



<p class="wp-block-paragraph">So don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">for this video is my function called divide test because notice how in the entry point of math</p>



<p class="wp-block-paragraph">remember the c module the driver it calls on this math function the math function eventually or</p>



<p class="wp-block-paragraph">immediately calls on divide test and so here&#8217;s the prototype basically for my divide test function</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">to preserve the argument register so I should have just said yeah we are going to use some</p>



<p class="wp-block-paragraph">registers and therefore we&#8217;re going to preserve them so our 12 14 and 15 oh why am I not using</p>



<p class="wp-block-paragraph">13 I must have erased that at an earlier date whatever we&#8217;re going to use these three registers</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">push r12 push r14 push r15 respect the abi if you don&#8217;t other pro other parts of the program</p>



<p class="wp-block-paragraph">will probably crash epilogue is just popping all those in reverse order so that I can basically</p>



<p class="wp-block-paragraph">basically preserve them for the caller.</p>



<p class="wp-block-paragraph">Okay, so now we&#8217;re good with all our registers.</p>



<p class="wp-block-paragraph">Next thing I want to do is I want to load up, let&#8217;s see,</p>



<p class="wp-block-paragraph">I want to load up RAX with a number.</p>



<p class="wp-block-paragraph">Now maybe I&#8217;ll fix this right now.</p>



<p class="wp-block-paragraph">What was 256?</p>



<p class="wp-block-paragraph">It was the my integer B.</p>



<p class="wp-block-paragraph">So I&#8217;m going to hit main memory,</p>



<p class="wp-block-paragraph">which is a little slower than using an immediate,</p>



<p class="wp-block-paragraph">but hey, it&#8217;s more flexible.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">go back to the book real fast if our intention is to divide a 128 bit uh uh like combined register</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">if you wanted to you could just you know take rdx and just set a bunch of zeros to it but that</p>



<p class="wp-block-paragraph">would probably only work if you were guaranteed that rax was a positive number so that its sign</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">So there&#8217;s a special instruction we can use called CQO.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">The system wants a 128-bit integer.</p>



<p class="wp-block-paragraph">basically this instruction right here CQO whoops what happened how come it&#8217;s not lined up</p>



<p class="wp-block-paragraph">CQO basically just takes RAX whatever is in there and stretches it onto the 128 bit combination of</p>



<p class="wp-block-paragraph">RDX colon RAX so remember when you see this kind of thing remember when you see RDX colon RAX or</p>



<p class="wp-block-paragraph">just like a register colon another register that&#8217;s usually telling you that the system will think of</p>



<p class="wp-block-paragraph">in a combined way as one longer register so that&#8217;s really what it is cqo is going to say</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">combined they make sense as just one number um and you know that that would work pretty easily</p>



<p class="wp-block-paragraph">like i said before if it was a positive number but not necessarily in all cases so we&#8217;ll just use</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">And you know, we can hard code the 233 there, but I think I&#8217;ve already decided,</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">when the system is using your registers like rax here it knows rax is the 64-bit form of the a</p>



<p class="wp-block-paragraph">register so if you use different forms of your register then it knows how many bits you want</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">the system to really know that that memory location is 64 bits how does it</p>



<p class="wp-block-paragraph">how does it know if I&#8217;m if I&#8217;m trying to load from memory a one byte integer or</p>



<p class="wp-block-paragraph">two byte integer or four bytes or eight bytes it doesn&#8217;t really know so I kind</p>



<p class="wp-block-paragraph">of have to specify what the data size is of the integer at that place in memory</p>



<p class="wp-block-paragraph">this probably wouldn&#8217;t even compile. But if it did compile, you probably should be nervous that</p>



<p class="wp-block-paragraph">it would compile and do something wrong. So I&#8217;m just going to specify exactly what the data size</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">to just actually divide. Let me copy paste that real fast. So now we actually divide. Why is my</p>



<p class="wp-block-paragraph">Oh, I know what&#8217;s going on.</p>



<p class="wp-block-paragraph">I have the tab size set up differently between my two editors.</p>



<p class="wp-block-paragraph">I got to fix that.</p>



<p class="wp-block-paragraph">Or how about never fix it so I can always complain and you can laugh at me.</p>



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



<p class="wp-block-paragraph">So on line 85, we&#8217;re going to divide the numerator by the denominator.</p>



<p class="wp-block-paragraph">And we&#8217;ll say the numerator is, you know, RAX with RDX in front of it.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">okay r12 so if you look back at the I got a little confused here because uh the book you know it</p>



<p class="wp-block-paragraph">wants uh the d and the a registers as the numerator but then it says op64 as the denominator you can</p>



<p class="wp-block-paragraph">just kind of use a bunch of different stuff for that but I&#8217;m choosing to divide by another register</p>



<p class="wp-block-paragraph">So basically I&#8217;m taking the denominator and loading it up into R12.</p>



<p class="wp-block-paragraph">And then when I call my idiv instruction, I&#8217;m just saying integer division.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">At that point, RAX and RDX will be overwritten.</p>



<p class="wp-block-paragraph">So that data is now destroyed, but they contain the answer.</p>



<p class="wp-block-paragraph">So RAX now contains the answer.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna say quotient and the RAX register contains the remainder so for</p>



<p class="wp-block-paragraph">example if you are familiar with the let me pull this up real fast is that the</p>



<p class="wp-block-paragraph">calculator no there is if you&#8217;re familiar with the modulo instruction like if we</p>



<p class="wp-block-paragraph">said let&#8217;s see what was it again 233 okay so let&#8217;s do 256 divided by 233 this</p>



<p class="wp-block-paragraph">this gives us the number one and then some sort of like a fractional remainder</p>



<p class="wp-block-paragraph">with integer division we&#8217;re not going to get the fractional remainder it&#8217;s just literally going to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">modulo operator which is going to be 23 so it&#8217;s going to be one remainder 23</p>



<p class="wp-block-paragraph">is what we should see unless i&#8217;m totally wrong so then right away</p>



<p class="wp-block-paragraph">of our division which is a good practice you know like the rex and rdx those are registers that</p>



<p class="wp-block-paragraph">could be quickly destroyed especially if you called another function or a system call or</p>



<p class="wp-block-paragraph">something so i&#8217;m just going to save them right away so that&#8217;s why we saved 14 and 15</p>



<p class="wp-block-paragraph">in addition to r12 so r12 we were using for the denominator and then we&#8217;re using 14 and 15 to</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">fast copy paste this and then just kind of briefly talk about oh look I&#8217;m using a system call</p>



<p class="wp-block-paragraph">explained in another video to print out that little prefix message you know the result of your</p>



<p class="wp-block-paragraph">division here&#8217;s the quotient and then a colon and then I can actually print the number</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">printing r14 I&#8217;m giving it to the call to the function as the first argument which is rdi</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">prefix of the remainder you know here&#8217;s your remainder and then I&#8217;m going to actually print</p>



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



<p class="wp-block-paragraph">And the remainder was in R15.</p>



<p class="wp-block-paragraph">So 14 and 15 had the answer.</p>



<p class="wp-block-paragraph">And then the other register just kind of had, or the R12 just kind of had the denominator.</p>



<p class="wp-block-paragraph">So I think that&#8217;s pretty much all I need.</p>



<p class="wp-block-paragraph">Oh, there&#8217;s one more function that I forgot to put in there.</p>



<p class="wp-block-paragraph">I&#8217;m fond of writing a little CRLF function.</p>



<p class="wp-block-paragraph">This is not part of the video, really.</p>



<p class="wp-block-paragraph">But it&#8217;s just a special function that I can call on to do a carriage return line feed.</p>



<p class="wp-block-paragraph">And I&#8217;m just, I just can never stop using this.</p>



<p class="wp-block-paragraph">stop using this. I know I could just prefix or suffix my strings, you know, with like a CRLF,</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">Let&#8217;s see if it actually works. Is that my terminal? Yeah. Okay. I&#8217;m going to go clear</p>



<p class="wp-block-paragraph">and make, hopefully it actually works. So, oh yeah, there we are. Okay. So everything seemed</p>



<p class="wp-block-paragraph">It printed hello from the driver.</p>



<p class="wp-block-paragraph">And then when we went inside of the assembly module,</p>



<p class="wp-block-paragraph">this is really what we&#8217;re interested in.</p>



<p class="wp-block-paragraph">It said the division result, the quotient is the number one</p>



<p class="wp-block-paragraph">and the division remainder is the number 23.</p>



<p class="wp-block-paragraph">That&#8217;s what we said before, right?</p>



<p class="wp-block-paragraph">So it&#8217;s going to be 23 is the remainder.</p>



<p class="wp-block-paragraph">If you just do the modular operator or if you&#8217;re dividing,</p>



<p class="wp-block-paragraph">whoops, if you&#8217;re dividing, then you&#8217;ll just get a one.</p>



<p class="wp-block-paragraph">Remember like the quotient is just going to, you know,</p>



<p class="wp-block-paragraph">truncate or remove or throw away all the extra data.</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">anything past the decimal point is gone and that&#8217;s why the uh the remainder is pretty important</p>



<p class="wp-block-paragraph">then of course you know just for fun we could i don&#8217;t know put like some other numbers in here</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">number and see what happens because these are 64-bit numbers we can do it um i probably should</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">divide that by some other stuff see if this comes up with a more fun answer so one more time clear</p>



<p class="wp-block-paragraph">and make and it&#8217;s telling me that uh whoa that&#8217;s the result I guess probably should have printed</p>



<p class="wp-block-paragraph">out the original numbers but let me just put that into the calculator just for fun we&#8217;ll say this</p>



<p class="wp-block-paragraph">just sort of go back to the calculator it enters so uh oh the squiggly lines</p>



<p class="wp-block-paragraph">that means that&#8217;s not exactly the result i got to figure out how to change the mode on</p>



<p class="wp-block-paragraph">this calculator so i can get precise let&#8217;s see can i do that right here</p>



<p class="wp-block-paragraph">exact no can i try it again with exact all right this is not a tutorial on the</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">off three digits so that the calculator will at least show me something okay run it one more time</p>



<p class="wp-block-paragraph">and then so now we have smaller stuff the answer should have been 38677662 38677662 yeah that looks</p>



<p class="wp-block-paragraph">the decimal point so the point six is not on there and then we run the modulo</p>



<p class="wp-block-paragraph">to see what the remainder is and it should say one two eight six three yeah</p>



<p class="wp-block-paragraph">okay and then the seven is getting returned I don&#8217;t know do you want to</p>



<p class="wp-block-paragraph">return the remainder for some reason we could do it let&#8217;s just return the</p>



<p class="wp-block-paragraph">remainder might as well make this video a little bit more fun so recall that in</p>



<p class="wp-block-paragraph">order to return something if the return value is an integer and not a float or</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">R15 nice now this is a function with a return type I&#8217;m going to say along here and then</p>



<p class="wp-block-paragraph">up here, I guess, RAX is already going to be pre-filled now after that call.</p>



<p class="wp-block-paragraph">But you could imagine if you were doing more stuff between the call and the return,</p>



<p class="wp-block-paragraph">you would probably want to save the return value from RAX in some other register like R12.</p>



<p class="wp-block-paragraph">And then that means you got to add a push pop pair to preserve it.</p>



<p class="wp-block-paragraph">And then we&#8217;ll definitely just, well, maybe I&#8217;ll say move RAX into RAX</p>



<p class="wp-block-paragraph">totally useless. That&#8217;s why I&#8217;m commenting it out. So we&#8217;ll do one less instruction.</p>



<p class="wp-block-paragraph">So basically after we get back from divide test, RAX is going to have the</p>



<p class="wp-block-paragraph">return value and math, this function would also use RAX for its return value. So if we just sort</p>



<p class="wp-block-paragraph">of don&#8217;t do anything, we should now see that the driver receives the remainder as its return value.</p>



<p class="wp-block-paragraph">Let me open that up again real fast just to make sure we understand what&#8217;s going on with</p>



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



<p class="wp-block-paragraph">Even though this is not a driver video, it says external long math.</p>



<p class="wp-block-paragraph">It names that as a long function.</p>



<p class="wp-block-paragraph">And then when we call the function, we just kind of grab a long result, 64 bit int, and</p>



<p class="wp-block-paragraph">then we print it with printf because this is a C program, not C++.</p>



<p class="wp-block-paragraph">integers, assigned integers. Let&#8217;s see in assembly. Let&#8217;s see. Where&#8217;s that? Yeah, there we go.</p>



<p class="wp-block-paragraph">So just keep in mind what I&#8217;ve been showing you is idiv, which is a signed integer, which is what</p>



<p class="wp-block-paragraph">you probably want, which means the integer can be positive or negative. And, you know, if you know</p>



<p class="wp-block-paragraph">for sure that you&#8217;re going to be dividing two positive numbers, and there&#8217;s no chance that</p>



<p class="wp-block-paragraph">that the numerator or denominator or the quotient or you know any of those things are going to be</p>



<p class="wp-block-paragraph">negative then you could you could divide unsigned integers and then you can use larger values</p>



<p class="wp-block-paragraph">because remember when we have signed values we basically lose half of the I guess maximum</p>



<p class="wp-block-paragraph">possible value that we can represent because we lose one bit to the fact that the integer is signed</p>



<p class="wp-block-paragraph">is for signed sorry i div is for signed and regular div is for unsigned and then</p>



<p class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">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 class="wp-block-paragraph">Hey everybody! Thanks for watching this video again from the bottom of my heart. I really</p>



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



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



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



<p class="wp-block-paragraph">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 class="wp-block-paragraph">able to do more videos, longer videos, better videos, or just I&#8217;ll be able to keep making</p>



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



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping in the middle of the night</p>



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



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me</p>



<p class="wp-block-paragraph">or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now,</p>



<p class="wp-block-paragraph">the screen right now you should see a qr code which you can scan in order to go to the website</p>



<p class="wp-block-paragraph">which i think is also named somewhere at the bottom of this video and it&#8217;ll take you to my</p>



<p class="wp-block-paragraph">main website where you can just kind of like see all the videos i published and the services and</p>



<p class="wp-block-paragraph">tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh</p>



<p class="wp-block-paragraph">clarifications or errata or just future videos that you want to see please leave a comment or</p>



<p class="wp-block-paragraph">what&#8217;s going on you know just send me a comment whatever I also wake up for those in the middle</p>



<p class="wp-block-paragraph">of the night I get I wake up in a cold sweat and I&#8217;m like it would really it really mean the world</p>



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



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



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



<p class="wp-block-paragraph">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>Command Line Arguments in Yasm Assembly with GCC: A Practical Guide</title>
		<link>https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/</link>
					<comments>https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 14 Sep 2025 23:46:11 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[argv argc]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[coding tutorials]]></category>
		<category><![CDATA[command line args]]></category>
		<category><![CDATA[command line arguments]]></category>
		<category><![CDATA[GCC assembly]]></category>
		<category><![CDATA[GCC linking]]></category>
		<category><![CDATA[hybrid programming]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[programming guide]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[x86 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=227</guid>

					<description><![CDATA[<p>Learn to process command line arguments in Yasm assembly with GCC. Practical x86-64 guide for hybrid programs with clear examples. #Assembly #Programming</p>
<p>The post <a href="https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/">Command Line Arguments in Yasm Assembly with GCC: A Practical Guide</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="Command Line Arguments in Yasm Assembly with GCC: A Practical Guide" width="1380" height="776" src="https://www.youtube.com/embed/r5K10mL2OPE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Want to master command line arguments in Yasm assembly? This video breaks down how to access and process args in x86-64 hybrid programs linked with GCC. From understanding argv and argc to looping through arguments, we cover it all with practical examples. Perfect for programmers diving into assembly or looking to level up their low-level coding skills. Check out my other videos for more on Yasm and pure assembly! Subscribe for more coding tutorials. #AssemblyProgramming #YasmAssembly #GCC #x86_64</p>



<p class="wp-block-paragraph">Introduction 00:00:00<br>Command Line Arguments Overview 00:00:03<br>Yasm Assembly and GCC Linking 00:00:07<br>Hybrid Program Explanation 00:01:27<br>Makefile Overview 00:02:16<br>Assembly Program Structure 00:03:16<br>Main Entry Point and Registers 00:03:51<br>Accessing Command Line Arguments 00:04:26<br>Argument Count and Pointers 00:05:03<br>Loop Initialization for Arguments 00:08:57<br>Loop Logic and Dereferencing 00:10:52<br>Printing Arguments 00:13:02<br>Incrementing Pointers in Loop 00:14:08<br>Running the Program 00:15:35<br>Practical Application of Arguments 00:17:24<br>Conclusion and Next Steps 00:17:51<br>Call to Subscribe and Outro 00:18:22</p>



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



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



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



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



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



<li>Subscribing to our Blog</li>



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



<p class="wp-block-paragraph">Hello there. In this video,</p>



<p class="wp-block-paragraph">I&#8217;m going to talk to you about how to accept incoming command line arguments to</p>



<p class="wp-block-paragraph">an x86-64 Yasm assembly program that is probably linking to</p>



<p class="wp-block-paragraph">GCC and is thus probably a hybrid program under the hood.</p>



<p class="wp-block-paragraph">If you don&#8217;t know how to program in Yasm assembly yet,</p>



<p class="wp-block-paragraph">check out my other videos. If you don&#8217;t understand command line arguments yet,</p>



<p class="wp-block-paragraph">yet check my other videos I&#8217;m going to assume a lot of knowledge here I&#8217;m</p>



<p class="wp-block-paragraph">really just going to show you how to pull the arguments inside of assembly</p>



<p class="wp-block-paragraph">using using the GCC setup that they&#8217;ve given you so just a quick just a very</p>



<p class="wp-block-paragraph">quick recap this is not a command line arguments video if we have the program</p>



<p class="wp-block-paragraph">echo and we give it like one argument of just the word hello then it&#8217;s it&#8217;s</p>



<p class="wp-block-paragraph">going to receive one command line argument in addition to its program</p>



<p class="wp-block-paragraph">echo as argument zero and hello as argument index one.</p>



<p class="wp-block-paragraph">And it&#8217;s just going to be able to look at them and say,</p>



<p class="wp-block-paragraph">oh, the user wants me to print this hello string.</p>



<p class="wp-block-paragraph">So it just prints it.</p>



<p class="wp-block-paragraph">And so that&#8217;s kind of how you can tell a program what to do,</p>



<p class="wp-block-paragraph">either when you&#8217;re running them directly</p>



<p class="wp-block-paragraph">or having one program automate another program.</p>



<p class="wp-block-paragraph">Anyway, so in our Yasm assembly programs,</p>



<p class="wp-block-paragraph">how do we actually get that out?</p>



<p class="wp-block-paragraph">Well, for starters, we are dynamically linking.</p>



<p class="wp-block-paragraph">Sorry, not dynamically linking.</p>



<p class="wp-block-paragraph">We are linking a hybrid program.</p>



<p class="wp-block-paragraph">So I just want to make sure that you understand this is the right video for you.</p>



<p class="wp-block-paragraph">If you&#8217;re trying to do this in pure assembly, or if you need to know that for some reason,</p>



<p class="wp-block-paragraph">then you&#8217;ll probably want to watch the next video that I publish.</p>



<p class="wp-block-paragraph">But for now, this is a hybrid program.</p>



<p class="wp-block-paragraph">Hybrid programs, for those of you who don&#8217;t know, it just means you have modules that</p>



<p class="wp-block-paragraph">are written in different source code and they&#8217;re compiled differently, different source code</p>



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



<p class="wp-block-paragraph">So you might have a source code file that&#8217;s written in C++ another one that&#8217;s written in C another one that&#8217;s written in assembly</p>



<p class="wp-block-paragraph">And you compile them all down to their own object files and then you link them together into an executable</p>



<p class="wp-block-paragraph">If GCC is part of the linking process</p>



<p class="wp-block-paragraph">Then it&#8217;s going to end up giving you a main function as your entry point and it&#8217;s going to make things a little bit easier</p>



<p class="wp-block-paragraph">Here&#8217;s my make file. I&#8217;m just going to skim through it real fast. This is not a make file video</p>



<p class="wp-block-paragraph">So if you don&#8217;t understand it go look at my other videos</p>



<p class="wp-block-paragraph">But basically I&#8217;m just going to assemble with Yasm and I&#8217;m going to use G++ as the linker</p>



<p class="wp-block-paragraph">and I&#8217;m going to gobble up all of the object files. Here&#8217;s like a little menu.</p>



<p class="wp-block-paragraph">I have a target for running and building only. And here&#8217;s the main thing that I&#8217;m doing. This</p>



<p class="wp-block-paragraph">is not really a hybrid program. This is just kind of a pure assembly program that is linked with GCC,</p>



<p class="wp-block-paragraph">but you can imagine you could add other source files in C and C++ and still totally get away</p>



<p class="wp-block-paragraph">GCC. But anyway, I&#8217;m just compiling one source code of assembly and,</p>



<p class="wp-block-paragraph">and compiling it down to an object file right here.</p>



<p class="wp-block-paragraph">And then when it comes time to linking, uh, you can kind of tell here, uh,</p>



<p class="wp-block-paragraph">especially if you&#8217;re familiar with make files that I&#8217;m just gobbling up all the</p>



<p class="wp-block-paragraph">object files and linking them together into the executable.</p>



<p class="wp-block-paragraph">So that&#8217;s as far as I&#8217;ll go there again,</p>



<p class="wp-block-paragraph">I&#8217;ve got another video that totally explains a lot more in my assembly program.</p>



<p class="wp-block-paragraph">video just for pure assembly so if you don&#8217;t understand assembly see my other videos but for</p>



<p class="wp-block-paragraph">now we&#8217;re just going to say i&#8217;ve got a data section it&#8217;s got a couple of null terminated strings we&#8217;ve</p>



<p class="wp-block-paragraph">got a hello message a begin message a goodbye message carriage return line feed string i&#8217;ve</p>



<p class="wp-block-paragraph">got the system call codes for you know printing to standard output and to exit the program and then</p>



<p class="wp-block-paragraph">i&#8217;ve got the standard output file handle and then my exit code zero for success again this is all</p>



<p class="wp-block-paragraph">other videos. So because I&#8217;m linking with GCC, my text section here, the actual place where the</p>



<p class="wp-block-paragraph">instructions are, not the data, is going to have a main entry point. So of course, if you had a</p>



<p class="wp-block-paragraph">hybrid program, you could have main somewhere else in a different module and then just call on a</p>



<p class="wp-block-paragraph">function that was inside of assembly. You could totally do that. But for now, I&#8217;m just going to</p>



<p class="wp-block-paragraph">say this is the only source code file. So I mark main as global and I put main right here. And then</p>



<p class="wp-block-paragraph">I preserve all of the Kali saved registers that I&#8217;m supposed to by pushing them at the beginning and popping them in reverse order at the end.</p>



<p class="wp-block-paragraph">And then I return a return value.</p>



<p class="wp-block-paragraph">Standard assembly stuff covered in other videos.</p>



<p class="wp-block-paragraph">But here&#8217;s the key for getting command line arguments in assembly.</p>



<p class="wp-block-paragraph">If you recall, the ABI specifies that in x86-64 programs, the first argument is always supposed to come into a function as RDI in the RDI register.</p>



<p class="wp-block-paragraph">register or rather I should say the first integer argument I haven&#8217;t talked</p>



<p class="wp-block-paragraph">about to float arguments in any of my videos yet in the future you should</p>



<p class="wp-block-paragraph">probably search for other videos that that explain how to do float arguments</p>



<p class="wp-block-paragraph">but for now all of the arguments I&#8217;m talking about in this video are just</p>



<p class="wp-block-paragraph">integer arguments and pointer arguments which is kind of the same thing a</p>



<p class="wp-block-paragraph">pointer is an integer it&#8217;s a 64-bit integer but we&#8217;ll just use it as a</p>



<p class="wp-block-paragraph">using it as an integer so anyway first argument comes in on RDI second argument</p>



<p class="wp-block-paragraph">comes in on RSI and you can imagine that basically this is the thing you&#8217;re</p>



<p class="wp-block-paragraph">probably used to seeing in your C++ programs if we do this int main and then</p>



<p class="wp-block-paragraph">integer arg C and then a character pointer arg V and then an array those are</p>



<p class="wp-block-paragraph">those two registers right there I probably want to change this into long</p>



<p class="wp-block-paragraph">because you know rdi is in 64-bit form but an integer is an unsigned 32-bit int.</p>



<p class="wp-block-paragraph">I don&#8217;t like that so I&#8217;m just going to put long. I probably should put unsigned but I don&#8217;t really</p>



<p class="wp-block-paragraph">care. Anyway so we&#8217;re just going to grab those two things so obviously rdi is probably pretty</p>



<p class="wp-block-paragraph">easy for you to guess how to use it&#8217;s just the number of arguments. In this program we&#8217;re going</p>



<p class="wp-block-paragraph">to use it to control a loop counter that&#8217;s going to loop through all incoming arguments.</p>



<p class="wp-block-paragraph">which is going to be the name of the program in this case it&#8217;ll be I think</p>



<p class="wp-block-paragraph">just main is what I called it which is confusing against the entry point I</p>



<p class="wp-block-paragraph">admit it but imagine that my main program that sits in the file system is</p>



<p class="wp-block-paragraph">actually called program or hello or whatever so that&#8217;s the number of</p>



<p class="wp-block-paragraph">arguments in RDI and then RSI notice how it&#8217;s a character pointer to an array of</p>



<p class="wp-block-paragraph">characters that means it&#8217;s actually a pointer to a pointer and the reason for</p>



<p class="wp-block-paragraph">every argument on the command line like for instance if i went back here and i said echo</p>



<p class="wp-block-paragraph">hello let&#8217;s just do without quotes hello you i&#8217;ve given it two arguments and so when the program</p>



<p class="wp-block-paragraph">launches it&#8217;s going to receive three total it&#8217;s going to receive its own program name at index zero</p>



<p class="wp-block-paragraph">echo it&#8217;s going to receive at index one the first argument hello and it&#8217;s going to receive at index</p>



<p class="wp-block-paragraph">two the second argument as you it prints them both so how does echo know how many arguments i have</p>



<p class="wp-block-paragraph">strings. Remember argc is the number of arguments so that&#8217;s kind of easy but how does it dereference</p>



<p class="wp-block-paragraph">all of the strings if there could be any number of strings? Well that&#8217;s this right here.</p>



<p class="wp-block-paragraph">This whole symbol argv is a pointer and it points to an array of pointers, of character</p>



<p class="wp-block-paragraph">pointers. That&#8217;s why it&#8217;s written like this like an array. So it&#8217;s a pointer that points</p>



<p class="wp-block-paragraph">points to an array.</p>



<p class="wp-block-paragraph">So like if you go to that location in memory,</p>



<p class="wp-block-paragraph">then what you will see there for the first eight bytes</p>



<p class="wp-block-paragraph">is just the address of some other memory location where a string has been stored.</p>



<p class="wp-block-paragraph">Then if you advance that pointer another eight bytes for it,</p>



<p class="wp-block-paragraph">you know, in memory, because all pointers are eight bytes or 64 bits,</p>



<p class="wp-block-paragraph">then again, you&#8217;ll see that that value of that next eight bytes</p>



<p class="wp-block-paragraph">is actually a memory location that points to another string somewhere else.</p>



<p class="wp-block-paragraph">So these strings could be all over the place,</p>



<p class="wp-block-paragraph">But the pointers to those strings are contiguous in memory starting with what you were given in RSI.</p>



<p class="wp-block-paragraph">So it&#8217;s a character pointer pointer or an array of character pointers, however you want to imagine that.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ll grab those and then I&#8217;ll just show you how to use them down further in the code.</p>



<p class="wp-block-paragraph">For starters, I&#8217;m calling on an intro function which just basically prints some stuff.</p>



<p class="wp-block-paragraph">I have a custom function that I wrote called print null terminated string.</p>



<p class="wp-block-paragraph">That&#8217;s not the point of this video.</p>



<p class="wp-block-paragraph">I have that explained in other videos a little bit.</p>



<p class="wp-block-paragraph">So it&#8217;s up to you if you want to even care.</p>



<p class="wp-block-paragraph">Right now I just want to be able to print something.</p>



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



<p class="wp-block-paragraph">So after we print our welcome message in the intro, then we print another message basically</p>



<p class="wp-block-paragraph">saying okay now we&#8217;re about to start printing all the arguments.</p>



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



<p class="wp-block-paragraph">Nothing complicated at this point.</p>



<p class="wp-block-paragraph">Then I have another label here with my preferred style of adding a suffix after the function</p>



<p class="wp-block-paragraph">after the function that I&#8217;m currently inside of.</p>



<p class="wp-block-paragraph">And I&#8217;m going to say, all right, this is where I initialize my loop.</p>



<p class="wp-block-paragraph">This is not a looping video, so I&#8217;m going to skim through it kind of.</p>



<p class="wp-block-paragraph">But I&#8217;m basically going to say, let&#8217;s start the loop as basically saying</p>



<p class="wp-block-paragraph">the index of the argument that we&#8217;re currently looking at is zero</p>



<p class="wp-block-paragraph">because we&#8217;ll start with zero.</p>



<p class="wp-block-paragraph">So that means we&#8217;ll print like if we were going to, if our program was named Echo,</p>



<p class="wp-block-paragraph">we would also print Echo itself and not just all the incoming arguments</p>



<p class="wp-block-paragraph">that the user might have typed.</p>



<p class="wp-block-paragraph">index zero and then we&#8217;re going to use r15 as the current character pointer so r15 its current</p>



<p class="wp-block-paragraph">character pointer is going to be coming from r13 which was the argv argument so that means</p>



<p class="wp-block-paragraph">r15 is now going to hold a pointer uh let&#8217;s see it&#8217;s going to hold</p>



<p class="wp-block-paragraph">when you have an array and the array is contiguous in memory then the pointer itself that points to</p>



<p class="wp-block-paragraph">the array is also a pointer to the first item that&#8217;s just kind of the way it works like if we</p>



<p class="wp-block-paragraph">have 10 items and it&#8217;s an array then your pointer to that array is also a pointer to the first item</p>



<p class="wp-block-paragraph">in the array if that makes sense so if r13 was a pointer to an array it&#8217;s also a pointer to the</p>



<p class="wp-block-paragraph">go off to a null terminated string i know that sounds weird you got to get used to double dereferencing</p>



<p class="wp-block-paragraph">here so all we really need to do to access uh the first pointer to the actual string is take the</p>



<p class="wp-block-paragraph">array pointer that we have and dereference it one time so remember we&#8217;re receiving an argv</p>



<p class="wp-block-paragraph">a pointer to a pointer or like a you know a double pointer if we dereference it once then we should</p>



<p class="wp-block-paragraph">terminated string. Anyway, so that&#8217;s the initialization part. I&#8217;m going to start looking</p>



<p class="wp-block-paragraph">at the first string here and I&#8217;m going to say we&#8217;re at index zero. And then here&#8217;s the top of</p>



<p class="wp-block-paragraph">my loop. You can imagine this is a while loop. This is not a while video, but you know, there it is.</p>



<p class="wp-block-paragraph">And we&#8217;re just going to ask first, are we actually done? Do we need to break the loop? I like to do</p>



<p class="wp-block-paragraph">that at the top of my while loop. And how we&#8217;ll do that is we&#8217;ll say, all right, R14 is the index</p>



<p class="wp-block-paragraph">the number of arguments so notice how like right here we grabbed RDI right</p>



<p class="wp-block-paragraph">into R12 right away so basically I&#8217;m saying if the index we&#8217;re currently</p>



<p class="wp-block-paragraph">looking at is greater than or equal to the number of arguments then we&#8217;re</p>



<p class="wp-block-paragraph">actually already done remember if we&#8217;re using zero based indexing and you</p>



<p class="wp-block-paragraph">suppose that you have three items in your array their indexes would be zero</p>



<p class="wp-block-paragraph">So that means if the size of your array is three, like the count, like the RFC, that</p>



<p class="wp-block-paragraph">means the last valid index is two.</p>



<p class="wp-block-paragraph">It&#8217;s one less than the size.</p>



<p class="wp-block-paragraph">You know, it&#8217;s one less than the count.</p>



<p class="wp-block-paragraph">So I&#8217;m saying at this point that if we ever reach an index number that is equal to the</p>



<p class="wp-block-paragraph">size, then we&#8217;ve already finished.</p>



<p class="wp-block-paragraph">We do not need to look at the current item.</p>



<p class="wp-block-paragraph">So I&#8217;m comparing those two registers.</p>



<p class="wp-block-paragraph">You know, where are we looking at versus what is the count?</p>



<p class="wp-block-paragraph">You know, what is the index we&#8217;re looking at versus the count?</p>



<p class="wp-block-paragraph">If the index we&#8217;re looking at is greater than or equal to the count, then that means we&#8217;re done.</p>



<p class="wp-block-paragraph">I&#8217;m going to jump out of the loop to a label called main loop done.</p>



<p class="wp-block-paragraph">So then, you know, that&#8217;s down here.</p>



<p class="wp-block-paragraph">Basically, we just say goodbye and then we jump to our exit function.</p>



<p class="wp-block-paragraph">And all that does is it just returns from main with some kind of a success code.</p>



<p class="wp-block-paragraph">You know, so just you can imagine in C++, it&#8217;s just return zero.</p>



<p class="wp-block-paragraph">So, you know, breaking the loop doesn&#8217;t really do anything except end the program and say goodbye.</p>



<p class="wp-block-paragraph">However, if we did not jump, then the, let&#8217;s see if we did not jump, let&#8217;s, let&#8217;s see if</p>



<p class="wp-block-paragraph">we did jump, we compared it, we realized that we&#8217;re done.</p>



<p class="wp-block-paragraph">Then it&#8217;s going to jump down to the, to the done.</p>



<p class="wp-block-paragraph">But if we&#8217;re not done, execution is going to fall through to the next statement.</p>



<p class="wp-block-paragraph">So that means we&#8217;re going to end up doing something with the current item inside of</p>



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



<p class="wp-block-paragraph">So what are we going to do?</p>



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



<p class="wp-block-paragraph">was your pointer to a pointer it was your double pointer there if we dereference it once then</p>



<p class="wp-block-paragraph">instead of being a pointer to an array of pointers it&#8217;s going to be a pointer to one string one one</p>



<p class="wp-block-paragraph">character that starts one string so dereferencing it once means you know i put brackets around the</p>



<p class="wp-block-paragraph">double pointer it&#8217;s now dereferenced once that means rdi is going to receive a pointer to one</p>



<p class="wp-block-paragraph">into my function my function takes two arguments it wants a pointer to a string and it wants</p>



<p class="wp-block-paragraph">the file descriptor to print to again other videos explain you know printing to standard</p>



<p class="wp-block-paragraph">output but i&#8217;m just going to print the string so this part right here should actually print</p>



<p class="wp-block-paragraph">the argument and then after we&#8217;re done with that uh well maybe i should change this</p>



<p class="wp-block-paragraph">so that it&#8217;s a little bit more clear i&#8217;m going to maybe push this down and say</p>



<p class="wp-block-paragraph">these lines up here actually do something they do the printing by</p>



<p class="wp-block-paragraph">dereferencing and then here we just sort of increment along the array of pointers</p>



<p class="wp-block-paragraph">and jump back to the top of the loop so what are we doing here with r14</p>



<p class="wp-block-paragraph">remember r14 was the index that we&#8217;re currently looking at so we we start</p>



<p class="wp-block-paragraph">looking at index 0 line 83 is going to say let&#8217;s next look at index 1 and then</p>



<p class="wp-block-paragraph">this part right here add 8 to r15 that just basically means remember r15 was</p>



<p class="wp-block-paragraph">remember r15 was the double pointer the double pointer like i said before it&#8217;s looking at an</p>



<p class="wp-block-paragraph">array of pointers so it&#8217;s actually a pointer to a pointer if i increase the memory location that</p>



<p class="wp-block-paragraph">r15 holds then it&#8217;s now looking at the next pointer it&#8217;s moving through the array r15 at</p>



<p class="wp-block-paragraph">that point would no longer be a valid pointer to the original array it&#8217;s sort of like a running</p>



<p class="wp-block-paragraph">pointer it&#8217;s kind of like scanning all of the pointers but a pointer is eight bytes so if we&#8217;re</p>



<p class="wp-block-paragraph">a pointer is eight bytes so if we&#8217;re just looking at the first pointer and we increase the memory</p>



<p class="wp-block-paragraph">location that we&#8217;re looking at by eight bytes now we&#8217;re looking at the second pointer so then on the</p>



<p class="wp-block-paragraph">next iteration of the loop if we dereference then we&#8217;re going to end up dereferencing to the second</p>



<p class="wp-block-paragraph">string then i just print a little new line here honestly probably should have put that up top</p>



<p class="wp-block-paragraph">sloppy code i was doing this quickly and then we jump to the top of the loop so then we just</p>



<p class="wp-block-paragraph">going up to the top of the loop and printing arguments and advancing to the</p>



<p class="wp-block-paragraph">next pointer. And we just keep going at it.</p>



<p class="wp-block-paragraph">Let me see if this works. Hopefully I didn&#8217;t mess this up while I was screwing</p>



<p class="wp-block-paragraph">around. All right. So I&#8217;m going to do this make run. Okay.</p>



<p class="wp-block-paragraph">So under the hood inside of my make file,</p>



<p class="wp-block-paragraph">maybe let&#8217;s open the make file real fast just so you can see.</p>



<p class="wp-block-paragraph">I&#8217;m just going to nano it real fast here under the hood.</p>



<p class="wp-block-paragraph">So when I call the program to run, notice how I&#8217;m giving it arguments.</p>



<p class="wp-block-paragraph">You can see right here on this line, or actually let me get the line numbers up.</p>



<p class="wp-block-paragraph">You can see on line 48, I am calling my executable, which is named main.</p>



<p class="wp-block-paragraph">In the make file, I&#8217;m using a variable, so don&#8217;t worry about that.</p>



<p class="wp-block-paragraph">But I&#8217;m giving it four arguments.</p>



<p class="wp-block-paragraph">And I&#8217;m just saying first arg, second arg, third arg, fourth arg.</p>



<p class="wp-block-paragraph">So that&#8217;s why you see that printed in the previous screen.</p>



<p class="wp-block-paragraph">screen it&#8217;s just going through all the arguments that I gave it it&#8217;s saying</p>



<p class="wp-block-paragraph">first arg second arg third arg fourth arg notice how it knows when to stop</p>



<p class="wp-block-paragraph">because of the argc that came in on RDI now that the program is built you know</p>



<p class="wp-block-paragraph">I&#8217;ve got my main executable which I named main which I said before was kind</p>



<p class="wp-block-paragraph">of confusing now that I&#8217;ve got it I can just execute it again and give it like</p>



<p class="wp-block-paragraph">one argument of like hello notice how it prints hello on a line by itself you do</p>



<p class="wp-block-paragraph">do it again dudes and notice how every argument I give it it just prints it no</p>



<p class="wp-block-paragraph">matter how many I do I can go a b c d e f g I could probably do this so many</p>



<p class="wp-block-paragraph">times that I exhaust you know a long integer which is would be a horrible</p>



<p class="wp-block-paragraph">endeavor but yeah everything that I put on there it just loops through it and</p>



<p class="wp-block-paragraph">prints it so when you are linking with GCC because you&#8217;re probably using a</p>



<p class="wp-block-paragraph">if you&#8217;re just linking with GCC.</p>



<p class="wp-block-paragraph">GCC makes it really, really easy</p>



<p class="wp-block-paragraph">to access the command line arguments.</p>



<p class="wp-block-paragraph">Imagine now instead of just printing these things,</p>



<p class="wp-block-paragraph">you used them to somehow decide</p>



<p class="wp-block-paragraph">what your program was gonna do.</p>



<p class="wp-block-paragraph">Maybe the user will give you a sub command.</p>



<p class="wp-block-paragraph">Like if you&#8217;re a Git user, we say Git status, right?</p>



<p class="wp-block-paragraph">So the Git says like, oh, you Git launches</p>



<p class="wp-block-paragraph">and it goes, you want the status of something?</p>



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



<p class="wp-block-paragraph">So in your program now,</p>



<p class="wp-block-paragraph">you could read what the user typed,</p>



<p class="wp-block-paragraph">figure out a way to parse it and interpret it</p>



<p class="wp-block-paragraph">have your program&#8217;s behavior adjust to whatever the user typed in okay that&#8217;s it for this video</p>



<p class="wp-block-paragraph">the next video I&#8217;m going to post is how to do basically exactly the same thing but using pure</p>



<p class="wp-block-paragraph">assembly without linking against GCC so like LD is going to be the linker just like pure assembly</p>



<p class="wp-block-paragraph">no extra libraries okay thank you so much for watching this video I hope you learned a little</p>



<p class="wp-block-paragraph">tell your friends and i&#8217;ll see you in the next video</p>



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



<p class="wp-block-paragraph">i do hope you did learn something and have some fun uh if you could do me a please a small little</p>



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



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



<p class="wp-block-paragraph">and grow this community so we&#8217;ll be able to do more videos, longer videos, better videos,</p>



<p class="wp-block-paragraph">or just I&#8217;ll be able to keep making videos in general. So please do me a kindness and subscribe.</p>



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



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



<p class="wp-block-paragraph">what happens every single time. So you could do it as a nice favor to me or you could troll me if</p>



<p class="wp-block-paragraph">you want to just wake me up in the middle of the night, just subscribe and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">just wake up I promise that&#8217;s what will happen also if you look at the middle of</p>



<p class="wp-block-paragraph">the screen right now you should see a QR code which you can scan in order to go</p>



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



<p class="wp-block-paragraph">video and it&#8217;ll take you to my main website where you can just kind of like</p>



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



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



<p class="wp-block-paragraph">or errata or just future videos that you want to see please leave a comment or if you just want to</p>



<p class="wp-block-paragraph">say hey what&#8217;s up what&#8217;s going on you know just send me a comment whatever i also wake up for</p>



<p class="wp-block-paragraph">those in the middle of the night i get i wake up in a cold sweat and i&#8217;m like it would really it</p>



<p class="wp-block-paragraph">really mean the world to me i would really appreciate it so again thank you so much for</p>



<p class="wp-block-paragraph">watching this video and um enjoy the cool music as as i fade into the darkness which is coming for us</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/">Command Line Arguments in Yasm Assembly with GCC: A Practical Guide</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/command-line-arguments-in-yasm-assembly-with-gcc-a-practical-guide/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
