<?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>idiv instruction Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/idiv-instruction/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/idiv-instruction/</link>
	<description></description>
	<lastBuildDate>Sun, 14 Dec 2025 09:12:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>idiv instruction Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/idiv-instruction/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
	</channel>
</rss>
