John.Wiley.and.Sons.Rapid.Mobile.Enterprise.Development.for.Symbian.OS.An.Introduction.to.OPL.Application.Design.and.Programming.May.2005.eBook-LinG (779881), страница 3
Текст из файла (страница 3)
Hector and Russell, the Kiltmakers. And finally Ed,Frankie, and the Hawkins brothers, for the music that this book was(mostly) written to.I know I’ve probably missed a bundle of people involved in OPL, butthanks go to them as well. Get in touch with me and if there’s a secondedition, I’ll add you in!xxAUTHOR ACKNOWLEDGMENTSBut the biggest thanks of all go to Vikki, Eilidh, and Mairi. For beingwith me in my life, spending time with me, and putting up with everythingI had to do to write this book – and everything else.
. . I can never thankthem enough.Symbian Press AcknowledgmentsSymbian Press would like to thank Ewan for his steadfast dedicationto the cause of OPL. Thanks too must go to young Phil Spencer forbeing a veritable cornucopia of information in times of need.
Eternalgratitude to Phil n’ Freddie for their master class in publishing, no onecan underestimate the value of a few choice words, even when spokenthrough the froth of a pint.Cover concept by Jonathan Tastard.About the CoverThe mobile phone has traditionally connected the mouth to the ear – atSymbian’s Exposium 2003, Symbian introduced the concept of SymbianOS, enabling a new generation of connected communications devicesby connecting the mouth to the ear to the eye. To realize this vision, themobile phone industry is working together through Symbian to developthe latest technologies, support open industry standards, and ensureinteroperability between advanced mobile phones as networks evolvefrom 2.5G to 3G and beyond.
. .Symbian licenses, develops, and supports Symbian OS, the platform fornext-generation data-enabled mobile phones. Symbian is headquarteredin London, with offices worldwide. For more information see the Symbianwebsite, http://www.symbian.com/. ‘Symbian’, ‘Symbian OS’, and otherassociated Symbian marks are all trademarks of Symbian Software Ltd.Symbian acknowledges the trademark rights of all third parties referred toin this material. Copyright Symbian Software Ltd 2004.
All rights reserved. No part ofthis material may be reproduced without the express written permissionof Symbian Software Ltd.Part 11Programming PrinciplesIn this chapter you will learn:• the essential parts of a mobile computer and how they relate toeach other• why there are many languages to program a computer with, and whythey are all different• the main elements of computer coding; so-called ‘variables’, theDO...UNTIL loop, the WHILE...ENDWH loop, and the IF...ENDIFstructure.1.1 The Language of ComputersTo program your Symbian OS Smartphone the first thing to remember isthat it is a computer. Sure, it may be a lot smaller than the one on yourdesk, and it has a phone built in to it – and it may or may not have a fullQWERTY keyboard – but nonetheless it is a computer.
And to program acomputer, you need to speak its language.1.1.1Storing InformationA computer is a digital machine, which has a language that consists oftwo characters, 1 and 0. That’s it. These represent the two electrical statesthat each tiny portion of the computer can have (on or off). This is calleda bit, and it is the smallest structure in the language of a computer. Amemory chip can hold millions and millions of these states, and like anylanguage, collections of these pieces (individual ‘letters’ if you will) canbe grouped together to make more complex ‘words’.If you have a collection of 8 bits (eight 1s, eight 0s or a mix of them)then this is called a byte.
A byte can represent any number from zero(00000000) to 255 (11111111). Why does this equal 255 and not elevenmillion, one hundred and eleven thousand, one hundred and eleven!?4PROGRAMMING PRINCIPLESWell, that is a subtle complexity of the way this special ‘binary’ languageis interpreted by computers. If you don’t want to know, please feel freeto skip this bit – it’s in no way essential to your understanding of OPL. Ifyou’re just a little curious then there are various excellent sources on theInternet which explain the binary system in detail – for example, searchfor ‘binary numeral system’ on www.wikipedia.org/ to learn more.When counting bytes, it starts to get unwieldy when you get to arounda thousand bytes; which is where the kilobyte comes in. A kilobyteis 1024 bytes, and is commonly written as 1 K (or 1 Kb).
And whenwe get to a thousand kilobytes, we have another term: 1024 kilobytesequals one megabyte, written as 1 Mb. You’ll probably be aware thatyour Smartphone has a certain number of megabytes of memory. This iswhere that number comes from. So if you have 4 Mb of memory, youcan store thirty three and a half million ones and zeros. These bits, bytesand kilobytes represent information – not readable to you as a user, butbasically all the phone itself understands.1.1.2 Processing InformationWhat can we do with this information? Well, we can’t do much with it.But the phone can – it can take this information from its memory andthen process it somehow. This happens in the Central Processing Unit(CPU), more commonly just called the ‘processor’.The CPU reads information from the memory and follows the instructions that it finds there.
We said above that a group of bits makes upa ‘word’. The length of this word (8 bits, 16 bits, 32 bits or more) is anindicator of how complex a CPU is. Home computers of the 1980s werebased around CPUs that could read 8-bit words. Nowadays, desktopcomputers are generally 32-bit or 64-bit.
Your Symbian OS Smartphoneis 32-bit, so each word is made up of 32 bits.Inside the CPU there is a ‘dictionary’ of all the unique words that theCPU understands, and what it should do when it reads in one of thesewords from memory. This is the essence of a computer program; a list ofthings to do.1.1.3 Talking to the UsersIt’s all well and good having the processor reading information frommemory, but how do we know what’s happening? Or tell it what information to read? This is where inputs and outputs (I/O) come into play.I/Os are how computers are told what to do (input) and how they reportback on what they have done (output).The most common form of input is a keyboard; but inputs can alsoinclude things like touchscreen display, a microphone, a light sensor, infact anything that passes information into the computer.
Information fromthe Internet is regarded as an input as it is passed into the computer.SPEAKING THE LANGUAGE5Talking to the Internet is also an output, as your computer needs togive information to the Internet (e.g., downloading a web page).
What isdisplayed on the screen is an output, as is a speaker, a buzzer, a flash ona digital camera. . .anything that comes out of the computer.Memory can be regarded as something that does I/O operationsto the processor: it passes stored information to the processor (input)and takes the responses or results from the processor and stores thatinformation (output).1.1.4 Keeping a Note of ThingsMemory on a computer is not like the memory you and I have. Justbecause something is put in memory does not mean it stays there for ever.A computer’s memory is like a temporary workspace.
The processor willcopy a program into memory from a storage device. Some commandsmay ask the processor to read information from storage for the programto use (for example, a list of phone numbers). Any changes to informationwill need to be made to the equivalent information in storage if it is to bekept permanently.And yes, a storage device is an I/O device, but it’s an important one,so gets recognized in its own right.1.1.5Putting it All TogetherIn abstract form, your computer looks like the model in Figure 1.1.The Life of a ProgramWhen a user runs a program, they start it off with an input (maybe typing acommand or selecting an icon).
The Central Processing Unit, (commonlyknown as the processor or CPU) recognizes this command from itsdictionary and reads the program itself in from the storage device, copyingit into memory where it can be accessed directly by the processor. Theprocessor can only read from memory directly, hence this intermediatestep is almost always required. The processor reads these commands fromthe memory one at a time, looking each one up in its dictionary.
Someof these commands may be to output information (such as the result of acalculation). When there are no more words to read in from the memory,the program is finished and the processor waits for more input.1.2 Speaking the LanguageNow we know how a program works, how do we write our first program?Well, when computers first came about, everybody programmed directlyin binary. That is, they actually wrote down and manually inputted all the6PROGRAMMING PRINCIPLESMemoryCentralProcessing UnitInputsOutputsStorageFigure 1.1Abstract computer modelones and zeros themselves, by looking up what they wanted to do in ahuman dictionary of commands (similar to the one inside the processor).This is called, naturally, machine code, because it is the code of themachine itself.
If you work at this level, then you are said to be workingat a low level – the lowest of all, in fact – hence the term Very Low LevelProgramming.1.2.1 Assembly LanguageAfter a short period of time, one programmer came up with a smart idea.Instead of writing down each word of 1s and 0s, he wrote down thelist of commands so that one command matched one computer word.So 10101010 could also be written as load hl.