- 10 months ago
In this practical x86-64 assembly language tutorial we dive deep into conditional branching using YASM (also works with NASM) on Ubuntu/Linux.
We cover:
- The CMP instruction and how it sets the RFLAGS register
- All major conditional jump instructions (JE, JNE, JL, JLE, JG, JGE, JB, JBE...)
- Important difference between signed (JL/JG) vs unsigned (JB/JA) jumps
- Real code examples showing how to implement if-style decisions
- How to handle the "jump out of range" assembler error
- Visual flow diagrams explaining control flow
Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.
Highly recommended companion resource: Professor Ed Jorgensen's free x86-64 Assembly Language Programming book (linked in comments).
Enjoy learning assembly!
Intro to Conditional Branching in Assembly 00:00:00
Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23
Unconditional vs Conditional Jumps 00:01:39
How Conditional Branching Works 00:02:06
The CMP Instruction Explained 00:03:21
Overview of Conditional Jump Instructions 00:04:50
Signed vs Unsigned Jump Instructions 00:06:10
Jump Equal & Jump Not Equal (JE/JNE) 00:07:00
Main Conditional Jumps Summary 00:08:14
Starting the Code Example 00:09:09
Program Structure & Printing Setup 00:10:35
Function Prologue & Getting User Input 00:13:21
First Branch: Is Number Greater Than 0? 00:15:17
Live Demo: Testing Greater Than Zero 00:21:53
Second Branch: Is Number Less Than 10? 00:22:52
Live Demo: Testing Range 0-10 00:26:06
Third Branch: Is Number == 5? 00:26:52
Live Demo: Testing Equal to 5 00:28:00
Jump Out of Range Error & Workaround 00:29:00
Final Summary & Closing 00:32:17
Thanks & Call to Subscribe 00:32:57
=-=-=-=-=-=-=-=-=
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
- Twitter / X: https://x.com/NeuralLantern
- Rumble: https://rumble.com/c/c-3696939
- BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt
- Daily Motion: https://www.dailymotion.com/neurallantern
- Minds: https://www.minds.com/neurallantern/
- Odysee: https://odysee.com/@NeuralLantern:5
Please show your support!
- Buy me a coffee: https://ko-fi.com/neurallantern
- Subscribe + Sharing on Social Media
- Leave a comment or suggestion
- Subscribe to Blog: https://www.NeuralLantern.com
- Watching the main "pinned" video of this channel for offers and extras
We cover:
- The CMP instruction and how it sets the RFLAGS register
- All major conditional jump instructions (JE, JNE, JL, JLE, JG, JGE, JB, JBE...)
- Important difference between signed (JL/JG) vs unsigned (JB/JA) jumps
- Real code examples showing how to implement if-style decisions
- How to handle the "jump out of range" assembler error
- Visual flow diagrams explaining control flow
Perfect for students learning low-level programming, reverse engineering, operating systems, or anyone who wants to understand how if-statements, comparisons, and decisions are actually implemented at the machine level.
Highly recommended companion resource: Professor Ed Jorgensen's free x86-64 Assembly Language Programming book (linked in comments).
Enjoy learning assembly!
Intro to Conditional Branching in Assembly 00:00:00
Recommended Book: x86-64 Guide by Ed Jorgensen 00:00:23
Unconditional vs Conditional Jumps 00:01:39
How Conditional Branching Works 00:02:06
The CMP Instruction Explained 00:03:21
Overview of Conditional Jump Instructions 00:04:50
Signed vs Unsigned Jump Instructions 00:06:10
Jump Equal & Jump Not Equal (JE/JNE) 00:07:00
Main Conditional Jumps Summary 00:08:14
Starting the Code Example 00:09:09
Program Structure & Printing Setup 00:10:35
Function Prologue & Getting User Input 00:13:21
First Branch: Is Number Greater Than 0? 00:15:17
Live Demo: Testing Greater Than Zero 00:21:53
Second Branch: Is Number Less Than 10? 00:22:52
Live Demo: Testing Range 0-10 00:26:06
Third Branch: Is Number == 5? 00:26:52
Live Demo: Testing Equal to 5 00:28:00
Jump Out of Range Error & Workaround 00:29:00
Final Summary & Closing 00:32:17
Thanks & Call to Subscribe 00:32:57
=-=-=-=-=-=-=-=-=
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
- Twitter / X: https://x.com/NeuralLantern
- Rumble: https://rumble.com/c/c-3696939
- BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt
- Daily Motion: https://www.dailymotion.com/neurallantern
- Minds: https://www.minds.com/neurallantern/
- Odysee: https://odysee.com/@NeuralLantern:5
Please show your support!
- Buy me a coffee: https://ko-fi.com/neurallantern
- Subscribe + Sharing on Social Media
- Leave a comment or suggestion
- Subscribe to Blog: https://www.NeuralLantern.com
- Watching the main "pinned" video of this channel for offers and extras
Category
🤖
TechTranscript
00:01Hey there, in this video, we're going to talk about conditional branching in an assembly
00:05program.
00:06We're going to be looking at YASM x86-64 assembly or AMD 64 assembly within Ubuntu, but this
00:12will probably be a useful video if you're just thinking about how to branch in assembly
00:16in general.
00:23So for starters, I'm going to promote one of my favorite books as I often do.
00:30This is a book that is free and open source.
00:32You can get a copy yourself.
00:34I did not write this.
00:35The author of this book is a genius.
00:38His name is Professor Ed Jorgensen, PhD.
00:41He made this whole book and released it under an open source license.
00:45So you can literally just go to his website and get a copy of this for free.
00:48You can use this to make yourself into an assembly expert.
00:52So I highly recommend this to everybody anyway.
00:55So I'm going to open up this book.
00:56I'm going to assume that maybe you'll get a copy or you're just going to follow along
00:59with what I'm doing on the screen.
01:01But I'm going to open this book and I'm going to go to section seven, which is entitled instruction
01:06set overview.
01:07I'm going to double click it.
01:09So open the subsections and then I'm going to click on 7.7 control instructions.
01:17And then under that, I'm going to click on 7.7.3 conditional control instructions.
01:24So that's a lot of subsections, isn't it?
01:26I love a book with a ton of subsections.
01:28It makes things so much easier to find.
01:31If you or someone, you know, is going to write a book, especially if it's technical, please
01:35use lots and lots of subsections anyway.
01:40So conditional control instructions.
01:42In my last video, we talked about the jump instruction where we can just be sitting somewhere
01:46in our program and then just jump somewhere else anywhere we want based on the label.
01:50So conditional branching or conditional control instructions are kind of the same thing, except
01:55they will decide whether or not to actually jump.
01:57They might fall through to the next instruction or they might jump away to somewhere else.
02:03So conditional branching or conditional jumping, it basically kind of goes like this.
02:10Step one, you compare two values to each other.
02:14They will probably be sitting in registers.
02:17And then step two, you will use a conditional branching instruction or conditional jump instruction.
02:25There's lots of synonyms for these, but basically in the regular jump instruction that I just talked
02:30about in the last video, you just jumped and it was only one thing that you had to do.
02:34But in this case, we first have to compare two items and then we'll issue the jumping instruction
02:39because the jumping instruction will have to decide whether to jump or not based on the
02:44result of your comparison, which makes sense, right?
02:46Like if you're going to implement something that's like if, you know, I'll put like a expr
02:52for expression, then well, the first thing that we have to decide before we jump either
02:59into the block of the, uh, if, or if we jump into the block of the else is whether or not
03:04that expression is true, right?
03:06So that's kind of what the compare instruction is doing sort of in higher level languages
03:10like C++, it can get a lot more complicated than that, but you know, at a basic level that's,
03:14that's basically it.
03:16Okay.
03:17So, uh, we're going to do conditional branching instructions.
03:20Um, let me, let me show you a few of those.
03:23Okay.
03:24Here's, here's the compare instruction right here on page 126.
03:29I think it's actually 127 in the PDF, but basically, you know, 126, um, we do CMP followed
03:37by two operands.
03:38So if we want to compare, I don't know, uh, let's say R12 with R13, that's how you would
03:43do it.
03:44Just a two operand instruction.
03:46There's no results that are going to get stored in one of these operands.
03:49Both of these operands are just for input.
03:52The result actually gets stored in a special register called R flags, which we hardly ever
03:56use or look at, um, in, in the most common assembly, uh, instructions and design patterns.
04:02We just kind of like let the R flags do whatever it's going to do.
04:05And the compare instruction will fill that up.
04:07So imagine that, uh, you know, depending on whether R13 is like greater than R12 or less
04:14than, or they're equal or something else is going on between the two of them.
04:18The compare instruction just fills up all sorts of flags defining the relationship between
04:23those two registers or those two operands R12 and R13.
04:27And then after the R flags register is all set up, then the conditional branching instruction
04:32will sort of rely on R flags in order to decide whether or not it's going to jump or not.
04:38Okay.
04:39So let's see, there are tons of conditional, well, not tons, I guess like half a page worth,
04:44but there are lots of conditional branching instructions we can use.
04:48The first two I want to show you.
04:50And, uh, after this, by the way, I'm going to write a sample program just to show you how
04:53it is, uh, in practice.
04:55But, uh, so the first one is J E, which means let's jump if the two operands were equal.
05:00So again, you know, we compare, uh, R12 and R13, maybe I'll put our flags as a, uh, comment
05:08up here.
05:09We'll say sets up our flags and then we'll decide like what we're going to do.
05:13Are we going to jump if they're equal to some label, I'll write some label here.
05:19So that means, uh, the J E instruction will look at the R flags register and decide did
05:24compare, think that R 12 and R 13 were equal.
05:27If it did, then we'll jump.
05:28If not execution will fall down to the next line.
05:31So I'll put an open instruction here, just, you know, a non-operation instruction.
05:35Um, so if they were equal, then we'll end up jumping to some label.
05:39And if they were not equal, we'll end up letting execution fall through, uh, to the nope online
05:4410.
05:45And, uh, same thing for jump, not equal and all these other ones.
05:49But, uh, there are actually two different versions of some of these, uh, instructions that kind
05:54of seem the same.
05:55Like for instance, this one right here, J L that means jump.
05:59If the first operand was less than the second operand and notice how J B is a jump below that
06:07jumps also, if the first operand was less than the second operand.
06:10So what's the difference between these two?
06:12Let me get my pen.
06:13And I love excuses to do my little pen now.
06:15Well, I've got my pen.
06:17It's working.
06:18Oh, I erased it.
06:21Okay.
06:22Okay.
06:23It's working.
06:24Anyway.
06:25So notice how jump less than and jump below are basically doing the same thing, but there's
06:30one difference.
06:32One of them works on signed operands and one of them works on unsigned operands.
06:38So keep this in mind, if you are comparing two signed integers, you want to use, uh, you
06:43know, this group up here of instructions, but if you're using unsigned integers or just,
06:48you know, bits, uh, then you want to use this group of instructions down here.
06:52You definitely want to know what kind of data you have that you're comparing notice though,
06:57that, uh, jump equal and jump not equal.
07:00They don't really care if the operands are signed or unsigned.
07:03That's because if you want to check to see if two things are equal, you just look at all
07:08of their bits.
07:09Their bits are the same.
07:10Then you just say they're equal.
07:11You don't even care.
07:12Actually, if it's assigned or unsigned integer, if it's a float, if it's a, whatever you just,
07:16you just go all the bits equal or are at least one of the bits unequal, then we'll say it's
07:22equal or not equal.
07:23That's it.
07:24So keep that in mind for all of your jump instructions, where you're trying to see if things are equal
07:28or not equal, you're going to definitely be using these, whether you're using signed
07:32or unsigned integers or floats or anything else like that.
07:35But when it comes to actually, you know, integers that are signed or unsigned, these are the
07:40instructions you use for signed.
07:43And these are the instructions, whoops, that you use for what have I done that you'll use
07:48for unsigned.
07:49I keep confusing the on off button for the red button is what I'm doing.
07:53Oh, okay, whatever, I guess we're done anyway.
07:59So keep that in mind in this video, we're just going to be using the signed integer instructions
08:03because that's the example that I have set up.
08:05But just imagine you could very easily translate this to, you know, unsigned integers or floats.
08:11Okay, so let's see, we've got jump less than we've got jump less than or equal to then we've
08:18got jump greater than and then we've got jump greater than or equal to and when you combine
08:22those with jump equal to or jump not equal to that's pretty much all the comparison that
08:27you need.
08:28And if you want to do something much, much more complex.
08:30Well, then you can just, you know, start stacking logic on top of logic and branching on top
08:36of branching to do something that's like this is what I'm talking like if you want to do
08:39this expression and expression or expression and then maybe like a nested expression, right?
08:53If you want to do something super complicated, like we can do in a higher level language, you're
08:57just going to have to make a bunch of mini jumps or find another way to reduce your logic.
09:01But in this video, we're just going to be taking a very simple comparison.
09:06All right, so I think that's everything that I can show you inside of this book.
09:09Let's go to the code now.
09:12So here's a little empty code, source code, a page.
09:15I'm going to stick all my code inside of it.
09:18Another reminder that if you don't know how to write assembly at all, you should see my
09:22other videos because I'm not going to cover the basics of assembly in this video.
09:26If you want to learn how to make a make file or compile or assemble or like do all the
09:30basics that I'm not talking about in this video, I've covered them all in my other
09:34videos up to this point.
09:35So for now, we're just going to focus only on conditional branching, conditional
09:40jumping inside of an assembly module.
09:43So I'm going to start off by setting up my data section real fast.
09:46I'm going to grab my solution here and I'm going to say paste.
09:52And again, you know, this is covered in other videos, but a long story short, I'm just going
09:55to be printing out a bunch of strings while this program executes.
09:58So I'm going to announce that the branching test is going to begin.
10:02And then I'm going to ask the user for some input.
10:05And then I'm going to, based on whatever the user inputs, I'm going to just print out
10:10a message that sort of, you know, understands what the user inputted.
10:14So if the user inputted something greater than zero, it's going to output that.
10:19If they entered something that was less than 10, it's going to print that.
10:21If they entered something that was equal to five, it's going to print that.
10:24And then it's going to say, we've ended the branching tests.
10:29Then I have like a CRLF.
10:30Again, all this stuff is explained in other videos, but we're just basically printing.
10:35So I'm going to start the text section of my program, which is where all the instructions
10:38are.
10:39And I'm going to say that we have an external function that I can call on.
10:45Again, this video does not cover a bunch of other extra stuff.
10:49I'm just going to show you how to branch.
10:50Just trust me when I say that this helps me easily print numbers and things, or I guess
10:56in this particular case, it'll help me input a number from the user.
11:00But for you, if you're trying to run this at home, you can just start hard coding numbers
11:04just to make sure that you can branch correctly.
11:06And then eventually work your way up to a library that can take input for you or see more of my
11:12videos in the distant future.
11:13I don't know, years from now.
11:14I don't know, I might release the library or some other stuff to the casual viewer.
11:22So now here's our entry point.
11:23Again, I mentioned that there's going to be other parts of this program that I'm not showing.
11:27So this is a hybrid program where a C++ driver actually will call on my cool function.
11:32So just imagine from somewhere else in the program, you're calling on the cool function.
11:37How do we know it's a function?
11:38Because it's a label called cool, and then it has a return statement at the very end.
11:43And that's pretty much all we need to know right now.
11:46Let me get rid of that load.
11:47I don't know why I always have the word load in all of my things.
11:49I think I put it in there on an early example, and then I just kept copy pasting to my heart's
11:54content and never stopped.
11:56We mark the function cool as global so that other modules can call on it, like the C++ module
12:02that I'm not showing you.
12:03But if you're writing a pure assembly module, you don't really need to mark your functions
12:07as global.
12:08If everything's within the same piece of source code, it's totally fine.
12:11So then I'm going to make a function call to a function called branch test.
12:16So very quickly before I actually add branch test, I'm just going to add another function
12:21that we're not covering in this video called CRLF.
12:24And long story short, all it does is just it prints a new line in the terminal, and that's
12:29it.
12:29Like it's just it's a convenience function.
12:31I like to be able to call CRLF and have a new line.
12:34Okay, so let's start our branch test function.
12:38So here's the signature.
12:42It is just branch test with a void in front of it.
12:45I'm going to put a RET to say that this is a function that we will return from.
12:49And I left myself a little note here saying that I'm going to use register R12 as the
12:55number that the user inputs.
12:56So that means I'm going to have to preserve that because you have to respect the ABI.
13:03And the ABI says that R12 is a Kali saved register.
13:06Again, see my other videos if you don't know what I'm talking about.
13:09But I'm just going to preserve it in a section that I call the prologue or that actually the
13:13book calls the prologue.
13:14I do that now though too.
13:16And, you know, restore it in a section called the epilogue.
13:19So we have prologue and epilogue.
13:21We can have a push pop pair.
13:22And then in between we can have, you know, the meat of our function.
13:27Okay, so for the introduction, I'm just going to write a few instructions that just print
13:32out a message.
13:32So we're going to print out that intro message and then call CRLF to print a new line and then
13:37return.
13:38So maybe actually at this point, we could probably check to see if this program works
13:42by trying to run it.
13:44So let me say clear and make run.
13:48Okay, so now the driver just prints hello.
13:51The driver is elsewhere.
13:53And we get that welcome message that says begin branching test and then the driver retains
13:58control after that.
13:59So let's do a goodbye message.
14:08real fast.
14:10So we'll do our goodbye message.
14:11It's just another string that I'm printing.
14:13It's no big deal right now.
14:14But if I run the program again, you can see that begin branching test and end branching
14:20test.
14:20Okay, so far, we're not really doing branching yet.
14:23The next thing we need to do is ask for a number.
14:27So this is how I'm going to do it in my program.
14:29In your program, it might be different or you might have a hard coded number or something
14:34like that.
14:35So for now, just trust me when I say that I'm going to print a prompt basically saying,
14:39could you please enter a number?
14:41And then the next two lines are going to be just calling on one of my custom functions
14:46that will input a number from the user.
14:47So the user can type at the terminal.
14:50And we will receive their number through REX, which is the return value register for integer
14:54return values.
14:56And we're going to store it in R12.
14:57So that's why I had to preserve R12 because I'm actually going to start, you know, messing
15:00with the value of R12 now.
15:02Okay, so we ask for a number and then we sort of don't do anything after that.
15:06If we run the program again, it's just asking for a number and I can just like type some
15:10stuff and hit enter.
15:11And then the program ends.
15:12Nothing actually happens.
15:14We're building, we're building.
15:17So now let's ask ourselves, is the number greater than zero?
15:23So let me copy paste some code here.
15:24So right now we've just finished inputting a number from the user and we'll ask, you
15:29know, is that number that they inputted greater than zero?
15:31So again, the first thing we do is we use a compare instruction, CMP.
15:36We give it two operands.
15:38The order doesn't necessarily matter.
15:41For instance, right now I'm asking is R12 greater than zero?
15:44So I can do a jump less than or equal to like a different branch, or I can jump greater than
15:49equal to if I switch the operands or if I had the labels in a different order, but I'm just
15:53choosing to say, let's compare the two and then let's jump if R12 is less than or equal
16:00to, which basically means not greater than, this label right here, which is just branch
16:06test after greater test.
16:09So I'm going to do a print statement right after that.
16:13And then I'm going to draw out what the, what the code is kind of doing.
16:18So let me make a label here.
16:20So this actually will compile.
16:22Whoops.
16:23I'm in the wrong window.
16:26Okay.
16:27There we go.
16:28So what am I saying here?
16:30Okay.
16:31Do the comparison.
16:32And then if the condition seems to be satisfied, then we'll jump to this, which means we'll just
16:38say goodbye and then exit the program because we'll return to the caller.
16:41But if that condition was not satisfied, then we're going to end up executing this stuff in
16:45the middle.
16:46So think about this.
16:47This is kind of the idea.
16:48Let's see if I can remember how to draw this right now off the top of my head.
16:51Once we have like some sort of an input, um, I'll, I'll call this in put and, uh, then
17:00maybe, uh, the input is going to be, you know, uh, whether R 12 and R and zero are equal or
17:07greater than or whatever.
17:08So I'm just gonna say R 12, uh, and zero God, that's awful.
17:13I really need to practice this draw pad.
17:15I spend like a whole week practicing with a different draw pad.
17:18And then when I come back to this one, I'm even worse than when I started.
17:21So we, we come in and we kind of look at R 12 versus zero, and then, um, we will jump,
17:29uh, in one direction.
17:31If, uh, that's, uh, not, uh, you know, greater than or equal to, or, or less than or equal
17:37to, and we'll jump in another direction otherwise.
17:39So basically here we'll say, uh, jump less than equal to.
17:43So I'll say, um, less than or equal to whoops.
17:47Maybe I'll do R 12, uh, uh, uh, I need to somehow adjust the pressure on this thing.
17:54R 12 less than or equal to.
17:58And so this is sort of, uh, has to do with, you know, this branching path that we take.
18:03Like if R 12 was less than or equal to a zero, then we'll take the right branching path, um,
18:09which will mean we jump to after I'll just put the word after we'll jump to the after greater
18:18test sub label.
18:20And otherwise, if R 12 is, uh, sorry, I said less than or equal to, yeah, if R 12 is greater
18:28than zero, then we jump to this other thing, which is going to be the fall through.
18:32So it's going to be line 96, but I'll just say, uh, I'll say fall for fall through.
18:39Basically the jump won't happen if it's greater than or equal to.
18:43So you could imagine if you want to reverse the logic so you can use a jump greater than
18:48instruction, you could, but I'm choosing to do it this way.
18:51And, um, then both of those will eventually reach, let's see.
18:57We'll eventually actually, yeah, the fall through instructions, those will eventually
19:03reach the after instruction.
19:04I'll say, uh, always here.
19:09And then the after instruction will just kind of like finish the program and exit and all
19:15that stuff.
19:15So if you think about the control path here, we have an input.
19:19We're looking at, uh, R 12 with, uh, with the zero, with the compare instruction.
19:24Uh, and then we use a conditional, uh, uh, branching instruction.
19:29So maybe I should write, uh, what do you want to write here?
19:33Maybe I'll just put J L E up here because usually the compare instruction in the conditional
19:38jump or branching instructions kind of, you know, come as a pair.
19:41So both of these combined mean let's look at R 12 versus zero.
19:45And, um, if R 12 is less than or equal to zero, meaning if it will, we'll jump over less than
19:51or equal to the right operand R 12 less than equal to the right operand.
19:55Then we go down this path on the right to the after, uh, label.
19:58If that is false, meaning R 12 was greater than zero, then we'll fall through, uh, to this stuff
20:04right here.
20:06And then whether or not we actually, um, uh, fall through, we'll always end up at the, at
20:13the after area because we're either going to jump directly to the after area or we're going to fall
20:18through.
20:18And then the fall through falls through to the after area anyway, as well, let me just
20:22show you that real fast.
20:24Oh man, I'm having a hard time with this.
20:26Okay.
20:27So let's pretend that, uh, R 12 was, uh, indeed less than or equal to.
20:34So that means instructions are coming, you know, instructions are getting executed.
20:38We're going down and down and down and down and down.
20:40Once we see jump less than or equal to, if that's true, then we jumped to the after label,
20:45which means we just kind of jump around this message.
20:49And so we're not going to exit, uh, sorry, we're not going to execute that code.
20:52So in the case where that is true, where it is less than or equal to, we're not going to
20:57execute those instructions.
20:58So we're not going to say their number was greater than zero.
21:00Uh, we're just going to simply say goodbye.
21:03However, on the other hand, if, uh, that is not true, meaning if the, uh, if R 12 was, uh,
21:10indeed greater than zero, then we're going to fall through because, uh, this jump
21:15less than or equal to instruction, it will only jump if the R flags, if the comparison,
21:21uh, instruction thought that, uh, R 12 was less than or equal to zero.
21:24If that's not true, if it's greater than, then instructions are the, uh, the control
21:29path is just going to fall through.
21:30So that means we will actually execute these.
21:33And then when they're done, uh, execution will continue to fall through to the rest.
21:38So that means we're either going to see a message saying their number was greater than
21:42zero or not based on whether their number actually was greater than zero.
21:47Okay.
21:48Hopefully I explained that somewhat clearly.
21:50Now let's run the code and see if it works.
21:53Hopefully I didn't forget anything.
21:55This is kind of a partial program at this point.
21:56I'm going to add a bunch more stuff to it.
21:58So hopefully this actually compiles.
22:00Enter an integer five.
22:01Notice how it says your number was greater than zero.
22:03And if I run it again and I just type zero, it does not say that it was greater than zero.
22:09If I run it again, I type like a negative five.
22:11It also does not say that it was greater than zero.
22:13You know, type a huge giant number here.
22:15It'll say your number was greater than zero.
22:17You could probably type a number that was just absolutely huge and have it overflow.
22:22I actually don't know how many nines I need to type.
22:26And I don't even know what the behavior is going to be.
22:28I don't know if it's just going to crash the program because this also relies on my ability
22:31to input a number, which is happening in a different library.
22:35Let's see if that even works.
22:38Greater than zero.
22:39Okay.
22:40It probably overflowed and went to a very, very low negative number.
22:43So, okay.
22:44I guess at least I know my library will probably work sort of.
22:48Okay.
22:49So let's go back to the code here and let's upgrade it.
22:51We know now how to check for jump less than or equal to.
22:57We're going to say goodbye right there.
23:00So now the next thing we should do is check to see if the number was less than 10.
23:07So after the greater test, it's going to be here.
23:12So again, the same thing, like we can just reuse R12 because we're not really modifying
23:15it and we're not calling.
23:17Well, I guess even if we were calling a function, R12 is supposed to be callee preserved.
23:21So we're just comparing it and then maybe printing a message if we want to.
23:26And regardless, we're going to end up at 103, this after greater test label.
23:31So then we'll compare again R12 with the number 10.
23:34And we'll say jump if it's greater than or equal to branch test after less test.
23:40So I'm going to do like right here.
23:48So now at this point, we're saying compare R12 with 10.
23:55If it's greater than or equal to 10, then just skip over the message.
23:59So if it's greater than or equal to 10, then wait a minute.
24:03Wait, wait.
24:04Yeah.
24:04If it's greater than or equal to 10, then skip the message.
24:07Otherwise, the message that is printed is going to say that their number is less than 10.
24:11So again, you could imagine a little diagram.
24:15You want me to write it out for you?
24:17I don't know.
24:17I think once is probably enough, but let me just do it again.
24:20We'll do input is coming in and it's just going to be, you know, comparing
24:24and then jumping greater than or equal to.
24:30The input is going to be where we're just looking to see if R12, how it compares to zero.
24:36God, the number one is awful.
24:38My entire penmanship is awful.
24:40Oh, okay.
24:41One more time.
24:42I hope you're laughing at home.
24:43We'll do R12 and 10.
24:48And so that's just sort of what we're comparing with the compare instruction and then the jump
24:52instruction.
24:53And so if we go on the left, I'm going to say that's the fall through path again.
24:59And if we go to the right, it's going to be R12 greater than or equal to 10 is going to
25:10be along the right side path.
25:12And then here, this is going to be after less, I'll say the after label, the after less label.
25:21So if we fall through, it's just going to print a message.
25:23Maybe I should do another little bubble here.
25:25That's just like print P for power, P for print, P for print, it's going to fall through and
25:31then it's going to print and then it's going to go to the after label.
25:34So you can see that the print instructions are only reached if R12 is not greater than
25:39equal to 10, which is the same thing as saying it's going to fall through.
25:43If R12 is less than 10 and then after we finally get to the after label, then we're just going
25:49to do, you know, the rest of the instructions.
25:50We're going to exit the program.
25:51We're going to say goodbye.
25:52We're going to do whatever.
25:54Okay.
25:55So say there never was less than 10.
25:59And then regardless, here's like the ending label that we were conditionally jumping to.
26:04Let's see if this works.
26:06Make run.
26:07So we'll enter five.
26:09Notice how it says your number was greater than zero and your number was less than 10.
26:14Let's do an 11, I guess.
26:18It says your number was greater than zero, but it does not say that our number was less
26:21than 10 because 11 is not less than 10.
26:23If I do a negative three here, it says that our number was less than 10, but it doesn't
26:27say that it's greater than zero.
26:29So we have like a lot of decisions that we could make, right?
26:32Like imagine if instead of just printing a message based on something happening, you could
26:37call a full function or skip a bunch of extra code, you know, whatever you want it to do.
26:41So I'm going to go to the next condition where we're going to compare R12 to the number five.
26:49And we're going to ask, you know, is R12 equal to five?
26:56So this is going to be the jump not equal branch.
27:00And again, you know, if you wanted to say jump, if it's equal to the place where we say,
27:04and then maybe like right after that fall through and just do an unconditional jump, you can.
27:08I just think there are less jumps if I do it this way.
27:11So here we're just going to print, you know, your number was equal to five.
27:15And so basically if it was not equal, we should have another label here that allows us to skip
27:21that.
27:22So same thing.
27:27I'm not going to draw the diagram this time, but basically we're saying, you know, compare
27:32R12 with five.
27:33And if it's not equal to five, then jump to this label down here, which just means let's
27:37say goodbye and not actually say that their number was equal to five.
27:41But if it was equal to five, then this JNE conditional branch won't actually jump anywhere.
27:47Execution will fall through to the next statements, which is just printing the message that their
27:52number is equal to five.
27:53And then when that's done, it'll fall through to this other label and eventually fall through
27:57to the return statement.
27:58So let's see, I think that's actually like a whole thing already.
28:02Nice.
28:02That was easy.
28:04Let's do another run and let's enter the integer one.
28:09It says one is greater than zero and one is less than 10.
28:15Greater than zero and less than 10.
28:16Is there any way I can get nothing to print out greater than zero and less than 10?
28:23You don't think I can do it.
28:25Let me try it.
28:25Nine.
28:26No.
28:26No.
28:28Why did I even put nine?
28:30Okay.
28:30It's always going to say zero or 10.
28:32Okay.
28:32So let's do a four.
28:34We'll get both of those greater than zero and also less than 10.
28:37Let's do a five.
28:38Now we should get all those messages.
28:39Your number was greater than zero.
28:40Your number was less than 10.
28:42Your number was equal to five.
28:44And if I just do a six in there again, it doesn't print your number was equal to five.
28:47We have to enter an actual five for that.
28:50So that's the basics of conditional branching.
28:53Oh, there's one other thing that I wanted to make sure to tell you.
28:55I mentioned this in my previous video where we only talked about jumps.
28:59Bear in mind that these conditional branching statements or instructions, you know, JE, JNE,
29:05all these things, they have a limited range of jumps of where they can jump to.
29:10So if you have a gigantic program and you're trying to jump very, very, very far away based
29:16on the condition, you might have an assembler error where your program won't even compile,
29:20won't even assemble telling you, I think the message is, I wrote it down somewhere, jump
29:26out of range coming from the assembler.
29:28That basically means that these instructions can only jump about 128 bytes away from whatever
29:33instruction they are at or whatever memory location they are at.
29:37So, you know, after your assembler assembles your program and after your linker links
29:44your program and you just have like a binary, you know, if you were to inspect all the instructions
29:48inside of your executable, your binary, you would see that they have offsets, right?
29:52You know, like one instruction is basically eight bytes away from the one that comes right
29:56after it, but some instructions are a lot further away.
29:59So if you're trying to jump to an instruction that is more than 128 bytes away, it won't work.
30:05You'll get that error.
30:06So how do you solve that problem?
30:09Well, maybe I'll just write this down real fast.
30:13I'm not going to make this part of the code example, but just imagine we have, you know,
30:17jump equal to some label and maybe I'll do a comment very, very far away.
30:24And here's the label some label, right?
30:27So if your assembler says, Hey, you can't jump to some label because it's too far away, it's
30:34a jump out of range assembler error, then basically you can just make some other labels here.
30:40You can say, you can say short jump true and a short jump false, you know, just make up
30:47some sort of a label and basically say if it is, um, well, false.
30:58Yeah.
30:58Okay.
30:59I guess this is the way I'll do it.
31:00This is not super efficient, but instead of jumping to the very far away label in the
31:05case of true, you can just jump to the true label.
31:07So I'm going to say jump, uh, to the short jump true label.
31:11Otherwise, if it's not true, I'm going to just do an unconditional jump to the false label.
31:18So now you're definitely jumping in either case and both of these labels are close by.
31:23So then I can pretty much just have the close by true label do an unconditional jump to some
31:30label and that overcomes the limitation of branching instructions.
31:33So, you know, JMP, the regular jump instruction, it has an unlimited jump that it can do.
31:39It can go just to like very, very, very far away instructions.
31:42Uh, so that overcomes it.
31:44And then for the false, you know, I don't know, do whatever you want and then jump wherever
31:49you want.
31:53Uh, it doesn't really matter.
31:54I'm just trying to show you that if you make a short jump to a label that will only have
31:59an unconditional jump.
32:00So it's sort of like two jumps, you know, like a short jump and then a long jump, then
32:04you can overcome that limitation.
32:05Again, bearing in mind that, uh, some label is supposed to be very, very far away and
32:10sort of unreachable.
32:13Okay.
32:13So let me just double check that.
32:15I talked about everything that I wanted to, we looked at the branching instructions, the
32:19sign in this, I drew a diagram for you.
32:22We solved the jump out of range error.
32:24Okay.
32:24So that's, I think that's basically it.
32:26Um, hopefully you feel like an expert now at conditional branching and how to make decisions
32:32in future videos, I'll talk about how to implement an if else block and how to implement loops
32:36and all that stuff.
32:36But, uh, for now, now, you know, that your program can make decisions in YASM x86, 64 assembly.
32:44And conceptually you can apply this to other assembly languages too.
32:48So thanks for watching my video.
32:50I hope you learned a little bit of stuff and had a little bit of fun.
32:52I'll see you in the next video.
32:57Hey everybody.
32:58Thanks for watching this video again from the bottom of my heart.
33:01I really appreciate it.
33:02I do hope you did learn something and have some fun.
33:05Uh, if you could do me a please, a small little favor, could you please subscribe and follow
33:10this channel or these videos or whatever it is you do on the current social media website
33:15that you're looking at right now?
33:16Um, it would really mean the world to me and it'll help make more videos and grow this community.
33:21So we'll be able to do more videos, longer videos, better videos, or just, I'll be able
33:25to keep making videos in general.
33:27So please do, do me a kindness and, uh, and subscribe.
33:31You know, sometimes I'm sleeping in the middle of the night and I just wake up because I know
33:35somebody subscribed or followed.
33:37It just wakes me up and I get filled with joy.
33:39That's exactly what happens every single time.
33:41So you could do it as a nice favor to me or you could, you could troll me if you want to
33:45just wake me up in the middle of the night, just subscribe and then I'll, I'll just wake up.
33:48Uh, I promise that's what will happen.
33:49Um, also, uh, if you look at the middle of the screen right now, you should see a QR code,
33:55which you can scan in order to go to the website, which I think is also named somewhere
33:58at the bottom of this video.
34:00And it'll take you to my main website where you can just kind of like see all the videos
34:04I published and the services and tutorials and things that I offer and all that good stuff.
34:09And, uh, if you have a suggestion for, uh, uh, clarifications or errata or just future videos
34:18that you want to see, please leave a comment.
34:19Or if you just want to say, Hey, what's up, what's going on?
34:22You know, just send me a comment, whatever.
34:24I also wake up for those in the middle of the night.
34:26I get, I wake up in a cold sweat and I'm like,
34:28it would really, it really mean the world to me.
34:31I would really appreciate it.
34:32So again, thank you so much for watching this video.
34:35And, um, enjoy the cool music as, as I fade into the darkness, which is coming for us all.
Comments