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

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>assembly language programming Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/assembly-language-programming/</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>
		<item>
		<title>x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &#038; ADD (YASM on Ubuntu Linux)</title>
		<link>https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/</link>
					<comments>https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 30 Nov 2025 07:30:56 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly arithmetic]]></category>
		<category><![CDATA[assembly language programming]]></category>
		<category><![CDATA[cpu registers assembly]]></category>
		<category><![CDATA[imul instruction]]></category>
		<category><![CDATA[intel imul]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[nasm imul]]></category>
		<category><![CDATA[reverse engineering basics]]></category>
		<category><![CDATA[signed multiplication assembly]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[ubuntu assembly]]></category>
		<category><![CDATA[x86_64 tutorial]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<category><![CDATA[x86-64 multiplication]]></category>
		<category><![CDATA[Yasm tutorial]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=259</guid>

					<description><![CDATA[<p>Hands-on x86-64 assembly tutorial showing how to perform signed integer multiplication using the IMUL instruction in YASM/NASM on Linux. Covers immediate and global operands, two/three-operand forms, basic addition, with complete working code and live execution.</p>
<p>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/">x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &amp; ADD (YASM on Ubuntu Linux)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &amp; ADD (YASM on Ubuntu Linux)" width="1380" height="776" src="https://www.youtube.com/embed/QmIdCYsxTTM?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 actually understand how signed integer multiplication works at the CPU level? In this straight-to-the-point x86-64 assembly tutorial we dive into the IMUL instruction &#8211; the proper way to multiply signed integers in YASM/NASM on Linux.</p>



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



<ul class="wp-block-list">
<li>The difference between unsigned (MUL) and signed (IMUL) multiplication</li>



<li>Two-operand vs three-operand IMUL forms</li>



<li>Multiplying immediate values vs global variables</li>



<li>Loading values into registers (mov) and performing fast multiplication</li>



<li>Basic addition with ADD and INC</li>



<li>Full working example that prints results so you can see it in action</li>



<li>Why you should respect the ABI and save callee-saved registers (R12-R15)</li>
</ul>



<p class="wp-block-paragraph">Everything is built with YASM on Ubuntu, linked with a tiny C driver, and run instantly. No fluff, just real assembly code you can copy and run right now.</p>



<p class="wp-block-paragraph">Code on screen, calculator verification, and clear explanation of every line. Perfect if you&#8217;re learning low-level programming, reverse engineering, or just want to know what really happens when you write a = b * c; in C.</p>



<p class="wp-block-paragraph">Introduction to Integer Arithmetic 00:00:00<br>Recommended Book and Resources 00:00:39<br>Instruction Set Overview 00:01:17<br>Addition Instruction (ADD) 00:01:43<br>Unsigned vs Signed Multiplication 00:02:20<br>Signed Multiplication with IMUL 00:03:05<br>IMUL Three-Operand Form 00:03:40<br>IMUL Two-Operand Form 00:04:12<br>Squaring with IMUL 00:04:50<br>Setting Up the Sample Program 00:05:08<br>Data Section and Strings 00:06:13<br>Text Section and External Functions 00:06:58<br>Math Function Entry Point 00:08:01<br>Multiply Test Function Setup 00:08:42<br>Multiplying Immediate Values 00:09:48<br>Printing the Immediate Result 00:10:21<br>Running and Verifying Immediates 00:12:53<br>Multiplying Global Variables 00:13:49<br>Loading Globals into Registers 00:14:07<br>IMUL with Globals Demo 00:14:37<br>Addition with INC and ADD 00:15:36<br>Final Results and Verification 00:17:09<br>Wrap-Up and Closing 00:17:22<br>Outro and Subscribe Request 00:17:44</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">okay</p>



<p class="wp-block-paragraph">hey everybody in this video i&#8217;d like to talk to you a little bit about</p>



<p class="wp-block-paragraph">integer arithmetic in yasm assembly on an x86-64 system</p>



<p class="wp-block-paragraph">so what am i talking about well integer arithmetic i want to take integers and i want to add them</p>



<p class="wp-block-paragraph">together multiply them together whatever this video is mostly going to focus on signed integer</p>



<p class="wp-block-paragraph">but I&#8217;m going to briefly skim through unsigned integer multiplication and also</p>



<p class="wp-block-paragraph">addition and anyway so for starters let me go up to this the top of this book</p>



<p class="wp-block-paragraph">here that I&#8217;ve got I&#8217;m going to show you pages from a book that I really really</p>



<p class="wp-block-paragraph">love I mentioned it in a lot of my other videos it&#8217;s called x86 64 assembly</p>



<p class="wp-block-paragraph">language programming with Ubuntu and it is written by a brilliant doctor</p>



<p class="wp-block-paragraph">professor who is just interested in everyone learning this is an open source</p>



<p class="wp-block-paragraph">you can get yourself a copy for free just look that title and name up you can find his website</p>



<p class="wp-block-paragraph">you can grab a copy and you too can become an assembly expert just by reading this book</p>



<p class="wp-block-paragraph">honestly this book has everything that you really need to to really level yourself off</p>



<p class="wp-block-paragraph">level yourself up well beyond i think the master&#8217;s level of college probably</p>



<p class="wp-block-paragraph">not as much as the person who wrote the book but you never know anyway let me uh</p>



<p class="wp-block-paragraph">I want to go down to section 7 here and it&#8217;s called instruction set overview.</p>



<p class="wp-block-paragraph">So here you can have like an overview of like all different types of instructions, but I&#8217;m</p>



<p class="wp-block-paragraph">going to focus on the integer arithmetic instructions.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go to 7.5.</p>



<p class="wp-block-paragraph">Notice how the first section in here is addition.</p>



<p class="wp-block-paragraph">I&#8217;m not going to give you an addition example because it&#8217;s just too easy.</p>



<p class="wp-block-paragraph">You pretty much use just use the add instruction and then you have a source and destination</p>



<p class="wp-block-paragraph">comma source in x86 yasm assembly it&#8217;s pretty much the destination is the first operand so uh</p>



<p class="wp-block-paragraph">that&#8217;s kind of implied usually when you have just like a two operand instruction now we could add</p>



<p class="wp-block-paragraph">using three operands and the the first one would be the destination and then the other two would</p>



<p class="wp-block-paragraph">be the sources but i&#8217;m going to scroll down past that to the integer multiplication section 7.5.3</p>



<p class="wp-block-paragraph">unsigned multiplication is a little tricky and</p>



<p class="wp-block-paragraph">You kind of have to decide what data size you&#8217;re going to go with and then you use the instruction</p>



<p class="wp-block-paragraph">uh, and then you end up with some some uh, some answers that are spread across multiple registers</p>



<p class="wp-block-paragraph">I&#8217;m going to eventually make other videos where I can talk to you about how to manipulate multiple registers or at least know that they&#8217;re there like</p>



<p class="wp-block-paragraph">not 64-bit registers so if we were clever we could just sort of manipulate those so that</p>



<p class="wp-block-paragraph">the whole answer showed up on one single 64-bit register if we wanted to but just know you can</p>



<p class="wp-block-paragraph">multiply unsigned integers it&#8217;s a little bit more complicated than signed integers so in this video</p>



<p class="wp-block-paragraph">i&#8217;m just going to talk about signed integers let&#8217;s see so signed integer multiplication</p>



<p class="wp-block-paragraph">so in order that&#8217;s a section 7.5.3.2 if you want to multiply two integers and they&#8217;re signed</p>



<p class="wp-block-paragraph">and they&#8217;re signed meaning they have a plus or a minus sign meaning they&#8217;re designated as either</p>



<p class="wp-block-paragraph">positive or negative rather than not designated at all which would usually just mean positive</p>



<p class="wp-block-paragraph">you can use imall as an instruction and well you can just provide you know one operand two operands</p>



<p class="wp-block-paragraph">or three operands let&#8217;s do the three operand version first because that&#8217;s probably the easiest</p>



<p class="wp-block-paragraph">So we would take a number that&#8217;s sitting in a register and then we would multiply it by some immediate.</p>



<p class="wp-block-paragraph">Notice how IMM is sitting here.</p>



<p class="wp-block-paragraph">The result of multiplying a register by some number would just go into the destination operand.</p>



<p class="wp-block-paragraph">So that means you&#8217;d put a register here.</p>



<p class="wp-block-paragraph">You&#8217;d put a register here.</p>



<p class="wp-block-paragraph">You&#8217;d put an immediate here in one instruction.</p>



<p class="wp-block-paragraph">Then you&#8217;d have some sort of an answer on what these two numbers were multiplied against each other.</p>



<p class="wp-block-paragraph">then you&#8217;re allowed to specify a register for the destination and a register for the source.</p>



<p class="wp-block-paragraph">But notice how the destination gets overwritten here.</p>



<p class="wp-block-paragraph">So, you know, it&#8217;s going to multiply this number by this number</p>



<p class="wp-block-paragraph">and then just store the result in the first register.</p>



<p class="wp-block-paragraph">So you will kind of overwrite something.</p>



<p class="wp-block-paragraph">It&#8217;s maybe more convenient for you to use the three operand version</p>



<p class="wp-block-paragraph">if you only want to multiply by an immediate.</p>



<p class="wp-block-paragraph">But, you know, anyway, and then for the, let&#8217;s see, let&#8217;s see, I think you can square a number</p>



<p class="wp-block-paragraph">just by specifying its source here.</p>



<p class="wp-block-paragraph">It&#8217;ll multiply a number by itself.</p>



<p class="wp-block-paragraph">And then let me just go down a little bit.</p>



<p class="wp-block-paragraph">I&#8217;m starting to get lost in this book.</p>



<p class="wp-block-paragraph">Let me go down a little bit.</p>



<p class="wp-block-paragraph">Well, I guess that&#8217;s all we really had to say.</p>



<p class="wp-block-paragraph">Okay, so let me show you a sample program that I&#8217;ve prepared.</p>



<p class="wp-block-paragraph">that I&#8217;ve prepared so that we can multiply. Okay, sample program. I think I need to rehearse myself</p>



<p class="wp-block-paragraph">a little bit more before I record videos. Anyway, I&#8217;ve got this empty source code file here. Keep in</p>



<p class="wp-block-paragraph">mind that I don&#8217;t need that. Keep in mind that I have a sample program here already. I&#8217;m not going</p>



<p class="wp-block-paragraph">to really focus on the contents of it because this is not what did I do wrong here? Main and math.</p>



<p class="wp-block-paragraph">main empty no no where&#8217;s my make file oh I forgot to open it this is not a video</p>



<p class="wp-block-paragraph">about make files or hybrid programs or anything like that that&#8217;s covered in my</p>



<p class="wp-block-paragraph">other videos so I&#8217;m just gonna say hey I&#8217;ve got a make file that compiles my</p>



<p class="wp-block-paragraph">assembly source code and then I&#8217;ve got a driver C source code file which its</p>



<p class="wp-block-paragraph">entire job is just to have the main entry point because I&#8217;m linking against</p>



<p class="wp-block-paragraph">the GCC libraries and then its job is just a call on a math function which</p>



<p class="wp-block-paragraph">So now my assembly module is blank.</p>



<p class="wp-block-paragraph">What should we do?</p>



<p class="wp-block-paragraph">First I&#8217;m going to copy paste from my solution here, just a bunch of strings.</p>



<p class="wp-block-paragraph">So I&#8217;ve got a data section that I&#8217;m going to get set up.</p>



<p class="wp-block-paragraph">This is not a video about the basics of assembly programming.</p>



<p class="wp-block-paragraph">See my other videos, but for now I&#8217;m just going to say, well, I&#8217;ve got a bunch of strings.</p>



<p class="wp-block-paragraph">I&#8217;m going to say the result of multiplying immediates is this, the result of multiplying</p>



<p class="wp-block-paragraph">globals is that.</p>



<p class="wp-block-paragraph">multiplications and then see the results. Okay, so then a couple more variables</p>



<p class="wp-block-paragraph">before we&#8217;re finished is I&#8217;m gonna now say that we have a system call for</p>



<p class="wp-block-paragraph">system right which is not what this video is about. See my other videos for</p>



<p class="wp-block-paragraph">system calls. Same thing for file descriptors it&#8217;s there I&#8217;m not gonna</p>



<p class="wp-block-paragraph">explain it too much I&#8217;m just gonna print a standard output and then from this</p>



<p class="wp-block-paragraph">program we&#8217;re gonna I&#8217;m gonna return just an integer for whatever reason I&#8217;m</p>



<p class="wp-block-paragraph">for whatever reason. I&#8217;m just saving it as a variable. And then we want to multiply some</p>



<p class="wp-block-paragraph">integers. So I&#8217;m storing two integers in the global section, you know, still inside of the</p>



<p class="wp-block-paragraph">dot data section. So that&#8217;s, these are going to be global variables. They&#8217;re going to be both</p>



<p class="wp-block-paragraph">be quad words. So I can load up into a quad registers or quad word registers. And now I&#8217;m</p>



<p class="wp-block-paragraph">ready to start my text section. So the first thing that I&#8217;m going to do is just, you know,</p>



<p class="wp-block-paragraph">that&#8217;s where all of our instructions go and then I&#8217;m going to name two external</p>



<p class="wp-block-paragraph">symbols don&#8217;t worry about these symbols they are not the point of this video but</p>



<p class="wp-block-paragraph">you know I have a little library that I wrote that helps me print and and take</p>



<p class="wp-block-paragraph">input from the user I&#8217;m gonna print an integer I&#8217;m gonna take an input from the</p>



<p class="wp-block-paragraph">from the user so don&#8217;t worry about that that&#8217;s not the point of this video</p>



<p class="wp-block-paragraph">really I just want to make it so that when I demo I can just easily type in an</p>



<p class="wp-block-paragraph">and then just print it to the user.</p>



<p class="wp-block-paragraph">So let&#8217;s look at our main entry point here.</p>



<p class="wp-block-paragraph">Remember the driver calls on a function called math.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna make an entry point called math,</p>



<p class="wp-block-paragraph">mark it as global so the driver can call on it.</p>



<p class="wp-block-paragraph">And then I&#8217;m gonna, whoops, why did I put load here?</p>



<p class="wp-block-paragraph">Do, I&#8217;m gonna put, I&#8217;m gonna say do it right there.</p>



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



<p class="wp-block-paragraph">I&#8217;m just gonna call on a function I wrote</p>



<p class="wp-block-paragraph">called multiply test, kind of pointless</p>



<p class="wp-block-paragraph">if you think about the fact that there&#8217;s like no code</p>



<p class="wp-block-paragraph">in the main function here.</p>



<p class="wp-block-paragraph">like no code in the main function here but that&#8217;s the way i like to do it when we&#8217;re done we&#8217;re just</p>



<p class="wp-block-paragraph">going to return our return value that&#8217;s already been defined and so now i&#8217;m ready to start setting</p>



<p class="wp-block-paragraph">up our multiply test maybe i&#8217;ll just copy paste the header here and i&#8217;ll say this is going to be</p>



<p class="wp-block-paragraph">a label called multiply test because i&#8217;m going to call on it and try to use it like a function</p>



<p class="wp-block-paragraph">i&#8217;m going to put a return instruction at the very end so now it really is a function</p>



<p class="wp-block-paragraph">I&#8217;m going to use these registers.</p>



<p class="wp-block-paragraph">So I&#8217;m going to use R12, R13, R14, and R15 to hold temporary immediates and globals</p>



<p class="wp-block-paragraph">just for multiplication.</p>



<p class="wp-block-paragraph">So I&#8217;m basically going to be using the registers to hold my data.</p>



<p class="wp-block-paragraph">And because I&#8217;m going to use all of those registers, remember you have to respect the</p>



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



<p class="wp-block-paragraph">So that means we have to have a push pop pair on all of those registers because they&#8217;re</p>



<p class="wp-block-paragraph">designated as callee saved.</p>



<p class="wp-block-paragraph">saved if anyone calls on your functions and you&#8217;re not respecting the abi then you&#8217;re probably about</p>



<p class="wp-block-paragraph">to crash the program in some way or even if you are the only person who writes any code that your</p>



<p class="wp-block-paragraph">code calls on or gets called from you&#8217;re probably still going to regret not respecting the abi</p>



<p class="wp-block-paragraph">eventually when you forget what&#8217;s going on anyway so let&#8217;s multiply some immediates first thing i&#8217;m</p>



<p class="wp-block-paragraph">and move it into R12 because that&#8217;s where we&#8217;re going to hold some temporary</p>



<p class="wp-block-paragraph">immediates and then the number 256 put that into R13 and then I&#8217;m going to use</p>



<p class="wp-block-paragraph">the I&#8217;m all instruction to just multiply those two values together because this</p>



<p class="wp-block-paragraph">is the two operand version of that instruction the result will also be</p>



<p class="wp-block-paragraph">stored in R12 so that means 233 will be erased from R12 and the answer that will</p>



<p class="wp-block-paragraph">the results for the immediate because I like my programs to be pretty. I&#8217;m not just going to print</p>



<p class="wp-block-paragraph">the numbers and then hope I can remember which number comes first. That&#8217;s usually a huge mistake</p>



<p class="wp-block-paragraph">if you&#8217;re trying to debug something even a little bit complicated. So I&#8217;m going to print my string,</p>



<p class="wp-block-paragraph">which I&#8217;m calling the immediate prefix. And if you look back up at the top right here,</p>



<p class="wp-block-paragraph">it&#8217;s just going to say the result of multiplying immediates is, and then it&#8217;s going to put three</p>



<p class="wp-block-paragraph">asterisks. And then I&#8217;m going to print the actual result. And then the suffix is just going to be</p>



<p class="wp-block-paragraph">And then the suffix is just going to be three more asterisks.</p>



<p class="wp-block-paragraph">So we should basically see the results surrounded by asterisks.</p>



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



<p class="wp-block-paragraph">And then I&#8217;m going to use my library</p>



<p class="wp-block-paragraph">to call a special printing function</p>



<p class="wp-block-paragraph">so that I can just print to the result.</p>



<p class="wp-block-paragraph">So R12 is where the result of the multiplication is.</p>



<p class="wp-block-paragraph">I&#8217;m going to give that as the first argument,</p>



<p class="wp-block-paragraph">which is the RDI register, if we&#8217;re talking about integers.</p>



<p class="wp-block-paragraph">And then I&#8217;m just going to call my function</p>



<p class="wp-block-paragraph">that&#8217;ll print for me.</p>



<p class="wp-block-paragraph">for me and then I&#8217;m going to print the suffix so the suffix is just that other</p>



<p class="wp-block-paragraph">string we talked about just it trails with asterisks I&#8217;m then going to call</p>



<p class="wp-block-paragraph">on a custom function called crlf all that&#8217;s going to do is just print a new</p>



<p class="wp-block-paragraph">line I don&#8217;t know why I do that I could easily put that into the string but it&#8217;s</p>



<p class="wp-block-paragraph">more fun to call functions although to be fair every time you call a function</p>



<p class="wp-block-paragraph">you are jumping to an instruction elsewhere and so the CPU does pay a</p>



<p class="wp-block-paragraph">trying to write programs for high performance you might not want to do that anyway so this is pretty</p>



<p class="wp-block-paragraph">fast because i&#8217;m really just using an immediate and then i&#8217;m loading it into a register i&#8217;m not</p>



<p class="wp-block-paragraph">actually touching global memory so this should be a lightning fast multiplication operation</p>



<p class="wp-block-paragraph">let&#8217;s see if this actually works if i didn&#8217;t screw this up whoops what did i what did i do there oh no</p>



<p class="wp-block-paragraph">i have too many videos now</p>



<p class="wp-block-paragraph">gosh okay clear and make run oh what have I done undefined symbols CRLF did I</p>



<p class="wp-block-paragraph">forget oh I forgot to copy paste my CRLF function yeah so again don&#8217;t worry</p>



<p class="wp-block-paragraph">about CRLF all I&#8217;m doing is just printing a new line carriage return it&#8217;s</p>



<p class="wp-block-paragraph">just you know this is from another example where I was trying to prove to</p>



<p class="wp-block-paragraph">somebody you need to preserve registers and respect the ABI so don&#8217;t even worry</p>



<p class="wp-block-paragraph">you know what I&#8217;m going to leave it as is because if I take that out then I have to take the time</p>



<p class="wp-block-paragraph">to remove the push pop pair because there&#8217;s no point at that I&#8217;m doing that if I&#8217;m not</p>



<p class="wp-block-paragraph">messing with those registers forget about that it&#8217;s not part of this video</p>



<p class="wp-block-paragraph">anyway I&#8217;m going to run the program again and you can see that the result of multiplying</p>



<p class="wp-block-paragraph">immediates is and so this whole string right here was my prefix so again not really the point of</p>



<p class="wp-block-paragraph">the point of this video and then this is the result of multiplying those two different numbers</p>



<p class="wp-block-paragraph">and then the suffix with the stars after so let me just prove this to you where&#8217;s the dang calculator</p>



<p class="wp-block-paragraph">oh man i need to work on my icons like i just did something screwy and i have like no good icons</p>



<p class="wp-block-paragraph">anymore on this virtual machine so we were going to multiply 233 by whoops by what was it 256.</p>



<p class="wp-block-paragraph">five nine six four eight and you can see that&#8217;s on the screen five nine six four eight so we have</p>



<p class="wp-block-paragraph">successfully multiplied signed integers and since they&#8217;re signed we could multiply negative numbers</p>



<p class="wp-block-paragraph">if we wanted to i&#8217;m not going to here but you can now the next thing let&#8217;s try is let&#8217;s multiply</p>



<p class="wp-block-paragraph">stuff sitting in global memory okay so i&#8217;m just going to copy this um where the heck is it</p>



<p class="wp-block-paragraph">at the top we made two global integers we said integer a is equal to 233 and then 256</p>



<p class="wp-block-paragraph">those were the same numbers that we just multiplied so we should probably get the same result</p>



<p class="wp-block-paragraph">if we&#8217;re lucky so i&#8217;m saying i&#8217;m going to move both of these into registers so i&#8217;m going to move</p>



<p class="wp-block-paragraph">a into r14 and i&#8217;m going to move b into r15 just to prove to you that we can and then</p>



<p class="wp-block-paragraph">instruction we did before the result is going to go into r14 so let&#8217;s see i&#8217;m gonna print the</p>



<p class="wp-block-paragraph">prefix real fast here same kind of concept that we talked about before there&#8217;s just going to be</p>



<p class="wp-block-paragraph">a prefix before the result and then i&#8217;m actually going to print the result with this line right</p>



<p class="wp-block-paragraph">here or these lines i&#8217;m just going to print r14 which is holding the result at this point</p>



<p class="wp-block-paragraph">and hey we can trust that R14 wasn&#8217;t killed by the system call because the</p>



<p class="wp-block-paragraph">system call respects the ABI and then I&#8217;m gonna print my suffix right here and</p>



<p class="wp-block-paragraph">then a CRLF and that&#8217;s basically the idea let&#8217;s just double check here that</p>



<p class="wp-block-paragraph">we get the same result twice make run notice how it prints the same result</p>



<p class="wp-block-paragraph">twice once with globals once with immediates so if you were wondering</p>



<p class="wp-block-paragraph">before this video how to multiply immediates or how to multiply globals</p>



<p class="wp-block-paragraph">well there you go just put them into registers first and then call I&#8217;m all</p>



<p class="wp-block-paragraph">pretty fast let&#8217;s just do one other thing for fun I&#8217;m going to let&#8217;s see add</p>



<p class="wp-block-paragraph">one number to our 14 so to the result of the multiplication and then I&#8217;m going to</p>



<p class="wp-block-paragraph">add five to it just to prove to you that we can increase an integer by one and we</p>



<p class="wp-block-paragraph">we print it before we do the prefix extra stuff just for fun so here I&#8217;m gonna increase r14 and</p>



<p class="wp-block-paragraph">I&#8217;m gonna say that it is a q word I guess that&#8217;s implied already because r14 is considered a 64-bit</p>



<p class="wp-block-paragraph">register but if we wanted to increase I don&#8217;t know just for the sake of argument if we wanted</p>



<p class="wp-block-paragraph">to like increase this right here let me just show you real fast how we do it we would say increase</p>



<p class="wp-block-paragraph">would say increase q word and then name the integer in global memory so in global memory</p>



<p class="wp-block-paragraph">the system doesn&#8217;t really know by default if it&#8217;s a one byte integer a two byte integer or four or</p>



<p class="wp-block-paragraph">eight so you just have to specify the data size so it knows which integers to look at when it&#8217;s</p>



<p class="wp-block-paragraph">considering what the original value is and how to overflow and when to overflow but you know just</p>



<p class="wp-block-paragraph">for fun I&#8217;m putting that in here so it&#8217;s going to increase the result by one and then it&#8217;s going to</p>



<p class="wp-block-paragraph">and it&#8217;s going to do that using the addition instruction.</p>



<p class="wp-block-paragraph">So we&#8217;re going to add R14 with 5.</p>



<p class="wp-block-paragraph">We can put an immediate there,</p>



<p class="wp-block-paragraph">and the result is going to be stored in R14.</p>



<p class="wp-block-paragraph">So basically R14 is equal to R14 plus 5.</p>



<p class="wp-block-paragraph">And so overall, when we add those two things in there,</p>



<p class="wp-block-paragraph">the second result should now be about 6 higher than the first result.</p>



<p class="wp-block-paragraph">So if you kind of look at this right here,</p>



<p class="wp-block-paragraph">we have 648, and then we have 644,</p>



<p class="wp-block-paragraph">which is 6 numbers higher than the original result.</p>



<p class="wp-block-paragraph">than the original result. So these are the basics of integer arithmetic with</p>



<p class="wp-block-paragraph">signed multiplication and addition. I think that&#8217;s all I really wanted to show</p>



<p class="wp-block-paragraph">you. Yeah, okay. So I hope you learned a little bit of stuff from this video. I</p>



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



<p class="wp-block-paragraph">happy coding! Hey everybody! Thanks for watching this video again from the</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.</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</p>



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



<p class="wp-block-paragraph">wake up because i know somebody subscribed or followed it just wakes me up and i get filled</p>



<p class="wp-block-paragraph">with joy that&#8217;s exactly what happens every single time so you could do it as a nice favor to me or</p>



<p class="wp-block-paragraph">you could 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 i&#8217;ll just wake up i promise that&#8217;s what will happen also uh if you look at the middle</p>



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



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



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



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



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



<p class="wp-block-paragraph">And if you have a suggestion for clarifications or errata or just future videos that you want to see,</p>



<p class="wp-block-paragraph">please leave a comment.</p>



<p class="wp-block-paragraph">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.</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night.</p>



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



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



<p class="wp-block-paragraph">and um 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>The post <a href="https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/">x86-64 Assembly: Signed Integer Multiplication and Addition with IMUL &amp; ADD (YASM on Ubuntu Linux)</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/x86-64-assembly-signed-integer-multiplication-and-addition-with-imul-add-yasm-on-ubuntu-linux/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
