<?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>c++ tutorial Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/c-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/c-tutorial/</link>
	<description></description>
	<lastBuildDate>Sun, 09 Nov 2025 01:47:42 +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>c++ tutorial Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/c-tutorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</title>
		<link>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/</link>
					<comments>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 09 Nov 2025 01:47:40 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly tutorial]]></category>
		<category><![CDATA[build system]]></category>
		<category><![CDATA[C and assembly]]></category>
		<category><![CDATA[C++ assembly interop]]></category>
		<category><![CDATA[c++ tutorial]]></category>
		<category><![CDATA[extern C]]></category>
		<category><![CDATA[hybrid programming]]></category>
		<category><![CDATA[linking object files]]></category>
		<category><![CDATA[low level programming]]></category>
		<category><![CDATA[Makefile tutorial]]></category>
		<category><![CDATA[mixed language programming]]></category>
		<category><![CDATA[name mangling]]></category>
		<category><![CDATA[object files]]></category>
		<category><![CDATA[systems programming]]></category>
		<category><![CDATA[x86-64 assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=248</guid>

					<description><![CDATA[<p>Learn to build a hybrid program mixing C++, C, and x86-64 assembly. Covers object files, linking, extern "C", name mangling, and a full Makefile. Run one executable with modules calling each other.</p>
<p>The post <a href="https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/">Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Mix C++, C, and Assembly into One Program - Hybrid Program Full Build Tutorial" width="1380" height="776" src="https://www.youtube.com/embed/Izu807v7NbU?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 write a single program that uses C++ for high-level logic, C for portability, and assembly for speed? This video shows exactly how to do it.</p>



<p class="wp-block-paragraph">We build a complete hybrid program from scratch:</p>



<ul class="wp-block-list">
<li>driver.cpp &#8211; contains main(), prints intro, calls assembly</li>



<li>c.c &#8211; simple printf() function</li>



<li>cpp.cpp &#8211; C++ function (with extern &#8220;C&#8221; to disable name mangling)</li>



<li>hello.asm &#8211; assembly module that prints messages, calls C and C++ functions</li>
</ul>



<p class="wp-block-paragraph">You’ll see:</p>



<ul class="wp-block-list">
<li>Why hybrid programs matter</li>



<li>How source files → object files → final executable</li>



<li>The role of the driver and why main() lives once</li>



<li>Name mangling in C++ and how extern &#8220;C&#8221; fixes interop</li>



<li>Global / extern in assembly to expose and call functions</li>



<li>A clean Makefile that compiles each module separately</li>



<li>Live build and execution with output explained line-by-line</li>
</ul>



<p class="wp-block-paragraph">By the end you’ll know how to call across languages safely, pass basic args via registers, and structure projects for fast incremental builds.</p>



<p class="wp-block-paragraph">No prior assembly required &#8211; just basic C/C++ familiarity. Code is copy-paste ready.</p>



<p class="wp-block-paragraph">Timestamps in comments. Source files linked below.</p>



<p class="wp-block-paragraph">Like &amp; subscribe for more systems programming!</p>



<p class="wp-block-paragraph">What is a Hybrid Program 00:00:00<br>Why Use Hybrid Programs 00:00:21<br>Program Modules Overview 00:01:10<br>Compilation to Object Files 00:02:23<br>Makefile Introduction 00:04:26<br>Driver Module Setup 00:06:43<br>Name Mangling Explained 00:07:18<br>C++ Module with extern C 00:07:43<br>Driver Main Function 00:10:12<br>C Module Implementation 00:11:52<br>Assembly Module Setup 00:12:41<br>Assembly Data Section 00:13:03<br>Assembly Text Section 00:14:18<br>Global and External Functions 00:14:35<br>Assembly System Calls 00:16:20<br>Calling C from Assembly 00:18:58<br>Calling C++ from Assembly 00:20:32<br>Program Execution Flow 00:21:05<br>Running the Program 00:23:59<br>Passing Arguments Between Modules 00:26:23<br>Hybrid Program Summary 00:29:11</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. Let&#8217;s talk about hybrid programs.</p>



<p class="wp-block-paragraph">What&#8217;s a hybrid program?</p>



<p class="wp-block-paragraph">A hybrid program is basically a program where you have multiple modules,</p>



<p class="wp-block-paragraph">many written in different languages, all compiled into one executable program.</p>



<p class="wp-block-paragraph">Why would you want to make a hybrid program? Well, you know,</p>



<p class="wp-block-paragraph">maybe you have C++ as one of the modules that you&#8217;re creating and C++ is a high</p>



<p class="wp-block-paragraph">And C++ is a high level language, so it makes it easier to write big, robust programs with GUIs and like big design, big fancy design patterns and things.</p>



<p class="wp-block-paragraph">But maybe at some point you want to interface with the C program.</p>



<p class="wp-block-paragraph">So it might make sense to, you know, kind of jump into some C code.</p>



<p class="wp-block-paragraph">Maybe also you want to write a little module in assembly because maybe one part of your code needs to be really, really fast and efficient in some way.</p>



<p class="wp-block-paragraph">the code compiles down to machine code.</p>



<p class="wp-block-paragraph">Assembly is just, you know, a level up from machine code.</p>



<p class="wp-block-paragraph">So let&#8217;s see, I&#8217;m just gonna write a quick program</p>



<p class="wp-block-paragraph">to demonstrate that you can do this</p>



<p class="wp-block-paragraph">and you can have these modules call each other.</p>



<p class="wp-block-paragraph">First off, I should probably draw a little bit</p>



<p class="wp-block-paragraph">about what I&#8217;m even talking about with multiple modules.</p>



<p class="wp-block-paragraph">So I&#8217;m gonna do my little annotator here</p>



<p class="wp-block-paragraph">and yeah, there we go.</p>



<p class="wp-block-paragraph">Basically imagine that you have a C++ module</p>



<p class="wp-block-paragraph">and we&#8217;ll call it the driver.</p>



<p class="wp-block-paragraph">and we&#8217;ll call it the driver and I&#8217;ll say like driver.cpp. Okay I think I&#8217;m</p>



<p class="wp-block-paragraph">drawing this backwards. I&#8217;m gonna draw this down here. Driver.cpp.</p>



<p class="wp-block-paragraph">Excuse my penmanship I don&#8217;t have a smoother setup on this thing. So we have</p>



<p class="wp-block-paragraph">driver.cpp that&#8217;s one source code. Maybe you have like another module, another</p>



<p class="wp-block-paragraph">just some C code. The driver, usually the way I like to refer to it, and one of my favorite,</p>



<p class="wp-block-paragraph">you know, assembly books refers to it is the driver is where the main function is so that</p>



<p class="wp-block-paragraph">it&#8217;s sort of like the entry point of your program. But then maybe we&#8217;ll have another module that has</p>



<p class="wp-block-paragraph">some other kind of CPP code in there. I&#8217;m just going to call this file cpp.cpp. And maybe we</p>



<p class="wp-block-paragraph">have another module that&#8217;s written in assembly. I&#8217;m going to call this hello.asm. Okay, so just</p>



<p class="wp-block-paragraph">Okay, so just several source code files.</p>



<p class="wp-block-paragraph">We will compile them each to an object file.</p>



<p class="wp-block-paragraph">Sometimes, I think, with new programmers,</p>



<p class="wp-block-paragraph">they think that you just compile a source code directly to the executable.</p>



<p class="wp-block-paragraph">We&#8217;re not going to do that in this case.</p>



<p class="wp-block-paragraph">We&#8217;re going to get a little bit fancier.</p>



<p class="wp-block-paragraph">Doing it this way, by the way, will increase your compiling efficiency</p>



<p class="wp-block-paragraph">when you start getting used to make files.</p>



<p class="wp-block-paragraph">I don&#8217;t know if you&#8217;re already using those, but it&#8217;s a good idea.</p>



<p class="wp-block-paragraph">So we&#8217;ll write a script to compile or make file to compile</p>



<p class="wp-block-paragraph">or some kind of a build system to compile.</p>



<p class="wp-block-paragraph">We&#8217;ll compile the CPP driver to just a file called</p>



<p class="wp-block-paragraph">driver.o, O for object file.</p>



<p class="wp-block-paragraph">So O for object file depends on driver.cpp.</p>



<p class="wp-block-paragraph">We&#8217;ll have another file called c.o,</p>



<p class="wp-block-paragraph">so the object file c.o depends on c.c and so forth.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna go cpp.o, circle it,</p>



<p class="wp-block-paragraph">and then down here we&#8217;ll do hello.o.</p>



<p class="wp-block-paragraph">Notice how all of these modules, regardless of the language they are written in, compile</p>



<p class="wp-block-paragraph">down to an object file.</p>



<p class="wp-block-paragraph">That&#8217;s really what it is.</p>



<p class="wp-block-paragraph">The higher level languages are just an illusion for us humans.</p>



<p class="wp-block-paragraph">So then at the very top, we want our actual executable file.</p>



<p class="wp-block-paragraph">We&#8217;ll call it main because that&#8217;s kind of a standard thing sometimes.</p>



<p class="wp-block-paragraph">So it&#8217;s like our main, we&#8217;ll have a file named main that we can execute.</p>



<p class="wp-block-paragraph">is going to be the main function.</p>



<p class="wp-block-paragraph">Spoiler, it&#8217;s going to be inside the driver.</p>



<p class="wp-block-paragraph">Remember, the main function, if you&#8217;re using GCC libraries,</p>



<p class="wp-block-paragraph">has to appear in exactly one place, one module in your program.</p>



<p class="wp-block-paragraph">It should appear one time and one time alone, not zero or more than one.</p>



<p class="wp-block-paragraph">So the main executable depends on the driver object file,</p>



<p class="wp-block-paragraph">the C object file, and the C++ object file,</p>



<p class="wp-block-paragraph">and the assembly hello object file.</p>



<p class="wp-block-paragraph">Looks complicated at first until you stare at it a while,</p>



<p class="wp-block-paragraph">complicated at first until you stare at it a while and then you just realize,</p>



<p class="wp-block-paragraph">oh, every source code is going to compile to its own object file.</p>



<p class="wp-block-paragraph">And then after we have all the object files,</p>



<p class="wp-block-paragraph">we&#8217;re going to have our executable just sort of linked together at the final</p>



<p class="wp-block-paragraph">linking stage. So with that in mind,</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste a make file that I already wrote.</p>



<p class="wp-block-paragraph">It&#8217;s like my pre solution because this is not a make file video.</p>



<p class="wp-block-paragraph">And I just want you to know just real fast what it looks like.</p>



<p class="wp-block-paragraph">All I&#8217;m doing is I&#8217;m going to make some variables, uh,</p>



<p class="wp-block-paragraph">for my C++ compiler flags and my C compiler flags and my Yasm assembler flags.</p>



<p class="wp-block-paragraph">I&#8217;m really not doing anything fancy.</p>



<p class="wp-block-paragraph">I&#8217;m just sort of setting up some variables so I don&#8217;t have to type.</p>



<p class="wp-block-paragraph">The newer Ubuntu stuffs need a no executable stack flag and a no pi flag.</p>



<p class="wp-block-paragraph">So here we&#8217;re just like making variables for linking.</p>



<p class="wp-block-paragraph">My executable&#8217;s name is going to be main over here.</p>



<p class="wp-block-paragraph">I personally like to make make files that default to a cute help menu.</p>



<p class="wp-block-paragraph">help menu. I know most people just default to building, but, uh, you know, for educational</p>



<p class="wp-block-paragraph">purposes, I think this is a little more fun, a little easier. So this way later I can just type</p>



<p class="wp-block-paragraph">make menu or make build or make run or make clean or whatever I want to do. And, um,</p>



<p class="wp-block-paragraph">I&#8217;m going to make a target called run, which is what we&#8217;re going to be using. And then a target</p>



<p class="wp-block-paragraph">called build, which just only builds everything. The run target obviously depends on build so that</p>



<p class="wp-block-paragraph">to run it and and if you don&#8217;t know make files at this point it&#8217;s okay this is not a make file video</p>



<p class="wp-block-paragraph">i&#8217;m going to make some make file videos in the future</p>



<p class="wp-block-paragraph">then my binary the main program is just going to depend on all these object files notice how it</p>



<p class="wp-block-paragraph">depends on the driver the hello the c plus plus and the c object files so really uh at the linking</p>



<p class="wp-block-paragraph">stage we&#8217;re just going to use g plus plus to just sort of link all the object files together</p>



<p class="wp-block-paragraph">Then we have a little section to compile each source code file.</p>



<p class="wp-block-paragraph">So in order to make driver.o, I&#8217;m going to compile,</p>



<p class="wp-block-paragraph">I&#8217;m going to depend on the driver.cpp.</p>



<p class="wp-block-paragraph">And then when it comes to actually compiling, compiling it,</p>



<p class="wp-block-paragraph">I&#8217;m just going to type the commands to compile it here.</p>



<p class="wp-block-paragraph">Don&#8217;t worry about these crazy variables.</p>



<p class="wp-block-paragraph">I&#8217;m going to make more videos in the future.</p>



<p class="wp-block-paragraph">And then each module gets compiled in its own way.</p>



<p class="wp-block-paragraph">Then I just have a little target at the bottom called clean because I like to be</p>



<p class="wp-block-paragraph">able to just clean my build area of object files.</p>



<p class="wp-block-paragraph">I don&#8217;t know about you, but I like a clean build area.</p>



<p class="wp-block-paragraph">I&#8217;m a clean build area guy.</p>



<p class="wp-block-paragraph">I don&#8217;t like a dirty build area.</p>



<p class="wp-block-paragraph">So that was the make file.</p>



<p class="wp-block-paragraph">Again, this is not a make file video,</p>



<p class="wp-block-paragraph">so don&#8217;t worry too much about it right now.</p>



<p class="wp-block-paragraph">The next thing we should do is make a driver.</p>



<p class="wp-block-paragraph">Again, I&#8217;m gonna copy paste from my solution here</p>



<p class="wp-block-paragraph">because the driver should be easy.</p>



<p class="wp-block-paragraph">This is really, this video is mostly gonna be</p>



<p class="wp-block-paragraph">for people who have never done assembly before.</p>



<p class="wp-block-paragraph">So I&#8217;m just gonna assume you know a little bit</p>



<p class="wp-block-paragraph">about C++ and C already.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say touch, let&#8217;s see, driver.cpp so that I can get the driver file in there.</p>



<p class="wp-block-paragraph">And I&#8217;m going to go nano driver, probably put it down in the first place.</p>



<p class="wp-block-paragraph">Then I&#8217;m just going to paste my solution.</p>



<p class="wp-block-paragraph">Oops, you can tell I copy pasted this from my IDE because I have the extra tabs in there, which I love.</p>



<p class="wp-block-paragraph">One thing to keep in mind is that, I guess like one of the first rules that I want to tell you for hybrid programs</p>



<p class="wp-block-paragraph">is that assembly doesn&#8217;t understand something called name mangling.</p>



<p class="wp-block-paragraph">mangling. So what is name mangling? Name mangling is basically when you have a,</p>



<p class="wp-block-paragraph">oh, this is not going to get called. So this is maybe not a great idea to show you name</p>



<p class="wp-block-paragraph">mangling. Let me, let me start on one of the other ones real fast too. Let&#8217;s do</p>



<p class="wp-block-paragraph">the C++ module. Okay. So I&#8217;m going to go nano CPP dot CPP. I know it&#8217;s a silly name. I know it.</p>



<p class="wp-block-paragraph">So CPP dot CPP, this is just a simple C++ program, which is meant to be called by</p>



<p class="wp-block-paragraph">by the assembly module just to show you that we can.</p>



<p class="wp-block-paragraph">And so we just have a little function here,</p>



<p class="wp-block-paragraph">void, you know, the CPP function.</p>



<p class="wp-block-paragraph">So it&#8217;s just a function that doesn&#8217;t really return anything.</p>



<p class="wp-block-paragraph">Inside of it, I&#8217;m just going to print,</p>



<p class="wp-block-paragraph">hello, this is the C++ function, no problem.</p>



<p class="wp-block-paragraph">Notice at the top,</p>



<p class="wp-block-paragraph">this is the first important thing that you should understand.</p>



<p class="wp-block-paragraph">In C++, we have overloads, right?</p>



<p class="wp-block-paragraph">You can have the same function name,</p>



<p class="wp-block-paragraph">but with different argument lists.</p>



<p class="wp-block-paragraph">And so that gets resolved under the hood</p>



<p class="wp-block-paragraph">by the name actually kind of changing</p>



<p class="wp-block-paragraph">based on what the argument signature is,</p>



<p class="wp-block-paragraph">what the prototype signature is.</p>



<p class="wp-block-paragraph">So that&#8217;s great and all,</p>



<p class="wp-block-paragraph">but in assembly, at least at this level,</p>



<p class="wp-block-paragraph">there&#8217;s no way to really differentiate between overloads.</p>



<p class="wp-block-paragraph">In assembly, we&#8217;ll just call a name of some function.</p>



<p class="wp-block-paragraph">But if we try to call a name,</p>



<p class="wp-block-paragraph">let&#8217;s say of just the CPP function,</p>



<p class="wp-block-paragraph">if we say call the CPP function in assembly,</p>



<p class="wp-block-paragraph">it&#8217;s not going to work</p>



<p class="wp-block-paragraph">because the actual function has not been compiled to have that name.</p>



<p class="wp-block-paragraph">It&#8217;s been compiled to have the CPP function</p>



<p class="wp-block-paragraph">and a bunch of extra symbols indicating what the argument list is.</p>



<p class="wp-block-paragraph">This is great for C++ programs, but it&#8217;s poor.</p>



<p class="wp-block-paragraph">It&#8217;s bad for other modules to be able to call the C++ function</p>



<p class="wp-block-paragraph">because they won&#8217;t really know what the actual name is</p>



<p class="wp-block-paragraph">after the mangling has been done.</p>



<p class="wp-block-paragraph">So what you want to do is make a little block called Xtern C,</p>



<p class="wp-block-paragraph">just like this.</p>



<p class="wp-block-paragraph">You say extern and then C in quotes and then do a scope with braces.</p>



<p class="wp-block-paragraph">And inside of that, just put the prototypes of any function that you want to be available to other modules.</p>



<p class="wp-block-paragraph">So, for example, we want the CPP function to be available to assembly modules and C modules, not just other C++ modules.</p>



<p class="wp-block-paragraph">So, I&#8217;m just going to put the prototype of it right there.</p>



<p class="wp-block-paragraph">You can put as many prototypes as you want.</p>



<p class="wp-block-paragraph">I think they should come from the same source file.</p>



<p class="wp-block-paragraph">different source file where you want something to be available elsewhere,</p>



<p class="wp-block-paragraph">you would put an extern C block for its prototype.</p>



<p class="wp-block-paragraph">This disables name mangling,</p>



<p class="wp-block-paragraph">which means the compiled name of the CPP function will actually be the CPP</p>



<p class="wp-block-paragraph">function. So it&#8217;s pretty good. This will make it available to assembly.</p>



<p class="wp-block-paragraph">So now that I&#8217;ve kind of explained that I can maybe close this and go back to</p>



<p class="wp-block-paragraph">what I was doing before. So this is the main driver program.</p>



<p class="wp-block-paragraph">We don&#8217;t need to engage in a dis,</p>



<p class="wp-block-paragraph">we don&#8217;t need to disable name mangling for the main function because no one is going to call main</p>



<p class="wp-block-paragraph">except the GCC libraries or like the OS. But when the C++ module calls other modules, it needs to</p>



<p class="wp-block-paragraph">be able to call the right name. And remember in C++, there&#8217;s name mangling enabled by default.</p>



<p class="wp-block-paragraph">So if I tell it, let&#8217;s say on this line right here, or you can do alt N to get line numbers on nano.</p>



<p class="wp-block-paragraph">we want to call on the hello module will C++ by default will assume that hello has name</p>



<p class="wp-block-paragraph">mangling applied to it, which means it will actually try to call some name that is totally</p>



<p class="wp-block-paragraph">different based on the prototype signature, the incoming arguments and things like that.</p>



<p class="wp-block-paragraph">But so we want to disable that because we wanted to just call the actual name because</p>



<p class="wp-block-paragraph">hello is going to be a function that we make inside of assembly.</p>



<p class="wp-block-paragraph">So it&#8217;s not going to be name mangled.</p>



<p class="wp-block-paragraph">So that&#8217;s why up here on line seven, we enter its signature.</p>



<p class="wp-block-paragraph">Enter its signature. Oh, actually this is from a different version of something that I was doing. I&#8217;m not going to return anything from hello</p>



<p class="wp-block-paragraph">We&#8217;ll do name a demangling with our extern C block just void hello</p>



<p class="wp-block-paragraph">And now when we call hello, it&#8217;s actually going to call a function called hello without any other</p>



<p class="wp-block-paragraph">mangling same thing for the C S E A</p>



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



<p class="wp-block-paragraph">That was from a different version. I wrote we&#8217;re not going to be calling C in this module. Don&#8217;t worry</p>



<p class="wp-block-paragraph">We&#8217;re going to keep it simple</p>



<p class="wp-block-paragraph">So pretty much we&#8217;re going to print hello and then we&#8217;re going to print,</p>



<p class="wp-block-paragraph">then we&#8217;re going to call the hello and then we&#8217;re going to say goodbye.</p>



<p class="wp-block-paragraph">And then that&#8217;s going to be the end of the program.</p>



<p class="wp-block-paragraph">So let me close out of that and just double check what I have written right now.</p>



<p class="wp-block-paragraph">So we&#8217;ve got CPP and okay, we need to do C, nano C dot C.</p>



<p class="wp-block-paragraph">Okay, so let me get my solution real fast here.</p>



<p class="wp-block-paragraph">It&#8217;s a simple program.</p>



<p class="wp-block-paragraph">Literally, it&#8217;s just a C program that prints something.</p>



<p class="wp-block-paragraph">Remember in C++, we&#8217;ve got the IO stream, but we don&#8217;t have that in C.</p>



<p class="wp-block-paragraph">that in C. So I&#8217;m just going to use printf. Oh, by the way, this is a C function. Hi.</p>



<p class="wp-block-paragraph">So done with that. Pretty easy. Shoot. You know what? I need to start learning how to do</p>



<p class="wp-block-paragraph">clear and LSA. So I can just go up, up, up. So C++, C and the driver</p>



<p class="wp-block-paragraph">and the make file, which is huge. I just realized that. And now I guess we can start</p>



<p class="wp-block-paragraph">or hello function.</p>



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



<p class="wp-block-paragraph">So maybe I want to write this in the Genie program.</p>



<p class="wp-block-paragraph">So I&#8217;m going to, well, maybe I&#8217;ll first, I&#8217;ll say touch,</p>



<p class="wp-block-paragraph">and I&#8217;ll say hello.assembly.</p>



<p class="wp-block-paragraph">And then now that it&#8217;s there, I can try to open it up.</p>



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



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



<p class="wp-block-paragraph">Okay, so now let&#8217;s write an assembly program.</p>



<p class="wp-block-paragraph">I&#8217;m not going to spend too much time talking about this,</p>



<p class="wp-block-paragraph">but I&#8217;ll say like data section here,</p>



<p class="wp-block-paragraph">and we&#8217;ll say section.data.</p>



<p class="wp-block-paragraph">We&#8217;ll say section.data and then we&#8217;ll say system calls.</p>



<p class="wp-block-paragraph">This is not a system calls video.</p>



<p class="wp-block-paragraph">I&#8217;m just gonna do this very quickly without explaining.</p>



<p class="wp-block-paragraph">System write is gonna be code one, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;re gonna do file descriptors, one for stdout.</p>



<p class="wp-block-paragraph">Whoops, stdout, it&#8217;s gonna be equal to one.</p>



<p class="wp-block-paragraph">This is not a file descriptors video.</p>



<p class="wp-block-paragraph">I&#8217;ll put FDs, I guess.</p>



<p class="wp-block-paragraph">And then we&#8217;ll make some C strings.</p>



<p class="wp-block-paragraph">C strings, you know, just character arrays, basically.</p>



<p class="wp-block-paragraph">I&#8217;ll say, well, you know what?</p>



<p class="wp-block-paragraph">I don&#8217;t want to type this all out</p>



<p class="wp-block-paragraph">because this is not a lesson on variables.</p>



<p class="wp-block-paragraph">I&#8217;m just going to copy paste my existing solution here.</p>



<p class="wp-block-paragraph">And, you know, long story short,</p>



<p class="wp-block-paragraph">I&#8217;m going to try to make this a pretty program</p>



<p class="wp-block-paragraph">that prints hello and goodbye and things.</p>



<p class="wp-block-paragraph">And so we just have like some variables</p>



<p class="wp-block-paragraph">that sort of just you know old strings and hold the strings length and we&#8217;re</p>



<p class="wp-block-paragraph">gonna do one that calls a new line and so forth actually that&#8217;ll be a different</p>



<p class="wp-block-paragraph">video but I&#8217;ll leave it in there for now so now let&#8217;s make the text section</p>



<p class="wp-block-paragraph">remember the text section in Yasm is just for the actual code of your program</p>



<p class="wp-block-paragraph">and then we need to set up some external symbols. So right now, at least for the purposes of this</p>



<p class="wp-block-paragraph">source code, this source code is inside of the hello module. So you can imagine we&#8217;re inside of</p>



<p class="wp-block-paragraph">hello.o. All the o&#8217;s get linked together in the executable, but between themselves, there&#8217;s kind</p>



<p class="wp-block-paragraph">of a little bit of like a border situation happening. There&#8217;s like some separation. So</p>



<p class="wp-block-paragraph">are going to be available to the other modules,</p>



<p class="wp-block-paragraph">even if they&#8217;re part of the same executable.</p>



<p class="wp-block-paragraph">It&#8217;s kind of convenient because you would like sometimes</p>



<p class="wp-block-paragraph">to be able to write tons and tons of functions</p>



<p class="wp-block-paragraph">that are only available inside of your assembly module,</p>



<p class="wp-block-paragraph">and they might not be available to other modules,</p>



<p class="wp-block-paragraph">and therefore you don&#8217;t want to pollute the namespace,</p>



<p class="wp-block-paragraph">I guess, of the global namespace of the executable.</p>



<p class="wp-block-paragraph">So by default, if I make a function called hello,</p>



<p class="wp-block-paragraph">how you make a function, just a quick recap here.</p>



<p class="wp-block-paragraph">This is not a functions video.</p>



<p class="wp-block-paragraph">make a symbol with the name of the function you want and then you just put a ret after it and then</p>



<p class="wp-block-paragraph">you hope that whoever got you there is going to use the call instruction but anyway for now</p>



<p class="wp-block-paragraph">we&#8217;re just going to say this is our hello function okay i cannot call the hello function now from</p>



<p class="wp-block-paragraph">another module i have to make it visible to the other modules with a special keyword called global</p>



<p class="wp-block-paragraph">our program the hello function will be available to the other modules but not anything else that</p>



<p class="wp-block-paragraph">I type without marking it as global so let&#8217;s go ahead and do let&#8217;s do a welcome message and this</p>



<p class="wp-block-paragraph">is not a system call a video maybe I should just copy paste this so just looking at it this is a</p>



<p class="wp-block-paragraph">going to quickly you know breeze through what we&#8217;re doing in case you&#8217;re interested but you</p>



<p class="wp-block-paragraph">don&#8217;t have to care too much about this right now we&#8217;re going to tell rax what system call we want</p>



<p class="wp-block-paragraph">to make the code for that is what i put up in system right which is just code one means let&#8217;s</p>



<p class="wp-block-paragraph">write to a file we&#8217;ll give it the file handle uh that&#8217;s going to be the variable we made called fd</p>



<p class="wp-block-paragraph">std out which means we&#8217;re going to be giving it the file handle of one which means we&#8217;re telling</p>



<p class="wp-block-paragraph">to print to standard output because file handle one is always standard output.</p>



<p class="wp-block-paragraph">And then we just give it the a pointer to the first character of the message</p>



<p class="wp-block-paragraph">string that we made and then tell it how long that message is. Then we do system call this should</p>



<p class="wp-block-paragraph">actually um this this should actually print a string. Okay so then uh let&#8217;s see down here I&#8217;m</p>



<p class="wp-block-paragraph">going to make a function called crlf because I&#8217;m lazy and I don&#8217;t like constantly adding extra stuff</p>



<p class="wp-block-paragraph">constantly adding extra stuff to the string so that there will be like a new line line feed</p>



<p class="wp-block-paragraph">situation so i usually like to make a little function somewhere called crlf carriage return</p>



<p class="wp-block-paragraph">line feed is what it stands for and it&#8217;s the same thing it&#8217;s just like load up the system call</p>



<p class="wp-block-paragraph">registers so that it knows i want to print the string which is just a line feed</p>



<p class="wp-block-paragraph">carriage return line feed and then actually call system call and then return to the caller so this</p>



<p class="wp-block-paragraph">back up here let&#8217;s see crlf i guess i am going to be doing it it&#8217;s just a string with the the</p>



<p class="wp-block-paragraph">slash r slash n or the carriage return line feed characters in ascii those are 13 comma 10 so i can</p>



<p class="wp-block-paragraph">put that there instead of a literal quoted string okay so we got that and then that means after the</p>



<p class="wp-block-paragraph">welcome message i can just like call crlf in order to have uh the cursor jump down to the next uh</p>



<p class="wp-block-paragraph">after that string is notice how I don&#8217;t have any line feeds after my strings that&#8217;s kind of why I</p>



<p class="wp-block-paragraph">like to do crlf and it lets me just call crlf a bunch of times if I want to just like space things</p>



<p class="wp-block-paragraph">out temporarily okay so we&#8217;ve got the hello function and now we&#8217;re going to print another</p>



<p class="wp-block-paragraph">message here I&#8217;ll just explain this real fast so I&#8217;m going to print a message about calling a c</p>



<p class="wp-block-paragraph">So same thing, we&#8217;re just going to tell the system call service that we want to print a string and it&#8217;s going to be our string here that says, you know, we&#8217;re about to print this.</p>



<p class="wp-block-paragraph">We&#8217;re about to, what do we say?</p>



<p class="wp-block-paragraph">We&#8217;re about to call the C function and then later we&#8217;re going to call the C++ function.</p>



<p class="wp-block-paragraph">So we&#8217;re just going to print a message announcing and do CRLF.</p>



<p class="wp-block-paragraph">So that&#8217;s really nothing new at all.</p>



<p class="wp-block-paragraph">And then down here we actually call the C function, which is not going to be in this module.</p>



<p class="wp-block-paragraph">It&#8217;s going to be in the C module, the other source code.</p>



<p class="wp-block-paragraph">Somewhere in this program, we need to let our assembly module know</p>



<p class="wp-block-paragraph">that the C function is available because it kind of goes both ways.</p>



<p class="wp-block-paragraph">In assembly, I have to mark a function as global if I want other modules to be able</p>



<p class="wp-block-paragraph">to call that function, but I also have to mark a function as external</p>



<p class="wp-block-paragraph">if I want to be able to call some other modules function.</p>



<p class="wp-block-paragraph">So we have to go back up to the top.</p>



<p class="wp-block-paragraph">I mean, somewhere in the text section, I think would be fine.</p>



<p class="wp-block-paragraph">But for me, I just like to put it at the top.</p>



<p class="wp-block-paragraph">put it at the top at the top so I&#8217;ll say externals and then I&#8217;ll go extern</p>



<p class="wp-block-paragraph">keyword and then just name the function that I intend to call this way when the</p>



<p class="wp-block-paragraph">linker is trying to link all the objects together it doesn&#8217;t get upset because</p>



<p class="wp-block-paragraph">you tried to call a C function which is not in the current module it&#8217;ll</p>



<p class="wp-block-paragraph">understand that it&#8217;s gonna happen you know like it&#8217;s gonna call other modules</p>



<p class="wp-block-paragraph">for that it usually gets upset right away and then while we&#8217;re at it let&#8217;s do</p>



<p class="wp-block-paragraph">Let&#8217;s turn the CPP function because that&#8217;s going to come from the C++ module.</p>



<p class="wp-block-paragraph">So now we have both of our external functions set up.</p>



<p class="wp-block-paragraph">We&#8217;ve marked our global hello function so other modules can call us.</p>



<p class="wp-block-paragraph">And then let&#8217;s see, oh, what did I do wrong?</p>



<p class="wp-block-paragraph">I put the C message in the wrong spot.</p>



<p class="wp-block-paragraph">I was inside of the CRLF function.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to copy paste that back up here.</p>



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



<p class="wp-block-paragraph">Then we&#8217;ll do the same thing for the C++ message,</p>



<p class="wp-block-paragraph">or for the C++ stuff.</p>



<p class="wp-block-paragraph">I will first print a C++ message</p>



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



<p class="wp-block-paragraph">That&#8217;s just like a regular system call,</p>



<p class="wp-block-paragraph">and then I&#8217;ll do a CRLF, so nothing new,</p>



<p class="wp-block-paragraph">just printing something.</p>



<p class="wp-block-paragraph">And then I will actually call the C++ function</p>



<p class="wp-block-paragraph">called the CPP function.</p>



<p class="wp-block-paragraph">Then I&#8217;ll say goodbye with another block of text.</p>



<p class="wp-block-paragraph">Again, not super important or complicated,</p>



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



<p class="wp-block-paragraph">Just another string.</p>



<p class="wp-block-paragraph">string. And then when we&#8217;re done, I&#8217;ll just say, you know, return, we&#8217;re done. So that means if</p>



<p class="wp-block-paragraph">you notice how we don&#8217;t have like any symbols in here called underscore start, that means this is</p>



<p class="wp-block-paragraph">not a pure assembly program. And we don&#8217;t even have a symbol inside of here that is called main.</p>



<p class="wp-block-paragraph">So that means this hello ASM module is not going to be the entry point of our program. It&#8217;s just</p>



<p class="wp-block-paragraph">going to expose a function called hello, which does some stuff. The entry point of our program</p>



<p class="wp-block-paragraph">really, let&#8217;s see, where is it? Oh, gosh, I think I already closed it for some reason. It&#8217;s the</p>



<p class="wp-block-paragraph">driver program, which we talked about. So remember the driver program, it&#8217;s got a main here. So that</p>



<p class="wp-block-paragraph">means this is going to be what the GCC libraries or the operating system calls, however you want</p>



<p class="wp-block-paragraph">to think of it. So this is the entry point of our program, it&#8217;ll just print something quickly,</p>



<p class="wp-block-paragraph">then it&#8217;ll call the hello function, which means execution will jump in to the hello module. So</p>



<p class="wp-block-paragraph">module and and also it would be really easy to apply this logic to have a c module call on an</p>



<p class="wp-block-paragraph">assembly module and in fact you can use this logic to have every type of module call on any other i</p>



<p class="wp-block-paragraph">mean just if you have a c++ function either being defined in the current module or being referenced</p>



<p class="wp-block-paragraph">in another module just make sure that you have an extern c block and then if you&#8217;re inside of a c</p>



<p class="wp-block-paragraph">and you&#8217;re going to call a non-name mingled module like C or assembly,</p>



<p class="wp-block-paragraph">then just do this block here.</p>



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



<p class="wp-block-paragraph">What else can I really tell you?</p>



<p class="wp-block-paragraph">The C module, we looked at that.</p>



<p class="wp-block-paragraph">The C++, we looked at that.</p>



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



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



<p class="wp-block-paragraph">I mean, I guess we&#8217;re kind of done already.</p>



<p class="wp-block-paragraph">So what we should see now is a little intro here.</p>



<p class="wp-block-paragraph">Hello, my name is whatever.</p>



<p class="wp-block-paragraph">And then when we jump into the hello module,</p>



<p class="wp-block-paragraph">we should see a little welcome message about being in the assembly module.</p>



<p class="wp-block-paragraph">And then we should see a message about calling the C function.</p>



<p class="wp-block-paragraph">And then the C function should print.</p>



<p class="wp-block-paragraph">Let me open that up for you.</p>



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



<p class="wp-block-paragraph">The C function should print.</p>



<p class="wp-block-paragraph">Just, oh, by the way, this is the C function.</p>



<p class="wp-block-paragraph">And then when it returns at the very end, remember, even in a void function, there is</p>



<p class="wp-block-paragraph">a return statement implicit at the end of scope.</p>



<p class="wp-block-paragraph">So when it returns, it&#8217;ll come back down here.</p>



<p class="wp-block-paragraph">And then the C++ message will get printed.</p>



<p class="wp-block-paragraph">printed and then the C++ function will get called which is going to end up being</p>



<p class="wp-block-paragraph">just it just prints you know another message this is the C++ function</p>



<p class="wp-block-paragraph">then when it returns implicitly at the end of the function</p>



<p class="wp-block-paragraph">then we get down to here where we just basically say goodbye and then return to the caller</p>



<p class="wp-block-paragraph">the caller in this case is going to be the driver program because this whole time we were just inside</p>



<p class="wp-block-paragraph">called. So then the driver will say goodbye and then it will return control to the GCC libraries</p>



<p class="wp-block-paragraph">or the operating system ending the program. Let&#8217;s see if that works. Okay, clear. And so,</p>



<p class="wp-block-paragraph">you know, the way I wreck my make files, I&#8217;d like to do a little menu. I type make and it&#8217;s like,</p>



<p class="wp-block-paragraph">oh, the menu, make menu, make build, just in case, just to see what&#8217;s up. Okay, clear.</p>



<p class="wp-block-paragraph">say, let&#8217;s do clear and make run because that&#8217;s really what we&#8217;re after.</p>



<p class="wp-block-paragraph">Again, run will implicitly call build.</p>



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



<p class="wp-block-paragraph">And it seems to have succeeded.</p>



<p class="wp-block-paragraph">It wasn&#8217;t actually very exciting.</p>



<p class="wp-block-paragraph">Let me do a make clean so you can see it compile real fast.</p>



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



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



<p class="wp-block-paragraph">That was still kind of fast.</p>



<p class="wp-block-paragraph">So up here is basically the make file, all these lines.</p>



<p class="wp-block-paragraph">They&#8217;re just calling your normal compiler and assembler tools and linker tools.</p>



<p class="wp-block-paragraph">linker tools and then here&#8217;s the driver like we said it prints the hello message</p>



<p class="wp-block-paragraph">you know my name is whatever I love that name I have a strange name generator</p>



<p class="wp-block-paragraph">that I found online that&#8217;s not my name you can call me that if you want to and</p>



<p class="wp-block-paragraph">then we have the message that we&#8217;re going to call the hello module so now</p>



<p class="wp-block-paragraph">we&#8217;ve called into the hello function inside of the assembly module assembly</p>



<p class="wp-block-paragraph">module prints hello you know I&#8217;m inside of here now and then the assembly module</p>



<p class="wp-block-paragraph">Now we&#8217;re going to call the C function and then the C function actually</p>



<p class="wp-block-paragraph">executes and it is now printing with its print F function call that it did,</p>



<p class="wp-block-paragraph">you know, by the way, this is a C function. Hi.</p>



<p class="wp-block-paragraph">And then it returns to the caller.</p>



<p class="wp-block-paragraph">Maybe I could have added another print statement if I felt like I wanted to</p>



<p class="wp-block-paragraph">spend more time, but it returns to the caller, which is the hello function.</p>



<p class="wp-block-paragraph">Then we get to the next part of the hello function where it&#8217;s like now calling</p>



<p class="wp-block-paragraph">the C function as a print statement.</p>



<p class="wp-block-paragraph">Then it actually does call the C function. The C function prints itself out.</p>



<p class="wp-block-paragraph">prints itself out hello this is the C function and then the assembly module</p>



<p class="wp-block-paragraph">says goodbye it says it&#8217;s finished and then it returns to the caller so finally</p>



<p class="wp-block-paragraph">at the end of this print statement the there&#8217;s like that little return statement</p>



<p class="wp-block-paragraph">at the bottom of the hello module that returns to the caller which was the</p>



<p class="wp-block-paragraph">driver dot CPP program so then the driver prints the driver is now back in</p>



<p class="wp-block-paragraph">the driver is now back in control. You can see that message behind the terminal here.</p>



<p class="wp-block-paragraph">The driver is now back in control. Then it says goodbye. Then it returns zero to the operating</p>



<p class="wp-block-paragraph">system and the whole program is done. So it&#8217;s important to understand that if you want to</p>



<p class="wp-block-paragraph">pass arguments, right now I&#8217;m just going to say, you know, I&#8217;m going to make another video that</p>



<p class="wp-block-paragraph">is more robust in the future about passing arguments back and forth between these. It&#8217;s</p>



<p class="wp-block-paragraph">important to understand for now that you can pass arguments between all these different</p>



<p class="wp-block-paragraph">between all these different module functions if you want.</p>



<p class="wp-block-paragraph">Like if I wanted to give the.</p>



<p class="wp-block-paragraph">If I wanted to give the hello function.</p>



<p class="wp-block-paragraph">An incoming argument, let&#8217;s say like an integer or a character value or a pointer</p>



<p class="wp-block-paragraph">or something like that, I could just load up appropriate registers.</p>



<p class="wp-block-paragraph">Remember, we have argument registers in assembly.</p>



<p class="wp-block-paragraph">I think I can never remember if RSI or RDI are the first one.</p>



<p class="wp-block-paragraph">I&#8217;m going to say RDI is the first argument just off the top of my head.</p>



<p class="wp-block-paragraph">I always look that up.</p>



<p class="wp-block-paragraph">So if I wanted to call, let&#8217;s say give an integer argument to hello when I originally</p>



<p class="wp-block-paragraph">called it, then from the driver, I literally would just have to type an integer argument</p>



<p class="wp-block-paragraph">in there and then under the hood, the RDI register, assuming that I&#8217;m right about RDI</p>



<p class="wp-block-paragraph">being the first argument, the RDI register would get loaded up with that value.</p>



<p class="wp-block-paragraph">your assembly program all you&#8217;d have to do is do something with the RDI register</p>



<p class="wp-block-paragraph">you know load it up save it do whatever it is that you think you&#8217;re gonna do so</p>



<p class="wp-block-paragraph">that&#8217;s how you pass an integer or like a character or a pointer from C++ to</p>



<p class="wp-block-paragraph">assembly it works the same exact way if you want to pass arguments when calling</p>



<p class="wp-block-paragraph">from C to assembly and when if you want to pass integers from assembly to C or</p>



<p class="wp-block-paragraph">or C++, it&#8217;s the same thing just backwards.</p>



<p class="wp-block-paragraph">First, you load up RDI as the first argument register with whatever value you want to pass</p>



<p class="wp-block-paragraph">to C or C++.</p>



<p class="wp-block-paragraph">And then in the signature of the C++ function, you just expect, you know, a long or, you</p>



<p class="wp-block-paragraph">know, whatever you were going to get and the register will already be loaded up.</p>



<p class="wp-block-paragraph">Remember, the C++ is not really happening on the machine.</p>



<p class="wp-block-paragraph">It gets compiled down to assembly.</p>



<p class="wp-block-paragraph">So this source code that we&#8217;re looking at, it&#8217;s another illusion for our benefit.</p>



<p class="wp-block-paragraph">benefit. It&#8217;s not happening. It&#8217;s getting assembled and then compiled down to machine code. And then</p>



<p class="wp-block-paragraph">that is what&#8217;s actually happening on the machine. So when you write that you have an argument in a</p>



<p class="wp-block-paragraph">C++ function signature prototype, you&#8217;re not really writing that at all. You&#8217;re just saying</p>



<p class="wp-block-paragraph">that the A variable should get loaded from whatever the RDI register had.</p>



<p class="wp-block-paragraph">Or if you previously loaded the RDI register and you want to call a C++ function, that&#8217;s</p>



<p class="wp-block-paragraph">a C++ function that&#8217;s what it would expect the RDI register so it can work pretty seamlessly</p>



<p class="wp-block-paragraph">just calling things back and forth um so yeah I guess that&#8217;s all I really had to say</p>



<p class="wp-block-paragraph">this is the basics of writing a hybrid program thank you for watching I hope you learned a</p>



<p class="wp-block-paragraph">little bit of stuff and had a little bit of fun see you in the next video</p>



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



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



<p class="wp-block-paragraph">this channel or these videos or whatever it is you do on the current social media</p>



<p class="wp-block-paragraph">website that you&#8217;re looking at right now it would really mean the world to me and</p>



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



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



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



<p class="wp-block-paragraph">in the middle of the night 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. 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 or you could troll me if you want to just wake me up in</p>



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



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



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



<p class="wp-block-paragraph">the bottom of this video 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 and all that</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/">Mix C++, C, and Assembly into One Program &#8211; Hybrid Program Full Build Tutorial</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/mix-c-c-and-assembly-into-one-program-hybrid-program-full-build-tutorial/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!</title>
		<link>https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/</link>
					<comments>https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Wed, 11 Jun 2025 19:56:00 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[beginner coding]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[c++ tutorial]]></category>
		<category><![CDATA[caveman debugging]]></category>
		<category><![CDATA[code troubleshooting]]></category>
		<category><![CDATA[coding hacks]]></category>
		<category><![CDATA[coding tips]]></category>
		<category><![CDATA[debug c++]]></category>
		<category><![CDATA[debug code fast]]></category>
		<category><![CDATA[fix code bugs]]></category>
		<category><![CDATA[print statement debugging]]></category>
		<category><![CDATA[programming for beginners]]></category>
		<category><![CDATA[programming tutorial]]></category>
		<category><![CDATA[software development]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=151</guid>

					<description><![CDATA[<p>Master caveman debugging! Easy C++ trick to fix code fast with print statements. Clear tips for beginners &#038; pros. Watch now to squash bugs!</p>
<p>The post <a href="https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/">Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!</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="Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!" width="1380" height="776" src="https://www.youtube.com/embed/hsGaRukvpF8?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">Hey coders! Ever hit a wall with a program that just won’t work? In this video, I’m spilling the beans on caveman debugging—a super simple, no-fuss way to hunt down bugs in your C++ code (or any language, from assembly to Python)! No need to wrestle with complex debuggers; this method is all about using print statements to trace what your code is doing, step by step.</p>



<p class="wp-block-paragraph">I’ll show you a real C++ program where things go wrong (think integer overflows and mystery crashes) and walk you through how to sprinkle in cout statements to spot the problem fast. You’ll learn how to label decision points like loops and if-statements, break down complex expressions with temporary variables, and format your debug output so it’s easy to read. Plus, I share a neat trick called short circuiting to toggle your debug prints on or off without deleting them—saving you tons of time!</p>



<p class="wp-block-paragraph">Whether you’re a beginner just starting out or a pro debugging a massive codebase, caveman debugging is a lifesaver. It’s quick, it’s intuitive, and it fits my “let’s figure this out” vibe perfectly. Stick around to see how I catch a sneaky bug in a for-loop and fix it in minutes.</p>



<p class="wp-block-paragraph">Why watch?</p>



<ul class="wp-block-list">
<li>Learn a beginner-friendly debugging technique that works in any language.</li>



<li>See real code examples with clear, relatable explanations.</li>



<li>Get tips to make your debug output readable and avoid hours of frustration.</li>



<li>Discover how to short circuit print statements for reusable debugging.</li>
</ul>



<p class="wp-block-paragraph">If you’re ready to level up your coding game and squash bugs like a champ, smash that play button! Don’t forget to subscribe and hit the bell for more coding tutorials, from beginner hacks to advanced tricks. Drop a comment if you’ve ever used caveman debugging or have a bug you need help with—I read every one! Check out my next video on using a proper debugger for even more debugging goodness.</p>



<p class="wp-block-paragraph">Scan the QR code on-screen or visit my website (linked below) for more tutorials, services, and coding resources. Let’s keep the coding community growing—your support means the world! #CavemanDebugging #CodingTips #DebuggingHacks</p>



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m gonna try to familiarize you with a concept</p>



<p class="wp-block-paragraph">called caveman debugging. It&#8217;s just as bad as it sounds but it&#8217;s really really</p>



<p class="wp-block-paragraph">really useful. So suppose for the sake of argument you&#8217;re writing a program I&#8217;m</p>



<p class="wp-block-paragraph">gonna show a C++ program here but this this concept is not just for C++</p>



<p class="wp-block-paragraph">debugging it&#8217;s also for assembly and any other kind of language that you can come</p>



<p class="wp-block-paragraph">other kind of language that you can come up with but suppose I have a program and I&#8217;m trying to</p>



<p class="wp-block-paragraph">figure out why it&#8217;s not working so if you look at this program right here that I&#8217;ve kind of written</p>



<p class="wp-block-paragraph">up already we have a main function here and we have like a little hello message that&#8217;s not my</p>



<p class="wp-block-paragraph">name I love that name though and we have a variable a and then we just sort of do some nonsense to it</p>



<p class="wp-block-paragraph">to try and compute a value for a when I say nonsense I mean literally I&#8217;m just I just made</p>



<p class="wp-block-paragraph">I&#8217;m just I just made up instructions the point of this video is not to show you some advanced program</p>



<p class="wp-block-paragraph">It&#8217;s just to show you that you know</p>



<p class="wp-block-paragraph">This is how you can possibly debug a program that has gone awry</p>



<p class="wp-block-paragraph">So you can imagine at home that this code you&#8217;re seeing right here might be your larger project your larger</p>



<p class="wp-block-paragraph">Code base whatever it is that you&#8217;re doing that is not working for some reason and you&#8217;re not sure why</p>



<p class="wp-block-paragraph">So, um, let me run the program real fast</p>



<p class="wp-block-paragraph">It basically is just like a little for loop and then it just sort of</p>



<p class="wp-block-paragraph">of you know not randomly but it just kind of increases the value of a for no reason</p>



<p class="wp-block-paragraph">a bunch of times and then it calls a function that will crash later</p>



<p class="wp-block-paragraph">so let me show you what happens when we try to run this program okay do do do</p>



<p class="wp-block-paragraph">okay i want to go clear and make run if you don&#8217;t know how to do a make file or compile</p>



<p class="wp-block-paragraph">or link or anything like that uh then go see my other videos so clear and make run</p>



<p class="wp-block-paragraph">What did I do wrong?</p>



<p class="wp-block-paragraph">TempResult was not declared in this scope.</p>



<p class="wp-block-paragraph">Okay, well, this is now a video on reading compiler errors.</p>



<p class="wp-block-paragraph">Line 59, I probably left something out.</p>



<p class="wp-block-paragraph">Oh, because I just changed it.</p>



<p class="wp-block-paragraph">I forgot to put it back in there.</p>



<p class="wp-block-paragraph">Return tempResult, we should just return input times two.</p>



<p class="wp-block-paragraph">And I&#8217;m just making this up.</p>



<p class="wp-block-paragraph">It&#8217;s not really like a valid algorithm.</p>



<p class="wp-block-paragraph">Okay, so we run our program.</p>



<p class="wp-block-paragraph">We print the final result and we realize for some reason the final result is wrong.</p>



<p class="wp-block-paragraph">result is wrong. So I don&#8217;t, I mean, I just made this up. So I don&#8217;t really know what the right</p>



<p class="wp-block-paragraph">answer is. But just imagine that you are looking at some sort of an output, some sort of a final</p>



<p class="wp-block-paragraph">state, maybe a crash. And you realize, oh, my program doesn&#8217;t work, but it&#8217;s too complicated</p>



<p class="wp-block-paragraph">to figure out. Caveman debugging to the rescue. Caveman debugging kind of is as bad as it sounds.</p>



<p class="wp-block-paragraph">It&#8217;s just really, really simple. But it&#8217;s also really useful, especially if you just want to do</p>



<p class="wp-block-paragraph">on you don&#8217;t want to dive deep into a full-on debugger or do something more advanced it literally</p>



<p class="wp-block-paragraph">is just printing a lot of things while your program is running i do it all the time i usually</p>



<p class="wp-block-paragraph">don&#8217;t even go to a debugger unless i really get in trouble with like some huge mess um okay so</p>



<p class="wp-block-paragraph">the first thing i&#8217;m going to say is in this for loop we uh you know we&#8217;re kind of like iterating</p>



<p class="wp-block-paragraph">And so probably this is a good idea to announce that we&#8217;re iterating in the for loop.</p>



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



<p class="wp-block-paragraph">In C++, you know, we have the Cout object that we can use to stream texts to standard output,</p>



<p class="wp-block-paragraph">which usually goes to the terminal.</p>



<p class="wp-block-paragraph">So use whatever construct you&#8217;re using for your particular language.</p>



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



<p class="wp-block-paragraph">And I should say also, if you&#8217;re programming in assembly,</p>



<p class="wp-block-paragraph">if you are programming in assembly,</p>



<p class="wp-block-paragraph">it&#8217;s a little bit more complicated than just writing a Cout.</p>



<p class="wp-block-paragraph">little bit more complicated than just writing a C out you probably have to have some pre-computed</p>



<p class="wp-block-paragraph">messages and then you can use a library to print a number to the screen or use printf to print stuff</p>



<p class="wp-block-paragraph">to the screen but you can do it just takes a little bit longer so anyway conceptually I&#8217;m</p>



<p class="wp-block-paragraph">just going to say this is the first iteration of my loop so I&#8217;m going to sort of announce it I&#8217;m</p>



<p class="wp-block-paragraph">going to say main and then I&#8217;m going to call it main four or how about primary four or first four</p>



<p class="wp-block-paragraph">but I want to try to label the for loop.</p>



<p class="wp-block-paragraph">And maybe instead of leaving those parentheses blank,</p>



<p class="wp-block-paragraph">I could say iteration number i right there,</p>



<p class="wp-block-paragraph">or maybe I could do iteration i.</p>



<p class="wp-block-paragraph">That would be pretty good.</p>



<p class="wp-block-paragraph">Then I just have to replace the i with the actual variable i</p>



<p class="wp-block-paragraph">so that the i keeps increasing.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to like do another stream operator here</p>



<p class="wp-block-paragraph">and I&#8217;m going to say i.</p>



<p class="wp-block-paragraph">Then I&#8217;ll print a new line.</p>



<p class="wp-block-paragraph">we&#8217;ve got, you know, one iteration of our loop.</p>



<p class="wp-block-paragraph">Maybe I should also print what is the current value of A</p>



<p class="wp-block-paragraph">because the point of this, you know, weird nonsense code</p>



<p class="wp-block-paragraph">is we&#8217;re coming up with a value for A, right?</p>



<p class="wp-block-paragraph">So I should just print what A is at the top of each iteration of the for loop.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say A is equal to, and then do another A at the end of it.</p>



<p class="wp-block-paragraph">And then maybe at the bottom of the for loop,</p>



<p class="wp-block-paragraph">when the for loop is actually ending one of its iterations,</p>



<p class="wp-block-paragraph">I can just say we&#8217;re at the bottom of the iteration so maybe I&#8217;ll put to end</p>



<p class="wp-block-paragraph">iteration and then print the final value of a and then at the top I&#8217;ll say begin</p>



<p class="wp-block-paragraph">iteration at this point you might start to feel like hey aren&#8217;t you using too</p>



<p class="wp-block-paragraph">many words here like why can&#8217;t I just print a bunch of numbers I mean you</p>



<p class="wp-block-paragraph">could but when you&#8217;re coding you need all the brainpower you can get to not</p>



<p class="wp-block-paragraph">just code but to debug coding is already hard enough without making it harder</p>



<p class="wp-block-paragraph">harder for yourself than you need it to be. So, you know, I see people all the time, they&#8217;re</p>



<p class="wp-block-paragraph">trying to debug their code and they just like start printing numbers. And after not very long</p>



<p class="wp-block-paragraph">at all, they&#8217;re looking at the debug output and they&#8217;re just like, what did that number mean?</p>



<p class="wp-block-paragraph">Where is the number I&#8217;m looking for? It&#8217;s just a bunch of numbers. It&#8217;s harder to debug when you</p>



<p class="wp-block-paragraph">do it that way. Do it the nice and pretty way. It just costs you a couple of extra keystrokes to do,</p>



<p class="wp-block-paragraph">you know, nice letters and new lines and things, nice words, labels. Isn&#8217;t that still a lot better</p>



<p class="wp-block-paragraph">Isn&#8217;t that still a lot better than debugging for an extra six hours?</p>



<p class="wp-block-paragraph">Because you can&#8217;t really understand what you&#8217;re seeing.</p>



<p class="wp-block-paragraph">Anyway, let me show you what this looks like so far with just those two lines added.</p>



<p class="wp-block-paragraph">Now we can kind of see, alright, alright, on iteration number 61, where I was 61, then</p>



<p class="wp-block-paragraph">this was the first value of A and then after that this was the second value of A, you know,</p>



<p class="wp-block-paragraph">when we were finished with the iteration.</p>



<p class="wp-block-paragraph">It seems to have gone up by a certain amount.</p>



<p class="wp-block-paragraph">you know it was like a negative blah blah blah 836 and then negative blah blah blah 708 so it</p>



<p class="wp-block-paragraph">kind of increased in value all right that&#8217;s a little bit helpful now it&#8217;s a really good uh</p>



<p class="wp-block-paragraph">place to uh print our decision points so anytime in your code where you have an if or an else or</p>



<p class="wp-block-paragraph">a function call or anything where you are kind of like you know making a decision or processing your</p>



<p class="wp-block-paragraph">data in some way it&#8217;s probably a good idea to announce your decisions so i could say up here i</p>



<p class="wp-block-paragraph">say up here i can go maybe i&#8217;ll say uh main first four and then i&#8217;ll call it uh maybe i&#8217;ll do a</p>



<p class="wp-block-paragraph">double colon there and i&#8217;ll say top if just to make it easy to read that i&#8217;m kind of like at</p>



<p class="wp-block-paragraph">the first if statement um maybe i&#8217;ll even put parentheses i&#8217;ll say a is more than 20 just to</p>



<p class="wp-block-paragraph">kind of like name what i&#8217;m actually doing and then i could replace the a with the actual value of a</p>



<p class="wp-block-paragraph">actual value of a so i could say a like that and maybe uh when i&#8217;m reading this later i might want</p>



<p class="wp-block-paragraph">to kind of actually see the variable name so i could say uh you could just like put a as a string</p>



<p class="wp-block-paragraph">and then maybe its value in parentheses or something like that do a new line um and then</p>



<p class="wp-block-paragraph">maybe after that top thing i&#8217;ll say uh true to indicate that we&#8217;re inside of the the if statement</p>



<p class="wp-block-paragraph">the if statement and then maybe at the bottom of that if statement we&#8217;ll just say</p>



<p class="wp-block-paragraph">a is now true and we&#8217;ll say like a is now you know whatever the new value of a was so just</p>



<p class="wp-block-paragraph">basically we&#8217;re announcing that we decided to go into this if block and then we announce what we</p>



<p class="wp-block-paragraph">ended up changing a to and also you probably anytime you see a compound expression like this</p>



<p class="wp-block-paragraph">You know B time something or a plus something or just you know a big part of multiple parts of the expression any expression</p>



<p class="wp-block-paragraph">That&#8217;s more than just like one variable or one number. It&#8217;s probably a good idea if you break that up into temporary variables, too</p>



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



<p class="wp-block-paragraph">Print out each variable and I know that&#8217;s a lot of stuff to read but again it beats debugging for an extra six hours</p>



<p class="wp-block-paragraph">I&#8217;m gonna leave that be for now though</p>



<p class="wp-block-paragraph">leave that be and</p>



<p class="wp-block-paragraph">I&#8217;m just gonna continue adding comments into the rest of the code. So I&#8217;m gonna do the second if block here</p>



<p class="wp-block-paragraph">So I&#8217;m going to do the second if block here.</p>



<p class="wp-block-paragraph">I&#8217;m going to say we&#8217;re in the first floor.</p>



<p class="wp-block-paragraph">We&#8217;re in the bottom if more than 20 true.</p>



<p class="wp-block-paragraph">And then we&#8217;ll print out what is the value of a after we&#8217;re finished.</p>



<p class="wp-block-paragraph">We&#8217;ll say a is now is now whatever value for a.</p>



<p class="wp-block-paragraph">And then I&#8217;ll do the same thing for this function call.</p>



<p class="wp-block-paragraph">Because, you know, when I call this function, it&#8217;s going to change the value somehow.</p>



<p class="wp-block-paragraph">So we&#8217;re going to do cout about to a plus equals f, you know, call to f.</p>



<p class="wp-block-paragraph">And then inside the arguments, I&#8217;m obviously putting i plus 3.</p>



<p class="wp-block-paragraph">But because i is a variable, we could just print that.</p>



<p class="wp-block-paragraph">So I&#8217;m going to do quote, quote, and then just stick the actual i in there.</p>



<p class="wp-block-paragraph">And then another new line.</p>



<p class="wp-block-paragraph">a finished a plus equals f and then I&#8217;ll just say you know a is now you know a you know print the</p>



<p class="wp-block-paragraph">new value of a you could also say a equal equal a and then print the value whatever you want but</p>



<p class="wp-block-paragraph">the point is I&#8217;m just printing everything here so now let&#8217;s go down a little bit more because</p>



<p class="wp-block-paragraph">there is a function call if you look at this function call down here here&#8217;s like some nonsense</p>



<p class="wp-block-paragraph">being used here&#8217;s some other like you know disabled code really what i&#8217;m doing at the</p>



<p class="wp-block-paragraph">very end is i&#8217;m just taking the input that the function received and i&#8217;m just multiplying it by</p>



<p class="wp-block-paragraph">two and returning it so again imagine that your code is a lot more complicated than this</p>



<p class="wp-block-paragraph">maybe input times two is actually input times two divided by three and then some other function</p>



<p class="wp-block-paragraph">call and then some other this and some other that and whatever so a lot of people will just kind of</p>



<p class="wp-block-paragraph">stuff like a really complicated expression in one line or one assignment or one return statement</p>



<p class="wp-block-paragraph">And that&#8217;s super confusing because you can&#8217;t really be sure that every part of the expression is as you thought.</p>



<p class="wp-block-paragraph">So it&#8217;s a really good idea to break it all up into parts and print each part.</p>



<p class="wp-block-paragraph">So you could imagine that we have like a C out here and we&#8217;ll say C out, you know, begin F.</p>



<p class="wp-block-paragraph">I&#8217;ll say like begin for F and then F receives an input of input.</p>



<p class="wp-block-paragraph">Do an end L.</p>



<p class="wp-block-paragraph">And then I&#8217;ll make a temporary variable here.</p>



<p class="wp-block-paragraph">variable here I&#8217;ll say int temp results equals input times two and then I&#8217;ll</p>



<p class="wp-block-paragraph">print that f and maybe I&#8217;ll do the input again</p>



<p class="wp-block-paragraph">temp result is now and then say temp result so I&#8217;m just gonna print what the</p>



<p class="wp-block-paragraph">temp result actually was and I could print something about I just multiplied</p>



<p class="wp-block-paragraph">thing about I just multiplied the input by two and then you know the more</p>



<p class="wp-block-paragraph">complicated your expression is the more temporary variables you want to use and</p>



<p class="wp-block-paragraph">just kind of print every single one of them and print you know this new</p>



<p class="wp-block-paragraph">variable is the result of dividing by two this new variable is the result of</p>



<p class="wp-block-paragraph">calling some other function and so forth</p>



<p class="wp-block-paragraph">so then we have like a basically you know basically a pretty good idea maybe</p>



<p class="wp-block-paragraph">at the end though instead of multiplying by two again you want to make it more</p>



<p class="wp-block-paragraph">want to make it more consistent with your debug output so like here temp result is obviously going</p>



<p class="wp-block-paragraph">to be the result that i return why would you do the expression all over again for the return part</p>



<p class="wp-block-paragraph">when you could easily get it wrong why not instead just return the temp result that you made right so</p>



<p class="wp-block-paragraph">then that way your output completely matches what you&#8217;re actually returning and for now i think</p>



<p class="wp-block-paragraph">that&#8217;s all we need to do let me run it one more time and then you&#8217;ll see a bunch of stuff now uh</p>



<p class="wp-block-paragraph">Now, we have a lot of information that we can use to trace how our program was thinking and hopefully find the problem.</p>



<p class="wp-block-paragraph">But isn&#8217;t this like a little bit starting to get a little bit hard to read, right?</p>



<p class="wp-block-paragraph">You can see that there are kind of blocks happening whenever we iterate.</p>



<p class="wp-block-paragraph">So, you know, for me, I always try to keep in mind you need all the brainpower you can get.</p>



<p class="wp-block-paragraph">Why not just format it a little bit better so it&#8217;s really easy to just quickly look at one block and see, you know,</p>



<p class="wp-block-paragraph">the couts kind of like belong together so i&#8217;m just going to add one more cout in the main for loop</p>



<p class="wp-block-paragraph">here just at the very bottom i&#8217;m going to go couts and l just so that i get a new line and if i run</p>



<p class="wp-block-paragraph">the program again notice how it&#8217;s easier to see the blocks like oh that&#8217;s clearly one iteration</p>



<p class="wp-block-paragraph">right here you know your mind just grabs onto it faster and that increases your brain power and</p>



<p class="wp-block-paragraph">makes it easier to debug and read the debug output so i can go all right all right okay so</p>



<p class="wp-block-paragraph">all right okay so we&#8217;re right up here oh around uh iteration um number 83 uh a was this number</p>



<p class="wp-block-paragraph">and then it got increased by uh this other number and the reason that that happened is because the</p>



<p class="wp-block-paragraph">function returned 172 so it basically increased it by 172 and then it ended up being that number</p>



<p class="wp-block-paragraph">oh okay i think i see what went wrong oh no right in this particular program if we scroll up just a</p>



<p class="wp-block-paragraph">little bit we can probably see that the integer is overflowing right the integer was getting bigger</p>



<p class="wp-block-paragraph">right the integer was getting bigger and bigger and bigger here and then eventually</p>



<p class="wp-block-paragraph">it jumped into the negative at some point so right here we can see oh okay when did this</p>



<p class="wp-block-paragraph">number become negative suppose we didn&#8217;t want it to be negative um what was this this is like</p>



<p class="wp-block-paragraph">a million this is a billion no wait wait wait this is uh no that&#8217;s a million right there so it&#8217;s like</p>



<p class="wp-block-paragraph">299 million and then it overflowed oh no no it didn&#8217;t it didn&#8217;t overflow it started at 299</p>



<p class="wp-block-paragraph">it started at 299 then at the bottom it was sitting at 899 million that&#8217;s kind of close to a billion</p>



<p class="wp-block-paragraph">and it did jump up from 300 million to 900 million so that&#8217;s a jump up of like 600 million</p>



<p class="wp-block-paragraph">oh you know it&#8217;s like kind of jumping up faster with each iteration oh and i&#8217;m using a 32-bit</p>



<p class="wp-block-paragraph">signed integer which has a maximum value of around 2 billion so now i could possibly realize at this</p>



<p class="wp-block-paragraph">the number is just like it became too big and I need to change my data type or I need to change</p>



<p class="wp-block-paragraph">my algorithm for some reason right so you can kind of get to the bottom of things pretty quickly</p>



<p class="wp-block-paragraph">so here&#8217;s another problem that people encounter when they&#8217;re using caveman debugging</p>



<p class="wp-block-paragraph">basically you you add all these cout statements or print statements or whatever you&#8217;re doing in</p>



<p class="wp-block-paragraph">whatever language you have and then when you&#8217;re finished you&#8217;re kind of like well I guess uh</p>



<p class="wp-block-paragraph">I need to delete all of these Couts because I don&#8217;t want all this you know</p>



<p class="wp-block-paragraph">Junk being printed in my program after I fix the problem or maybe you&#8217;re trying to debug a different part of your program</p>



<p class="wp-block-paragraph">So then you start commenting out all the print statements or you start</p>



<p class="wp-block-paragraph">You know deleting them or whatever and then whoops later on you realize maybe that part of my program</p>



<p class="wp-block-paragraph">Was not actually fixed and I have to add all the Couts all over again from scratch that costs time</p>



<p class="wp-block-paragraph">So that&#8217;s not fun. So here&#8217;s a trick that I like to use. It&#8217;s called a short circuit</p>



<p class="wp-block-paragraph">it&#8217;s called a short circuit it&#8217;s a sort of like a beginning C++ thing or just</p>



<p class="wp-block-paragraph">when you&#8217;re first learning how to use logical operators for boolean</p>



<p class="wp-block-paragraph">expressions so I&#8217;m gonna do a boolean and I&#8217;m just gonna name it after what</p>



<p class="wp-block-paragraph">problem I&#8217;m trying to solve so we could just call this you know main C outs or</p>



<p class="wp-block-paragraph">something like that let&#8217;s I don&#8217;t know primary problem I&#8217;ll call it primary</p>



<p class="wp-block-paragraph">the boolean as true because then what will happen is uh we can then short circuit all of the cout</p>



<p class="wp-block-paragraph">statements we can say primary problem couts and cout like that and the way the short circuit works</p>



<p class="wp-block-paragraph">works is if you have a logical and expression you can see right here we have two sides</p>



<p class="wp-block-paragraph">of that statement now one&#8217;s on the left which is just the boolean and one&#8217;s on the right which is</p>



<p class="wp-block-paragraph">have a logical and then you know one and zero is equal to zero true and false is equal to false so</p>



<p class="wp-block-paragraph">both things have to be true for the expression to evaluate to true which means if the first thing</p>



<p class="wp-block-paragraph">is false there&#8217;s no point in even looking at the second thing at all because if i if i set that to</p>



<p class="wp-block-paragraph">false then the second part the c out is it doesn&#8217;t matter the whole expression is going to evaluate</p>



<p class="wp-block-paragraph">to false anyway so uh the logical operator itself will just you know block out the rest of the</p>



<p class="wp-block-paragraph">you know, block out the rest of the statement.</p>



<p class="wp-block-paragraph">That&#8217;s called a short circuit.</p>



<p class="wp-block-paragraph">So I&#8217;m going to copy the short circuiting to all of my C out statements like this.</p>



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



<p class="wp-block-paragraph">And do that.</p>



<p class="wp-block-paragraph">And then the C out there.</p>



<p class="wp-block-paragraph">And I guess I&#8217;ll leave the final result there.</p>



<p class="wp-block-paragraph">And for the moment, you know, this is just a regular function.</p>



<p class="wp-block-paragraph">apply the short circuiting to this other f unless I do a separate boolean but</p>



<p class="wp-block-paragraph">keep in mind if this was a class and you had two different methods that you</p>



<p class="wp-block-paragraph">wanted to short circuit see outs inside of at the same time you could just use a</p>



<p class="wp-block-paragraph">member variable and name it after the problem you&#8217;re currently trying to solve</p>



<p class="wp-block-paragraph">and then just short circuit each see out in the appropriate method with that</p>



<p class="wp-block-paragraph">boolean so I guess in this regular program that doesn&#8217;t have a class I</p>



<p class="wp-block-paragraph">could move the boolean into the global if I wanted to but I&#8217;m not going to right</p>



<p class="wp-block-paragraph">if I wanted to, but I&#8217;m not going to right now.</p>



<p class="wp-block-paragraph">And if you&#8217;re writing assembly or some simpler language</p>



<p class="wp-block-paragraph">and you&#8217;re thinking, hey, I can&#8217;t short circuit an assembly</p>



<p class="wp-block-paragraph">or some other language, well, you could write a function</p>



<p class="wp-block-paragraph">that just takes a couple arguments.</p>



<p class="wp-block-paragraph">You could write a function that takes a string to print</p>



<p class="wp-block-paragraph">and maybe a number to print or just, you know,</p>



<p class="wp-block-paragraph">something simple like that.</p>



<p class="wp-block-paragraph">And then in the function itself that prints,</p>



<p class="wp-block-paragraph">you could call it debug print if you want.</p>



<p class="wp-block-paragraph">Have it look at a global variable or define</p>



<p class="wp-block-paragraph">that you&#8217;ve set up at the top of the assembly module.</p>



<p class="wp-block-paragraph">set up at the top of the assembly module, which you can just turn on and off,</p>



<p class="wp-block-paragraph">like just do a data byte and set it to a zero or set it to a one.</p>



<p class="wp-block-paragraph">And then the function debug print will just look at that variable to decide</p>



<p class="wp-block-paragraph">whether or not it&#8217;s going to actually print or not.</p>



<p class="wp-block-paragraph">So, you know, it&#8217;s a little more complicated in assembly,</p>



<p class="wp-block-paragraph">but you can do it basically the same concept.</p>



<p class="wp-block-paragraph">Let&#8217;s run this again just to make sure it still works.</p>



<p class="wp-block-paragraph">Okay, so it still works.</p>



<p class="wp-block-paragraph">So now suppose I want to turn off all the Cout statements.</p>



<p class="wp-block-paragraph">If I just change the true to false,</p>



<p class="wp-block-paragraph">the true to false notice how it&#8217;s all gone except for the stuff inside of f which i told you i</p>



<p class="wp-block-paragraph">wasn&#8217;t going to touch but notice how easy it was to mute it and then if i want to bring it back</p>



<p class="wp-block-paragraph">later i just go true to unmute it of course if you want to do a little bit more typing you can</p>



<p class="wp-block-paragraph">also do if statements like if this condition is true and that condition is true then print the</p>



<p class="wp-block-paragraph">debug statement so then you can have a more complicated way of muting and not muting but</p>



<p class="wp-block-paragraph">I&#8217;m going to leave it as is right now.</p>



<p class="wp-block-paragraph">Let&#8217;s see, what else do we got here?</p>



<p class="wp-block-paragraph">I think actually at this point, this is the basic idea of caveman debugging.</p>



<p class="wp-block-paragraph">Just basically print everything.</p>



<p class="wp-block-paragraph">Print all of your decision points.</p>



<p class="wp-block-paragraph">Use pretty labels that are really, really easy for you to understand.</p>



<p class="wp-block-paragraph">Use formatting so that your brain doesn&#8217;t have to struggle to understand what you&#8217;re seeing.</p>



<p class="wp-block-paragraph">If you just print a bunch of numbers, you&#8217;re going to be shooting yourself in the foot.</p>



<p class="wp-block-paragraph">you&#8217;re going to be shooting yourself in the foot and use short circuiting or if statements to</p>



<p class="wp-block-paragraph">selectively mute them so you can do less typing and and debugging the debugging and so forth but</p>



<p class="wp-block-paragraph">yeah i think i think this is all i really wanted to show you in this particular video</p>



<p class="wp-block-paragraph">i hope you feel like an expert in caveman debugging i certainly am and it you know it</p>



<p class="wp-block-paragraph">kind of fits my personality a little bit i&#8217;m like what&#8217;s going on um and like i said before</p>



<p class="wp-block-paragraph">debugger it&#8217;s probably a better idea but I usually don&#8217;t unless I&#8217;m in big trouble</p>



<p class="wp-block-paragraph">and I have a huge mess on my hands I usually just go directly to caveman</p>



<p class="wp-block-paragraph">debugging we&#8217;ll look at the same piece of code in my next video that I&#8217;m going</p>



<p class="wp-block-paragraph">to publish which is going to talk about using a proper debugger so watch the</p>



<p class="wp-block-paragraph">next one also but for now I hope you&#8217;ve enjoyed this video thank you for</p>



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



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



<p class="wp-block-paragraph">whoops what the heck</p>



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



<p class="wp-block-paragraph">i really appreciate it i do hope you did learn something and have</p>



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



<p class="wp-block-paragraph">subscribe and follow this channel or these videos</p>



<p class="wp-block-paragraph">or whatever it is you do on the current social media website that you&#8217;re looking</p>



<p class="wp-block-paragraph">at right now it would really mean the world to me and</p>



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



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



<p class="wp-block-paragraph">please do do me a kindness and and subscribe you know sometimes I&#8217;m</p>



<p class="wp-block-paragraph">sleeping in the middle of the night and I just wake up because I know somebody</p>



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



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



<p class="wp-block-paragraph">or you could you control me if you want to just wake me up in the middle of the</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 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</p>



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



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



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



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



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



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



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/">Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/caveman-debugging-simple-trick-to-debug-c-code-like-a-pro/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
