Want to master command line arguments in Yasm assembly? This video breaks down how to access and process args in x86-64 hybrid programs linked with GCC. From understanding argv and argc to looping through arguments, we cover it all with practical examples. Perfect for programmers diving into assembly or looking to level up their low-level coding skills. Check out my other videos for more on Yasm and pure assembly! Subscribe for more coding tutorials. #AssemblyProgramming #YasmAssembly #GCC #x86_64
Introduction 00:00:00
Command Line Arguments Overview 00:00:03
Yasm Assembly and GCC Linking 00:00:07
Hybrid Program Explanation 00:01:27
Makefile Overview 00:02:16
Assembly Program Structure 00:03:16
Main Entry Point and Registers 00:03:51
Accessing Command Line Arguments 00:04:26
Argument Count and Pointers 00:05:03
Loop Initialization for Arguments 00:08:57
Loop Logic and Dereferencing 00:10:52
Printing Arguments 00:13:02
Incrementing Pointers in Loop 00:14:08
Running the Program 00:15:35
Practical Application of Arguments 00:17:24
Conclusion and Next Steps 00:17:51
Call to Subscribe and Outro 00:18:22
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
Introduction 00:00:00
Command Line Arguments Overview 00:00:03
Yasm Assembly and GCC Linking 00:00:07
Hybrid Program Explanation 00:01:27
Makefile Overview 00:02:16
Assembly Program Structure 00:03:16
Main Entry Point and Registers 00:03:51
Accessing Command Line Arguments 00:04:26
Argument Count and Pointers 00:05:03
Loop Initialization for Arguments 00:08:57
Loop Logic and Dereferencing 00:10:52
Printing Arguments 00:13:02
Incrementing Pointers in Loop 00:14:08
Running the Program 00:15:35
Practical Application of Arguments 00:17:24
Conclusion and Next Steps 00:17:51
Call to Subscribe and Outro 00:18:22
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
Category
🤖
TechTranscript
00:00hello there in this video i'm going to talk to you about how to accept incoming command line
00:06arguments to an x8664 yasm assembly program that is probably linking to gcc and is thus
00:13probably a hybrid program under the hood if you don't know how to program in yasm assembly yet
00:24check out my other videos if you don't understand command line arguments yet check my other videos
00:29i'm going to assume a lot of knowledge here i'm really just going to show you how to pull the
00:33arguments inside of assembly uh using using the gcc setup that they've given you so just a quick just
00:42a very quick recap this is not a command line arguments video if we have the program echo
00:47and we give it like one argument of just the word hello then it's it's going to receive one command
00:51line argument um in addition to its program name it's going to receive echo as argument zero and
00:57hello as argument index one and it's just going to be able to look at them and say oh the user wants
01:03me to print this hello string so it just prints it and so that's kind of how you can tell a program
01:08what to do either when you're running them directly or having one program automate another program
01:13anyway so in our yasm assembly programs how do we actually get that out well for starters
01:19we are dynamically linking uh sorry not dynamically linking we are linking a hybrid program so i just
01:27want to make sure that you understand this is the right video for you if you're trying to do this in
01:31pure assembly or if you need to know that for some reason then uh you'll probably want to watch the
01:36next video that i publish but um for now this is a hybrid program hybrid programs for those of you
01:42who don't know it just means you have modules that are written in different source code and they're
01:48compiled differently different source code languages so you might have a source code file that's written
01:55in c plus plus another one that's written in c another one that's written in assembly and you
01:59compile them all down to their own object files and then you link them together into an executable
02:05if gcc is part of the linking process then it's going to end up giving you a main function as your
02:09entry point and it's going to make things a little bit easier here's my make file i'm just going to
02:14skim through it real fast this is not a make file video so if you don't understand it go look at my
02:18other videos but basically i'm just going to assemble with yasm and i'm going to use g plus plus as the
02:25linker and i'm going to gobble up all of the object files here's like a little menu i have a target for
02:31running and building only and um here's the main thing that i'm doing this is not really a hybrid program
02:38this is just kind of a pure assembly program that is linked with gcc but you can imagine you could
02:43add other source files in c and c plus plus and still totally get away with gcc but anyway i'm just
02:51compiling one source code of assembly and and compiling it down to an object file right here
02:57and then when it comes time to linking you can kind of tell here especially if you're familiar with
03:03make files that i'm just gobbling up all the object files and linking them together into the executable
03:08so that's as far as i'll go there again i've got another video that totally explains a lot more
03:13in my assembly program again this is not an assembly video just for pure assembly so if you don't
03:18understand assembly see my other videos but for now we're just going to say i've got a data section
03:23it's got a couple of null terminated strings we've got a hello message a begin message a goodbye message
03:29carriage return line feed string i've got the system call codes for you know printing to standard output and
03:37to exit the program and then i've got the standard output file handle and then my exit code zero for
03:42success again this is all explained in other videos so because i'm linking with gcc my text section here
03:50the actual place where the instructions are not the data is going to have a main entry point so of course
03:57if you had a hybrid program you could have main somewhere else in a different module and then just
04:00call on a function that was inside of assembly you could totally do that but for now i'm just going
04:05to say this is the only source code file so i mark main as global and i put main right here and then
04:10i just make sure that i preserve all of the collie saved registers that i'm supposed to by pushing them
04:18at the beginning and popping them in reverse order at the end and then i return a return value standard
04:23assembly stuff covered in other videos but here's the key for getting command line arguments in assembly
04:29if you recall the abi specifies that in x86 64 programs the first argument is always supposed to
04:37come into a function as rdi in the rdi register or rather i should say the first integer argument
04:44i haven't talked about uh float arguments in any of my videos yet in the future you should probably
04:49search for other videos that uh that explain how to do float arguments but for now all of the arguments
04:55i'm talking about in this video are just integer arguments and pointer arguments which is kind of
05:02the same thing a pointer is an integer it's a 64-bit integer but we'll just use it as a pointer rather
05:07than using it as an integer so anyway first argument comes in on rdi second argument comes in on rsi and
05:14you can imagine that basically this is the thing you're probably used to seeing in your c++ programs if we
05:21do this int main and then integer argc and then a character pointer argv and then an array those are
05:32those two registers right there i probably want to change this into long because you know rax sorry rdi
05:40is in 64-bit form but an integer is an unsigned 32-bit int i don't like that so i'm just going to put long
05:46i probably should put unsigned but i don't really care anyway so we're just going to grab those two
05:51things so obviously rdi is probably pretty easy for you to guess how to use it's just the number of
05:56arguments in this program we're going to use it to control a loop counter that's going to loop through
06:01all incoming arguments so we'll start at zero which is going to be the name of the program in
06:06this case it'll be i think just main is what i called it which is confusing against the entry point
06:11i admit it but imagine that my main program that sits in the file system is actually called programmer
06:17hello or whatever so that's the number of arguments in rdi and then rsi notice how it's a character
06:25pointer to an array of characters that means it's actually a pointer to a pointer and the reason for
06:30that being is that every argument on the command line like for instance if i went back here and i said
06:35echo hello let's just do without quotes hello you i've given it two arguments and so when the
06:41program launches it's going to receive three total it's going to receive its own program name at index
06:46zero echo it's going to receive at index one the first argument hello and it's going to receive at
06:52index two the second argument as you it prints them both so how does echo know how many arguments i have
06:58and how does it access all the strings remember rxc is the number of arguments so that's kind of easy but
07:04how does it dereference all of the strings if there could be any number of strings
07:08well that's this right here the uh this whole symbol argv
07:17argv is a pointer and it points to an array of pointers of character pointers that's why it's
07:25written like this like an array so it's a pointer that points to an array so like if you go to that
07:30location in memory then what you will see there for the first eight bytes is just the address of some
07:36other memory location where a string has been stored then if you advance that pointer another
07:41eight bytes forward you know in memory because all pointers are eight bytes or 64 bits then again
07:47you'll see that that value of that next eight bytes is actually a memory location that points to another
07:52string somewhere else so these strings could be all over the place but the pointers to those strings
07:57are contiguous in memory starting with uh what you were given in rsi so it's a character pointer pointer
08:05or an array of character pointers however you want to imagine that anyway so we'll grab those and then
08:12i'll just show you how to use them down further in the code for starters i'm calling on an intro
08:18function which just basically prints some stuff i have a custom function that i wrote called print
08:22null terminated string that's not the point of this video i have that explained in other videos a
08:27little bit so it's up to you if you want to even care right now i just want to be able to print
08:32something and then let's see so after we print our welcome message in the intro then we print another
08:40message basically saying okay now we're about to start printing all the arguments okay cool
08:44nothing complicated at this point then i have another label here with my preferred style of
08:50adding a suffix after the function that i'm currently inside of and i'm going to say all right this is where
08:55i initialize my loop this is not a looping video so i'm going to skim through it kind of
09:00but i'm basically going to say let's start the loop as basically saying the index of the argument
09:06that we're currently looking at is zero because we'll start with zero so that means we'll print
09:11like if we were going to if our program was named echo we would also print echo itself and
09:15not just all the incoming arguments that the user might have typed so we're going to start at index zero
09:20and then we're going to use r15 as the current character pointer so r15 its current character
09:29pointer is going to be coming from r13 which was the argv argument so that means
09:37r15 is now going to hold a pointer uh let's see it's going to hold well it's going to be pointing
09:46to a pointer remember when you have an array and the array is contiguous in memory then
09:53the pointer itself that points to the array is also a pointer to the first item that's just kind
09:58of the way it works like if we have 10 items and it's an array then your pointer to that array is also
10:04a pointer to the first item in the array if that makes sense so if r13 was a pointer to an array
10:10it's also a pointer to the first pointer which itself will then uh go off to a null terminated
10:17string i know that sounds weird you got to get used to double dereferencing here so all we really
10:22need to do to access uh the first pointer to the actual string is take the array pointer that we have
10:31and dereference it one time so remember we're receiving an argv a pointer to a pointer or like
10:37a you know double pointer if we dereference it once then we should actually just have
10:42one pointer to one null terminated string anyway so that's the initialization part i'm going to start
10:47looking at the first string here and i'm going to say we're at index zero and then here's the top
10:52of my loop you can imagine this is a while loop this is not a while video but you know there it is
10:57and we're just going to ask first are we actually done do we need to break the loop i like to do that
11:03at the top of my while loop and how we'll do that is we'll say all right r14 is the index that we're
11:07currently looking at we'll compare that to r12 which was the number of arguments so notice how like
11:15right here we grabbed rdi right into r12 right away so basically i'm saying uh if the index we're
11:23currently looking at is greater than or equal to the number of arguments then we're actually already
11:28done remember if we're using zero based indexing and and you suppose that you have three items in
11:34your array their indexes would be zero and one and two not one and two and three so that means if the
11:41size of your array is three like the count like the argc that means the last valid index is two it's one
11:47less than the size you know it's one less than the count so i'm saying at this point that if we ever
11:52reach an index number that is equal to the size then we've already finished we do not need to look
11:59at the current item so i'm comparing those two registers you know where are we looking at versus
12:03what is the count you know what is the index we're looking at versus the count if the index we're looking
12:09at is greater than or equal to the count then that means we're done i'm going to jump out of the loop
12:13to a label called main loop done so then you know that's down here basically we just say goodbye
12:20and then we jump to our exit function and all that does is it just returns from main with some kind
12:27of a success code you know so just you can imagine in c plus plus it's just return zero
12:31so you know breaking the loop doesn't really do anything except end the program and say goodbye
12:37however if we did not jump then the let's see if we did not jump let's let's say if we did jump we
12:43compared it we realized that we're done then it's going to jump down to the to the done but if we're not
12:48done execution is going to fall through to the next statement so that means we're going to end
12:53up doing something with the current item inside of our loop so what are we going to do we're going
13:00to dereference r15 if you recall remember i said r15 was your pointer to a pointer it was your double
13:06pointer there if we dereference it once then instead of being a pointer to an array of pointers it's going
13:12to be a pointer to one string one one character that starts one string so dereferencing it once means
13:19you know i put brackets around the double pointer it's now dereferenced once that means rdi
13:25is going to receive a pointer to one string
13:31so i'm just sending this into my function my function takes two arguments it wants a pointer to
13:35a string and it wants the file descriptor to print to again other videos explain you know printing to
13:40standard output but i'm just going to print the string so this part right here should actually
13:45print the argument and then after we're done with that uh well maybe i should change this
13:53so that it's a little bit more clear i'm going to maybe push this down and say and increment so
14:00these lines up here actually do something they do the printing by dereferencing and then here we just
14:05sort of increment along the array of pointers and jump back to the top of the loop so what do we do
14:10in here with r14 remember r14 was the index that we're currently looking at so we we start looking
14:16at index 0 line 83 is going to say let's next look at index 1 and then this part right here add 8 to r15
14:26that just basically means remember r15 was the double pointer
14:32the double pointer like i said before it's looking at an array of pointers so it's actually a pointer to a
14:37pointer if i increase the memory location that r15 holds then it's now looking at the next pointer
14:44it's moving through the array r15 at that point would no longer be a valid pointer to the original
14:50array it's sort of like a running pointer it's kind of like scanning all of the pointers but a pointer is
14:56eight bytes so if we're just looking at the first pointer and we increase the memory location that we're
15:02looking at by eight bytes now we're looking at the second pointer so then on the next iteration of
15:08the loop if we dereference then we're going to end up dereferencing to the second string
15:14then i just print a little new line here honestly probably should have put that up top sloppy code i was
15:19doing this quickly and then we jump to the top of the loop so then we just basically keep going up to
15:24the top of the loop and uh printing arguments and advancing to the next pointer and we just keep going at
15:31it let me see if this works hopefully i didn't mess this up while i was screwing around all right
15:36so i'm going to do this make run okay so under the hood inside of my make file maybe let's open the
15:42make file real fast just so you can see um i'm just going to nano it real fast here under the hood
15:52when i call the program to run notice how i'm giving it arguments you can see right here on this line
15:57or actually let me get the line numbers up you can see on line 48 i am calling my executable which is
16:05named main in the make file i'm using a variable so don't worry about that but i'm giving it to
16:10four arguments and i'm just saying first arg second arg third arg fourth arg so that's why you see that
16:16printed in the previous screen it's just going through all the arguments that i gave it it's saying
16:22first arg second arg third arg fourth arg notice how it knows when to stop because of the argc that
16:29came in on rdi now that the program is built you know i've got my main executable which i named main
16:36which i said before was kind of confusing now that i've got it i can just execute it again and give it
16:42like one argument of like hello notice how it prints hello on a line by itself you do it again dudes and
16:50notice how every argument i give it it just prints it no matter how many i do i can go a b c d e f g i
16:58could probably do this so many times that i exhaust you know a long integer which is would be a horrible
17:06endeavor but um yeah everything that i put on there it just loops through it and prints it so
17:11when you are linking with gcc because you're probably using a hybrid program or at least if you're
17:16just linking with gcc gcc makes it really really easy to access the command line arguments imagine
17:24now instead of just printing these things you used them to somehow decide what your program was going
17:29to do maybe the user will give you a sub command like if you're a git user we say git status right
17:34so the git says like oh you git launches and it goes you want the status of something yeah sure
17:38so in your program now you could read what the user typed figure out a way to parse it and
17:43interpret it and then have your program's behavior adjust to whatever the user typed in
17:50okay that's it for this video the next video i'm going to post is how to do basically exactly the
17:55same thing but using pure assembly without linking against gcc so like ld is going to be the linker
18:03just like pure assembly no extra libraries okay thank you so much for watching this video i hope you
18:09learned a little bit of stuff and you had a little bit of fun tell your friends and i'll see you in
18:14the next video hey everybody thanks for watching this video again from the bottom of my heart i
18:21really appreciate it i do hope you did learn something and have some fun uh if you could do me
18:26a please a small little favor could you please subscribe and follow this channel or these videos
18:32or whatever it is you do on the current social media website that you're looking at right now
18:36um it would really mean the world to me and it'll help make more videos and grow this community so
18:42we'll be able to do more videos longer videos better videos or just i'll be able to keep making videos
18:47in general so please do do me a kindness and uh and subscribe you know sometimes i'm sleeping in the
18:53middle of the night and i just wake up because i know somebody subscribed or followed it just wakes me
18:58up and i get filled with joy that's exactly what happens every single time so you could do it as a nice
19:03favor to me or you could you could troll me if you want to just wake me up in the middle of the night
19:06just subscribe and then i'll i'll just wake up i promise that's what will happen also uh if you
19:12look at the middle of the screen right now you should see a qr code which you can scan in order to go to
19:17the website which i think is also named somewhere at the bottom of this video and it'll take you to my
19:22main website where you can just kind of like see all the videos i published and the services and
19:26tutorials and things that i offer and all that good stuff and uh if you have a suggestion for uh
19:34uh clarifications or errata or just future videos that you want to see please leave a comment or if
19:40you just want to say hey what's up what's going on you know just send me a comment whatever i also wake
19:45up for those in the middle of the night i get i wake up in a cold sweat and i'm like it would really it
19:51really mean the world to me i would really appreciate it so again thank you so much for watching this video
19:55and um enjoy the cool music as as i fade into the darkness which is coming for us all
20:25so
20:28so
23:08
|
Up next
Recommended
10:25
0:59
2:27
0:29
Be the first to comment