In this beginner-to-intermediate assembly language tutorial, we dive deep into unconditional jump instructions (JMP) in x86-64 assembly using Yasm syntax.

We cover:
- What unconditional jumps really are (basically a "go to" for assembly)
- How labels work and how to create them
- Why JMP has unlimited range (unlike conditional jumps)
- Practical demo showing how to skip code sections using jumps
- Comparison between jumping over code vs letting it execute
- Quick look at why this matters before learning conditional branching

We also reference the excellent free open-source textbook by Professor Ed Jorgensen (May 2024 version) which is highly recommended for anyone serious about learning x86-64 assembly.

Whether you're preparing for university courses, reverse engineering, operating systems development, or just love low-level programming, this video will give you a clear understanding of how unconditional control flow works in modern x86-64 assembly.

Next video will cover conditional jumps (je, jne, jg, jl, etc.) and their limitations.

Enjoy the video and happy coding at the machine level!

Introduction to Jump Instructions 00:00:00
Recommended Free Assembly Textbook 00:00:23
What Unconditional Jumps Actually Do 00:01:27
Labels Explained with Examples 00:02:40
Unlimited Jump Range Advantage 00:04:43
Overview of the Demonstration Program 00:06:56
Building and Running the Jump Test 00:09:21
Live Jump Test Demonstration 00:10:53
Effect of Removing the Jump Instruction 00:13:50
Jumping in Different Directions Example 00:14:58
Summary and Next Video Teaser 00:17:28
Closing Remarks and Call to Action 00:17:44

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

#AssemblyLanguage #x86_64 #LowLevelProgramming #Yasm #ComputerScience
Transcript
00:00Hello there. In this video,
00:02we're going to be talking about jump instructions in assembly.
00:06This video is going to be about x8664 Yasm assembly,
00:09but I think probably anyone who's interested in jump instructions will benefit
00:12from this video because the concept is usually the same throughout any system
00:16that you might use.
00:23So for starters, I want to direct your attention to a textbook that I think is
00:27wonderful.
00:27This is an open source free textbook that will help you become an expert in
00:32assembly. It's not mine. I didn't write it.
00:34The author is professor Ed Jorgensen, PhD.
00:38He releases this textbook for free. It's under a copyleft license.
00:43So you can literally just go to his website and download it and send it to your
00:47friends and everything. And it's, it's wonderful. This book will,
00:50will take you from, from zero to hero when it comes to Yasm assembly.
00:54It's wonderful and amazing. So this is the book,
00:57and I just want to show you the section on jump instructions real fast.
01:02And then I'll talk about them a little bit and then I'll show you a sample
01:05program that uses jump instructions.
01:07So this version of the book that I'm working with right now is, uh, May 2024 version 1.1.56.
01:13Um, I'm going to go down to, let's see, section seven instruction set overview inside of there.
01:20There's another subsection called where the heck is it control instructions, 7.7.
01:25Uh, within that, there's another subsection called 7.7.2. That's why I love this other,
01:32not one of the many reasons that I love this book. It has so many subsections.
01:36There's just so many yummy subsections to organize everything in a way that you can find it all so fast.
01:42Um, okay. So we're looking at, uh, unconditional control instructions. In other words, a jump instruction.
01:49If you're an old school programmer, especially if you use some sort of like a basic language or a language with go-to's,
01:54you might recognize jumps as just being a go-to meaning we're not actually going to call a function and then return from it,
02:01which is what the modern programs tend to do. We're just going to say, let's write a jump instruction and we will literally just change execution to,
02:12to jump to some other location, just kind of go there forever. Maybe we come back, but if we do, it's going to be because there was a different jump instruction that told us to jump back.
02:21So we're not calling and returning. We're just going somewhere and that's it. Um, uh, obviously it's a little bit more convenient to be able to call functions,
02:31but that's sort of like an abstraction that has to be implemented after we understand how to jump.
02:37So anyway, the jump instruction is pretty simple. You just put JMP in YASM anyway, and then follow it with a label.
02:45So, you know, just as a quick little recap here, what's a label? Imagine we have an assembly program here and maybe here's our, uh, text section and we put some instructions.
02:56Maybe there's like a, an entry point, uh, right here. I'll say a global, uh, entry point and literally just taking the word entry point and putting a colon after it now makes that a label.
03:09So if there are any instructions underneath, I'm going to put a bunch of nopes. Then if someone somewhere else in the program decides to say jump entry point,
03:18they should be able to go right here to instruction eight and then start executing downward.
03:22Uh, I guess maybe I didn't need to put the global keyword. Uh, global just means let's make this label available to other modules within the same program.
03:30So if you have a multi-source program or a hybrid program with multiple different languages, then, you know, you should do this.
03:37Uh, but if it's just a pure assembly program and there's only one source code filed, you don't need to mark a label as a global.
03:42You could just make a label, uh, just as a quick example here, entry points. Uh, I'll just put hello as a label and I'll say like, do exit stuff.
03:53So imagine online 16, you add some instructions just to kind of exit. Uh, if I wanted to skip all these nope instructions for some reason, I could just do this.
04:01I could say jump hello. And what would happen is execution. Oh, I can use my pen execution would just sort of, uh, you know,
04:12it'd come into the tech section. It would go down through the label and it would execute this first jump instruction.
04:18And then execution would jump over the nopes into the hello label. And then, you know, if there was other stuff here,
04:24then it would get executed. So by jumping over, um, the nopes, I'm essentially saying that the nopes
04:31should not actually end up being executed. They'll be there in the program, but they won't actually execute.
04:36So that's the basics of a jump instruction. Okay. So what else do I need to tell you real fast?
04:42Uh, Oh, one thing that's really good about jump instructions is they have unlimited jump range.
04:48So you can jump from a place at the very, very beginning of your assembly program and,
04:55and jump to a place that is at the very, very, very end of your assembly program.
04:59There's not going to be a limitation on how far you can jump. I mean, in theory, there's a limit,
05:03but practically speaking, there's not a limit. Um, why would you care that there's not a limit? Well,
05:08because in a future video that I'm going to release, we're going to talk about conditional
05:12branching, which is sort of a jump that only jumps if a certain condition is true. And those have
05:17limited ranges where they can jump. Um, so there's going to be a bunch of different instructions,
05:23but one of the conditional branching instructions is J N E. Uh, and another one is J G E. And there's
05:29another one that's J E basically, you know, jump. If something is equal, jump. If something is not equal,
05:33you know, whatever, those types of instructions can only jump about 128 bytes away. So after your
05:39assembler, you know, assembles and compiles, uh, down to object code, and then after your linker
05:44links, your final, final executable, um, wherever it is that the instructions, uh, happen to end up
05:50inside of your program, uh, the conditional jumps, the conditional branching instructions,
05:55they can't jump more than 128 bytes away to some other instruction. So keep that in mind. Um, even if
06:02later on you graduate to making decisions in your program, like I'm going to do in the next video,
06:06you can only jump so far. And if you have to jump too far, you actually might not be able to jump at
06:11all unless you jump a very short jump to a regular jump instruction. And then that jump instruction
06:18jumps very, very far away. That's kind of the workaround for it. I'm not going to talk about
06:22that in this video though. This is not a video for a conditional branching. I just wanted you to be
06:27aware of one of the benefits of regular jump instructions. Okay. So, um, we're looking at the book here.
06:36There's not really a whole lot to the jump instruction, just jump and then a label. Uh,
06:40we talked about its benefit over conditional, uh, branch instructions, but we also talked about its, uh,
06:46I guess it's a shortcoming, meaning it can't actually make a decision. It will always jump to a label
06:51no matter what, there's no condition. So there's the book there. And now I'm going to make a sample
06:56program and, uh, show you how to run it. I'm just going to run it. I'm going to show you what it does,
07:03uh, in order to implement, uh, conditional, uh, branches. So for starters, I want you to know that
07:08there's a make file that I've generated under the hood. And, uh, we're not going to be talking about
07:13that in this video. This is also a hybrid program. So there's a C plus plus, uh, entry point, a driver module,
07:19under the hood of this. We're not going to talk about that. If you want to know how to
07:22make hybrid programs, you want to generate, make files, you want to learn the basics of assembly
07:26and so forth. You should see my other videos for now. We're only going to be talking about
07:30jump instructions. So I'm going to skip a lot of information. Okay. So for starters,
07:35I'm going to make a little data section here. And again, this is explained in other videos,
07:41but for now we'll just trust that we can make a data section that contains strings, C strings and other
07:47uh, values. So pretty much I'm just going to, uh, make a string called begin jump test just to
07:54announce to the user that we're, we're going to start doing this. And then, uh, I'm going to make
07:58a string called this message should not appear. So in the code, I'm going to try to print that message,
08:04but then I'm going to jump over the call to print it just to prove to you that there are instructions
08:09that would print that message, but we're jumping over them with the jump instruction.
08:13And then there's like an exit message. And then there's a CRLF, which is just a carriage return
08:18line feed. Again, all of this stuff is in other videos already. Uh, we're going to print to, uh,
08:24we're going to use system call one to print. We're going to print a file descriptor one,
08:28which is just standard output for a new program. Then we're going to start the text section where the
08:34actual code lives. So, uh, this text section is here and, um, it's supposed to be at line 37 already.
08:44I think I missed a bunch of lines. Oh no, I think I missed some comments anyway. So we have a text
08:49section here and an entry point and I'm calling it cool. And I am marking it as global because in
08:55this particular program that I'm building, it's a hybrid program. There's going to be a C++ module
08:59that will call on our cool function. So cool has to be global. And then I'm just going to call on
09:04a method called jump test. I don't know why I have the words load there. I'm just going to get rid of
09:08that real fast, uh, locally and in my solution up above. And, um, so we're going to call a function
09:14called jump test. And then when we're finished, we're going to return to the caller, which is going
09:18to be the driver. And that'll pretty much be it. So if I comment this out real fast, let's see,
09:24this might actually work. Let's see if I can get it to run in the terminal,
09:27but there's a bunch more code that we have to add. So I'm not really sure.
09:31So let's do clear and make run. And, uh, it seems to not have a shared object directory.
09:41Let me pause the video while I copy paste one of my stupid libraries into the program.
09:45You don't need this library. It just helps me print things.
09:51Okay. So now I have copy pasted my shared object, which allows me to do extra printing stuffs
09:56just for, just to make this demo easier for me, but you don't need to know it or you don't need
10:01to have it to, uh, to learn jump instructions anyway. So I'm going to do that again.
10:06And now it actually prints something. Okay. So hello from the main CPP driver.
10:10And then it says the driver has regained control. So nothing really happened. Okay, cool.
10:13So now let's, uh, make a call to jump test here and then, uh, let's start the actual jump test, uh,
10:23function. So I'm going to do, well, I guess this thing is kind of short. I could copy paste the
10:29whole thing all at once. So let's do, uh, yeah, let's just jump. Let's just call the whole thing.
10:36Okay. I'm going to copy paste the whole thing. Then I'll explain it a little bit to you.
10:39So, uh, there is a function that I have in here called CRLF. Just, just ignore it. It's just a
10:44convenience function that I made so I can print a carriage return line feed. Uh, the real, uh,
10:51interesting thing here is the jump test function. So we were just making a call to jump test.
10:55Now we're making the actual jump test function. It's got a signature of just void with no arguments.
11:00So it's not super interesting from the caller's perspective, but it does some stuff. So for starters,
11:05it, uh, has an intro message. So this will print, you know, hello, welcome to the, uh,
11:11welcome to the jump test. In fact, if I do a return call here, it should actually just
11:15print that and do nothing else. Right. Okay. Uh, notice how it printed, uh, begin the jump test.
11:22And then, uh, right after that, there's a jump instruction, just proving to you that we can jump
11:27over other instructions. So look at this, this piece of code should never, uh, actually be called
11:33because we're going to jump over it. What it is, is it's printing that jump shouldn't happen message.
11:39So if you look back up at the top here, jumps shouldn't happen. So it's trying to print out
11:44this message should not appear, but we're going to jump over that, uh, by using this jump instruction
11:49here on line 66. Again, note that, uh, the jump instruction is just JMP followed by a label.
11:56The label specified has to be where you want to jump. It's never going to return from that place unless you
12:01specifically jump back somehow, uh, later on. Like, I guess if we wanted to, we could put a label on
12:07line 67, call it the return point and then jump back from it after the jump point. In fact, maybe that
12:13would be kind of interesting to do at the end of this video, but otherwise we're going to, you know,
12:17just let's see, we're going to end up jumping over. So let me reduce the font size just for a second here.
12:25So imagine execution, uh, comes into this program, you know, we're executing, uh, instructions.
12:31We're calling CRLF. We're just executing, executing. As soon as we hit this jump instruction,
12:36then execution jumps over into the label that I specified. So this whole, uh, this whole code
12:45section here just never even gets called. So that's why we will not see that message.
12:50And then, uh, at the very end, all I'm doing is I'm just properly, uh, you know, I'm printing the
12:57exit message. So I'm just printing another string saying, uh, the exit or the jump test is done.
13:03And then when I returned to the caller execution goes all the way back up to just, you know, right
13:09here, right after call jump test was executed. And then the cool function will return to the caller.
13:14And that's just a C++, uh, main function that does nothing. So at this point we should see the
13:20whole entire point of the program. And then I'll start tweaking it. So you can kind of see the
13:24difference with the jump instruction, uh, there and not there. So let's run one more time and notice how
13:31it says, begin the jump test and then end jump test. And then it goes back to the driver
13:38that is regain control. It never says this message should not be printed. So this whole, uh, section
13:44was just skipped. Let's comment out line 66 so that we don't actually jump over that code. And then now
13:50you'll see that that message does get printed. So notice how it says this message should not appear.
13:57Okay. So if I uncomment it again and then run the program one more time. Now that message does not
14:03appear pretty cool. Um, now let's, let's do that double jumping thing just to show you. I mean,
14:08this is not something that you actually want to do. You probably want to write functions and function
14:13calls, but, uh, if you wanted to, you could do something like this. Here's the exiting and maybe,
14:21maybe right after this, let's make it, let's make another label. Let's do, oh gosh,
14:28what am I going to do? Cause if I jump after the exiting label and I jumped back up to some label
14:33up here, it's just going to be an infinite loop. So maybe, um, I don't know, let's make a,
14:41I mean, if I make another label down at the bottom, you'll kind of think it's a function
14:45just without a return statement. So let's actually jump within the same function. Let's do, um,
14:50um, let's do a label that starts right after we jump over the, uh, never area. So I'm going to say jump
14:59test and I'm going to write never. So now we have a label that tells us where the, uh, the never, uh,
15:06printed message actually starts. So if we jump over it to the exiting, then we're good.
15:11But then if I, up here, if I say jump, uh, instruction that subverts the, uh, skipping
15:23of the never message. So I'm just, I'm just leaving a comment, not code. I could then say,
15:29let's jump to jump test never. And what will happen now is, uh, we'll still see the never message
15:38because what will happen is execution comes down, you know, through here, all these instructions
15:43are executing. And then we see a jump that tells us to go to the, uh, the never label. So we actually
15:51jump over this exiting jump or this, like, uh, the skipping jump, the jump that skips the message.
15:58And then we actually do print the never message and we just keep going down and down and down
16:02until we're finished, um, with that. And we end up just sort of exiting normally. So that means
16:11the only code that doesn't get executed in this case is the, is the one right here that skips over
16:18the never message. Hopefully that makes sense. Let's run the program just to prove it real fast.
16:25So I'm going to do this again.
16:26And now you see the message should not appear. This message should not appear. You see that message.
16:34So again, if we comment out that jump, that subverts the skip, then execution will fall through
16:40and we'll end up executing, uh, line 69, the, uh, sub the skipping instruction. And if we run it again,
16:48now that message does not appear and we could jump back and forth if we wanted to, I don't know,
16:53should I do a back and forth and I don't really want to, I think at this point you understand
17:04we can jump anywhere we want, right? Like I could take a bunch of time in this video to rewrite this
17:09program and have it say, let's jump downward and then let's jump upward again and let's let it fall
17:15through. And then let's jump over something and whatever. I mean, just wherever you want to jump,
17:18just make a label and then jump to it. Then you have to figure out what your execution path
17:23is actually going to be. And, uh, maybe that'll be complicated, but, uh, I hope I've made my point
17:28by now. Anyway, so that's the basics of just jumping around. It's not super useful. Conditional
17:35branching is a lot better. So see my next video. And, uh, I, I thank you for watching this and I hope
17:40you learned a little bit and had a little fun. See you soon. Hey everybody. Thanks for watching this
17:48video again from the bottom of my heart. I really appreciate it. I do hope you did learn something
17:53and have some fun. Uh, if you could do me a please, a small little favor, could you please
17:58subscribe and follow this channel or these videos or whatever it is you do on the current social
18:03media website that you're looking at right now? Um, it would really mean the world to me and it'll
18:07help make more videos and grow this community. So we'll be able to do more videos, longer videos,
18:13better videos, or just I'll be able to keep making videos in general. So please do, do me a kindness
18:19and, uh, and subscribe. You know, sometimes I'm sleeping in the middle of the night and I just
18:23wake up because I know somebody subscribed or followed. It just wakes me up and I get filled
18:27with joy. That's exactly what happens every single time. So you could do it as a nice favor to me or you
18:32could, you could troll me if you want to just wake me up in the middle of the night, just subscribe
18:36and then I'll, I'll just wake up. I promise that's what will happen. Also, uh, if you look at the middle of the
18:42screen right now, you should see a QR code, which you can scan in order to go to the website, which
18:46I think is also named somewhere at the bottom of this video. And it'll take you to my main website
18:51where you can just kind of like see all the videos I published and the services and tutorials and things
18:56that I offer and all that good stuff. And, uh, if you have a suggestion for, uh, uh, clarifications or
19:05errata or just future videos that you want to see, please leave a comment. Or if you just want to say,
19:09Hey, what's up? What's going on? You know, just send me a comment, whatever. I also wake up for
19:14those in the middle of the night. I get, I wake up in a cold sweat and I'm like, it would really,
19:19it really mean the world to me. I would really appreciate it. So again, thank you so much for
19:24watching this video and, um, enjoy the cool music as, as I fade into the darkness, which is coming for us all.
19:39It's insane.
19:41It's insane.
19:43It's insane.
19:45It's insane.
19:48It's not even so,
19:49All right.
19:51There it is.
19:53It's insane.
19:55It's insane.
19:57We have a dream.
19:59It's not even so,
20:01this, and, uh,
20:03where we can find it.

Recommended