1
00:00:08,610 --> 00:00:19,480
let us now discuss the simple risc isa so
simple risc is an assembly language that we
2
00:00:19,480 --> 00:00:30,189
shall create you and me shall create from
scratch ah so this is supposed to be a very
3
00:00:30,189 --> 00:00:39,240
very simple riscs like language that has a
very few ah that the that is few instructions
4
00:00:39,240 --> 00:00:44,570
and the instructions have a very regular structure
so as we go through the next few slides we
5
00:00:44,570 --> 00:00:48,110
will see what it actually takes to build and
design an assembly language and we shall look
6
00:00:48,110 --> 00:00:52,430
at the netegrities of designing one such language
so let's take a look at simple risc so simple
7
00:00:52,430 --> 00:00:58,880
risc is a very simple ah you know as a name
suggests it is a very very simple assembly
8
00:00:58,880 --> 00:01:04,140
language one of the easiest to learn so my
thinking is that it's better to learn a very
9
00:01:04,140 --> 00:01:10,369
simple assembly language and get an idea first
before moving on to real world assembly languages
10
00:01:10,369 --> 00:01:17,070
such as arm and x eighty six because they
have a lot of other ah details which might
11
00:01:17,070 --> 00:01:22,560
overwhelm the retain at this point of time
so we will design an assembly language called
12
00:01:22,560 --> 00:01:28,979
simple risc and then we will design a simple
binary encoding for it which means to convert
13
00:01:28,979 --> 00:01:35,530
ah instructions into a sequence as zeros and
ones and then in the processor chapter we
14
00:01:35,530 --> 00:01:44,659
will create a processor to actually implement
the simple risc instruction set
15
00:01:44,659 --> 00:01:52,810
so let's have a brief survey of ah instruction
sets say ah almost every processor vendor
16
00:01:52,810 --> 00:02:03,490
ah has created it's own ah instruction set
so ah let's go back to way back to nineteen
17
00:02:03,490 --> 00:02:10,090
seventy seven when the wax instruction set
was created it was a very complex cisc instruction
18
00:02:10,090 --> 00:02:16,860
set with a lot of instructions ah it had sixteen
registers and it used a little endian format
19
00:02:16,860 --> 00:02:25,070
ah then ah we ah one of the more popular instruction
sets was a suns sparc instruction set ah so
20
00:02:25,070 --> 00:02:31,450
there were actually two variants of sparc
the thirty two bit variant was introduced
21
00:02:31,450 --> 00:02:38,640
in nineteen eighty six and the sixty four
bit variant was introduced in nineteen ninety
22
00:02:38,640 --> 00:02:41,950
three
so what thirty two bits means that one instruction
23
00:02:41,950 --> 00:02:49,540
was thirty two bits in sparc the size of a
register was thirty two bits so that's what
24
00:02:49,540 --> 00:02:57,290
it means to be a thirty two bit instruction
set and what it means to be a sixty four bit
25
00:02:57,290 --> 00:03:01,599
instruction set is also the same size of a
register is sixty four bits ah or the size
26
00:03:01,599 --> 00:03:04,790
of an instruction in the case of a risc architecture
is sixty four bits so the sixty four bit instruction
27
00:03:04,790 --> 00:03:08,190
set was actually bi endian means it could
be considered to the either little endian
28
00:03:08,190 --> 00:03:11,630
or big endian and where thirty two registers
then we come to the powerpc instruction set
29
00:03:11,630 --> 00:03:17,450
which was mostly used by i b m and latal and
later apple and motorola used it ah
30
00:03:17,450 --> 00:03:23,470
so powerpc showed a similar revolution they
had a thirty two bit instruction set in nineteen
31
00:03:23,470 --> 00:03:30,120
ninety two and then migrated to a sixty four
bit instruction set later on and both of them
32
00:03:30,120 --> 00:03:35,050
were bi endian with thirty two bit ah with
thirty two registers similarly h p had it's
33
00:03:35,050 --> 00:03:39,959
own instruction set pa risc that's still used
in high and h p servers and motorola had it's
34
00:03:39,959 --> 00:03:44,270
m sixty eight k or m sixty eight thousand
instruction set that is still ah well that
35
00:03:44,270 --> 00:03:48,220
was being used in motorola processors so now
of course they are not in the processor business
36
00:03:48,220 --> 00:03:53,150
anymore another very popular instruction set
is the mips instruction set which is also
37
00:03:53,150 --> 00:04:01,230
a risk instruction set mips has thirty two
registers ah so now mips is used in uh some
38
00:04:01,230 --> 00:04:05,050
small embedded processors the most of the
embedded processors have actually migrated
39
00:04:05,050 --> 00:04:13,629
to ah intel and arm here is one more instruction
set which is my processors favorite the reason
40
00:04:13,629 --> 00:04:21,269
being that this is the first instruction set
that i had learnt is the dec alpha instruction
41
00:04:21,269 --> 00:04:27,330
set introduced sirca nineteen ninety two so
it started out at the sixty four bit ah instruction
42
00:04:27,330 --> 00:04:35,330
set ah with thirty two registers then we come
ah to the x eighty six instruction set so
43
00:04:35,330 --> 00:04:37,650
it is actually a family of instruction sets
it's not just one instruction set but many
44
00:04:37,650 --> 00:04:42,099
so intel started his journey ah in nineteen
seventy eight with a sixteen bit instruction
45
00:04:42,099 --> 00:04:46,750
set ah that had eight registers ah
so this x eighty six instruction set is now
46
00:04:46,750 --> 00:04:53,509
an open standard ah so it is used by amd it's
actually used a many other vendors as well
47
00:04:53,509 --> 00:04:58,979
ah because ah so even ah since the standard
is open readers can implement their own processors
48
00:04:58,979 --> 00:05:03,099
with the x eighty six instruction set say
around nineteen eighty five intel transition
49
00:05:03,099 --> 00:05:10,490
to the thirty two bit instruction set again
with eight registers and then as recently
50
00:05:10,490 --> 00:05:21,099
as two thousand three ah both intel and a
m d together ah migrated to an x eighty six
51
00:05:21,099 --> 00:05:29,430
sixty four instruction set actually a m d
was the first to migrate and intel then migrated
52
00:05:29,430 --> 00:05:36,210
say a m d came up with a octoroon processor
that had a sixty four bit instruction set
53
00:05:36,210 --> 00:05:38,789
called x eighty six sixty four
so all ah intel instruction sets have been
54
00:05:38,789 --> 00:05:45,409
brittle endian and the sixty four bit instruction
set of intel which we shall also take a look
55
00:05:45,409 --> 00:05:59,389
at in the fifth chapter of this book has six
registers ah lastly the latest entrant in
56
00:05:59,389 --> 00:06:05,270
the world of processors and instruction sets
is arm so ah arm is a very interesting company
57
00:06:05,270 --> 00:06:16,729
in the sense that arm does not makes it's
own chips rather arm makes the design of a
58
00:06:16,729 --> 00:06:23,839
processor and sends the design to other companies
like texas instruments or volcom or samsung
59
00:06:23,839 --> 00:06:32,280
and then they make a chip and embed some additional
elements if required and then they make an
60
00:06:32,280 --> 00:06:39,069
fabricated chip out of it
so arm is not that new in fact arm used to
61
00:06:39,069 --> 00:06:46,199
exist ah way back in nineteen eighty five
when they came up with the thirty two bit
62
00:06:46,199 --> 00:06:52,210
instruction set but off late arm processors
are gaining in prominence and a big reason
63
00:06:52,210 --> 00:06:58,819
for that is that ah arm processes are used
in mobile phones and mobile phones have ah
64
00:06:58,819 --> 00:07:05,479
spread ah exponentially say in two thousand
eleven arm also came up with a sixty four
65
00:07:05,479 --> 00:07:12,860
bit instruction set for larger computing devices
such as tablets and you know possibly six
66
00:07:12,860 --> 00:07:17,469
servers and so this sixty four bit instruction
sets are always targeted for larger devices
67
00:07:17,469 --> 00:07:26,270
so we will see why so we will we will have
a lot of opportunities to discuss ah whats
68
00:07:26,270 --> 00:07:34,039
the advantage of a sixty four bit instruction
set but this is not the right point for it
69
00:07:34,039 --> 00:07:42,709
so arm is also bi endian little endian is
the default ah but it can be considered to
70
00:07:42,709 --> 00:07:50,759
be big endian so big endian or little endian
is just a matter of convention let us now
71
00:07:50,759 --> 00:07:54,699
discuss the basics of simple risc instruction
set so let's simple risc has sixteen registers
72
00:07:54,699 --> 00:07:59,249
and as is that convention let us number the
registers from r zero till r fifteen so the
73
00:07:59,249 --> 00:08:03,599
fourteenth and the fifteenth register r fourteen
and r fifteen let me refer to them ah with
74
00:08:03,599 --> 00:08:07,169
special names
so let us refer to r fourteen as the stack
75
00:08:07,169 --> 00:08:14,929
pointer or the or s p similarly let us refer
to r fifteen as the return address register
76
00:08:14,929 --> 00:08:23,589
or r a so the meaning of the term stack pointer
and the return address register will be ah
77
00:08:23,589 --> 00:08:29,699
ee is ah they actually correspond to pretty
complex concepts so we will discuss what they
78
00:08:29,699 --> 00:08:37,410
are in the future slides but let's just remember
for the time being that the stack pointer
79
00:08:37,410 --> 00:08:48,879
s p is r fourteen and r fifteen is ah we will
also be referred towards the return address
80
00:08:48,879 --> 00:08:57,600
register so they have some special meanings
so coming to the view of memory let's assume
81
00:08:57,600 --> 00:09:02,600
a von nuemam model so we will assume that
the memory is one larger ah array of bytes
82
00:09:02,600 --> 00:09:08,830
it contains data as well as the program and
a program assumes the entire memory is for
83
00:09:08,830 --> 00:09:14,829
it's own personal consumption and run over
their programs so let's play with some ver
84
00:09:14,829 --> 00:09:18,639
very simplistic assumptions for the time being
so let's also have a special flags register
85
00:09:18,639 --> 00:09:24,370
if it's contains the result of the last comparison
so let me give an example so this register
86
00:09:24,370 --> 00:09:25,579
is not visible to the assembly programmer
however it will be visible to the hardware
87
00:09:25,579 --> 00:09:29,019
designer and that's very important so let's
ah do let me show an example here ah so the
88
00:09:29,019 --> 00:09:34,339
example is let's have a compare in simple
risc assembly where we compare the contents
89
00:09:34,339 --> 00:09:44,089
of register r one and r two if the contents
are equal what the hardware would do is that
90
00:09:44,089 --> 00:09:52,399
it would set the e bit inside the flags register
say if let's say you know r one is equal to
91
00:09:52,399 --> 00:09:59,450
let me put a c style equal to if this is the
case then you set the hardware sets flags
92
00:09:59,450 --> 00:10:03,199
dot e equal to one if r one is greater than
r two well then flags dot e so the default
93
00:10:03,199 --> 00:10:07,660
e and g t zero so i am not mentioning that's
the e v a field in this case is clearly zero
94
00:10:07,660 --> 00:10:14,339
so we set flags dot g t greater than as one
if r one is less than r two two so clearly
95
00:10:14,339 --> 00:10:19,810
the quality flag flags dot e is zero and flags
dot g t is zero so we can thus make out what
96
00:10:19,810 --> 00:10:25,290
is the result of the last comparison we are
taking a look at these two flags flags dot
97
00:10:25,290 --> 00:10:31,910
e and flags dot g t so this is the broad idea
but we will get a chance to discuss ah the
98
00:10:31,910 --> 00:10:34,350
meaning of these two flags in great detail
so when we look at the compare instruction
99
00:10:34,350 --> 00:10:38,580
now it's time to take look at our first assembly
instruction so let's ah introduce our first
100
00:10:38,580 --> 00:10:42,650
basic assembly instruction it's called the
mov instruction ah the job of the mov instruction
101
00:10:42,650 --> 00:10:49,019
is very simple so it comes in two flavors
so in one flavor we transfer the contents
102
00:10:49,019 --> 00:10:54,649
of a register into one more register and in
the second flavor we transfer an immediate
103
00:10:54,649 --> 00:10:58,920
a constant ah to a register right so these
are the only two ah variants of the mov instructions
104
00:10:58,920 --> 00:11:05,410
that are possible and the way that we actually
write an assembly statement is like this that
105
00:11:05,410 --> 00:11:10,810
we first ah so in simple risc what we do is
that we first write the name of the assembly
106
00:11:10,810 --> 00:11:15,819
opcode which in this case is mov
then we write the destination and then we
107
00:11:15,819 --> 00:11:24,430
give a comma and then we write the name of
the source so for example if we write mov
108
00:11:24,430 --> 00:11:30,180
r one comma three if which essentially means
set r one equal to three that's what it means
109
00:11:30,180 --> 00:11:37,310
right let me also put comment sign to indicate
that ah you know this is not a valid assembly
110
00:11:37,310 --> 00:11:42,670
statement so what we are doing here is that
we are taking the value three and setting
111
00:11:42,670 --> 00:11:49,850
it to register r one so you can think of this
as similar to any statement in c or java where
112
00:11:49,850 --> 00:12:02,570
we write something on the form x equals three
so in this case x is the variable and we are
113
00:12:02,570 --> 00:12:09,949
setting the value of the variable two three
or four or five or any ah immediate ah value
114
00:12:09,949 --> 00:12:16,920
also we can have statement x equals y so in
this case this is same as mov r one r two
115
00:12:16,920 --> 00:12:25,259
but the contents of r two are mov to r one
so the value of the immediate in this case
116
00:12:25,259 --> 00:12:27,889
is actually embedded inside the instruction
so simple risc supports sixteen bit immediates
117
00:12:27,889 --> 00:12:33,639
and the range of an immediate is so this is
the twos complement number and the range of
118
00:12:33,639 --> 00:12:38,220
an immediate is from minus two raise to the
power fifteen to two to the power fifteen
119
00:12:38,220 --> 00:12:44,600
minus one so this is the standard twos complement
range for a sixteen bit number so now let's
120
00:12:44,600 --> 00:12:48,089
look at another family of instruction since
the entire family is very similar i have listed
121
00:12:48,089 --> 00:12:50,180
all six together in the same place
so simple risc has six arithmetic instructions
122
00:12:50,180 --> 00:12:55,439
and as the name suggests their ah addition
ah let me just change the pointer addition
123
00:12:55,439 --> 00:13:01,709
subtraction add sub mul for multiplication
div for division mod for getting the remainder
124
00:13:01,709 --> 00:13:09,800
right so this is basically getting the modulo
ah or the remainder and cmp is for compare
125
00:13:09,800 --> 00:13:13,029
so the compare instruction is slightly special
but the rest are very straight forward
126
00:13:13,029 --> 00:13:18,079
so let's take a look at the add instruction
first so the add instruction is actually a
127
00:13:18,079 --> 00:13:21,610
three address format instruction what this
essentially means is that it takes three arguments
128
00:13:21,610 --> 00:13:24,930
so the destination always has to be a register
the second argument has to be a register and
129
00:13:24,930 --> 00:13:29,519
so basically if you want me to write this
slightly formally so the destination which
130
00:13:29,519 --> 00:13:36,029
comes first has to be a register the second
ah operand which is actually the first source
131
00:13:36,029 --> 00:13:43,870
operand has to be a register the third operand
can either be a register or an immediate so
132
00:13:43,870 --> 00:13:49,850
that is a constraint also on the third operand
which can either be a register or an immediate
133
00:13:49,850 --> 00:13:56,860
so here ah as if we write add r one r two
r three essentially what we are instructing
134
00:13:56,860 --> 00:14:03,639
machine to do is take the contents of r two
take the contents of r three add them and
135
00:14:03,639 --> 00:14:09,759
save the result in r one
similarly if i say r two and ten i can say
136
00:14:09,759 --> 00:14:14,970
r two plus ten so this is the simple add instruction
which has a nice analogue with traditional
137
00:14:14,970 --> 00:14:22,769
programming languages where if you write you
know say ah c function of a form x equals
138
00:14:22,769 --> 00:14:32,510
y plus z we can directly translate it to an
assembly instruction of this form fine so
139
00:14:32,510 --> 00:14:38,499
after the addition instruction the rest three
ah the rest four instructions sub mul div
140
00:14:38,499 --> 00:14:44,249
and mod have exactly the same format so i
have not shown the immediate values over here
141
00:14:44,249 --> 00:14:49,970
but i will explain with examples so these
four instructions over here as the name suggests
142
00:14:49,970 --> 00:14:54,240
the subtract instruction subtracts the contents
of r three from r two
143
00:14:54,240 --> 00:15:02,410
so it sets r one equal to r two minus r three
so in this case it is possible to write the
144
00:15:02,410 --> 00:15:07,350
the the subtract instruction with the third
operand being an immediate as well but mind
145
00:15:07,350 --> 00:15:15,259
you only the third operand can be an immediate
not the other ones so i can write very well
146
00:15:15,259 --> 00:15:27,190
r one r two and ten so what this will essentially
do is that it will set r one to r two the
147
00:15:27,190 --> 00:15:32,309
contents of r two minus ten so i have not
shown this case for lack of space but the
148
00:15:32,309 --> 00:15:38,720
third operand can either be a register or
it can be an immediate any register or it
149
00:15:38,720 --> 00:15:43,550
can be a immediate same is the case for multiplication
exactly the same format
150
00:15:43,550 --> 00:15:47,329
so let me write a multiplication instruction
with the third operand as an immediate so
151
00:15:47,329 --> 00:15:51,470
we shall see that the ah way of specifying
the format is the same so i can very well
152
00:15:51,470 --> 00:16:00,199
write mul r one r two and ten so this would
set r one equal to r two multiplied with ten
153
00:16:00,199 --> 00:16:04,320
so division and mod have also the same format
where we divide r two by r three to get the
154
00:16:04,320 --> 00:16:06,624
quotient and we take r two mod r there to
get the remainder so these are the five adds
155
00:16:06,624 --> 00:16:07,624
or mul div and mod other five arithmetic operator
operating instructions and as we can see there
156
00:16:07,624 --> 00:16:08,624
are very similar to their high level programming
equivalence so it is just that they written
157
00:16:08,624 --> 00:16:09,624
slightly differently
so one thing that is common is that even in
158
00:16:09,624 --> 00:16:11,260
a high level language ah such as you know
the statement over here the destination comes
159
00:16:11,260 --> 00:16:13,529
first subsequently the arguments come later
this is a same case in assembly as well where
160
00:16:13,529 --> 00:16:17,079
the destination comes first and the operands
or arguments come later so this is one similarity
161
00:16:17,079 --> 00:16:21,050
ah the the other similarities are that we
also you know we clearly mention what is the
162
00:16:21,050 --> 00:16:24,149
operation that we want to perform so either
it's an add or subtract or multiplication
163
00:16:24,149 --> 00:16:27,959
division whatever is the operation that is
ah you know that is placed at a different
164
00:16:27,959 --> 00:16:29,810
point so plus is over here but in assembly
we first mention what is the operation but
165
00:16:29,810 --> 00:16:32,209
ah you know other than this small difference
this statements like add r one r two r ten
166
00:16:32,209 --> 00:16:33,420
is pretty similar to what we would actually
write in a high level programming language
167
00:16:33,420 --> 00:16:37,839
assuming r one and r two are variables we
would write r one equals r two plus ten
168
00:16:37,839 --> 00:16:41,730
so now let's took a take a look at the compare
instruction which is actually very interesting
169
00:16:41,730 --> 00:16:43,160
because it is different so we typically don't
ah write compare instructions of this type
170
00:16:43,160 --> 00:16:45,839
in a high level language so in this case what
we do is that we pretty much subtract r one
171
00:16:45,839 --> 00:16:50,540
minus r two if the result is equal to zero
then we can infer an equality so we set flags
172
00:16:50,540 --> 00:16:54,910
dot e which is the equality flag to one if
r one minus r two is greater than zero then
173
00:16:54,910 --> 00:16:59,500
we can set flags dot g t equal to one which
basically means that ah r one is greater than
174
00:16:59,500 --> 00:17:04,800
r two it indicates the fact that the greater
than flag is one
175
00:17:04,800 --> 00:17:09,750
similarly if r one minus r two is less than
zero then both the equality flag and the greater
176
00:17:09,750 --> 00:17:14,091
than flag both will be set to zero so we can
infer a less than condition so it is important
177
00:17:14,091 --> 00:17:18,010
to note that in simple risc the only instruction
that sets the flags is the compare instruction
178
00:17:18,010 --> 00:17:19,520
and the flags remain in their state till the
next compare instructions so let me explain
179
00:17:19,520 --> 00:17:23,850
with an example assume we have a compare instruction
so the compare instruction is essentially
180
00:17:23,850 --> 00:17:26,940
go and set the flags after that we can have
many many other instructions but till the
181
00:17:26,940 --> 00:17:28,750
next compare instructions the flags maintain
their value and they can be used by other
182
00:17:28,750 --> 00:17:29,929
instructions as we shall see later
now let's take a look at ah very simple program
183
00:17:29,929 --> 00:17:31,990
and let's try to convert it into assembly
language so let's outline a generic method
184
00:17:31,990 --> 00:17:33,031
so assume that we have this codes that are
here in a high level language the question
185
00:17:33,031 --> 00:17:38,740
is how do we convert it to assembly language
so the first thing that we do is that we assign
186
00:17:38,740 --> 00:17:40,409
the variables here to registers that's the
first thing that's always what we do that's
187
00:17:40,409 --> 00:17:43,870
the first thing that we assign the variables
to registers so let us assume that variable
188
00:17:43,870 --> 00:17:47,510
a is assigned to r zero b to r one c to r
two and d to r three right so this is the
189
00:17:47,510 --> 00:17:49,740
assignment of variables to registers so now
what we do is that a equals three just becomes
190
00:17:49,740 --> 00:17:52,660
mov three to r zero b equals five becomes
mov five to r one c equals a plus b this essentially
191
00:17:52,660 --> 00:17:53,660
means add r zero and r one and less assign
it to r two because r two and c are when r
192
00:17:53,660 --> 00:17:54,660
two and c are r two is supposed to contain
the value of c
193
00:17:54,660 --> 00:17:55,660
then to subtract c minus five again take r
two subtract five from it and save the result
194
00:17:55,660 --> 00:17:56,660
in r three which is essentially map to d e
so here we have four high level statements
195
00:17:56,660 --> 00:17:57,660
and four assembly statements because it's
a simple program but in general the number
196
00:17:57,660 --> 00:17:58,660
of assembly statements will typically be more
sometimes much more but the important thing
197
00:17:58,660 --> 00:18:00,480
to note that it is fairly simple to map a
high level program to assembly what essentially
198
00:18:00,480 --> 00:18:02,370
needs to be done is that the firs[t]-
so there are two steps first is assign the
199
00:18:02,370 --> 00:18:04,090
variables to registers that is step one step
two is that after the assignment each and
200
00:18:04,090 --> 00:18:05,090
every high level statements gets converted
to assembly so the important point that the
201
00:18:05,090 --> 00:18:06,090
all of you need to know that writing assembly
programs is pretty much similar to writing
202
00:18:06,090 --> 00:18:07,090
high level programs and irrespective of how
many books you read and how many online courses
203
00:18:07,090 --> 00:18:08,090
you take such as this one the only way that
a student will actually is the ability to
204
00:18:08,090 --> 00:18:09,090
write high level assembly code is to you know
physically go and write as many assembly programs
205
00:18:09,090 --> 00:18:10,090
as possible practice makes perfect
so only by writing more and more and more
206
00:18:10,090 --> 00:18:11,090
programs would a student actually get some
expertise of how to write assembly programs
207
00:18:11,090 --> 00:18:12,090
and would get the confidence of writing assembly
programs so the codes and books and other
208
00:18:12,090 --> 00:18:13,090
material is fine to at least introduce the
concepts but the real knowledge will be gained
209
00:18:13,090 --> 00:18:14,090
only by practice and knowledge so if we go
to the website of this page you will find
210
00:18:14,090 --> 00:18:15,090
simple disk emulators which allow you to write
assembly programs and subsequently the emulator
211
00:18:15,090 --> 00:18:16,090
will run the assembly program instruction
by instruction and finally print the result
212
00:18:16,090 --> 00:18:17,090
so let's now take a look at one more example
a equals three b equals five multiply a and
213
00:18:17,090 --> 00:18:18,090
b and compute c mod five so again the first
stage is assign the variables to registers
214
00:18:18,090 --> 00:18:19,090
say a you know r zero is mapped to a r one
is mapped to b r two to c and r three to d
215
00:18:19,090 --> 00:18:20,090
so the first two statements are the same as
what they were in the previous example then
216
00:18:20,090 --> 00:18:21,090
we multiply r zero and r one and we save the
result in r two so r two is c and then again
217
00:18:21,090 --> 00:18:22,090
we do r two mod five so i am putting the percentage
because that's what it is in c and we do r
218
00:18:22,090 --> 00:18:23,090
two mod five and save the result in r three
now let's take a look at the compare instruction
219
00:18:23,090 --> 00:18:24,090
the compare instruction is different it's
a different concept it's not there in high
220
00:18:24,090 --> 00:18:25,090
level languages so let's see we want to compare
three and five and then you know the idea
221
00:18:25,090 --> 00:18:26,090
compare three and five and then we want to
print the value of the flags not in assembly
222
00:18:26,090 --> 00:18:27,090
but at least as a part of the answer so you
should actually we write the value of the
223
00:18:27,090 --> 00:18:28,090
flags so assume a is three and b is five so
we first to a register assignment we mov three
224
00:18:28,090 --> 00:18:29,090
two zero five two r one and compare r zero
and r one since three is less than five we
225
00:18:29,090 --> 00:18:30,090
will have the less than condition right so
what we have is that three is less than five
226
00:18:30,090 --> 00:18:31,090
so there is no equality and there is no greater
than
227
00:18:31,090 --> 00:18:32,090
so the equal flag will be zero and the greater
than flag will be zero now let's compare five
228
00:18:32,090 --> 00:18:33,090
and three so in this case they get reversed
so we can clea[rly]- clearly see that five
229
00:18:33,090 --> 00:18:34,090
is greater than three so in this case the
flag for equality is zero with the flag for
230
00:18:34,090 --> 00:18:35,090
greater than flags dot g t is equal to one
so this essentially means that the comparison
231
00:18:35,090 --> 00:18:36,090
yielded a result and the result was that the
first operand is greater than the second operand
232
00:18:36,090 --> 00:18:37,090
so that's the reason the g t flag flags dot
g t is equal to one
233
00:18:37,090 --> 00:18:38,090
lastly let's compare two numbers with the
same value five and five a is five and b is
234
00:18:38,090 --> 00:18:39,090
five let's compare a and b so mov five to
r zero we mov five to r one hour compare r
235
00:18:39,090 --> 00:18:40,090
zero and r one so flags dot e in this case
is equal to one and flags dot g t is equal
236
00:18:40,090 --> 00:18:41,090
to zero the flags dot e is equal to one because
there is an equality we clearly see that five
237
00:18:41,090 --> 00:18:42,090
so i am writing the equal to the same way
that is written in c equal to equal to so
238
00:18:42,090 --> 00:18:43,090
we clearly see that five is equal to equal
to five there is equality there is no greater
239
00:18:43,090 --> 00:18:44,090
than so the greater than flag is zero the
equality flag is one
240
00:18:44,090 --> 00:18:45,090
now let's show some more examples so let's
say right assembly code is simple risc to
241
00:18:45,090 --> 00:18:46,090
compute thirty one divided by twenty nine
minus fifty and save the result in r four
242
00:18:46,090 --> 00:18:47,090
no problem so mind you this is the integer
division so thirty one divided by twenty nine
243
00:18:47,090 --> 00:18:48,090
the answer will be one so let's map them let's
map map thirty one to r one let's put twenty
244
00:18:48,090 --> 00:18:49,090
nine in r two as can be seen in these two
statements then let's divide r one divided
245
00:18:49,090 --> 00:18:50,090
by r two and the result will get saved in
r three and from r three we subtract fifty
246
00:18:50,090 --> 00:18:51,090
so we compute r three minus fifty is equal
to r four and r four contains the final result
247
00:18:51,090 --> 00:18:52,090
as mentioned in the question so this is how
we write assembly programs but these are very
248
00:18:52,090 --> 00:18:53,090
simple examples of assembly programs
so my advice to the student of the reader
249
00:18:53,090 --> 00:18:54,090
would be to actually go and write as many
assembly programs as possible and i would
250
00:18:54,090 --> 00:18:55,090
say that by the end of this course the student
should have written at least three thousand
251
00:18:55,090 --> 00:18:56,090
lines of assembly programs thousand each in
simple risc rman x eighty six that would at
252
00:18:56,090 --> 00:18:57,090
least give the student a basic understanding
of how assembly programs are to be written
253
00:18:57,090 --> 00:18:58,090
now let's move ahead in our journey of introducing
instruction so the introduced three more so
254
00:18:58,090 --> 00:18:59,090
these are logical instructions which compute
a bitwise and an or or a not so let's look
255
00:18:59,090 --> 00:19:00,090
at ah some of these so the and and or operations
are exactly in the same way in the three address
256
00:19:00,090 --> 00:19:01,090
format as the add subtract multiply operations
so in this so the first two operands have
257
00:19:01,090 --> 00:19:02,090
to be registers and the third operand has
to be either a register or an immediate so
258
00:19:02,090 --> 00:19:03,090
the and operand computes r one is equal to
r two and r three where this is a bitwise
259
00:19:03,090 --> 00:19:04,090
and so let me give an example of a bitwise
and in just case people have forgotten so
260
00:19:04,090 --> 00:19:05,090
let's say one one zero zero in a bitwise and
it with zero one zero one
261
00:19:05,090 --> 00:19:06,090
so one and zero is zero zero and zero is zero
one and one is one zero and one is zero similarly
262
00:19:06,090 --> 00:19:07,090
we have bitwise or which can do r two or r
three and so we have a bitwise or over here
263
00:19:07,090 --> 00:19:08,090
which is exactly the same but we replace the
and sign by an or sign then we have the not
264
00:19:08,090 --> 00:19:09,090
operand which is actually a two address format
operands they have only one source ah we have
265
00:19:09,090 --> 00:19:10,090
only one source operand which can be either
a register or an immediate so in this case
266
00:19:10,090 --> 00:19:11,090
what we do is that ah sorry so in this case
so if there is a single source operand so
267
00:19:11,090 --> 00:19:12,090
so what we can see as a general rule that
only one source operand is allowed to be an
268
00:19:12,090 --> 00:19:13,090
immediate but both the source operands are
not allowed to be immediates
269
00:19:13,090 --> 00:19:14,090
so when we say not r one r two what this essentially
means is that let's say the value of r two
270
00:19:14,090 --> 00:19:15,090
is zero zero one zero we take a not of it
once we take a not of it this becomes one
271
00:19:15,090 --> 00:19:16,090
one zero one so the second argument in this
case can either be a register or an immediate
272
00:19:16,090 --> 00:19:17,090
the second source argument so now let's compute
a or b so so now let's consider an example
273
00:19:17,090 --> 00:19:18,090
say in this example we compute a or b so assume
that a is stored in r zero and b is stored
274
00:19:18,090 --> 00:19:19,090
in r one so let's store the result in r two
so it's very simple we do it the same way
275
00:19:19,090 --> 00:19:20,090
as our add subtract multiply instructions
we compute or r two which is essentially equal
276
00:19:20,090 --> 00:19:21,090
to r zero or r one so this is similar to r
zero or r one
277
00:19:21,090 --> 00:19:22,090
so let's move ahead and introduce some more
instructions that are required so let's discuss
278
00:19:22,090 --> 00:19:23,090
the shift operations so shift operations are
something like this that let's first discuss
279
00:19:23,090 --> 00:19:24,090
the left shift operation so consider the number
zero zero one zero see we left shift it by
280
00:19:24,090 --> 00:19:25,090
two which means that every bit moves two positions
to the left so in this case if zero zero one
281
00:19:25,090 --> 00:19:26,090
zero is being left shifted by two positions
so what will we have what we will essentially
282
00:19:26,090 --> 00:19:27,090
have is that the one will come over here zero
will come over here and the two new positions
283
00:19:27,090 --> 00:19:28,090
will be created in this case we just add two
more zeros so zero zero one zero left shifted
284
00:19:28,090 --> 00:19:29,090
by two is equal to one zero zero so we can
clearly see that this number is equal to two
285
00:19:29,090 --> 00:19:30,090
and this number is equal to eight in decimal
so left shifting by n positions basically
286
00:19:30,090 --> 00:19:31,090
means that it is the same as multiplying a
number by two to the power n ah
287
00:19:31,090 --> 00:19:32,090
so why is this the case so we shall see in
some time so let me just give one more example
288
00:19:32,090 --> 00:19:33,090
of left shifting let's assume that the number
is one zero one zero and we left shift it
289
00:19:33,090 --> 00:19:34,090
by ah let say one position right so we left
shift the number by shifted to the left by
290
00:19:34,090 --> 00:19:35,090
one position say in this case zero will come
over here one will come over here and a new
291
00:19:35,090 --> 00:19:36,090
position will be created we put a zero over
here assuming the number is remaining within
292
00:19:36,090 --> 00:19:37,090
this four bits say the reason that this is
equivalent to multiplying it by two to the
293
00:19:37,090 --> 00:19:38,090
power n is can be found out there are many
arguments so may be let's start with the couple
294
00:19:38,090 --> 00:19:39,090
of examples so let's take a look at this example
this number is two in decimal and this number
295
00:19:39,090 --> 00:19:40,090
is eight in decimal
so what we are essentially doing is that we
296
00:19:40,090 --> 00:19:41,090
have left if left shifted it by two positions
which is equivalent to multiplying this number
297
00:19:41,090 --> 00:19:42,090
by two square so the multiplication argument
is actually interesting and the reason that
298
00:19:42,090 --> 00:19:43,090
you know this holds is that any number in
a binary expansion can be thought of as we
299
00:19:43,090 --> 00:19:44,090
will so so let's start ah counting from one
because we in general we have done that in
300
00:19:44,090 --> 00:19:45,090
other places as well so assume i goes from
one to n any binary numbers can be expressed
301
00:19:45,090 --> 00:19:46,090
in this form
now if we let say left shift the number by
302
00:19:46,090 --> 00:19:47,090
you know k positions so what this number essentially
becomes right ah you know that's the most
303
00:19:47,090 --> 00:19:48,090
important thing either this number essentially
becomes right so basically the number remains
304
00:19:48,090 --> 00:19:49,090
the same and it is left shifted by k positions
ah so so what it essentially becomes is that
305
00:19:49,090 --> 00:19:50,090
ah it is pretty much the same number but each
of these coefficients if the coefficients
306
00:19:50,090 --> 00:19:51,090
remain exactly the same that the set of coefficient
set because it's getting shifted to the left
307
00:19:51,090 --> 00:19:52,090
but these exponents over here pretty much
get ah get multiplied get offset by a factors
308
00:19:52,090 --> 00:19:53,090
so actually what i can do over here is that
i can you know replace this again as as it
309
00:19:53,090 --> 00:19:54,090
was so ah so for those who followed the map
over here what we have done is that we have
310
00:19:54,090 --> 00:19:55,090
taken number and we have left shifted it by
k positions but we have not in not gotten
311
00:19:55,090 --> 00:19:56,090
rid off any digits per say then what we have
done is this is exactly the same as instead
312
00:19:56,090 --> 00:19:57,090
of ah so the first k positions are zero so
we don't count them then from k plus one till
313
00:19:57,090 --> 00:19:58,090
n plus k from k plus one till k plus n pretty
much we have the same set of coefficients
314
00:19:58,090 --> 00:19:59,090
which we had earlier and ah but the thing
is that the powers of two pretty much get
315
00:19:59,090 --> 00:20:00,090
shifted or get multiplied by two the power
k so what we do is that we sort of do a little
316
00:20:00,090 --> 00:20:01,090
bit of algebra and ah so we separate the two
to the power k from the equations and so what
317
00:20:01,090 --> 00:20:02,090
we are left with this that this becomes the
original number n multiplied by two to the
318
00:20:02,090 --> 00:20:03,090
power k so what we see is that this is a nice
way in unless there are overflows right we
319
00:20:03,090 --> 00:20:04,090
are exceeding the range of the number system
then it's a separate issue but it that's not
320
00:20:04,090 --> 00:20:05,090
happening this is a nice way of actually multiplying
a number by any power of two right ah multiplying
321
00:20:05,090 --> 00:20:06,090
a number by two to the power k
so let me may be consider let say one more
322
00:20:06,090 --> 00:20:07,090
for example consider zero zero one one we
then left shift it by one position so this
323
00:20:07,090 --> 00:20:08,090
becomes zero one one zero in decimal this
number is three and this number is six so
324
00:20:08,090 --> 00:20:09,090
this is equivalent to multiplying three with
i am sorry multiplying three with two to the
325
00:20:09,090 --> 00:20:10,090
power one so we get six all right so ah given
that left shifting actually works let us see
326
00:20:10,090 --> 00:20:11,090
if left shifting holds for you know negative
twos compliment numbers here again we assume
327
00:20:11,090 --> 00:20:12,090
that the there are no overflows ah so so let's
uh so ah instead of cluttering of this ah
328
00:20:12,090 --> 00:20:13,090
power point slide
let me actually move to another software so
329
00:20:13,090 --> 00:20:14,090
now let's see that whether left shifting actually
holds a negative numbers so in this case let's
330
00:20:14,090 --> 00:20:15,090
consider a negative number minus two in a
four bit ah system so minus two is one one
331
00:20:15,090 --> 00:20:16,090
zero fine so let us take this number and let
us left shift it by one position so then this
332
00:20:16,090 --> 00:20:17,090
becomes one one zero zero so one one zero
zero what is it is minus four so we see that
333
00:20:17,090 --> 00:20:18,090
it holds let's consider a one more example
minus three so minus three is representation
334
00:20:18,090 --> 00:20:19,090
in twos complement notation is actually ah
plus thirteen so which is one one zero one
335
00:20:19,090 --> 00:20:20,090
no problem now let's left shift it by one
position so then what do we get one zero one
336
00:20:20,090 --> 00:20:21,090
zero so what is this so this is eight plus
two is ten so in decimal it is ten minus sixteen
337
00:20:21,090 --> 00:20:22,090
the minus six so what we see is that left
shifting holds for even positive number as
338
00:20:22,090 --> 00:20:23,090
well as negative twos complement numbers so
now that we have discussed left shifting let
339
00:20:23,090 --> 00:20:24,090
us discuss what is right shifting
so let's go back to the slides and discuss
340
00:20:24,090 --> 00:20:25,090
uh what is right shifting so in right shifting
what we do is there are two kinds of right
341
00:20:25,090 --> 00:20:26,090
shifting ah there is ah arithmetic right shifting
and logical right shifting so in arithmetic
342
00:20:26,090 --> 00:20:27,090
right shifting which is represented by another
operator if we right shift this number by
343
00:20:27,090 --> 00:20:28,090
one position what we do is that all the bits
move one position to the right but the important
344
00:20:28,090 --> 00:20:29,090
point is the sign bit so the sign bit in this
case is replicated so the sign bit was originally
345
00:20:29,090 --> 00:20:30,090
zero we just replicate the sign bit and have
one more zero and in the sign bit was originally
346
00:20:30,090 --> 00:20:31,090
one we just replicate the sign bit and have
ah a one
347
00:20:31,090 --> 00:20:32,090
so what my claim is that right shifting is
similar to dividing a sign number by two to
348
00:20:32,090 --> 00:20:33,090
the power n and the proof will exactly be
the same as the way we proved for the left
349
00:20:33,090 --> 00:20:34,090
shift and is just that instead of two to the
power plus k it will become two to the power
350
00:20:34,090 --> 00:20:35,090
minus k but let's consider examples that's
will be lot of fun so let's again consider
351
00:20:35,090 --> 00:20:36,090
a four bit system now let's consider a zero
one zero zero so this is equal to four now
352
00:20:36,090 --> 00:20:37,090
let's right shift it by two positions we will
get zero zero zero one so this number was
353
00:20:37,090 --> 00:20:38,090
four and this number is one so we see that
division holds
354
00:20:38,090 --> 00:20:39,090
now let's consider negative numbers so let's
say consider minus six which is one zero one
355
00:20:39,090 --> 00:20:40,090
zero so let's right shift it by one position
in this case if we right shift it by one position
356
00:20:40,090 --> 00:20:41,090
this is how all the bits will actually move
so no problem let's write one zero one and
357
00:20:41,090 --> 00:20:42,090
we replicate the sign bit so this number is
eight plus four twelve plus one thirteen so
358
00:20:42,090 --> 00:20:43,090
in twos complement this is thirteen minus
sixteen or this number is minus three so we
359
00:20:43,090 --> 00:20:44,090
clearly see that right shifting a number by
n positions can be a positive number or can
360
00:20:44,090 --> 00:20:45,090
be a twos complement negative number is same
as dividing a sign number by two to the power
361
00:20:45,090 --> 00:20:46,090
n so ah a logical left shift and in arithmetic
right shift essentially are very useful operations
362
00:20:46,090 --> 00:20:47,090
and they help us in multiplying or dividing
numbers by two raise to the power n by any
363
00:20:47,090 --> 00:20:48,090
power of two all right
so ah now let's consider a logical shift right
364
00:20:48,090 --> 00:20:49,090
which is actually represented by the three
greater than operators right this the terminology
365
00:20:49,090 --> 00:20:50,090
has come from java so in this case the sign
bit is always set to zero so this is as same
366
00:20:50,090 --> 00:20:51,090
as dividing the unsigned representation by
two to the power n right that is it's connotation
367
00:20:51,090 --> 00:20:52,090
ah so in this case we just move all bits two
steps i mean n steps to the right and msbs
368
00:20:52,090 --> 00:20:53,090
are filled with zeros so the food for thought
question our here is that why don't we have
369
00:20:53,090 --> 00:20:54,090
an arithmetic shift left well the reason is
that the sign bit always gets over written
370
00:20:54,090 --> 00:20:55,090
a numbers from the left so there is no meaning
of an arithmetic shift left you only have
371
00:20:55,090 --> 00:20:56,090
one shift left but two shift rights
so we have a couple of simple risc instructions
372
00:20:56,090 --> 00:20:57,090
which we will ah you know three to be pre
size and their format is exactly exactly the
373
00:20:57,090 --> 00:20:58,090
same as the add sub multiply instructions
so the logical shift left instruction the
374
00:20:58,090 --> 00:20:59,090
destination comes first which is the register
the first source operand is a register and
375
00:20:59,090 --> 00:21:00,090
a second source operand can either be a register
or an immediate reg slash m write either a
376
00:21:00,090 --> 00:21:01,090
register or an immediate and the name of the
instruction is the lsl logical shift left
377
00:21:01,090 --> 00:21:02,090
similarly we have a logical shift right operator
which is called l s r exactly same ah notation
378
00:21:02,090 --> 00:21:03,090
and we have an a s r an arithmetic shift right
operator which is called a s r so what are
379
00:21:03,090 --> 00:21:04,090
the three new instructions that we are introducing
the three new instructions are l s l l s r
380
00:21:04,090 --> 00:21:05,090
and a s r logical shift left logical shift
right and arithmetic shift right so let's
381
00:21:05,090 --> 00:21:06,090
give some in a neat examples with shift based
instructions let's assume we want to compute
382
00:21:06,090 --> 00:21:07,090
hundred one one zero one multiplied with six
with just shift based operators so the first
383
00:21:07,090 --> 00:21:08,090
task is register assignments so we mov hundred
one to register r zero so we shift r zero
384
00:21:08,090 --> 00:21:09,090
by one position which is essentially equal
to r one is equal to hundred one multiplied
385
00:21:09,090 --> 00:21:10,090
with two because we are shifting it by one
position and in this case now then again we
386
00:21:10,090 --> 00:21:11,090
compute r two which is shifting hundred one
by two positions and multiplying it by four
387
00:21:11,090 --> 00:21:12,090
then we add r one plus r two and we set the
result equal to r three so essentially r three
388
00:21:12,090 --> 00:21:13,090
becomes hundred one multiplied by six so ah
you know skeptic would ask that why did we
389
00:21:13,090 --> 00:21:14,090
do this we should have simply loaded hundred
one into r zero and you know written an instruction
390
00:21:14,090 --> 00:21:15,090
of this form as simple as this we should taken
r zero multiplied it with six and save the
391
00:21:15,090 --> 00:21:16,090
result in r zero or r three or any other register
right why did we have to have two shifts here
392
00:21:16,090 --> 00:21:17,090
is the answer so typically in hardware multiplication
and division right multiplication and div
393
00:21:17,090 --> 00:21:18,090
division are very very expensive operations
expensive in the sense that they take a lot
394
00:21:18,090 --> 00:21:19,090
of time right so i can say you know plus plus
in terms of time right they take a lot of
395
00:21:19,090 --> 00:21:20,090
time
so lot of compilers actually you know lot
396
00:21:20,090 --> 00:21:21,090
of compilers and good programmers encourage
the user not to use very expensive multiplication
397
00:21:21,090 --> 00:21:22,090
division instructions and use shifts instead
which are very fast right so shift instructions
398
00:21:22,090 --> 00:21:23,090
are ultra fast so the shift instructions are
ultra ultra fast and they are extremely fast
399
00:21:23,090 --> 00:21:24,090
so ah that's the reason it is a good idea
to use shift instructions in a place of expansion
400
00:21:24,090 --> 00:21:25,090
multiplication and division instructions wherever
it is possible
401
00:21:25,090 --> 00:21:26,090
so let's consider one more example which is
not that difficult once you know the answer
402
00:21:26,090 --> 00:21:27,090
so let's compute hundred two times seven point
five with the help of shift operators so what
403
00:21:27,090 --> 00:21:28,090
we can do the first thing is assign hundred
two to a register r zero so then we shift
404
00:21:28,090 --> 00:21:29,090
it to the left by three positions which means
r one is equal to hundred two multiplied by
405
00:21:29,090 --> 00:21:30,090
two to the power three which is eight and
then we shift it to the right right so since
406
00:21:30,090 --> 00:21:31,090
hundred two is positive logical and arithmetic
shift are the same so we shift it to the right
407
00:21:31,090 --> 00:21:32,090
by one position which means multiply hundred
two with point five well then it is very straight
408
00:21:32,090 --> 00:21:33,090
forward we subtract r one minus r two so r
three becomes equal to well this is exactly
409
00:21:33,090 --> 00:21:34,090
what we wanted to do so you see this is very
easy and instead of we have avoided the costly
410
00:21:34,090 --> 00:21:35,090
expensive time consuming division instruction
and in the place of that what we have done
411
00:21:35,090 --> 00:21:36,090
is that we have a very very nimble and efficient
solution with shift instructions ok
412
00:21:36,090 --> 00:21:37,090
now let's introduce some of the real the instructions
that help us write big and scalable programs
413
00:21:37,090 --> 00:21:38,090
so let's intros introduce load store instruction
so load store instructions job it's essentially
414
00:21:38,090 --> 00:21:39,090
a load instructions job is to take a value
from memory and bring it into a register so
415
00:21:39,090 --> 00:21:40,090
this is a load instruction and a store instruction
job is to take a value from a register and
416
00:21:40,090 --> 00:21:41,090
put it in memory right so what does a load
instruction do it takes a piece of data from
417
00:21:41,090 --> 00:21:42,090
memory and puts it in a register and similarly
a store instruction takes data from a register
418
00:21:42,090 --> 00:21:43,090
and puts it in memory
so let's take a look at the format of a load
419
00:21:43,090 --> 00:21:44,090
instructions so in a in a load instruction
the register which needs to be loaded or is
420
00:21:44,090 --> 00:21:45,090
a destination right so this is the destination
right in this two address format the first
421
00:21:45,090 --> 00:21:46,090
is the destination and the other uses base
index ah i am sorry not base index ah it is
422
00:21:46,090 --> 00:21:47,090
base offset addressing yeah it should be base
offset ah base offset ah addressing ah so
423
00:21:47,090 --> 00:21:48,090
in this case what we do is that the base address
is specified in register r two so we take
424
00:21:48,090 --> 00:21:49,090
register r two we add ten to it we get a new
memory address we access the memory using
425
00:21:49,090 --> 00:21:50,090
this address read the contents and put it
in r one ah so in this two address format
426
00:21:50,090 --> 00:21:51,090
instruction the second operand is always ah
points to a certain memory location a value
427
00:21:51,090 --> 00:21:52,090
and a memory location the hardwares job is
to read this value ah that is there in the
428
00:21:52,090 --> 00:21:53,090
memory location to read this value and put
it in a register
429
00:21:53,090 --> 00:21:54,090
so store instruction has exactly the same
format right but the store instruction if
430
00:21:54,090 --> 00:21:55,090
you think about it does not actually have
a destination the destination is memory it
431
00:21:55,090 --> 00:21:56,090
does not have a register destination so what
the store instruction does is that the register
432
00:21:56,090 --> 00:21:57,090
that needs to be read right the register that
is actually read is specified first and then
433
00:21:57,090 --> 00:21:58,090
the memory location is specified after this
using exactly same addressing mode or base
434
00:21:58,090 --> 00:21:59,090
offset addressing mode
so let's show this thing graphically so graphically
435
00:21:59,090 --> 00:22:00,090
what we do is that ah in the case of ah load
ah we take the value of r two ah one second
436
00:22:00,090 --> 00:22:01,090
we take the value in of r two from the register
file we add ten we get the address this address
437
00:22:01,090 --> 00:22:02,090
is used to access memory in a thirty two bit
system we read four bytes from memory which
438
00:22:02,090 --> 00:22:03,090
is the data the data comes back and it is
fed into the register r one so instead of
439
00:22:03,090 --> 00:22:04,090
four bytes it can be in a sixty four bit system
it can be eight bytes that doesn't matter
440
00:22:04,090 --> 00:22:05,090
so in this case in a thirty two bit system
we read four bytes from memory and put it
441
00:22:05,090 --> 00:22:06,090
in the register so store actually does the
reverse so in this case also we compute the
442
00:22:06,090 --> 00:22:07,090
address but instead of the flow of the data
being from memory to a register the flow of
443
00:22:07,090 --> 00:22:08,090
data is actually reverse it is from a register
to memory
444
00:22:08,090 --> 00:22:09,090
so let's give an example of a load store list
ah so let's consider an example with arrays
445
00:22:09,090 --> 00:22:10,090
ah so let's assume that ah an arrays base
address is stored in r zero so the way that
446
00:22:10,090 --> 00:22:11,090
an array would actually look like in memory
is something like this that an array with
447
00:22:11,090 --> 00:22:12,090
ten integers where one integer is four bytes
is a contiguous region of forty bytes so in
448
00:22:12,090 --> 00:22:13,090
this let's assume that the base address of
the array is r zero which means the first
449
00:22:13,090 --> 00:22:14,090
integer is stored in the locations that r
zero so let's assume that the contents of
450
00:22:14,090 --> 00:22:15,090
r zero are v so the first integer is stored
in the locations v two v plus three the second
451
00:22:15,090 --> 00:22:16,090
integer is stored in the location v plus four
v plus seven and so on
452
00:22:16,090 --> 00:22:17,090
so we can say that may be the starting address
of the array in this case array zeros starting
453
00:22:17,090 --> 00:22:18,090
address is actually v the starting address
of the first entry is v plus four and so on
454
00:22:18,090 --> 00:22:19,090
so similarly the starting address of the ninth
entry the ninth and the last entry in the
455
00:22:19,090 --> 00:22:20,090
array where we start counting from zero is
essentially v plus thirty six so given the
456
00:22:20,090 --> 00:22:21,090
simple argument which i am which i am sure
most of you would have learnt in your basic
457
00:22:21,090 --> 00:22:22,090
c or c plus plus or java programming class
let's try to implement this small program
458
00:22:22,090 --> 00:22:23,090
in assembly language
so we have an array of ten integers where
459
00:22:23,090 --> 00:22:24,090
we have assume the base of the array is saved
in register r zero we set the third element
460
00:22:24,090 --> 00:22:25,090
of five ah array three now the element at
index three to five the element at index four
461
00:22:25,090 --> 00:22:26,090
eight and we set ah r five is equal to r four
plus r three so all right so now let's ah
462
00:22:26,090 --> 00:22:27,090
do some amount of register assignments so
let's move the constraint five to r one and
463
00:22:27,090 --> 00:22:28,090
let's save the value r one in actually the
location array three
464
00:22:28,090 --> 00:22:29,090
so what is this starting address of array
three well the starting address of array three
465
00:22:29,090 --> 00:22:30,090
is pretty much r zero plus twelve the reason
being ah let's go back to this discussion
466
00:22:30,090 --> 00:22:31,090
over here ah so the starting address of array
zero is the contents of r zero starting address
467
00:22:31,090 --> 00:22:32,090
of r one is contents of r zero plus four similarly
starting address of array n is r zero plus
468
00:22:32,090 --> 00:22:33,090
four times n minus one ah sorry r zero plus
four times n so in this case it's r zero plus
469
00:22:33,090 --> 00:22:34,090
twelve so we write it as twelve r zero so
this is the very very important point to understand
470
00:22:34,090 --> 00:22:35,090
and a i want to ensure that all of you have
ah more or less understood it
471
00:22:35,090 --> 00:22:36,090
so ah the idea is that what ah what is the
contents of r zero r zero pretty much points
472
00:22:36,090 --> 00:22:37,090
to a memory address in memory that contains
forty contiguous bytes so the first four bytes
473
00:22:37,090 --> 00:22:38,090
r r zero the next four bytes rr one and so
on so what is the starting address of r zero
474
00:22:38,090 --> 00:22:39,090
it is ah ah of array is zero so starting address
of array zero is r zero right let's say the
475
00:22:39,090 --> 00:22:40,090
starting address yeah let's starting address
of array one r one is r zero plus four four
476
00:22:40,090 --> 00:22:41,090
bytes if the size of one integer is four bytes
similarly the starting address of r n is r
477
00:22:41,090 --> 00:22:42,090
zero plus four m so that's the reason the
starting address of r three is twelve r zero
478
00:22:42,090 --> 00:22:43,090
so we saved the values so we do the same so
we do what we have done here we do the same
479
00:22:43,090 --> 00:22:44,090
for r four where we save eight and r two and
then we store the value of r two in sixteen
480
00:22:44,090 --> 00:22:45,090
r zero which is essentially the same as setting
r four array four is equal to eight finally
481
00:22:45,090 --> 00:22:46,090
we add r one plus r two ah because they contain
the values that were stored and we save it
482
00:22:46,090 --> 00:22:47,090
in r three and the value of r three is again
saved in twenty ah twenty is the offset and
483
00:22:47,090 --> 00:22:48,090
r zero is the base so this is essentially
equal to the fifth memory address of the fifth
484
00:22:48,090 --> 00:22:49,090
element so here we save r three which is r
one plus r two or in a sense the same as so
485
00:22:49,090 --> 00:22:50,090
by this time the advantage of using of base
offset addressing mode should be clear that
486
00:22:50,090 --> 00:22:51,090
it allows us to implement arrays
so what we can do is that we can save the
487
00:22:51,090 --> 00:22:52,090
base address in an array and then we can use
the offset to actually access different elements
488
00:22:52,090 --> 00:22:53,090
of the array right so we have the always need
to be mind full of the size of an element
489
00:22:53,090 --> 00:22:54,090
so in this case the element is an integer
and the size of each element is four bytes
490
00:22:54,090 --> 00:22:55,090
that's the reason to go to the n th element
where we start counting from zero is the starting
491
00:22:55,090 --> 00:22:56,090
address plus four m so once that is clear
we can write programs of this type to load
492
00:22:56,090 --> 00:22:57,090
and store values from memory from you know
different data structures in memory the array
493
00:22:57,090 --> 00:22:58,090
being one to read them into registers work
on them and write them back
494
00:22:58,090 --> 00:22:59,090
now it's time to actually get to the real
fun part of assembly instructions and so what
495
00:22:59,090 --> 00:23:00,090
have we covered up till now we have covered
arithmetic logical instructions and we have
496
00:23:00,090 --> 00:23:01,090
covered ah load store instructions right so
it is now ah you know the apt time to actually
497
00:23:01,090 --> 00:23:02,090
get into the real fun part of assembly programming
which is to add branching when we add branching
498
00:23:02,090 --> 00:23:03,090
we will be able to implement high level concepts
such as if statements for loops while loops
499
00:23:03,090 --> 00:23:04,090
and so on so as i have mentioned in some of
the early slides that an assembly statement
500
00:23:04,090 --> 00:23:05,090
can be associated with a label and this label
ah can be used to uniquely indicate a assembly
501
00:23:05,090 --> 00:23:06,090
statement
what we can later on do is that we can have
502
00:23:06,090 --> 00:23:07,090
this statement b dot foo where the branch
statement ah where the branch statement would
503
00:23:07,090 --> 00:23:08,090
essentially transfer the control of the program
from this particular line over here to this
504
00:23:08,090 --> 00:23:09,090
line here so the program counter would initially
be pointing to this line and subsequently
505
00:23:09,090 --> 00:23:10,090
it will start pointing to this line and this
statement will get executed so the branch
506
00:23:10,090 --> 00:23:11,090
statement in assembly is very simple we just
do b and then the name of the label so the
507
00:23:11,090 --> 00:23:12,090
format in this case is b and the name of the
label so there are some conditional branch
508
00:23:12,090 --> 00:23:13,090
instructions as well where we will actually
see the power of having the flags ah register
509
00:23:13,090 --> 00:23:14,090
right
so let's have a beq instruction branch if
510
00:23:14,090 --> 00:23:15,090
equal so what this means is that look we have
first introduced we first have a compare instruction
511
00:23:15,090 --> 00:23:16,090
we have many more instructions that are not
compares and then we have a beq instruction
512
00:23:16,090 --> 00:23:17,090
so this instruction first takes look at a
flags dot e if flags dot e is equal to one
513
00:23:17,090 --> 00:23:18,090
means that this compare over here had resulted
in an equality it will go jump to foo very
514
00:23:18,090 --> 00:23:19,090
much branch to foo the same is true with the
b g t instruction which takes a look at flags
515
00:23:19,090 --> 00:23:20,090
dot g t and if flags dot g t had been set
to one by the last compare instruction the
516
00:23:20,090 --> 00:23:21,090
b g t instruction would jump or branch to
dot foo essentially the statement which is
517
00:23:21,090 --> 00:23:22,090
there at the dot foo label and these flags
mind your only set by compare instructions
518
00:23:22,090 --> 00:23:23,090
and who are they meant for they are meant
for later branch instructions like b e q and
519
00:23:23,090 --> 00:23:24,090
b g t to take a look at them and jump all
right
520
00:23:24,090 --> 00:23:25,090
so now it is time to write slightly difficult
programs and realize the power of assembly
521
00:23:25,090 --> 00:23:26,090
language so let's try to write this program
if r one is greater than r two then let's
522
00:23:26,090 --> 00:23:27,090
say four in r three else we say five in r
three so well that's easy the first thing
523
00:23:27,090 --> 00:23:28,090
that we do is that we compare r one and r
two so if r one is greater than r two then
524
00:23:28,090 --> 00:23:29,090
ah so we always come one statement down if
it's greater than r two the flags dot g t
525
00:23:29,090 --> 00:23:30,090
ah bit would be set to one so if it is greater
than we jump to g t label which is over here
526
00:23:30,090 --> 00:23:31,090
and we save four in r three otherwise we don't
jump and by default we come to the next statement
527
00:23:31,090 --> 00:23:32,090
where we save five in r three so this is a
simple program which shows us the power of
528
00:23:32,090 --> 00:23:33,090
the b g t instruction that if that element
condition is true in the flags register then
529
00:23:33,090 --> 00:23:34,090
we jump to the target in this case the target
is dot g t label otherwise we just go to the
530
00:23:34,090 --> 00:23:35,090
next or the subsequent instruction
now let's move on to a more complicated example
531
00:23:35,090 --> 00:23:36,090
and this is a genuine assembly program in
the sense it has it's most of it's features
532
00:23:36,090 --> 00:23:37,090
so let's compute the factorial of the variable
called num right ah so which is as specified
533
00:23:37,090 --> 00:23:38,090
in the c program over here so to ah write
a program in assembly language let us may
534
00:23:38,090 --> 00:23:39,090
be add a step zero if the program is complicated
it might actually make a lot of sense to first
535
00:23:39,090 --> 00:23:40,090
write it in a high level language such a such
as c or java the reason for that is that it
536
00:23:40,090 --> 00:23:41,090
will help us in a visualized all the parts
and it will become fairly easy for us to actually
537
00:23:41,090 --> 00:23:42,090
code it so ah we will follow this approach
over here
538
00:23:42,090 --> 00:23:43,090
so to compute a factorial first we define
the product variable which is ini initialized
539
00:23:43,090 --> 00:23:44,090
to one then we define an index that starts
at num and we assume you know for all practical
540
00:23:44,090 --> 00:23:45,090
purposes num is a large enough number so we
don't have to check for special cases so here
541
00:23:45,090 --> 00:23:46,090
if i d x is equal to we start the index for
loop equal to num and we keep going till num
542
00:23:46,090 --> 00:23:47,090
reaches one and one the moment reaches one
we jump out of the loop and we subtract the
543
00:23:47,090 --> 00:23:48,090
index i am sorry we keep going till i d x
reaches one and we subtract the index by one
544
00:23:48,090 --> 00:23:49,090
in each step and this is the multiplicative
step prod equals prod multiplied by i d x
545
00:23:49,090 --> 00:23:50,090
so this code is enough to compute the factorial
of a number so recall that the factorial of
546
00:23:50,090 --> 00:23:51,090
a number n factorial is one multiplied by
two all the way till n right
547
00:23:51,090 --> 00:23:52,090
so let us now to conve[rt]- let us now try
to convert this program to simple risc so
548
00:23:52,090 --> 00:23:53,090
in this case the first thing that we do step
one is register assignment so r one is assigned
549
00:23:53,090 --> 00:23:54,090
to prod and ah so we set prod equal to one
and ah we set ah so this is i d x so r two
550
00:23:54,090 --> 00:23:55,090
is assigned to i d x and we initialize it
with num so ah as the assumption here is that
551
00:23:55,090 --> 00:23:56,090
num is assigned to r zero so r zero contains
num so that is the assumption that is the
552
00:23:56,090 --> 00:23:57,090
assumption that we begin with so sometimes
if assumption is not specified in the question
553
00:23:57,090 --> 00:23:58,090
users can make their assumption as we have
done over here that ah the num variables whose
554
00:23:58,090 --> 00:23:59,090
factorial needs to be computed is there in
r zero and ah so what we do is that ah the
555
00:23:59,090 --> 00:24:00,090
variable i d x in the c program we assign
it to r two and we initialize we set r two
556
00:24:00,090 --> 00:24:01,090
equal to r zero where r zero contains num
so essentially the starting value of i d x
557
00:24:01,090 --> 00:24:02,090
is equal to num subsequently we add the dot
loop label over here to signify the fact that
558
00:24:02,090 --> 00:24:03,090
later on we might want to jump to this point
then what we do is that we multiply r one
559
00:24:03,090 --> 00:24:04,090
with r two recall that r one is mapped to
prod r two is mapped to i d x so we set r
560
00:24:04,090 --> 00:24:05,090
one equal to r one times r two which is equivalent
to prod equals the product is equal to product
561
00:24:05,090 --> 00:24:06,090
times i d x so this is the multiplicative
step then what we do is that we subtract the
562
00:24:06,090 --> 00:24:07,090
index we subtract one from the index which
is essentially this step over here we subtract
563
00:24:07,090 --> 00:24:08,090
one from the index i d x equals i d x minus
one
564
00:24:08,090 --> 00:24:09,090
so when do we actually stop the loop we stop
the loop when the index becomes equal to one
565
00:24:09,090 --> 00:24:10,090
because there is no point multiplying a number
by one so we compare r two with one all right
566
00:24:10,090 --> 00:24:11,090
so we compare r two with one in this step
and we see if r two contains i d x if i d
567
00:24:11,090 --> 00:24:12,090
x is greater than one or not if i d x is greater
than one then the flags dot g t bit would
568
00:24:12,090 --> 00:24:13,090
have been saved so in this case the b g t
condition would evaluate to true and we would
569
00:24:13,090 --> 00:24:14,090
jump to loop which is essentially we will
jump back like this
570
00:24:14,090 --> 00:24:15,090
so what we have done we have actually these
six lines i would advice the reader or the
571
00:24:15,090 --> 00:24:16,090
student the listener of this video to take
a look at this example five times ten times
572
00:24:16,090 --> 00:24:17,090
hundred times if required and understand each
and every line of this program because the
573
00:24:17,090 --> 00:24:18,090
program might be six lines but it is ten to
the power six times difficult it's difficult
574
00:24:18,090 --> 00:24:19,090
basically because you are looking at an assembly
program for the first time and an assembly
575
00:24:19,090 --> 00:24:20,090
program which is fairly complicated for the
first time so let me look at the three steps
576
00:24:20,090 --> 00:24:21,090
that we ah had in this exercise
the first step is we realize that the problem
577
00:24:21,090 --> 00:24:22,090
is difficult so we wrote our small program
in c that's the first thing we did in the
578
00:24:22,090 --> 00:24:23,090
program in c we had a product variable and
an index that goes down from the number till
579
00:24:23,090 --> 00:24:24,090
one and at each point we multiplied so we
are doing exactly the same in assembly nothing
580
00:24:24,090 --> 00:24:25,090
different so the first is that we do a register
assignment so num is assigned to r zero prod
581
00:24:25,090 --> 00:24:26,090
to r one i d x to r two we start the iteration
the same way as it's in the for loop with
582
00:24:26,090 --> 00:24:27,090
i d x equal to num in pretty much this statement
over here so then we do the multiplicative
583
00:24:27,090 --> 00:24:28,090
step we multiply and as i said am making some
simplistic assumptions over that ah num is
584
00:24:28,090 --> 00:24:29,090
actually large enough
so i am not doing some checks ah so then ah
585
00:24:29,090 --> 00:24:30,090
the next step is once you do the multiply
let's reduce the index by one so let's have
586
00:24:30,090 --> 00:24:31,090
i d x equals i d x minus one with a subtract
instruction and then we compare so if you
587
00:24:31,090 --> 00:24:32,090
have reached the exit condition which is if
r two is equal to one then we can exit the
588
00:24:32,090 --> 00:24:33,090
loop so we don't have to anything that b g
t instruction over here will evaluate to falls
589
00:24:33,090 --> 00:24:34,090
so we will just simply fall down and evaluate
the next instruction otherwise if the index
590
00:24:34,090 --> 00:24:35,090
is still not equal to one we need to do another
iteration of the loop so we will jump to dot
591
00:24:35,090 --> 00:24:36,090
loop
so similarly the loop will continue for num
592
00:24:36,090 --> 00:24:37,090
minus one times till the factorial computation
is over so i would request once again the
593
00:24:37,090 --> 00:24:38,090
reader to to take a look at this several times
may be work it out with the paper and pencil
594
00:24:38,090 --> 00:24:39,090
and only when when the reader is convinced
we actually move to slide number fifty four
595
00:24:39,090 --> 00:24:40,090
so here is a food thought for burger question
write a simple risc assembly program to actually
596
00:24:40,090 --> 00:24:41,090
find the ramanujan numbers this is difficult
but can be done ah it's a it's a fair amount
597
00:24:41,090 --> 00:24:42,090
of work but with the emulators that are there
on the websites can be done so the ramanujan
598
00:24:42,090 --> 00:24:43,090
number is a very interesting number and ah
so basically the story goes like this that
599
00:24:43,090 --> 00:24:44,090
once ah srinivas ramanujan was very sick and
his guide ah personally had come to meet him
600
00:24:44,090 --> 00:24:45,090
so number one seven two nine was written on
his taxi so he asked ramanujan do you know
601
00:24:45,090 --> 00:24:46,090
what is the significance of this number right
can you find something special with this number
602
00:24:46,090 --> 00:24:47,090
ramanujan brilliant as he was said yes so
one seven two nine is actually the smallest
603
00:24:47,090 --> 00:24:48,090
number that is a sum of two cubes in two different
ways it is ten cube plus nine cube which is
604
00:24:48,090 --> 00:24:49,090
also twelve cube plus one cube so it is a
sum of two cubes in two different ways so
605
00:24:49,090 --> 00:24:50,090
the question was can you write an assembly
program to actually compute the ramanujan
606
00:24:50,090 --> 00:24:51,090
number well the answer is very simple first
write the program in c after writing the program
607
00:24:51,090 --> 00:24:52,090
in c gradually convert it to assembly
so the approach would be that we take all
608
00:24:52,090 --> 00:24:53,090
the numbers starting from let's say two till
you know infinity right and we stop at the
609
00:24:53,090 --> 00:24:54,090
ramanujan number for two you essentially find
out is it possible to write it as sum of cubes
610
00:24:54,090 --> 00:24:55,090
well yes one cube plus one cube but any other
combination of cubes no so you go to the next
611
00:24:55,090 --> 00:24:56,090
go to the next keep going going going you
will have multiple for loops but the trick
612
00:24:56,090 --> 00:24:57,090
is first write and see then do a register
assignment and then write it in assembly language
613
00:24:57,090 --> 00:24:58,090
that's a trick if you are able to do this
our objectives are satisfied
614
00:24:58,090 --> 00:24:59,090
now let's discuss our last important concept
in this sub section of the lecture called
615
00:24:59,090 --> 00:25:00,090
modifiers so if you would recall we had discussed
that simple risc support sixteen bit ah immediates
616
00:25:00,090 --> 00:25:01,090
so it does not support larger ah immediates
so the question is that let's say i have an
617
00:25:01,090 --> 00:25:02,090
instruction of the form mov r zero and minus
three so what is it ah that would happen so
618
00:25:02,090 --> 00:25:03,090
if we consider what is minus three in a four
bit system minus three in a four bit system
619
00:25:03,090 --> 00:25:04,090
is plus thirteen or one one zero one what
is minus three in a sixteen bit system nothing
620
00:25:04,090 --> 00:25:05,090
just perform sign extension it remains the
same
621
00:25:05,090 --> 00:25:06,090
so now the question is that when this is being
moved to r zero which is actually a thirty
622
00:25:06,090 --> 00:25:07,090
two bit register ah what should be the default
behavior if i do this what should be the default
623
00:25:07,090 --> 00:25:08,090
behavior is that we have the contents of the
immediate mov to the lower sixteen bits and
624
00:25:08,090 --> 00:25:09,090
the upper sixteen bits is just a simple sign
extension so it preserves the sign of the
625
00:25:09,090 --> 00:25:10,090
immediate so this is the default behavior
which all of us ex[pect]- expect should be
626
00:25:10,090 --> 00:25:11,090
happening that is the sign extension will
happen so essentially we are treating the
627
00:25:11,090 --> 00:25:12,090
sixteen bit immediate as a sign number and
there is automatic sign extension happening
628
00:25:12,090 --> 00:25:13,090
so this ensures that if we have represented
minus three in ah in assembly minus three
629
00:25:13,090 --> 00:25:14,090
is also what gets stored in the register r
zero but of course since r zero is thirty
630
00:25:14,090 --> 00:25:15,090
two bits relevant sign extension is being
done
631
00:25:15,090 --> 00:25:16,090
now let us define two variants of this same
instruction and these variants hold for other
632
00:25:16,090 --> 00:25:17,090
instructions as well but let's only discuss
this instruction so let us define the mov
633
00:25:17,090 --> 00:25:18,090
u instruction so in the mov u instruction
we treat the sixteen bit immediate as an unsigned
634
00:25:18,090 --> 00:25:19,090
number so essentially what happens is that
in the thirty two bit field ah the first sixteen
635
00:25:19,090 --> 00:25:20,090
bits is when the is where the immediate comes
in and the remaining sixteen bits are all
636
00:25:20,090 --> 00:25:21,090
zeros so we treated as an unsigned number
and in mov h what is done ah it's actually
637
00:25:21,090 --> 00:25:22,090
have ah one graph over here one diagram over
here
638
00:25:22,090 --> 00:25:23,090
but let me na never the less explain it over
here so mov h what is typically done is that
639
00:25:23,090 --> 00:25:24,090
again if we consider a thirty two bit register
with two sixteen bit parts so what happens
640
00:25:24,090 --> 00:25:25,090
is that the immediate actually gets loaded
to the upper sixteen bits the most significant
641
00:25:25,090 --> 00:25:26,090
sixteen bits and the lower sixteen bits are
set to all zeros so you can think of left
642
00:25:26,090 --> 00:25:27,090
shifting the immediate and then loading it
to the upper sixteen to the lower sixteen
643
00:25:27,090 --> 00:25:28,090
are all zeros so the u and h are generic modifiers
so the default is always to treat the immediate
644
00:25:28,090 --> 00:25:29,090
as a fine number but or ah we can ah use the
u and h modifiers so the u treats it as an
645
00:25:29,090 --> 00:25:30,090
unsigned number and the h actually left shifts
the immediate by sixteen positions
646
00:25:30,090 --> 00:25:31,090
so what is the mechanism the mechanism is
that when an immediate is specified in an
647
00:25:31,090 --> 00:25:32,090
assembly instruction the processor will take
the immediate and put it in an internal register
648
00:25:32,090 --> 00:25:33,090
which is not visible and convert it into a
thirty two bit number right so essentially
649
00:25:33,090 --> 00:25:34,090
the immediate is part of the assembly instructions
it's somewhere it's somewhere inside this
650
00:25:34,090 --> 00:25:35,090
immediate in taken by the cpu and it is converted
to a thirty two bit immediate right and this
651
00:25:35,090 --> 00:25:36,090
thirty two bit immediate is used in the computations
so for all arithmetic logical computations
652
00:25:36,090 --> 00:25:37,090
this thirty two bit immediate would be used
so we can control the generation of this thirty
653
00:25:37,090 --> 00:25:38,090
two bit number internally so the default is
sign extension that we can treat the sixteen
654
00:25:38,090 --> 00:25:39,090
bit number ah as unsigned with the u suffix
or load it in the upper two bytes right the
655
00:25:39,090 --> 00:25:40,090
most significant two bytes with the x suffix
so let's take a look at some examples ah so
656
00:25:40,090 --> 00:25:41,090
let's first take a look at the default behavior
of a b one two whether the msb is equal to
657
00:25:41,090 --> 00:25:42,090
one so in this case we load a b one two in
the lower two bytes and the upper bytes upper
658
00:25:42,090 --> 00:25:43,090
two bytes consist of the sign bit right so
this is sign bit here is one so all these
659
00:25:43,090 --> 00:25:44,090
two sixteen bits are one
if we consider the unsigned so then again
660
00:25:44,090 --> 00:25:45,090
the byte a b is loaded into this ah byte and
ah so a b and one two are loaded into lower
661
00:25:45,090 --> 00:25:46,090
two bytes but the upper two bytes are set
to zeros right eight zeros and eight ah eight
662
00:25:46,090 --> 00:25:47,090
zeros sixteen zeros if we consider ah the
same thing mov h r one zero x a b one two
663
00:25:47,090 --> 00:25:48,090
so a b one two are actually move to the two
m b byte positions upper byte positions and
664
00:25:48,090 --> 00:25:49,090
the lower positions are set to zero
so let's take a look at some examples so the
665
00:25:49,090 --> 00:25:50,090
main advantage of having modifiers right why
have them the main advantage of having modifiers
666
00:25:50,090 --> 00:25:51,090
is that it is possible to load all kinds of
constants into registers we will load meaning
667
00:25:51,090 --> 00:25:52,090
not from memory but you know sort of store
immediates and registers all kinds of immediates
668
00:25:52,090 --> 00:25:53,090
and registers with a minimal number of instructions
for example if we want to save f f f f a three
669
00:25:53,090 --> 00:25:54,090
two b and r zero all that we do is that we
actually move in a three two b into r zero
670
00:25:54,090 --> 00:25:55,090
so it will load a three two b in a lower positions
and upper positions will replicate the sign
671
00:25:55,090 --> 00:25:56,090
bit since the sign bit is one the upper two
bytes will become f f and f f if we want to
672
00:25:56,090 --> 00:25:57,090
have set the upper two bytes as all zeros
right in this case all that we need to do
673
00:25:57,090 --> 00:25:58,090
is we need to have the u modifier which will
ensure that the most significant two bytes
674
00:25:58,090 --> 00:25:59,090
are zeros
likewise if we want a three and two b to be
675
00:25:59,090 --> 00:26:00,090
in the two upper positions most significant
positions then ah what we do is that we will
676
00:26:00,090 --> 00:26:01,090
use the h modifier to move them to the most
significant positions and set the rest of
677
00:26:01,090 --> 00:26:02,090
the bits or bytes as zero so now let's look
at our simple example let's assume that this
678
00:26:02,090 --> 00:26:03,090
complicated constant over here ah needs to
be ah loaded sorry i mean saved into r zero
679
00:26:03,090 --> 00:26:04,090
so this if we dint have the modifier it will
actually take us a lot of instructions it
680
00:26:04,090 --> 00:26:05,090
is complicated
so let us now break down this problem into
681
00:26:05,090 --> 00:26:06,090
two simple problems and solve the problems
in parts so the first problem is that we need
682
00:26:06,090 --> 00:26:07,090
to take one two a b and load them in the two
upper bytes that's very easy so we consider
683
00:26:07,090 --> 00:26:08,090
zero x one two a b right in the hex form we
use the h modifier to actually move this the
684
00:26:08,090 --> 00:26:09,090
two upper bytes then we need to move a nine
two d to the lower bytes so this is also easy
685
00:26:09,090 --> 00:26:10,090
so basically what do we have what is the status
of the register at the moment just before
686
00:26:10,090 --> 00:26:11,090
the second instruction we have one two and
a b and then we have zero zero and zero zero
687
00:26:11,090 --> 00:26:12,090
in the two lower bytes
so what we can do is that we can add this
688
00:26:12,090 --> 00:26:13,090
with zero zero a nine and two d so this will
give us the final result which is this result
689
00:26:13,090 --> 00:26:14,090
so that's the reason we have an add instruction
we have r zero and we have a nine two d but
690
00:26:14,090 --> 00:26:15,090
here is the cache if a nine two d goes into
the machine it will be expanded into a thirty
691
00:26:15,090 --> 00:26:16,090
two bit value and in the thirty two bit value
the lower two will be a nine two d the lower
692
00:26:16,090 --> 00:26:17,090
two bytes but the upper bytes will just replicate
the sign of a which is f f f f which is not
693
00:26:17,090 --> 00:26:18,090
something we want to do so that's the reason
we specify the u modifier with the add instruction
694
00:26:18,090 --> 00:26:19,090
so essentially the modifiers can be used with
any arithmetic logical instructions and the
695
00:26:19,090 --> 00:26:20,090
move instruction right so let me say this
again the u and h modifiers can be used with
696
00:26:20,090 --> 00:26:21,090
any instruction that uses immediates namely
the arithmetic logical instructions and the
697
00:26:21,090 --> 00:26:22,090
move instruction so in this case in the second
instructions this is instruction one and it
698
00:26:22,090 --> 00:26:23,090
is instruction two so in the second instruction
we use the u modifier to actually expand a
699
00:26:23,090 --> 00:26:24,090
nine two d internally into a nine two d and
the upper two bytes are zero zero and zero
700
00:26:24,090 --> 00:26:25,090
zero which is exactly what we want right have
we not given the modifier we would have gotten
701
00:26:25,090 --> 00:26:26,090
the wrong answer to get the right answer ah
we add the u modifier to ensure that these
702
00:26:26,090 --> 00:26:27,090
two upper bytes are zero and zero and then
we do the addition so we get one two a b a
703
00:26:27,090 --> 00:26:28,090
nine and two d which is exactly the answer
that we wanted
704
00:26:28,090 --> 00:26:29,090
so one thing that this example shows so what
is the take home point of this example the
705
00:26:29,090 --> 00:26:30,090
take home point of this example is that to
load a constant i am sorry i use the word
706
00:26:30,090 --> 00:26:31,090
load but the word should be taken in the context
in the connotation if you want to save an
707
00:26:31,090 --> 00:26:32,090
immediate into a register say it would have
otherwise been difficult had the modifiers
708
00:26:32,090 --> 00:26:33,090
not been there to actually put a constant
in to a register given r isa but with the
709
00:26:33,090 --> 00:26:34,090
modifiers it is actually become easy so we
use the h and u modifiers and with just two
710
00:26:34,090 --> 00:26:34,106
instructions we are able to put in thirty
two bits into a register which is great