Everybody and welcome to the next video.
So in this video we're going to be working with reading files and then writing it and then printing
it to the screen.
Now what's important here is I want you to follow along with this very carefully because we're gonna
be stringing multiple system calls together.
We're going to be stringing in the read in the right system calls together.
And that's gonna be very important when we're looking at this.
So the first thing we want to do is we need.
I'm sorry we have.
We need to.
We're gonna be stripping together the open read and write.
So we're gonna be using all of those calls together in sequence.
Now the first thing we need to do is something we haven't gone over yet.
We need to create a section now.
This is similar to what we've seen when we looking at windows files.
So this section is called Dot OBSS.
So this is the uninitiated section so we can we can set up in memory an allocation of a buffer size
of some kind that we can use later.
So what we're going to do is create an allocation called buffer and we're gonna reserve the byte Ah
yes for reserve and then be for byte and we're going to reserve for hundred or zero x four hundred which
is ten twenty four bytes
I'm sorry.
There are bits and bytes bits so ten twenty four bits.
Right.
So what this is doing is this is setting up in memory with the label buffer at a specific memory at
a available memory address we're reserving hundred and twenty four bits now once we've got that.
We need a file name so similar to how we've defined our bytes before a file name is just simply text.
So we'll see we'll name this file we'll be defined by it and we're going to want to give it we'll do
it need to be the dot slash because these be location and we'll call this data that time.
Right.
What's important here is we have 0 0 at the end which is a no byte which is what is used to determine
the end of a string.
In this case the end of the file.
Now we're needing this because of the file when it's standard data it's going to be dependent automatically
because of the way the buffers work in memory.
But in this particular case we're going to dependent specifically so we don't have any extra run in
staff because the file names need to match.
So now that we have are reserved buffer so we can show where our data will actually go when we read
it and we've got our file name.
The first thing we need to do is create a file.
So we need it.
So we'll do the eye data.
So in interior we'll do red team
do
Red Nation Gordon save will be kept data.
We have our integration perfect.
Now the first thing we're going to do is we need to work with opening the file so to do this we'll look
at the man page real quick.
So man to open.
So this looks like it takes is it takes in a path the flags and then in our case we're gonna be using
the mode.
So we're going to do it as a read only so you can see down here.
This uses its subsequent calls as read right.
Ellis l seek and see A.L. which is fine but in our case we're going to be looking at this.
So this tells us what parameters.
So this is going to go into PBX.
This is going to go into easy X and then this is edicts.
I'm sorry.
This is x.
We're not using flags.
So we are looking for this one.
Sorry we're gonna be using this one now.
All of these.
I'm sorry.
This one.
This is the one we're using.
You can use either it depending on how many parameters you pass.
So in our case we're going to be passing this in X and then this easy X
so as we've looked at are we've looked at the table before suddenly that up to you guys to look at the
second table which we've looked at and one of the first videos.
So in this case the open source call is 0 x 5.
So this is going to be open.
So we're going to want to do is move into a x zero x 5.
So that's our Siskel.
Then we're gonna want it to be X which is the path name we want to provide the file.
So move X we're gonna give it the file and then move easy x.
We're going to want to give it a mode in our case zero is read only.
Now there's some documentation that you can read if you'd like to to figure out what 0 is what 1 is
what 2 is.
We'll be working with a different mode later in the next video but zero is read only so we're only reading
the file we're not opening it for us to write to it.
So the file descriptor or the the file descriptor that's what we're gonna be handling which is our handler
to the file so if we write to the file descriptor or we read from it we will we have to use that file
descriptor which is similar to how we did right when we left the X completely empty.
That was because it was defaulting to the file descriptor of standard out now.
In our case we're going to be using that this particular file handler so we've created the file or we've
opened the file at this point.
However we still need our interrupt.
So that actually executes this this call.
So as soon as 0 x 80 our interrupt has happened this this call will go ahead and function and then it
will open up the file.
Now the file descriptor is then sent in an assigned to CETA X..
So the E X register after this interrupt call will have the file descriptors locate memory location.
So we want to keep that in our case we're not using SSI for anything.
So we're gonna copy the X into ECI or SSI and yes I will contain our register or file descriptor GSI
is the file descriptor for the open file.
Perfect.
So that's important to keep in mind.
Now the next thing we need to do is we need to read from a file descriptor.
So if we come in to here and demand to read there we have we're going to we're passing in a file descriptor
and X we're passing it a buffer an easy X and then we're passing it account or the size of the buffer
in EDI x.
So this should be look pretty straightforward.
So what we're gonna do is we're in a move into a x 0 x 3 that's on our so-called table for the for the
read Siskel.
Then we're going to move and do X we want to move in r file descriptor which happens to be an ECI or
ECI so that makes that nice and easy.
And then into easy X we want to move our buffer so the data that we want to store in this case when
we read from a file descriptor we need to store it somewhere in memory when we read it.
So in this case we're using buffer so this is the location we want to store it now in EDI X we need
to give it the size of the buffer.
So in our case we already know how big the buffer is 0 x four hundred or ten twenty four bits now what
we're gonna do here is we've set up our read completely.
So this is it.
So if we do 0 x 80 spurring this time the next line in 0 8.
So now once we've executed that what we've done is we've then we've opened the system we've opened the
file saved the file descriptor location we've set.
This is called to be read.
We've given it the file descriptor of the open file that we currently still have open.
Then we have our buffer where we're gonna store in the length and then we're going to we're going to
issue to interrupt.
So when this happens it's going to load the first twenty four bits of the file into the buffer perfect.
Makes sense.
Now what we're going to want to do is we want to transfer that text to the screen.
Now we've been doing that before.
We've been doing that with move x 0 x 4 for our right now.
Again R E B X was normally a particular file descriptor that we wanted to write to.
But in this case we're gonna do we're gonna specifically specify so we're going to do it move X to zero
x 1.
So we're going to do standard out as our as we're we're gonna want to specifically write this to now.
This is similar to our string.
So an easy X we're going to want to want our buffers.
This is what we're going to write and then in X we want our size 0 x 400.
Then we have our interrupt direct 80 that will actually print our data from the file onto the screen.
Now there's one bit of cleanup if you ran this it would work however that we're an assembly so we need
to actually clean ourselves up when we're doing this.
So we've opened a file we need to close it.
So if we look here and demand to close we can see it just takes a file descriptor.
Now if you if you left this completely as is we used this guy right here 0 x 1 which is standard output
as our file descriptor so if we decided to run that with just just calling if we said this is I think
close is 0 6.
So if we set that and didn't set the file descriptor it would just close.
Standard output file descriptor that's open on the system.
We don't want that.
So move x 0 6 which is r our sister called number for close then we're going to want to move into X
which is r file descriptor object or parameter right here.
Yes I then interrupt 0 8 so doing this allows us to read from the file.
So we open the file and get the file descriptor we save the descriptor and ISI then we go ahead and
read from the file descriptor and do our buffer which we've created here then we're gonna go ahead and
print the buffer to the screen then we're gonna close the file and then we exit the application.
So if we haven't made any coding mistakes we should be able to go ahead I'll get the data one more time
then we're gonna go ahead and compile and run it perfect.
So there we go.
We've opened up the file we've read it we wrote it and we closed the file play around with this.
See what happens when you run out of the size of the file.
So you put too much data and then you try to read from it.
I want you to think about why you're only reading so much at a time and think maybe brainstorm with
a little bit of a different way you could do this.