#1 How computers count? - YouTube

Channel: unknown

[0]
welcome to the embedded systems
[2]
programming course my name is Miro Samek
[4]
and in this lesson I'm going to show you
[6]
how computers count
[10]
here and throughout this course I'm
[13]
going to use the free evaluation version
[15]
of the IAR toolset that we have
[17]
downloaded and installed in lesson zero
[19]
to create your first project launch I re
[23]
w armed and select project create new
[25]
project menu expand the C project types
[28]
select main and click OK
[32]
this opens the file explorer where you
[35]
need to choose the directory for your
[37]
project I recommend that you create the
[40]
embedded programming directory on your C
[42]
Drive for the entire course within which
[45]
you will add sub directories for each
[47]
lesson to do this click on the C Drive
[49]
and then click on new folder next double
[60]
click on the newly created folder and
[62]
create a subdirectory lesson 1 for this
[64]
lesson double click on the lesson 1 sub
[72]
directory to get inside it and type the
[74]
name of the project for this course name
[77]
this project generically project finally
[82]
click Save as you can see the project
[86]
contains a generated main dot C file
[89]
which I will explain in a minute but
[93]
first let's check how the directories
[95]
and files look on the disk
[104]
to finish configuring your project you
[107]
need to adjust a few parameters click on
[109]
the project's options menu in the target
[113]
tab you need to adjust the process or
[115]
variant select device and click on the
[118]
selection button choose Texas
[126]
Instruments lm4f lm4f one-two-zero h5q
[134]
our device next select the C C++
[137]
compiler category here you can see that
[140]
the default language is C and that the C
[142]
dialect is c99 in this course I will
[145]
teach you this newer dialect of C
[148]
finally click on the optimizations tab
[150]
and make sure that the optimization
[152]
level is set too low which is a default
[154]
a couple of our first programs won't
[157]
work correctly at higher optimization
[159]
levels until I show you how to write
[161]
code that works even if highly optimized
[165]
as you are running the IAR toolset for
[169]
the first time you should also spend a
[170]
minute to tailor it to your preferences
[172]
click on Tools options menu here you can
[179]
change the fonts for example I prefer my
[182]
fixed-width font to be lucid our console
[185]
in the editor section increase the
[187]
indentation to 4 spaces and select
[190]
spaces instead of tabs I strongly
[193]
recommend avoiding tabs in your code
[195]
because they are rendered differently on
[197]
different devices such as printers so
[200]
finally we can get to the code generated
[202]
by the IAR toolset let's try first to
[205]
validate that this indeed is a valid C
[207]
code you do this by compiling the code
[210]
that is running it through a program
[212]
called the compiler in the project menu
[215]
select the make option and note that its
[218]
keyboard shortcut is f7 you will use the
[221]
f7 shortcut quite frequently since it is
[225]
the first time you build this project
[227]
the tool asks for the workspace name
[229]
type a generic name workspace and hit
[232]
return the build completes with 0 errors
[235]
and
[235]
zero warnings so congratulations for
[238]
your first legal C program now let's
[241]
reformat the generated code to use the
[243]
line saver racing placement and
[245]
indentation of four spaces which I
[248]
prefer the compiler doesn't really care
[250]
about any of this formatting and would
[252]
accept just as well the code formatted
[254]
as one long line but the formatting
[257]
makes quite a big difference to humans
[259]
who need to read and maintain the code
[261]
but obviously not everything you do to
[263]
the code is legal see for example let's
[266]
type something illegal and compile again
[268]
by pressing f7 this time the compiler
[271]
reports errors when you double click on
[273]
the error report the toolset brings you
[275]
right to the offending place in the code
[277]
which is rather cool after removing the
[280]
problem it's a good practice to ask the
[282]
compiler to check you again
[284]
by pressing f7 I'd like to convince you
[287]
from the get-go that the compiler is
[289]
your best friend constantly watching
[291]
over your shoulder all you need to do is
[293]
give him a chance
[294]
to help you by frequently pressing f7
[297]
now let's define the counter variable
[299]
which I will use to show you how
[301]
computers count a variable is a location
[305]
in computer memory to store a value such
[307]
as a number in C you have to define a
[310]
variable before you can use it you do
[313]
this by specifying the type of the
[315]
variable followed by the name and
[317]
optionally the initial value according
[321]
to my own advice let's check right away
[323]
if the compiler accepts the variable
[325]
definition well kind of there are no
[328]
errors but there is one warning that the
[331]
variable counter has been declared but
[333]
never referenced that's true so let's
[336]
move on now let's increment the variable
[338]
counter by one from its current value C
[342]
has a special operator plus plus for it
[345]
called pre increment as usual I let the
[348]
compiler check that the code still
[350]
compiles as we want to watch how the
[353]
computer will count let's increment the
[355]
counter several more times
[366]
let's compile one last time by pressing
[369]
f7
[373]
now I will show you how to run this
[376]
program first make sure that the project
[378]
is configured for the simulator actually
[381]
you can tell it by the presence of the
[382]
simulator menu at the top but to double
[385]
check click the project options menu in
[389]
the debugger category you should see the
[391]
simulator selection you have two options
[395]
to run the program either through the
[396]
project download in debug menu or by the
[399]
toolbar button which I will use now the
[401]
IAR toolset has switched to the debugger
[404]
mode let's make sure that the following
[406]
debugger views are visible disassembly
[409]
memory register and locals
[425]
let's rearrange the debugger windows so
[428]
we have the best view inside the
[429]
simulated arm cortex-m for process or
[439]
first let's take a look inside the
[442]
disassembly view this view shows the
[444]
Machine instructions that the compiler
[446]
generated from your program the
[449]
processor is stopped at the highlighted
[451]
instruction which is the beginning of
[453]
your main function the machine
[455]
instructions as anything else inside the
[457]
computer are just numbers the symbols to
[460]
the right of the instructions are
[461]
so-called instruction mnemonics and are
[464]
added by the computer by the debugger to
[466]
improve readability the column of
[469]
numbers left to the instructions are the
[471]
memory addresses of the instructions
[474]
memory addresses are simply numbers
[476]
assigned to bytes in memory to convince
[479]
you that the instructions are really
[480]
just numbers in memory let's take a look
[482]
in the memory view you can think of
[485]
memory as a big table of bytes which is
[487]
numbered sequentially starting from 0
[490]
these sequential numbers called the
[492]
addresses are shown in the column of
[495]
numbers along the left edge of the
[496]
memory view to better recognize the
[498]
machine instructions in memory let's
[500]
change the view to two times units
[502]
because most of the ARM Cortex
[504]
instructions occupied two bytes in
[505]
memory now you should easily recognize
[508]
the instructions for example at address
[511]
40 you see the same number as in the
[515]
disassembly view the same is true for
[517]
instructions at addresses
[519]
42 through 50 all right so let's step
[523]
through the code 1c line at the time to
[526]
do this click inside the code view and
[529]
click on the step into button as you can
[533]
see the current instruction advanced by
[535]
1 and the value of the counter variable
[538]
changed to 0 also if you look carefully
[542]
the value of the PC register has changed
[545]
to 42 PC stands for program counter
[549]
because it counts the instructions now
[553]
let's click the step into button again
[555]
to execute the next instruction this
[558]
time the value of the variable counter
[560]
has incremented to 1 the locals view
[563]
tells you also that the counter variable
[565]
is located in the r1 register indeed
[569]
when you check the register view you can
[571]
see that the value
[572]
of our 1 is 1 but what are these
[575]
registers anyway well if you ever used a
[578]
simple calculator you already should
[581]
have a good idea because registers in a
[583]
microprocessor are very similar to the
[586]
calculators memory register typically
[589]
you can add recall and clear the memory
[592]
register of the calculator the arm
[595]
cortex-m processor has 16 such registers
[598]
which are named are 0 through R 15
[601]
whereas R 15 is the other name of the PC
[605]
all these registers can hold 32-bit
[608]
numbers the importance of these
[612]
registers lies in the fact that machine
[614]
instructions can manipulate the
[616]
registers directly typically in just one
[618]
clock cycle you've already seen two
[621]
examples of instructions manipulating
[623]
register moving a 0 to R 1 and addict 1
[627]
to R 1 and all would let's keep stepping
[631]
through the code and watch the counter
[633]
variable increment everything seems to
[636]
work as expected but something
[638]
interesting happens when the counter
[639]
reaches 10 as you recall the counter is
[642]
located in the R 1 register but at value
[646]
10 the locals and the register views
[648]
seem to go out of sync because R one
[650]
shows value a this requires some
[653]
explanation
[656]
the locals view shows the value of the
[659]
counter in the decimal system which
[661]
people adopt it because regular people
[663]
have ten fingers
[667]
however C programmers grow 16 fingers
[670]
after a while on the job here for
[672]
example is the picture of my hands after
[675]
programming in C for over 20 years
[681]
programmers find it much more natural to
[683]
work in the hexadecimal system because
[685]
it Maps perfectly to the binary system
[687]
underlying all computers as you can see
[690]
in this comparison between decimal
[691]
binary and hexadecimal a single
[693]
hexadecimal digit represents a group of
[695]
4 bytes called the nibble
[697]
in contrast decimal system needs two
[700]
digits for numbers above 9 the
[702]
strange-looking
[703]
0x prefix in front of every hex digit is
[706]
the C language convention of encoding
[708]
hex numbers at the bottom of the picture
[711]
you can see an example of applying the
[713]
table above to encode a string of 32
[715]
bits into hexadecimal the bits are
[718]
grouped in four packs of eight which are
[721]
called bytes each byte contains two
[724]
4-bit nibbles as I just explained the
[727]
nibbles map directly to hex digits for
[731]
example the nibble 1 0 1 0 maps to the
[734]
hex digit a then they both 0 1 0 1 2 hex
[737]
digit 5 and so on in the end the whole
[741]
32-bit string is equivalent to hex - 6 0
[745]
f3 e5 a so now I hope that the debug
[751]
views make much more sense to you
[752]
because most of them show hex numbers as
[754]
you can tell by the 0x prefix now before
[759]
letting the program increment the
[760]
counter let's cheat a bit to test what
[763]
happens when the number gets really big
[764]
that's actually very easy to do in the
[767]
debugger because you can manually change
[768]
any variable by clicking on it and
[771]
typing a new value type in 0 x7
[777]
f-f-f-f-f-f-f and press return this
[782]
turns out to be a big number in decimal
[784]
now let your program increment the
[786]
counter by 1 whoops
[788]
something very strange happened the
[791]
counter becomes a huge negative number
[793]
in decimal well while it is hex eight
[796]
zero zero zero zero zero zero zero in
[799]
the r1 register again
[801]
this requires some explanation the
[804]
counter variable has been declared as an
[806]
integer int which in C is assigned
[809]
number meaning that it can hold both
[811]
positive and negative values
[813]
it turns out that computers represent
[816]
negative numbers in a rather peculiar
[817]
way called the two's complement
[819]
representation this circular graph
[822]
explains how it works every arrow in the
[825]
diagram represents increment by one for
[829]
small positive numbers everything works
[831]
as expected until the number fills all
[833]
the bits but the most significant one
[836]
this is the largest positive value a
[839]
32-bit signed integer can represent when
[843]
you increment this number by one you
[845]
overflow to the most significant bit at
[847]
which point the number becomes negative
[849]
in fact it becomes the smallest negative
[852]
number you can represent with 32 bits
[854]
from there when you keep incrementing
[857]
the value becomes less negative until
[859]
you fill all the bits at which point you
[861]
reach negative 1 when you increment
[864]
negative 1 you get 0 again and the cycle
[867]
repeats so back to our debug session
[870]
let's set the counter value to negative
[872]
1 and watch the content of the r-1
[874]
register next let your program increment
[877]
the counter one more time and close the
[879]
cycle by making the counter 0 again exit
[884]
the debugger by by pressing the X button
[892]
as a homework I'd like you to study
[895]
counting of unsigned integers to do this
[898]
we need to modify the type of the
[900]
counter variable to unsigned end
[903]
recompile and start the debugger just
[906]
like we did earlier in this session
[920]
finally as the last step of this lesson
[922]
I promised to show you how to run the
[924]
code on the launchpad board to do this
[927]
you need to modify the project options
[929]
click on the project options menu in the
[933]
dialog box choose the debugger category
[937]
from and from the drop down list select
[939]
the TI stellaris option next click on
[943]
the download tab and check the use flash
[945]
loader option as well as the verified
[948]
download option click OK at this point
[953]
you need to connect the launchpad board
[955]
to your computer by means of the
[957]
provided USB cable if this is the first
[960]
time you connect the board allow a
[962]
minute or two for the installation of
[964]
the USB driver for the USB debugger the
[967]
board is preloaded with the demo program
[970]
that blinks the LED now you can download
[973]
your code to the flash memory of your
[975]
board and start debugging in the exact
[978]
same way as you did on the simulator
[980]
this is rather cool when you think about
[982]
it and it's only possible because both
[984]
the USB debugger on the board and the
[987]
microcontroller itself has a special
[990]
circuitry to provide this capability for
[993]
you please note that your program stays
[995]
permanently programmed inside the
[997]
microcontroller so it will count but it
[1000]
won't blink anymore
[1001]
don't despair though we will make it
[1004]
blink and much more in the future
[1006]
this concludes this lesson about
[1008]
counting in the next lesson I'll show
[1010]
you how to replace the repetitions of
[1012]
instructions with a loop and I'll show
[1014]
you all sorts of ways to change the
[1016]
control flow through your code stay
[1018]
tuned and visit
[1020]
state-machine.com/quickstart for the
[1022]
class notes and project file downloads