• 11 months ago
In this in-depth x86-64 assembly tutorial using YASM, we dive deep into implementing complex if-else and if-elseif-else control structures from scratch. Starting with the fundamentals of conditional branching, we build up to full chained if-elseif-else blocks with multiple conditions - exactly how high-level languages handle them under the hood.

You'll see real working code that:
- Takes user integer input
- Tests against specific values (5, 6, etc.)
- Handles greater-than/less-than comparisons
- Properly branches so only one block executes
- Uses labels, cmp, conditional jumps (je, jl), and unconditional jumps (jmp) correctly

We cover the classic pattern: compare to conditional jump to true block to execute true code to jmp to end to false block falls through or jumps in. Everything is shown step-by-step with live compilation and runtime demos.

Perfect for anyone learning low-level programming, reverse engineering, or wanting to understand how compilers translate if-else chains into machine code. Prerequisites: basic conditional jumps (see my earlier videos).

Code shown works on Linux x86-64 with YASM/NASM syntax. Grab the concepts and apply them anywhere.

Introduction to If-Else in Assembly 00:00:00
Explaining the If-Else Design Pattern 00:00:56
Drawing the Basic If-Else Flow 00:01:01
Comparison and Conditional Jumps 00:02:30
Labels for True and False Blocks 00:03:07
Unconditional Jump to End 00:04:50
Diagram of Execution Flow 00:05:51
Alternative Pattern with Inverted Jump 00:07:00
Recapping the If-Else Pattern 00:08:45
Starting the Code Example 00:09:16
Setting Up Input and Strings 00:09:40
Calling External Functions 00:10:57
Entry Point and Prologue 00:11:40
Asking User for Integer Input 00:13:09
Creating the if_test Function 00:14:56
Preserving Callee-Saved Registers 00:15:51
Printing Begin Message 00:17:03
Implementing Simple If Block 00:18:29
Comparison and je Jump 00:19:18
True Block: Equality Message 00:21:08
Testing Simple If Examples 00:23:48
Transition to If-Else Blocks 00:24:21
Creating if_else_test Function 00:24:47
Setting Up Complex If-Else 00:26:25
First If: Equal to 5 00:27:22
True Block for Equal 5 00:28:33
Else If: Equal to 6 00:30:29
Else If: Less Than 10 00:34:17
Final Else Block 00:37:33
Done Label and Goodbye 00:38:23
Recap of Full Flow 00:39:06
Live Demo of All Branches 00:40:54
Signed vs Unsigned Jumps Note 00:43:38
Recommended Assembly Book 00:44:12
Conditional Jump Families 00:45:05
Closing and Practice Advice 00:46:48

Thanks for watching!

Find us on other social media here:
- https://www.NeuralLantern.com/social

Please help support us!

- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Transcript
00:00Hello there.
00:02In this video we're going to talk about implementing simple if-else blocks in YASM x86-64 assembly.
00:07Although if you're writing in a different assembly language, this video will probably
00:12still be useful to you because I'm going to explain the design pattern or how we can
00:17achieve that at the assembly level.
00:19So x86-64 YASM assembly, also known as AMD 64 YASM assembly.
00:32So if you have not seen my other videos about how to do conditional branching in the first
00:36place, you probably want to go check that first because that knowledge is required for
00:40this video.
00:41There's also a lot of other stuff that I'm just not going to explain in this video, such
00:45as creating a make file, you know, compiling and linking your executable and so forth.
00:50So see my other videos where all those concepts are explained already.
00:54For now, I'm just going to show you how to implement a simple if-else block.
00:59So for starters, maybe let me draw a little bit.
01:02Suppose we had, let me get rid of this thing and then I'll just do a regular notepad.
01:06Suppose we had a higher level language, just so you know what I'm talking about.
01:10Suppose we had a higher level language and we wanted to say if, you know, some expression
01:14is true.
01:15We wanted to say if expression is true, then print, you know, it was true.
01:21Anybody remember that old movie, Little Nicky?
01:24Somebody got exploded and then the guy next to him goes, it's not true.
01:27Okay.
01:28It was not true.
01:32So this is the basic idea of what we're going to implement in assembly.
01:35I'm going to write a full program for assembly to show you this, but you know, you're in
01:39C++, you're in C, you're in, I don't know, whatever language you're in.
01:43And you obviously know how to use if-else blocks at this point, hopefully.
01:47And now we're going to just try to figure out how to implement them in assembly.
01:50So it's important to understand that really under the hood, there's a bunch of stuff happening.
01:57Surprise, right?
01:57Okay.
01:58So first off, we look at this expression, which could be, I don't know, let's say five is
02:02greater than 10 or a is equal to b or whatever it is that you put in there.
02:07You could make a very complicated expression.
02:08We're going to use simple expressions for this video.
02:13So if we're comparing a to b, we'll end up using the compare instruction.
02:18Remember there are two steps to conditional branching in YASM.
02:20We first use the compare instruction against two operands.
02:24And then that will end up filling up the R flags register so that we can later conditionally
02:28jump based on the results of the comparison.
02:32So we do a comparison and then you can imagine that the beginning of this, you know, the true
02:38body, notice how it has a scope.
02:40I'm going to just put this brace on another line here to indicate that there is a scope
02:45from line four to six indicating all this code in here is executed only if the if statement
02:51was true.
02:52And then all of this other stuff is executed only if the original comparison was false, right?
03:00So you can imagine now label is something like my if was true.
03:11So we can make a label for where that body starts and another label for where the else starts.
03:17Label is something like if was false.
03:24And then what will happen is we can use a conditional jump instruction after we do our comparison.
03:29Maybe I should put a pair right here.
03:32Oh, I'm using I'm using assembly style comments.
03:35I should be using a C style comments if I'm actually writing C++ here.
03:40Let me just do that.
03:42OK, so this is the comparison instruction here.
03:45And then here's the beginning of the body of stuff to execute.
03:48If it was true, you can imagine that there could be many statements here.
03:52If it was true or if it was false.
03:57Just to prove to you that we can execute like a full body of stuff.
04:01So we have a label that designates when that body starts.
04:04And then we have another label designating when the else body starts.
04:08And then we should have a label that that designates when the whole thing is over.
04:13A label is something like if and or if is done or something like that.
04:22So basically what we want to do is to implement an if else, a simple if else in assembly, we're
04:29going to say let's do a comparison and then we'll do a conditional jump.
04:32Where are we going to conditionally jump?
04:34Well, if the comparison was true, then we will just jump right here into this label.
04:39You know, the if was true label.
04:42And then what will happen is execution will fall through.
04:46When it reaches the end, we want to have another jump statement that unconditionally
04:50jumps to the end of the if statement.
04:54If we didn't, then whenever the expression was true, we would end up executing all of the
05:00true statements and then it would fall through to all of the false statements or the not true
05:03statements.
05:04So we have to have many jumps in here just to sort of say let's get into the body and
05:09then let's finish and jump out of the body.
05:12On the other hand, if the expression was false, then our jump instruction is going to jump,
05:18you know, over the true body.
05:21So it's not even going to do that at all.
05:23It's going to execute all of the else stuff.
05:25And then it's, you know, sometimes a good idea if you have another jump here that just
05:29jumps to the end.
05:30But you can kind of see by the way I've written this out that there's not going to be anything
05:33between the end of the else body and the actual place where we're finished with everything,
05:38which means we don't really need an unconditional jump at the end of the else body.
05:42We can just let the execution fall through.
05:46So maybe if I can draw a little diagram here, I'll say, well, I don't know, I'll do like
05:54if, put it in a little bubble, and we'll say that if it was false, we jump to one place.
06:01And if it was true, we jump to another place.
06:06I hope you've already started to understand this by now.
06:09So we'll say if it's, if the expression is true, we jump here, if the expression is false,
06:14we jump over here.
06:17And so true would be saying, let's jump to the if was true.
06:21So we're going to jump to if was, I'll put a T there, because I'm running out of space,
06:27if was true.
06:28And then if it is false, we jump to if was, I'll put an F, false.
06:37And then at the end of the true, we jump to the done label.
06:41So I'm just going to put done maybe down here.
06:45So at the end of the true, we just unconditionally jump to the done area.
06:49And then at the end of the false, we jump unconditionally to the done area as well.
06:55Here's something interesting, though, when we have a comparison instruction, and then
06:59we have a conditional branch instruction, let's say, let's say A equals B, we did that comparison.
07:04And then we wanted to jump into the true area, if A equals B was true.
07:08So that means we'll say jump equal.
07:13So if A is equal to B after we compare, well, let's say A comma B, we're going to use registers
07:18when we come to the code, we'll say compare A and B, and then jump if they're equal to
07:22some label.
07:23And we're going to jump to if was true, right?
07:29But the conditional branching instructions, they only jump to one potential place or fall
07:35through.
07:36So if the comparison was false, meaning if those two things were not equal, then we're not
07:42going to actually be able to jump to a different label.
07:44We're going to simply fall through to the next instruction.
07:48So in that case, the very next instruction would get executed.
07:51Let's just put a jump there that jumps to the else.
07:54So we'll say jump if was false.
08:03Meaning we compare A and B. And if the two things are equal, we'll jump into the true block.
08:09Otherwise we fall through to the next instruction, which that's a very poorly written J, but we're
08:15going to unconditionally jump to false.
08:18So if we did not jump to the true area, we fall through to the next instruction where we
08:22will always jump to false.
08:24And that implements the diagram that you see of a book on the, you know, if we have statement,
08:28we compare, maybe I'll do, you know, A equals B.
08:33We jumped to true if was true and otherwise we end up falling through and then jumping
08:37to if was false.
08:40And then at the end of both of those, we have an unconditional jump instruction that jumps
08:45to the done.
08:46So I'll put JMP down here just to let you know, you know, at the end of each of these
08:50blocks, we can imagine that they are jumping out of themselves at the very end to the done
08:56area.
08:57So that we know both of these jumps end up jumping to the done area.
09:03That's the basic idea for how to implement an if else block, a very basic one.
09:08We're going to do more complicated ones later.
09:10But for now we kind of have the idea down.
09:12I think let's look at some code dang, I blew 10 minutes already on that.
09:17Okay.
09:19So I'm going to copy paste some code from my solution here.
09:21This again, this is not like a beginner's assembly video.
09:25If you need to learn how to write assembly in the first place, how to compile, you know,
09:30link, create a make file and so forth.
09:32You need to see my other videos first.
09:34But for now, we're just going to assume that you know how to make a data section in Yasm.
09:38And we're going to say, let's make a bunch of strings.
09:41So first I'm going to ask the user for an integer, and then I'm going to make a bunch
09:44of decisions like I'm going to do, you know, an if else block to test what kind of number
09:49they put, you know, did they make a number that equal one, if it was equal to something.
09:58Oh, did I ask twice?
09:59I can't remember what I'm doing, but basically we're going to print something.
10:03If their number was equal to something else, we're going to print something.
10:07If their number that they inputted was equal to five, we're going to print something else.
10:10If their number was equal to six, we're going to print again, something else.
10:14If their number was less than 10, we're just going to do a bunch of stuff.
10:17And then wait, isn't this the complicated example?
10:20Oh, no.
10:21I think I am using code for my more complicated example, else less than 10.
10:29I don't know.
10:30Maybe this is the simple one.
10:31Let's double check.
10:32I guess if I put more complicated code in here, you'll probably be happy, but whatever.
10:36I thought this was going to be a simple example.
10:39So I'm just defining strings at this point and, you know, file descriptors and system
10:43call codes, stuff that is covered in other videos.
10:47Now I'm going to start my text section.
10:50So my text section begins with a declaration that I'm going to use two external functions.
10:57So this video is not about this library that takes input and sends output.
11:01I have other videos for that, but basically I'm just using a library that lets you type
11:06an integer into the terminal and then it will print a different number to the terminal for
11:10you.
11:11You can imagine if you wanted to follow along with this code at home and you don't have
11:14this library.
11:16You can just hard code your numbers just to prove to yourself that you can get it to work
11:20or you can use a different library.
11:22If you have a hybrid program and you're linking against GCC, you can just use scanf and printf
11:27pretty much.
11:28Those take a little bit more work to do, but you can do it anyway.
11:32So our entry point is going to be called if tester.
11:36It's a function called if tester.
11:40Again this is a hybrid program and hybrid programs are not covered in this video, but you can imagine
11:45that there is a C++ module elsewhere in my source that is just going to call on a function
11:50called if tester.
11:53And so that's why I'm marking this as global so that other modules in my program can call
11:58on if tester.
11:59So it's just a little label that we can jump into or in this case call into.
12:03Let me go down to the bottom of that.
12:06We'll put a return statement at the very end.
12:09So now this is officially a function and notice how I made a note here that says R12 is the
12:15user's inputted integer.
12:17So that means I'm going to be using R12 and since R12 is designated in the ABI, the application
12:23binary interface as Kali saved or Kali preserved.
12:26That means I have to do a push pop pair to preserve it or I'll get in lots and lots of trouble.
12:32My program will crash and everyone will hate me.
12:35So I'm just going to call this the prologue, which my favorite book also calls the prologue
12:40and then I'm going to say epilogue.
12:44And so now we have a function that basically doesn't do anything, but we can at least jump
12:47into it.
12:48Let's see if this compiles as is.
12:50I think it probably will.
12:52Yeah.
12:53So the driver just says, hello, that's the C++ program with the actual main function and
12:58it calls my if tester function, but nothing happens.
13:02The if tester function returns control to the driver and then the driver just says, okay,
13:05I got control back.
13:06So nothing really happened.
13:08So now let's ask the user for some input.
13:11So again, this is not a video about this library or how to print with this with system calls.
13:16See my other videos if you need help on that.
13:18But basically we're going to print a message to the user, hey, please input an integer.
13:22And then we're going to call on a library function that lets them type in a number and then returns
13:27it to us in RAX.
13:28We're then going to store it in R12.
13:31And so, well, just basic stuff, right?
13:34So if I run this again, it's going to ask for an integer.
13:37Please enter an integer.
13:38If I can type that and then nothing else happens.
13:41Now we can kind of start making more decisions.
13:46So I'm going to, let's see, if test, if test, if test.
13:53Okay.
13:53So what I need to do now is run another function called if test several times.
14:00And I'm going to compare the number that the user inputted.
14:02So remember R12 is the user's input.
14:05So I'm going to load that as the first argument of a function call.
14:08And then I'm going to load the number five as the second argument of a function call.
14:12Probably not a great idea to hard code numbers in your assembly.
14:14You should probably define them up in the globals or the data section, at least as just regular
14:20defines and not numbers in memory.
14:24But I'm going to just keep these numbers here just so you can see them more.
14:28So three times we're going to call if test and then also another function called crlf.
14:33So first I'm going to paste in crlf.
14:38Where the heck is that?
14:39Oh, dude.
14:40Again, this is not a video about the basics.
14:42So you're just going to have to trust me or go watch my other videos if you don't understand
14:47what I'm doing here.
14:47But I have a function called crlf and its whole job in life is just to print a new line
14:52for me just because I like to do it that way.
14:55So that handles the crlf call.
14:57Now let's make another function called if test.
14:59So I'm going to start that by designating its label right after this block of code
15:07here, maybe before crlf.
15:09So we have like a basic if test function and here's my prototype just to remind myself what
15:14I'm going to be doing.
15:15It's going to take an input and it's going to take another input for test against me.
15:21So the first one is like the user's input.
15:23The second one is the number that I want to test it against.
15:25They're both longs, which means they are both integers, which means the incoming arguments
15:31are going to be RDI and RSI if you're respecting the ABI.
15:36And then some notes to remind myself I'm going to be using R12 and R13 inside of this function.
15:40So I'm going to start by putting a return statement there since that is what it takes to make
15:45a label into a return, into a function.
15:48Then I'm going to preserve the callee saved registers.
15:51Again, if you don't know what I'm talking about, see my other videos.
15:54We're going to push R12 and push R13 so that they are not ruined for the caller.
15:59We call this the prolog.
16:01Then at the very bottom of the function, we have the epilog, which just restores the registers
16:07in reverse order.
16:08You've got to do it in reverse order.
16:10See my other videos if you don't understand why.
16:11Okay, so that's basically a function that can get called.
16:16It doesn't do anything.
16:18Let me double check that the program still actually works.
16:2266 and nothing happens.
16:24We just printed CRLF a bunch of times.
16:27Okay, so now we're ready to continue.
16:29So let's grab the function arguments.
16:31Remember we were going to use R12 and R13 for the user's input and the number we will
16:35test against.
16:36Those came into our function with RDI and RSI.
16:39So I'm just going to copy those two incoming arguments into R12 and R13.
16:44And you're supposed to do it this way.
16:47If I keep the user's input in RDI, then the moment I call any other function or system call,
16:53I'm just going to lose that data.
16:55So I'm going to keep it inside of R12 and R13.
16:57So I grab the function arguments and then I print a begin message just to let the user
17:02know that we're going to start, you know, making tests against our number.
17:07So this is just basically a message saying, hey, begin the if test and then print what
17:12we're going to check against.
17:14So the next thing is I'm going to let the user know what the second incoming argument was.
17:21If you look at R13 here, that was RSI, which was the second argument.
17:27So the test against me number.
17:29So we're going to check the user's input against whatever we called for the second argument.
17:33So I just wanted to print it out, you know, like we're testing your number against whatever.
17:39So that means I need to make another call to my little printing library here.
17:42So I'm going to give R13 to RDI, which is the first argument.
17:46Notice how we already, we already destroyed RDI.
17:48That's why I'm keeping the input in R12 and R13.
17:52And then we're going to make that call and we can just assume everything will go according
17:56to plan at that point.
17:58And then, then we're going to print a special message only if something actually happens.
18:05So we're going to implement the if else block in a moment.
18:07Let me just run this real fast to make sure it works.
18:11It's 44 and then it says we're calling the function three times and we're saying the
18:16basic test has begun.
18:17We'll test against this number five.
18:20So we're testing your input against five and then six and then seven.
18:23And then we didn't actually do anything.
18:24We're about to.
18:26Okay.
18:27So then the next thing is we're going to print a special message only if the user entered
18:33the right number.
18:34So first off, remember we talked about before, we're going to think about taking a C++ or a
18:40C if else block and converting it into assembly.
18:43So I'm sort of placing that in comments for you.
18:46So the comparison instruction, I didn't want to put the R12 equals R13 inside of the same
18:54comment that lines up with the compare instruction because the compare instruction, as I've said
18:59in other videos already, it doesn't actually check to see if something's equal.
19:03It just makes a bunch of judgments against the two operands and it fills up a special
19:07register called R flags with information that we can later use to decide if the two things
19:13were equal or not equal or greater than or less than or whatever.
19:16So that's why I chose to put that on the next line.
19:18So at this point, we're saying if those two things were equal, and that's how I implement
19:24the expression in the middle, then let's jump to a special label called if test, if was
19:30equal, so you can come up with any scheme you want for your labels.
19:34But for me, when I have, when I have sub labels inside of a function, I like to just suffix the
19:40function's name with an underscore and then start thinking of sub labels after that.
19:45So everything's like kind of clean.
19:46There's less chance of overlap in labels.
19:49If you have like a giant module with tons of functions, so I'm going to say, this is like
19:54my main if that I'm checking and I'm going to jump to a label called was equal, meaning,
20:00you know, this evaluated to true.
20:01That means that should be the true part of the block.
20:04Let's see.
20:04Do I still have that code here?
20:06Yeah.
20:06Right.
20:07So here I'm going to jump to the true block.
20:12So, you know, if was true in the first example that we talked about, so I'm going to say if
20:17it's, if those things were equal, jump to the code for the true block.
20:21Okay.
20:22That means I actually need the true block, but I guess we're going to set that up in
20:26a second.
20:27Otherwise, if that did not jump away, it means that those two things are not equal.
20:31So I said I should jump to the else block or the false block.
20:37Hmm.
20:37Wait, do I have else in this example?
20:41Oh, okay.
20:41Okay.
20:41This first thing that we're looking at is only if, so we don't even have an else block yet.
20:46We're going to do that as the second example.
20:48So we're basically going to jump to the done area.
20:53If we didn't jump into the true area.
20:55And then at the end of the true area, we can either jump to the done area or we can let
20:59the execution fall through.
21:01Okay.
21:02So now that means I need if was equal.
21:05And if I just copy paste a big giant block of code and try to explain it to you real fast,
21:11let's do this.
21:15Okay.
21:16So we were going to jump to if was equal, if R12 was equal to R13.
21:21And then we have this, you know, label here.
21:27And notice how I've kind of like put a brace here indicating, hey, this is the beginning
21:30of the true block body.
21:32You should consider doing this too when you're first learning.
21:34And even after you've learned, because let's face it, assembly is tough.
21:38And so in the true area, I'm just going to print the equality message.
21:41I'm going to say, hey, your number was equal to, you know, whatever.
21:46And then I'm going to actually print the, let's see, R13 number.
21:50So that was, I think the number to compare against the test against me number.
21:56So that means here in this message, we're going to say, hey, your number was equal to
22:00the number that we tested against.
22:03So then otherwise, let's see, or sorry, after that, we'll print the suffix of the message.
22:10And so, you know, I just, I just like to make pretty, pretty printed messages.
22:15So let's see, where's the suffix here?
22:17I can't even find it.
22:18How about if yes equal to, oh, I should have put suffix instead of the number two.
22:26That would have been better.
22:26So what I wanted to do is say, your input was equal to, and then print the number.
22:31And then after that, print an exclamation, just to prove to you how easy it is to, to
22:35make a pretty message that's formatted nicely for the user or your professor or whoever.
22:40So, you know, you basically just print a number and then you, or sorry, you print the prefix
22:45and then you print the number and then you print the suffix, not too big of a deal.
22:49So let me go down here.
22:50We print the suffix and then that's the end of the true body.
22:55And then since we're done with the true body, we can basically just say, all right,
22:59now we're done with the if.
23:01So the next instruction that follows is the done area.
23:05Again, looking back at the example here, that would be sort of like here after the whole
23:09entire block was finished.
23:11We're ignoring the else in this code, but you can imagine we'll do that soon.
23:14So if the user's number matched something, we execute a true body.
23:19If not, we jump to the done area.
23:22And if the user's number did not match, then we just immediately jump to the done area so
23:26that we don't do the true area.
23:29And then we write comments to ourselves to help us remember, oh, look, here's like the
23:32comparison.
23:33And then here's the body of true.
23:35And then the done area is like we could put another comment if we want.
23:38We could say like this is done.
23:39But I kind of think like I kind of think the label is self-explanatory.
23:44So let's see if this worked.
23:46It's always possible that I screwed it up while I was talking too fast.
23:50We'll run it and we'll say 44.
23:53Okay.
23:55So nothing matched any of the numbers.
23:57So let's type the number six so that we get a little message on the second one.
24:00So I'm going to type the number six and you can see it did not match the number five
24:05and it did match the number six.
24:07So we got that true block executing when we called on that function where the number to
24:12compare it to was six and then it wasn't seven.
24:15So we saw nothing there.
24:16So now we know simple if blocks.
24:21The next thing we're going to do is if else blocks.
24:28All right.
24:28So let's see.
24:30I'm not sure if I'm going to cut the video and split this up into multiple parts.
24:34Probably would have been a smart idea.
24:36Let me know if I ended up doing it.
24:38We're at about 25 minutes now.
24:39But anyway.
24:42So now we're going to look at if else blocks.
24:47So I'm going to start off with another function.
24:50I'm going to call it if else test.
24:53So here's the tester.
24:54It called on if test.
24:56And then I'm going to, you know, I had the if test function that I did previously.
25:01And so now we're just going to make an if else test function.
25:05The if else test.
25:06It just has one input, one argument for input.
25:11And we're going to just sort of compare it against different values.
25:14We're not going to call this multiple times with different values to compare against.
25:19So that means let's see.
25:20We only need to use one register.
25:21So I've designated R12 as the user's input.
25:25So that's where we're going to store it.
25:26Which means we should preserve it in the prologue.
25:30So I'm going to push R12.
25:32And then again, if you don't understand some of the basic stuff that I'm skipping over,
25:37see my other videos where I explained everything you need to know to actually have this kind of a program.
25:42So I'm going to have a function that enters.
25:45And it returns at the end.
25:48It uses R12.
25:49So we will preserve it with a push pop pair.
25:51And then the first thing that I should do is grab the user's input from the first argument.
25:58And if you're respecting the ABI, that means it should come from RDI.
26:01So I'm going to move RDI into R12.
26:06Then I'm going to say hello.
26:09Somebody remind me that the first part of the program has to actually call this function or nothing will happen.
26:15So we're going to say hello.
26:16That's just a simple message that we talked about before.
26:18And then now we're going to actually implement the if-else block.
26:25So it's like a little bit more complicated than just the simple if block.
26:29It's going to be this whole example that we talked about before.
26:33Let me see.
26:34Maybe I should add the calls to this block real fast.
26:37So we have the if-tester and then run the complex if-else tests right before R12.
26:45Okay, let me just double check that I'm making a call at the right spot here.
26:50So we have R12 and 7 and then epilog.
26:53Okay, so now finally in our program, we're going to have a call to the if-else test function that we're just making right now.
27:04Okay, so we have that.
27:06And let me find that source again real fast.
27:10If-else test, we got the prolog.
27:12We take their input and then we say hello.
27:15Okay, so now we need another label that begins the if-block.
27:22So what we're doing is we're checking to see if the user's input was equal to 5,
27:28and then we're going to say something if it was, and otherwise we're going to do an else block on that.
27:33So again, I like to write my comparison instructions with a blank expression
27:36in terms of the C equivalent comment.
27:41So notice how we're checking to see if R12 is equal to 5.
27:45I put that on the next block because we're going to jump to the true part of the if-block
27:50if it equals 5.
27:52So that's why I put that there.
27:53The comparison instruction pretty much just compares R12 with 5,
27:57sets a bunch of values into the R-flags registers
27:59so that we can later conditionally jump if we want to.
28:04So basically we'll jump to the true place if that was true,
28:09and then if not, execution falls through to the next statement which will just jump to the else place.
28:16So we need more labels is what I'm saying.
28:19So now we need a body for if the statement was true or the expression was true,
28:25we want to be able to execute the true portion of the if-block.
28:28So that's this right here.
28:30So we're going to jump to if it did equal to 5.
28:32If you could imagine making a better label instead of equal 5,
28:36you could say first else if complicated block true scope or true block or something.
28:46But I just put equal to 5 basically saying we'll execute this code if it was equal to 5.
28:51So again, we're just kind of like comparing,
28:54and we'll jump into this label equal 5 if R12 was indeed equal to 5.
29:00So that means if it was, we execute all this code right here.
29:03We know we're finished when we have the very last line saying let's jump to finish the if-else block.
29:10Again, just to clarify,
29:14we do the comparison first,
29:17and if the comparison was true, then we'll jump into the true area.
29:20So, you know, we'll jump into the true area,
29:22and then all of these instructions get executed.
29:26But if we don't have a way to jump out of that block,
29:29then whoops, all of the else statements are going to get executed too, right?
29:34So we don't want that.
29:35We don't want to execute both the true and the false statements.
29:38We want to have a jump instruction at the very end.
29:40I'll put jmp just so that we jump to the end of the if-else block,
29:46you know, jump to the place where it's just all over and finished.
29:50So, okay, so now I'm going to look at, whoops, turn that off.
29:58So we have equal 5.
29:59So we're going to jump to if-else test if done.
30:04Let me just double check to make sure I'm not forgetting anything.
30:07Oh, we need to jump here,
30:09basically meaning we're going to jump into the else portion.
30:12So here we covered jumping into the true portion,
30:15you know, the regular top block.
30:17Now here we're going to jump into the else portion.
30:18We need a label and some code for that.
30:21So this is the else 6 begin.
30:25I copy that.
30:28Oh man, it's starting to get kind of hectic in my brain here.
30:31The copy pasting is worse than actually writing the program.
30:33So, if the comparison was not equal, like if R12 was not equal,
30:41then execution falls through to line 216,
30:44and then we unconditionally jump to the else 6 begin area,
30:47which is like down here.
30:48And then we'll print something else.
30:51We'll, oh, we'll check again.
30:54So we're doing like if, else, if, else.
30:58Okay, so originally when I was talking about the if-else block,
31:03I didn't do a,
31:04I didn't do like a very complex if-else statement.
31:08We'll say A is greater than B, something like that.
31:10So I'll just put more code here,
31:14just so you know that we can do if, else, if, else blocks.
31:18But just, you know, again,
31:19keep in mind that every scope here that you're going to try to run,
31:23based on some condition,
31:24you just make it its own label
31:25and make sure that at the very end of the scope,
31:27you jump away so that you reach the very end of all this stuff.
31:31Because just as a quick review,
31:33if we are just writing in C or C++,
31:36only one of these blocks is going to execute, right?
31:38Like if A is equal to B,
31:40then only that first block will execute.
31:42The second and third blocks will not execute.
31:45It'll jump after that all the way down to line 21.
31:48Only if A does not equal B
31:50do we even have the chance of checking to see if A is more than B.
31:54If it's false, then we'll have the chance to check the else.
31:57And if it's true,
31:58we will only execute the code in the line 12 block.
32:02You know, imagine there are more statements there.
32:04And when that block is done,
32:05then we will jump to line 21.
32:06So else will only execute
32:09if A is not equal to B
32:12and A is not greater than B.
32:16So just a quick, you know,
32:17C++, you know,
32:19design pattern review,
32:20if else block review.
32:21So we have like an else if here.
32:23We're going to say,
32:24check if the input was equal to six.
32:26So we just do the same thing that we did before.
32:28We compare R12, the user's input with six.
32:30We jump if they were equal to the else if equals true block.
32:36And if not execution falls through the line 237,
32:38we jump to the less than 10 begin block.
32:41So this is going to be like really complicated.
32:43How many lines did I actually,
32:46I think I got excited when I wrote this.
32:48Okay.
32:49Else if equal six true.
32:51My challenge to you is to come up with labels
32:53that are like way easier than the labels that I came up with.
32:57All right.
32:57So we're going to do this.
33:00I can almost guarantee that when I'm done copy pasting everything,
33:03something is not going to compile
33:04because I forgot a label somewhere.
33:06Else if equals six true.
33:09Okay.
33:11So what's happening here again?
33:15So at this point,
33:17you know,
33:17the first if expression was not true.
33:19R12 was definitely not equal to five.
33:21So we jumped down to else if equal six begin,
33:24which was here.
33:25And so then we just make another comparison to see,
33:28well,
33:28okay,
33:28it was those two things weren't equal.
33:29It wasn't equal to five.
33:31So let's check to see if it was equal to six
33:33and then do the same thing.
33:34If it's true,
33:35we jumped to yet another scope.
33:37If it was false,
33:38we go down to line 237
33:40and jump to yet another scope.
33:42So here is what will get executed
33:45if R12 was indeed equal to six.
33:47And then we're just basically going to say it to the user
33:48and then jump to the done label,
33:53meaning like we're totally done with our if else block.
33:55Notice how the first if here,
33:59when it was totally finished,
34:00it jumped to the done area.
34:02And this else if block is also jumping down to the if done area.
34:08So eventually we're going to need that label.
34:11Okay.
34:11So then next,
34:13we're going to check to see if the user's input was equal to a 10.
34:17So like kind of the same thing here.
34:19We're going to do another copy paste
34:21and we're going to say,
34:22all right,
34:22so if the user's input was not equal to,
34:27let's see,
34:27if it was not equal to five,
34:29then we jump down here for our next comparison.
34:33We check to see if it's equal to six.
34:35If it was not equal to six,
34:36then we jump down to the less 10 begin line,
34:39which is like all the way down here.
34:41We do another comparison to see,
34:42all right,
34:43well,
34:43is it less than 10?
34:44You know,
34:44if it was not equal to five or not equal to six,
34:47then we check.
34:48Is it less than 10?
34:50If it,
34:50if it is,
34:54then we jump to the else if equal less 10 true block,
34:58which is going to be the next block up here.
35:01I made this way too complicated.
35:03I realized that now,
35:04but it's too late.
35:05I'm going for it,
35:06man.
35:07Anyway,
35:08so
35:09if that statement is true,
35:12if R12 was indeed less than 10,
35:14then we jump to this block
35:15and we basically just say that to the user
35:16and then we jump to the done.
35:20So finally,
35:22notice how this part right here on line 259,
35:24it's basically saying if R12 was not less than 10,
35:27then we'll jump somewhere else.
35:30Notice how it's just else,
35:32right?
35:32So this is like the very,
35:34the very bottom.
35:35So I think the way that I wrote this code
35:38is we have two else ifs,
35:41right?
35:41We have like an if five,
35:42else if six,
35:44else if less than 10.
35:45So maybe I could do something like this.
35:48If R12 equals to five,
35:53do some stuff.
35:54Else if R12,
35:57I think I said six just now,
35:59right?
35:59Hopefully I actually did say six.
36:01Otherwise,
36:01if R12 is less than 10,
36:05then do some stuff.
36:06Otherwise,
36:07if nothing else matched,
36:08then we'll execute,
36:09you know,
36:09this block right here.
36:10So again,
36:11remember every single scope
36:12has to have its own label
36:13so that you know where to jump.
36:16And it's also a really,
36:17really smart idea for every single scope
36:19to have,
36:20you know,
36:22to have a little jump instruction
36:25that jumps past all of the if else,
36:28if else,
36:28if else stuff.
36:29So we'll say like,
36:30you know,
36:30label,
36:31you know,
36:31done so that we can make sure
36:34that only one of these blocks
36:35actually executes,
36:36which is how you're supposed to imagine
36:39C and C++ work.
36:41And so it's just complicated
36:43because there's a lot of stuff
36:44to copy paste,
36:44but you can just still see,
36:46you know,
36:47only one of these blocks
36:48ever is supposed to execute.
36:49So we give the first one a chance.
36:52We jump into it.
36:53If it's true,
36:54if it's not true,
36:55then we give the second one a chance.
36:56We jump into it.
36:57If it's true,
36:58if not,
36:58we jump to compare the third one.
37:01If it's true,
37:02then we jump to its scope.
37:04If not,
37:05we jump to the else
37:06and the else always executes
37:08if nothing else above was true.
37:11So that's the basic idea here.
37:12Oh dear,
37:13I've probably lost my place.
37:16Looks like I just copy pasted
37:17less 10 true,
37:19which was this right here.
37:20So,
37:21um,
37:22less than true
37:23or less than 10 true.
37:25Okay.
37:25So we told the user
37:26your stuff is less than 10
37:27and then we jumped to the done area.
37:30So that means
37:31we are probably working on the else area.
37:33Okay.
37:33Let me grab that.
37:35So now finally,
37:36we're going to have the else area,
37:37which is going to,
37:38you know,
37:39finish this all up.
37:40That's going to be
37:43after the if done.
37:47So if nothing else matched,
37:49then we will end up
37:50jumping to the else area
37:51and then we'll basically
37:51just tell the user
37:52none of the conditions
37:53seemed to have applied.
37:55And then even at the end
37:57of the else block,
37:57even though you could probably
37:58get away with just letting
37:59execution fall through
38:00just to save yourself
38:01one instruction,
38:02you know,
38:02you could comment that out
38:03assuming you were sure
38:04that the very next instruction
38:06was the beginning
38:07was the beginning
38:07of the done area.
38:08But otherwise,
38:09I'm just going to play it safe
38:10and jump directly there.
38:13Then we have to make
38:14the actual done area.
38:17So just so you know,
38:19a label doesn't have
38:20to have any instructions.
38:23We could,
38:23we could have something like this.
38:24If else test say goodbye.
38:26We could have two labels
38:29right next to each other
38:30and one doesn't actually
38:31have instructions.
38:31That's totally fine.
38:33If you jumped to the done area,
38:35then execution would just
38:35fall through to the next
38:38valid instruction,
38:39which could go through
38:40another label.
38:41So, you know,
38:42we could have a say goodbye label
38:43and an if done label.
38:45I'm just going to have the goodbye
38:47stuff happening inside of the done.
38:49But for clarity sake,
38:50you might want to keep that label
38:52in there that I just deleted.
38:53And all that we need to do
38:54at the very end,
38:55when we're done with everything
38:57is just say goodbye.
38:57So I'm just printing a message.
39:00So just to do a quick recap here,
39:02let's see.
39:06We come in with the user's input
39:08as R12.
39:09We ask,
39:10does R12 equal five?
39:12If that's true,
39:13we'll say that it was equal to five
39:14and then we'll go to the done area,
39:16meaning we'll skip past
39:17all the other blocks
39:19for the if else block
39:20or all the other scopes.
39:22But if that did not equal five,
39:24then we'll fall through
39:25to that jump instruction
39:26which takes us to the next test,
39:28which is going to be our
39:29else if equals six.
39:32So we are checking now
39:33to see if R12 is equal to six.
39:35If that's true,
39:36we jump to this next block.
39:37Why do we need to jump to a block
39:38that's so close?
39:39Well, if we don't,
39:42then we're going to end up
39:43definitely jumping to the next comparison
39:45and, you know,
39:45that wouldn't work.
39:46So we're just kind of jumping over
39:48this unconditional jump statement
39:49and we're saying,
39:51all right,
39:51your input was definitely equal to six.
39:56And then we're jumping to the done area,
39:57just past everything.
39:59But if that wasn't true,
40:00then we hit this unconditional jump
40:02that takes us to the next comparison
40:04to see if the user's input
40:05was less than 10.
40:07And so, you know,
40:08if that's true,
40:08then we go to the true area
40:09to print another message
40:10and then go to the done area.
40:12If it was false,
40:13we fall through to the else jump,
40:15which will take us to here.
40:17And notice how the else scope
40:20doesn't actually make any comparisons
40:21because, you know,
40:23when you have like an if else,
40:24sorry,
40:25when you have an if else if else if,
40:27else if,
40:27you know,
40:28any number of else if blocks,
40:30the else will always be executed
40:33if nothing above it actually did execute.
40:36So if you have an else block,
40:37that means something will execute.
40:39So we're not doing any comparisons.
40:41We're just saying,
40:42you know,
40:42when that scope is done,
40:43we're just going to jump to the done area.
40:45So all of these different scopes,
40:47they're jumping to the done area
40:48when they finish.
40:49And the done area
40:50is just this label right here
40:51where we say goodbye.
40:52And that's it.
40:53Let's see.
40:54Did I copy paste everything
40:55that I was supposed to?
40:56I think so, probably.
40:58So let's run the program
40:59and see if it works.
41:01Okay,
41:02let's just enter the number three.
41:04All right,
41:04it at least compiled.
41:05So the basic if test,
41:08that was the first part
41:09of either this video
41:10or the previous video,
41:11depending on whether I chose
41:13to split this up.
41:13So let me comment out those calls
41:15real fast.
41:18So I'm going to just comment out
41:20these calls real quick
41:21so we can only deal with
41:22the complicated if else block.
41:26So I'm going to enter like a three.
41:28Whoops.
41:29Let me do it again.
41:30Three.
41:30And so begin the if else test.
41:32Your input was definitely
41:34less than 10.
41:35And then we end the if else test.
41:38So notice how only one scope executed.
41:41Three was definitely less than 10.
41:42What other numbers did we have?
41:44Let me write them down somewhere
41:45so I don't forget.
41:45Oh,
41:46they're written down right here.
41:48So we entered a three,
41:49which was definitely not a five
41:50and not a six.
41:51So that's why the less than 10 block executed.
41:54Maybe I should pin this.
41:57Right.
41:57So now if I enter a five,
42:01only the first scope should execute.
42:04So it says your input was equal to five.
42:06Notice how it didn't mention
42:07that it was a six or less than 10.
42:09And then if we do a six,
42:11it should just tell us that we have a six.
42:13It will not mention the five.
42:15It will not mention the 10.
42:16So we did we did five already
42:19and then we did six
42:20and we did like a two,
42:22which was less than 10.
42:23We could also do a one,
42:24which was less than 10.
42:25We can do anything that was less than 10.
42:28And then the else would be
42:29if our input is probably like 11 or greater,
42:32meaning it's not less than 10
42:34and it's also not five or six.
42:36Just we can see the else block.
42:38So I'm going to do 11,
42:40which is the first number
42:41that should trigger the else block.
42:42And it says no conditions were satisfied,
42:46which was the message inside of our else block.
42:49Let me just show you that again real fast.
42:53This was the else block.
42:56So message if else else
42:58or labeling on my port.
43:01I realize that.
43:02But it basically says
43:03no conditions were satisfied.
43:06The if else else.
43:10I think that's basically everything
43:12that I wanted to show you.
43:13At this point,
43:14you should feel
43:15like you're starting to become an expert
43:17at complicated
43:18if else if else blocks
43:21converting those
43:22from a higher level language
43:23into assembly.
43:25And I honestly recommend
43:27that you practice this like crazy
43:29while you're trying to get more used to it.
43:31But, you know,
43:32hopefully you have everything
43:32you need at this point.
43:34Keep in mind,
43:35I've said this at the beginning.
43:36I've actually,
43:36I think I said this in a different video.
43:38When we use these branching instructions,
43:41notice how I have
43:42jump equal to,
43:43where's that less than?
43:46Yeah, notice how I have
43:47an instruction jump less than.
43:50The family of conditional branching instructions
43:53that compares less than,
43:55greater than,
43:56less than or equal to,
43:57greater than or equal to,
43:58the ones that I'm using here
44:00apply to signed integers
44:02and they won't necessarily work
44:04with unsigned integers
44:05and they won't work with floats
44:06if you compare floats later.
44:09So just keep that in mind.
44:10Let me pull up my favorite book again real fast.
44:14Where,
44:14what the heck is that book?
44:15Oh, dude.
44:17Where's my document viewer?
44:20Document viewer.
44:21Can I get,
44:22there,
44:22there we go.
44:24So I guess I didn't introduce this book
44:26at the beginning of this video,
44:27but I probably should have.
44:28This is my favorite assembly book.
44:29You can turn yourself into an expert
44:31with this book by,
44:32just on its own.
44:34It's open source and free.
44:36The author gives this away.
44:37He's a genius.
44:38The person who wrote this book
44:40is Dr. Ed Jorgensen.
44:42He's a professor.
44:44He wrote this for his own classes.
44:45And again,
44:46it's like a free open source book.
44:47You can just download a copy
44:49from his website
44:50if you look this up
44:51and convert yourself into an expert.
44:54So let me collapse everything here
44:57and then I'll go to
44:57instruction set overview,
44:59control instructions,
45:00conditional control instructions.
45:02And I just want you to see real fast,
45:04just as a recap,
45:05I talked about this in a previous video,
45:07but when it comes to comparing things
45:11and checking if they're equal or not equal,
45:14it's the same for signed or unsigned operands
45:19because if you want to see
45:21if two operands are equal,
45:22you just check to see
45:22if all of their bits are equal.
45:24You don't even really care
45:25whether they're both integers or not.
45:26On the other hand,
45:29notice how there is like a family here,
45:32jump less than,
45:33jump less than equal to,
45:34jump greater than,
45:35jump greater than equal to.
45:37Those instructions apply
45:38only if your operands are signed integers.
45:42They have a plus or minus sign.
45:43If they are unsigned
45:45or if you're using floats
45:46for your comparisons,
45:47you have to use this other family
45:49of conditional branching instructions
45:51called jump below,
45:54jump below equal,
45:55jump above,
45:56jump above equal.
45:56And I'm sure you can infer
45:57that jump below
45:58is the same thing
45:59as jump less than, right?
46:01Jump less than or equal to
46:03is the same thing
46:04as jump below or equal to.
46:05It's just that
46:06you need to use
46:06a different instruction
46:07if you're using signed integers
46:10or floats versus,
46:12sorry, sorry.
46:13You need to use
46:14different instructions
46:15if you're using signed integers
46:17versus unsigned integers
46:19or floats.
46:21So keep that in mind.
46:23The code demo
46:24that I just showed you,
46:24it's for signed integers.
46:25If you were going to use
46:27unsigned integers
46:28or floats,
46:28this program wouldn't work for you.
46:30You'd need to replace
46:32my branching instructions
46:33with the unsigned versions,
46:36which, I mean,
46:36they work the same thing.
46:38You're still going to do
46:38a comparison instruction,
46:39so it still works.
46:42Anyway,
46:43so after having shown you that
46:45and making sure
46:48that everything actually did work,
46:49I think we've talked about
46:50everything that we need to
46:51for this video.
46:53So, yeah.
46:54I hope you feel like an expert.
46:55Practice this on your own,
46:56you know,
46:57write your own programs
46:58just to make sure
46:58you know how to convert
46:59a higher level language block
47:01into an assembly block
47:02and you should be on your way.
47:04Thank you for watching this video.
47:07I hope you learned
47:08a little bit of stuff
47:08and had a little bit of fun.
47:09Tell your friends
47:10and I'll see you in the next video.
47:12Hey, everybody.
47:17Thanks for watching this video again
47:19from the bottom of my heart.
47:20I really appreciate it.
47:21I do hope you did learn something
47:23and have some fun.
47:24If you could do me a please,
47:26a small little favor,
47:27could you please subscribe
47:28and follow this channel
47:30or these videos
47:31or whatever it is you do
47:32on the current social media website
47:34that you're looking at right now?
47:36It would really mean the world to me
47:37and it'll help make more videos
47:39and grow this community
47:40so we'll be able to do more videos,
47:42longer videos,
47:43better videos
47:43or just I'll be able
47:45to keep making videos in general.
47:46So please do me a kindness
47:49and subscribe.
47:50You know, sometimes
47:51I'm sleeping in the middle of the night
47:53and I just wake up
47:54because I know somebody
47:55subscribed or followed.
47:56It just wakes me up
47:57and I get filled with joy.
47:58That's exactly what happens
47:59every single time.
48:00So you could do it
48:01as a nice favor to me
48:02or you could troll me
48:03if you want to just wake me up
48:04in the middle of the night
48:05just subscribe
48:05and then I'll just wake up.
48:07I promise that's what will happen.
48:09Also, if you look at the middle
48:12of the screen right now
48:12you should see a QR code
48:14which you can scan
48:15in order to go to the website
48:16which I think is also named
48:17somewhere at the bottom
48:18of this video
48:19and it'll take you to
48:20my main website
48:21where you can just kind of like
48:22see all the videos I published
48:24and the services
48:25and tutorials and things
48:26that I offer
48:26and all that good stuff
48:28and if you have a suggestion
48:32for clarifications
48:35or errata
48:35or just future videos
48:37that you want to see
48:37please leave a comment
48:38or if you just want to say
48:39hey what's up
48:40what's going on
48:41you know
48:42just send me a comment
48:42whatever
48:43I also wake up for those
48:44in the middle of the night
48:45I wake up in a cold sweat
48:46and I'm like
48:47it would really
48:49mean the world to me
48:50I would really appreciate it
48:52so
48:52again
48:53thank you so much
48:54for watching this video
48:54and
48:55enjoy the cool music
48:58as I fade into the darkness
49:00which is coming for us all
49:02next time
49:04and I'll see you next time
49:05and I'll see you next time

Recommended