nm0716: looks as though i'm trapped here so i'll er i'll leave the door open [laugh] er the last two lectures have been introducing er basically the model of the process of the memory and some simple addressing modes and we're looking at one particular instruction to see how er we er place or find operands either in registers or in the memory what i'm going to do now is to er look at writing programs in assembler and er an assembler is is a program translation system just like er a compiler although very much more simple-minded so we start off with a er source program file you created with an editor or whatever er you invoke the assembler program to assemble it for you and this will produce typically two files a binary program and a listing file now the binary file is the one that you load into the machine and actually execute in other words it's producing an executable program for you okay now the machines that we'll use i i'll be arranging some practicals a bit later on in the term er and we'll be using the er P-Cs okay now the P-Cs of course have an Intel architecture but i'm teaching you sixty- eight-thousand architecture well the er way round that of course is to write a program or a a a package which will er assemble and emulate the sixty-eight- thousand machine and that's what er we will use it has a certain few little peculiarities but it's sufficient for the purposes of er this term now you've been used to writing er programs in er object- oriented Pascal er and they are free format that is you can lay it out any way you like although i'm sure you've been told to indent er statements when they're in a loop and and so on all right so that you can read it easily but in principle you could start at the top you know at the top of the file and just write it as a single er complete statement because the or set of statements because in fact er a Pascal program is in fact er one er single er er entity as far as the compiler concerned it will take everything up to a full stop the assembler is different because it's one statement per line okay and the statement will have fields okay so immediately position within the the line is important all right whereas before in Pascal it doesn't matter as far here with assembler it does and a field obviously has to have a terminator you've got and er here this can be either a space typically a field will er have a one or more spaces as a terminator or end of line so there are three sorts of statement we're going to to use in in the assembler that you're going to employ the first one is an instruction statement which starts off with a null field okay and then we'll have three fields a mnemonic field an address field and a comment now the comment itself is er begins with a semicolon but it's optional you can either put a comment in or not now what i'll be doing is using the comment er as a way of er writing the informal Pascal that i'm going to use to er generate the assembler you can have a label statement which is s-, simply a name followed by a code and you can have up to fifteen letters it's a very simple assembler as i said normally one would be able to be a bit bit bit more flexible than that and directives okay these are an instruction to the assembler program itself okay to declare variables or symbolic constants so we're going to er construct programs er from sets of instructions we'll need labels because er we'll have to have explicit jumps or branches and we'll need labels to do that and we'll also have to declare variables in a simplistic way so what is a let's just have a look at one just to sort of see the shape we won't necessarily understand every statement in it but you can see the three types here okay but first of all that's the comment and er and it's a very simple program it says if A is greater than equal to B R becomes A else R becomes B and there's one or two other bits and pieces okay so it's a very simple little program here the label field the mnemonic field the address field although you don't have to line them up as long as you've got a terminator there it does make some sense to line them up so you can read it okay so here from the move down J-S-R these are instruction statements mm so there's a null field here mnemonic and the address A-comma-D- nought some you get we're fairly familiar with already down here that's how we declare variables okay so there's A-colon it's got word got to say how long it is and there's an integer value the er the integer values are er when the assembler generates the variables it well space for the variables it initializes A B and C to the values that we've put here here is a directive which is to to d-, er declare constants and this is in fact how we're going to interact with in a very simple way with the emulator okay but the emulator's going to run this program but we've actually got to say when to stop so you need to have an explicit address of where to jump to and here is a jump to the subroutine or procedure which will stop the program and er allow you to inspect what's gone on so there will always be one of those in every program that you write there'll be a few instructions like this and then there'll be some simple variables okay that's what we'll be doing for the time being just to sort of look at instructions a bit more so the mnemonic field contains the symbolic reputation of the operation you want now you've had er a new set of notes given out at the back if you'd like to have a look okay there's all the instructions okay th-, those are all the mnemonics [cough] now er the way it's it's expressed is a series of columns of course there is a a name which is er like add or er er branch always and so on there's the assemble syntax for all of the instructions that you er are going to use for n-, or you could use you you won't necessarily have to use all of these but i i've done a fairly complete set just to er er for those who are interested and then using the notation i developed in the first two lectures there is the operations all right that er occur that that the the the er when you execute the instruction but finally the illegal effective addresses if there are any okay it would be a good idea if you po-, possibly detach that from the notes 'cause you'll need that for the practicals okay so the mnemonic field it's one of the you select the operation you want at any particular point and put it in the mnemonic field the address field it's the operand addresses okay we've we and it can be either in symbolic form we can use a name or it's constants right the that's er so what would symbolic names they can be memory variables okay A B C and so on they can be er predefined register names D-nought D-one and so on and label names which we saw in the example program decimal constants oll constants can be either er decimal or hexadecimal or in fact character where we we enclose our character within er quotes so the language isn't very extensive right there's not there's not many rules so a symbolic constant symbol name equals a constant value the symbol name is the same as a er a label but er it doesn't have the the the colon after it and a constant value in this case is restricted to pos-, positive decimal or hexadecimal so there are sort of funny little inconsistencies here i i er if the it er i did write an assembler once and there's no particular reason why it should be so but that's what it is with the assembler we've got er what the assembler will do just as it does in a in in your Pascal program it will replace any er symbol name with that constant value okay so it it it works in in exa-, exactly the same way in Pascal of course [sniff] er one or more memory variables we can say we can add a label colon dot type where that's going to determine how long the er operand is or or er or the variable is and we can have a list of one or more constants there well what happens here is the assembler will reserve space at that particular point in the program okay and what it will do is associate the name with the address in memory okay so what we've got er and that's the address of the most significant byte okay so let's so here i'm declaring a byte variable a word variable a long word variable and if those were three successive statements in the er er in the er in the assembler then what would happen in memory is essentially you'd have er byte int then there'd be a word int so that would be two bytes er and a long word int four bytes and these would be initialized for the values you've got here if we're thinking about addresses this would be some address and that would be A er A-plus-one or A- plus- er -three right so it would associate the name for the particular address one er issue with er er the assembler is that or or the machine is that these have to be on word boundaries so you have to do something about that but what i want you to to to go away with is the fact that when you write the a the word byte int or word int in in a in a an instruction okay what happens is the assembler will replace it with the address that it's allocated the variable in now you can declare lists of bytes and words and long words and there's li-, er a word list and it will reserve three words and initialize them to those values okay so that's how we declare variables and we will normally declare them at the bo-, bottom of the program we'll only need for as i say the the examples and and from now on we'll only need to declare er simple er integer variables and er and and we'll just take that on for er through the next few lectures so we're only interested in word integers at this point now what we're going to do is to generate all right take Pascal statements okay which er and generate templates or rules for generating the assembler from them okay that's the er technique we're going to use and we're going to start with assignment now the general Pascal statement er is you assign an expression to a variable okay and to start with we'll make the simplest expression we can do is make our expression either a constant or er a variable so i-, in order to develop a template for this we've got to say what instructions will support this sort of activity well of course vary the move statement okay and and you can look it up in your list of whatever with there there's a move statement and this will move er the contents of the source operand to contents of the destination operand okay now let's take we want to do er an assignment which is just P becomes Q is that very simple then P and Q er our memory variables and this is fairly straightforward you just have to decide what length the wretched thing is all right because er you can either er be a byte a word or a long word as i say we'll mostly conc-, concentrate on that so P becomes Q but that of course is the destination and that's the source yeah you agree so you have to reverse P and Q to what the Pascal is yeah contents of Q er if we look at it the assembler instruction the contents of Q is assigned to contents of P but of course in our high level language we do it the other way round it's one of the qualities of of er working with a machine at this level right that that's the one thing you just that's just the that's the only re-, all right source destination just keep that quote in your head source destination and of course we could have some more examples of that flat screen they're all of the same type here where what i've done is to assign move er whoops i've got a dot there i don't need er first of all if i just write move without a qualifier qualifier-dot-B-dot-W-dot- L it takes a word by default so if i don't put anything in it's a word this is how we can move data between registers from a register to memory er and er from memory to a register which we will find we'll have to do er not infrequently and finally how do we assign the constants well you can just use the immediate form of addressing which we we introduced last time and we would move twenty-one in assign twenty-one-P and minus-thirty-four to D-four so that's one way of assigning er constants okay so we now know how to s-, assign constants and variables whichever they are and what we'll do is treat er registers as just predefined variable names okay from a symbolic point of view that's all they are they're just predefined variable names any questions so far okay now the one thing about assembler is you can always do things in other ways okay so there's always first of all i guess you do write initialize something to zero quite frequently don't you yeah well there's a hardware instruction to do that er and er it's called clear and you can clear registers or memory that way and so here is just vision clear and the Pascal form to the right okay now if a constant's less than two-hundred-and-fifty-six why two-hundred-and- fifty-six well why it's nought to two- er -five-five they use a field within the instruction which is a byte long yeah you all realize a byte can contain up to two-five-five yeah er so there's a field in the instruction and what it is is er you can use move quick for for smaller constants and what it does is to save space i-, the the r-, the instruction isn't as long as er a move with an immediate form okay you don't have any extension words in fact and this all comes about because er when the s-, sixty-eight-thousand and the Intel were all being designed it was very important to save memory i mean it's one of these cases where er the constraints of er machines at the time affected how the architecture was was put together it was very important to try to keep programs as small as possible also they tried to make the instructions as short in terms of machine cycles as possible 'cause they were very slow all right it was a er the er early er micros were extraordinarily slow i mean you you just well we were talking now typically er er you know a gigahertz clock we were talking about two megahertz all right so and memory cost an awful lot of money okay so er you wanted to have instructions as short a-, you know programs as short as you can so there were a lot of different variants of instructions and that's why there's partly why the sixty-eight-thousand looks like it does what we've done now in in the the last ten years or so is to have a completely different architectural design and where every instruction has the same length right as we'd used instruction set after it in fact we er can only do arithmetic for example using registers you can't er do arithmetic involving memory which you can with a sixty-eight- thousand what this means is is you can clock the processor very much faster all right and as we m-, make chips smaller and smaller so the clock rate goes up so that's really what's been happening okay so the reason for all of this is to it was to try to er minimize programs er both in terms of space and execution time what we'll do er next is to to just look at how do we encode s-, er expressions and assignment where they simple integer expressions so er okay now we looked add was our running example for the first two lectures okay w-, it and we could write it in a form where the er and there there's a subtract which is very similar of course and we can write down this is formally what happens that destination becomes destination so there is addition but also an assignment th-, there's as a single in er operation but just as there are two variants of add so there are two variants of subtract and we saw that we needed that if we wanted to add and subtract to memory and for this form there were various illegal forms of er the er effective address in the er that's all detailed on your instruction sheet er for any particular instruction now if the operands are in the right place then simple instructions can be encoded simple expressions can be encoded in one instruction yeah 'cause this looks quite useful doesn't it in terms of of how to encode a piece of Pascal so here we are this adds contents of a er memory variable to er a data register and here we're using the second form to whoops and that would subtract it but the trouble is that life isn't always quite as simple as that but okay well we'll just do another simple case which is just how we add constants and then we'll come back to more complicated expressions and and we'll learn a relatively simple technique which er will er to evaluating er expressions simple expressions we've already seen what's what's going on am i doing the encoding all right the source destination so D-six becomes D-six-plus-P yeah so i i take that and look at that generate A it's B the source is P so i write P comma destination is D-six so i write D-six well we often add constants I becomes I-plus-one is fairly familiar er sort of a construction in Pascal and here we obviously can use the immediate form as well just as we did with assigning the constants so here we can just add a constant to a register okay now this form obviously in general its sum becomes sum-plus-constant there are as before variants to er produce shorter or er faster er instructions and there's er an immediate which we'll do add there's here we add to memory and here add quick we can add again er add to memory there add quick is in fact very small 'cause we only got three bit field here you can only do between one and eight but if you think about it it's it's a subset of of all the the er additions that you do and those are the ones you probably use most frequently okay now of course what if you've what got two variables in memory okay what i've i've been doing at the moment i've had a variable in a register and a variable in a memory location okay now you can't have both a source and destination as memory variables the only instruction you can is move and you can't do it with the rest so how are we going to do it well what we've got to do is move the value of one of the varia-, er variables into a register do and do the addition okay so essentially we've broken it down into er two things where so we have to break it down into a simpler set of instructions but we haven't got a single instruction to do it so we have to this is why pretty much the data register there sm0717: can i just nm0716: yeah sm0717: the second line should be add shouldn't it nm0716: sorry sm0717: on the second line should it be add nm0716: yes thank you okay now in a very simple form we're using the data register as an accumulator as a temporary scratch piece of me-, er a temporary scratch variable to perform the addition so let's look at a rather more complicated form all right well not too complicated but here we are we've got A-plus-B-plus-C-minus-eighty-seven so essentially what we're going to have to do we start off assigning A to D-nought then add B to D-nought how about having a go at the next two yeah how about trying to continue you you got you see what i've done here i i've added i now want to add C er minus- eighty-seven from D-nought so have just have a go nm0716: okay well we add C to D-nought we then subtract eighty-seven from D- nought and then we assign the accumulator D-nought to the sum now that is just a straightforward way of doing it okay it's it's it's a it's obvious well you know you break it down into components and we we we do that there is a more sophisticated way of doing it which er m-, might be those who are re-, really interested might like to look up a book on a compiler all right how there is a general method for er evaluating expressions with arbitrary sets of brackets and operators and and so on all right not going to go into it here but it is interesting how to say 'cause you can write there's a pretty complicated expression before the compiler will give up i don't suggest you do you can't read it i mean i i tend to break expressions up just so that i can read what's going on but a compiler has to have a general way and in fact what it does it er uses a stack method to to do it and for those who are interested any book in compilers will have a little section on this so i'm not pretending to be a real compiler at this point we'll just take simple expressions and attack them in an obvious way okay looks like any questions then er looks like you're off for five minutes early so there we go it must go quicker than i thought