Low Level Assembly Lang., Machine Codes
Low Level Assembly Lang., Machine Codes
Introduction Learn what a programming language is, about high level and low level languages, about the program development cycle, what structured programming is.
1.0 What is a programming language? Natural Languages: English, Malay etc used to communicate ideas, instructions, information between people. human human Programming Languages: used to communicate instructions to the computers. human computers Programming is the activity of communicating with the computer and a set of instructions, called a computer program is the result of such an activity. hese instructions tell the computer what to do. !lthough it is fun to write instructions li"e #$ing a song%, the fact is that you have to write the instruction in a language the computer #understands%. ! computer program is a set of instructions written in a programming language.
1.1
&ivided into different levels ' low level and high level. $ome thought the levels describe how difficult or easy the programming language can be learnt which is not true. he levels indicate how #visible% is the system hardware to the programmer. (igh Level languages ) *!$I+, ,-. .!N, P!$+!L , +-*-L and + English)li"e. (igh level Instructions E.g. : *!$I+ : +: P!$+!L: PRINT Programming is fun + ' Mid level 0 printf(Programming is fun); write (Programming is fun):
PRINT, printf and write are computer commands for the various computer languages and they certainly loo" li"e English, don/t you thin" so 0
Introduction
,ig 1.1 (igh and low level languages Low level language Machine languages !ssem"ly languages ! low level language does not mean the language or the 2uality of the program produced is inferior or inefficient or minimal programming s"ills are re2uired to program the computer. -n the contrary, the codes produced are very efficient 3fast4 and compact. In you want the computer to do something very fast or anything that cannot be done in high level languages, you program it in a low level language ' not in a high level language. Machine language are instructions in binary forms of 1/s and 5/s. ! set of binary inputs 3 a pattern of 1/s and 5/s4 will produce a defined action when applied to a computer chip 3processor or microprocessor4. ,or e6ample, when a 785 microprocessor receives a binary pattern of 55111115 as an instruction, it will perform this action:
Load (store) the next incoming binary pattern as data into Accumulator A.
!n accumulator 3or register4 is a special type of memory in a microprocessor that is used for specific tas"s $imilarly when a Pentium 3or an Intel)based processor4 receives a binary pattern of 15115155 as an instruction, it will
Load (store) the next incoming binary pattern as data into the upper part of Accumulator A.
Introduction
,rom the above e6amples, you may observe that the two different binary patterns are re2uired to instruct the two different processors to do almost the same thing. hat is, a set of binary inputs may be a valid instruction to one processor but could be gibberish to another. :e say, machine languages are machine)dependent. he set of binary inputs and their corresponding actions are listed in the instruction set supplied the manufacturer of the processor. he second observation that you may ma"e is that to program the computer in a low level language , a programmer must have a reasonable good "nowledge about the internal organisation 3architecture4 of the computer and the surrounding hardware 3devices4 because he will have to write instructions to move data between devices and registers. he ability to manipulate the flow of data between these devices gives a programmer a feeling of being in #total control% of the system. hat is why, to those who program in low level languages, low level languages are so powerful. *elow is an e6ample of a machine language 3or ob;ect4 program in 785 codes that adds two numbers 3< and =4 and the result is stored in accumulator !. 55111115 Action: Load (store) the next incoming data in Accumulator A 55555151 This number- 5, is stored to Accumulator A ( 5) 11555115 Action: Add the next incoming data to Accumulator A 55551551 This number-!, is added to Accumulator A ( "#) >our turn Note : hroughout these notes you will encounter numerous #>our turn% bo6es. In these bo6es you will find activities that would reinforce what you have learnt. Please &N- s"ip them. 1 In your opinion, what could be the problems associated with this type of programming0 Easy to understand and debug 3find errors4 >es?No .esemble a human)readable format >es?No ime consuming?tiresome to write >es?No 66 o improve the situation, the instructions can be written using he6adecimal numbers, rather than binary. he above program in he6 version will be @E 5< +A 5= he he6 version is shorter to write and less tiresome but remembering he6 number codes is still 2uite impossible. he programming situation is further improved by assigning names to these instruction codes. hese instruction code names are called mnemonicsB they are li"e some chopped up English wordsC ! program written in mnemonics is called an assembly language program. he above program in mnemonics is shown below: LD A, 0 ADD 0!
Introduction
he mnemonics LD A, 0 stand for #Load !ccumulator ! with 5<% and the ne6t instruction ADD 0! means #!dd the 5= to !ccumulator !%. !s you can see, by assigning names to these instruction codes, programming becomes a little less tiresome. !n assembly language program to print the message #Programming is fun % on the computer screen is given below. he instruction codes are that of the 785 microprocessor.
; ; ; Program "#" Program to print t$e message : Programming is fun %&0 program LD 'L, ()**A+) N),T: LD A,('L) 0P A,0--' 3P %, )ND R*T 6&' IN0 'L 3P N),T R)T ;-et.$ a /etter ;Rea.$ en1 of message 2 ;4es, 5ump to )ND ;Print t$e /etter ;Point to ne7t /etter ;+o to N),T ;Point to ()**A+)
)ND:
Now you "now why assembly languages or machine languages are called low level languages0 o program the computer at low level, you need to "now the hardware 3) the lowest level 4 of the computer system. In high level language programming, the programmer is #shielded% from 9 +ome out with the hardwareof and it is not the programmer/s concern what lies inside the computer. hat is 1 -b;ectives the ideas for the program why a high level language is easier to learn. program +, unli"e most high level languages, has instructions to assess the hardware of a computer system. Li"e assembly language, + allows a programmer to have control over flow of data in a computer system. hat is why it is sometimes called a mid)level language. @ :rite the program ' E +ompile program createor source Note : he computer recognises only binary patterns as instructions dataB codes. it does not understand instructions li"e PRINT, write or printf()9 Dltimately, all these #word% instructions have to be converted to machine languages to be fed into the computer. If you don/t understand much of the assembly language commands written in this section, don/t errors worry, you will study more about assembly language programming in $emester 9. < Lin" program 1.# $rogram development cycle errors
(ere are the steps for creating a program. $ee ,igure 1.9. &on/t bother to memorise them because they will come naturally as you start creating a program.
bugs
Program -G ) ,inish
Introduction
$tep 1: $tep 9:
&etermine the ob;ective3s4 of the program. ,or e6ample, write a program to design transistor amplifiers. If you don/t have any ob;ectives, you won/t be writing programs. +ome out with ideas on how to go about doing the program. his is the most difficult step. his is where you thin" about the tas"s re2uired, the ways to accomplish them and the order in which these tas"s should be carried out. >ou devise an algorithm ' big word, huh0 $ome of the tools used to develop algorithms are flow chart and pseudo codes. :hen you write a program, you are creating the source codes of the program +ompilation. ! computer can only #understand% instructions in binary patterns ' ones and Heroes. ! program 3commonly referred to as source codes4 in a high level language must first be translated to machine language. his translation is done by a compiler. If synta6 errors 3missing commas, colon, brac"ets4 occur at this stage, you will have to go bac" to step @ to ma"e the necessary corrections.
$tep @: $tep E:
<
Introduction
$tep <:
Lin"ing. he compiler will produce an ob;ect file out of your source codes. he ob;ect file has to be IJlin"ed% to other ob;ect files if re2uired to produce an e6ecutable files. Errors due misspelled function names or commands may occur at this step. !gain, you will have to go bac" to step @ to correct the errors. he lin"er in step < will produces an e6ecutable file any you are ready to run the program. If the program produces results as e6pected, you ;ob is complete. If the program produces strange results and does not function as e6pected, we say there are bugs in the program. It time to pull your hair and get the $hellto6C No, it is time to go bac" to step @ or possibly step 9 if the algorithm is wrong. More a"out algorithms
$tep A:
1.%
!n algorithm gives the order on how things should be done. Dsually pseudo codes 3natural language4 are used to develop an algorithm of a program. Pseudo codes are short instructional statements ' li"e those that you find printed on the instant noodle pac"age. ,or e6ample, *oil for 9 minutes and simmer for < minutes. !nother method of representing the algorithm of a program is the use of flow charts. ,low charts are a series of bo6)li"e symbols with connecting arrows to indicate the flow of actions. he symbols used in flow charting are shown in able 1 below. In this course, where appropriate, the development of the algorithm is emphasised.
Introduction
able 1: $ymbols used in flow charts $ymbols Name erminal :here used $tarting and stopping a program
Process
&ata
&ecision
+omparing two 2uantities, chec"ing if an e6pression meet certain conditions. +onnect one part of the flow chart to another which may be on the ne6t page
+onnector
$tart
!s an illustration, on developing and representing an algorithm, the unfortunate .emoveconsider flat -pen up boot incident your have on your way to I *. >ou have a flat tyre. tyre (ow do you go about changing the tyre 0 he algorithm in pseudo codes:
Put in spare Ketup tools and $tep 1: -pen boot tyre spare tyre out $tep 9: Ket tools $tep @: .emove wheel cap and loosen nuts $tep E: Lac" up car $tep <: .emove nuts .eplace nuts $tep A: .emove flat tyre .emove wheel and tighten $tep M: ,i6 in the spare tyre 3assuming that it is not flat4 cap and loosen them $tep 8: Put in nuts and tighten them nuts $tep =: Lower car and tighten nuts again. $tep 15: hrow everything 3punctured tyre and tools4 into the boot and rush to class. Lac" to up represent car Dsing ,low chart the algorithm Lower car and tighten nuts again
! flow chart is another way of representing an algorithm. he above algorithm in flow chart representation is shown in ,ig 1.@.
.emove nuts hrow everything into the boot and rush to I *
M
End
Introduction
,ig 1.@
It is 2uite obvious that some steps must first be carried out before the ne6t steps can be ta"en. ,or e6ample, you cannot loosen the nuts 3$tep @4 if you don/t get the tools out 3$tep 94 first. .eally, an algorithm helps you to "eep the steps in order and logical. !fter the algorithm has been properly wor"ed out, you can then starting coding it in a programming language ' + in this course.
.emember : *efore you write a program, spend some time to wor" out the algorithm first.
Introduction
1.& 'tructured programming $tructure programming is a style of programming in which a program is written as a se2uence of steps to be e6ecuted one after anotherB and in a modular form. Modularity implies that codes are organised into small bloc"s, called functions, which can be re)used again and again in a program. he + language emphasises this style of programming. Let us compare a program written in + 3structured program4 and a program written in *!$I+ 3unstructured program4. his program as"ed for a number and compared it with a secret number. In *!$I+,
0 R)( Program "#6 "00 PRINT +uess a num:er ""0 INP;T +;)** "60 I- +;)** < *)0R)TN= T')N >00 )L*) "?0 PRINT 4ou@Ae got itB ">0 )ND >00 >"0 00 "0 D00 D"0 PRINT 4our guess is too $ig$ +=T= ">0 I- +;)** C *)0R)TN= T')N D00 )L*) "?0 +=T= ">0 PRINT 4our guess is to /ow +=T= ">0
00
In +,
EF Program "#? FE printf(+uess a num:erGn); s.anf(H1, I+;)**); if (+;)** < *)0R)TN=) printf(4our guess is too $ig$); e/se if (+;)** C *)0R)TN=) printf(4our guess is too /ow); e/se printf(4ou@Ae got itB);
In the above *!$I+ program, it is observed that one instruction can be followed by another instruction in a completely different part of the program. hus causing difficulties in following the flow of the program. Imagine if you have a very long program, you will be flipping pages all the time. It is obvious that the program in + is more orderly and structured. >ou may not understand most of what is written in the + program now. *ut, don/t worry, in due course, you will understand all the commands in the program.
Introduction
<
&evelop the algorithm for a busy e6ecutive in preparation to get to his office, assuming he has ;ust wo"e up.
66
15