Presentation on theme: "Week 3. Assembly Language Programming Difficult when starting assembly programming Have to work at low level Use processor instructions >Requires."— Presentation transcript:
Assembly Language Programming Difficult when starting assembly programming Have to work at low level Use processor instructions >Requires detailed understanding of instructions Manage memory and register resources Benefits are significant Better understanding of how high level languages (HLL) work Knowledge of limitations of the hardware Sometimes, programmer must resort to assembly language Issues of speed and code density However, today’s HLL compilers are very powerful and can generate highly optimised code
Using Assembly Language EditorAssembler Linker (opt) Source Code Object Code Executable Program test.a51test.objtest.hex test.lst
Using Assembly Language What happens after creating executable file? Must be verified and tested!! How is this done? There are 2 basic strategies 1. Use real hardware to run and test code 2. Use simulation software to simulate the operation of a real hardware -This is the approach that we will take EditorAssemblerLinker
Testing and Debugging Using Assembly Language
Arithmetic Instructions The arithmetic instructions provide instructions for performing the basic arithmetic operations of addition, subtraction, multiplication and division on integer operands For most of the arithmetic instructions, the accumulator register acts as an operand and the location of the result E.g. ADD A,#23 All the addressing modes previously encountered can be used
Binary Addition Adding binary values works the same as adding decimal numbers, except now the base is different, i.e. base 2 instead of 10 Example Add these 2 four bit numbers Carry bits
Arithmetic instructions: ADD The add instruction allows two 8-bit numbers to be added. As an example, lets consider the instruction ADD A,#23 and lets assume that A contains the value 31 beforehand. The outcome of the addition is as follows (23) (31) (54) Note that the result is nine bits, but our registers are only 8 bits. The extra bit is called the carry bit and it is stored in a special register called the program status word register (PSW)
Arithmetic instructions: ADD Another example (215) (127) (342) The value stored in A is or decimal 86 and the carry bit is a 1 The examples considered so far assume unsigned numbers, I.e. no negative numbers Since an 8 bit unsigned number has the largest value of 255, the largest possible sum is 510, which can be represented by 9 bits I.e. the 8 bit result in A and the carry bit So everything is great, or is it?????
Number Representations Unsigned number Has decimal value 150 Signed (2’s complement) number Has decimal value –106 How can this be???? ANS: The MSB of a signed number (using 2’s complement) has a negative weighting
Number Representations UnSigned Bit weightings Signed Bit weightings Example represents unsigned 160( ) Represents signed -96( )
Arithmetic instructions: ADD When dealing with 2’s complement values, can all results of additions be represented correctly? The answer is NO, but there is a way to check if the result is not correct There is an overflow (OV) bit in the PSW register which is set when the result of an arithmetic result is out of the allowable range for 2’s complement numbers See later how to access the OV bit with instructions
Well, normally signed numbers are needed and a 2’s complement representation is used. When using 8 bits, a 2’s complement representation allows values from –128 to +127 to represented. Consider an example Lets add 43 and –68 (43) (-68) (-25) The addition operation is carried out the same as before What is important here is our (the programmer’s ) intrepretation of the bits Arithmetic instructions: ADD
Example Add the contents of R1 to A and store result in R2 ADD A,R1 The carry (C) and the overflow (OV) bits are affected by the ADD instruction Before Add 0 38 After Add
Example 1 Consider the addition of the following binary values Before Add After Add ADD A,R1
Example 1 (Unsigned interpretation) This example illustrates dealing with unsigned numbers OV bit has no meaning in unsigned case Before Add After Add Consider the addition of the following binary values
Example 1 (signed interpretation) This example illustrates dealing with signed numbers C bit has no meaning in signed case Before Add After Add Consider the addition of the following binary values
Example 2 Consider the addition of the following binary values Before Add After Add ADD A,R1
Example 2 (Unsigned interpretation) This example illustrates dealing with unsigned numbers OV bit has no meaning in unsigned case Before Add After Add Consider the addition of the following binary values
Example 2 (Signed interpretation) This example illustrates dealing with signed numbers C bit has no meaning in signed case Before Add After Add Consider the addition of the following binary values
Arithmetic instructions: ADD The allowable instructions for addition are ADD A,source ADD A,#data Where source can be any of Rn, direct
Example program Consider a program to add the contents of the registers R4, R5 and R6 with the value in the address location 50H. Store the result in R7 MOV A,R4; A = R4 ADD A,R5; A = A + R5 ADD A,R6; A = A + R6 ADD A,50H; A = A + contents of 50H MOV R7,A; Move result of adds into R7
Example Program The above program will only work properly if the sum is within the range –128 to 127 To test the program, it is necessary to initialise the registers and memory location with test values MOV R4,#-7 MOV R5,#-37 MOV R6,#15 MOV 50H,#32 Testing OV or C bit after the last addition only indicates if the last addition overflowed!
Arithmetic instructions: ADDC A variation on the ADD instruction is the ADDC instruction. This is the known as the ADD with carry instruction Example ADDC A,#34 Here the value of the accumulator is added to 34 and the carry bit is then added, with the result stored in the accumulator. This instruction is particularly useful when performing 16 bit or 32 bit additions using 8 bit values
Arithmetic instructions: ADDC Example: Registers R1 and R0 together store a 16 bit signed integer. Register R3 and R2 also store a 16 bit signed integer. Add the two 16 bit numbers and store the result in R5 and R4. MOV A,R0 ADD A,R2 MOV R4,A MOV A,R1 ADDC A,R3 MOV R5,A
Subtraction Again, same idea as subtracting decimal numbers Just as a carry is used when doing an addition a borrow is used for subtraction. Example Subtract these 2 four bit numbers Borrow bits
Arithmetic instructions: SUBB Performs a subtraction operation with a borrow into the least significant bit (LSB) The borrow is actually in the carry bit in the PSW If no borrow in used, then make sure to clear the carry bit To do this, use the following instruction CLR C This instruction uses the accumulator as a source operand and the destination The OV bit and carry bit are affected by this instruction
Arithmetic instructions: SUBB Example Subtract the value in R1 from R0 and store result in R2, i.e. R2=R0-R1 CLR C MOV A,R0 SUBB A,R1 MOV R2,A Note C and OV bits before and after SUBB
Summary for arithmetic instructions The accumulator acts as an operand and destination for most arithmetic operands Certain bits in the PSW are modified following arithmetic instructions Carry (C) bit Overflow (OV) bit Only has meaning when numbers are treated as signed numbers Need to know what the data represents, so as to interpret results correctly Unsigned Signed (2’s complement) Other (e.g. BCD)
Programming Languages The first programmers hardwired circuits together and used punch cards to create a program High level programming languages helps bridge the natural communication gap between man and machine. Programmer is not concerned with the low level operation of computer hardware. Can think and develop programs at an abstract level. Because people like to express themselves in terms of natural languages like English, the use of high level languages make it easier to develop programs.
Types of Programming Languages Machine Code
Types of Programming Languages Assembly Language MOV A,R0 ADD A,R2 MOV R4,A MOV A,R1 ADDC A,R3 MOV R5,A
Types of Programming Languages High Level Language Large number of different high level languages if goals_teamA > goals_teamB then writeln(‘Team A won the match’') else if goals_teamA = goals_teamB then writeln(‘Match was a draw’); else writeln(‘Team B won the match’);
Getting started with a high level programming language Can write our code in a high level language, but the computer system can only understand machine code. Need to convert from a high level programming language to machine code in an unambiguous manner. Program in high level language ===> Machine code To begin to see how we can accomplish this, first need to create the high level program.
Text Editor An editor or text editor is an application program. There are many different editors available. Using an editor the computer acts like a typewriter. The user is able to enter or edit any text and save the text to disk. High level program files (source code) can be created/edited using an editor. Example
Translators A high level program ( source code ) needs to be translated to a machine readable format, before it can be executed (run) on a computer. Machine readable code is usually referred to as object code. A compiler is used to perform this process.
Programming Programmer needs to learn to be patient Proper design is key System analysis and design Look at ideas on problem solving in week3 Good demand for programmers There are always problems there to be solved!
Unsigned numbers An 8 bit unsiged number has a value determined as follows Where is 1 or 0 and represents the binary bits For example, given the value , then its value is
Signed numbers An 8 bit unsiged (2’s complement) number has a value determined as follows Where is 1 or 0 and represents the binary bits For example, given the value , then its value is
1.What is the process in going from coding an assembly language program to executable machine code? 2.What is the ALU? 3.Perform the binary addition of the following 2 bytes. Give the result in binary, decimal and hexadecimal How are signed numbers represented in binary? 5.Does the ADD instruction need to know whether signed or unsigned numbers are being added?
Exercises 6.When is the carry bit used to indicate overflow? 7.When is the OV bit used to indicate overflow? 8.Given the following 8 bit value What is its value if 6.Its interpreted as unsigned 7.Its interpreted as signed 9.Detail the code to add the contents of R0 to R5 and store the result in R0.
Exercises 10.What does the ADDC instruction do? Give an example use. 11.Detail how the following 2 binary values are subtracted What is the SUBB instruction used for? 13.If a borrow out from the MSB is generated when using SUBB, where is it stored.