Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Week 3 Assembly Language Programming Difficult when starting assembly programming Have to work at low level Use processor instructions > Requires detailed understanding of instructions Manage Benefits memory and register resources 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 Editor Assembler Linker (opt) Source Code Object Code Executable Program test.a51 test.obj test.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. 2. Use real hardware to run and test code Use simulation software to simulate the operation of a real hardware - Editor This is the approach that we will take Assembler Linker Edit Using Assembly Language Assembler Assembly errors No assembly errors Linker Link-time errors No linking errors Testing and Debugging Run-time and/or logical errors Run No run-time/ logical errors 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. All ADD A,#23 the addressing modes previously encountered can be used 23 A ALU 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 1 0 1 1 1 1 01 0 11 1 0 1 0 0 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) 00010111 (31) 00011111 (54) 000110110 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 (215) (127) (342) The example 11010111 01111111 101010110 value stored in A is 01010110 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 10010110 Has decimal value 150 Signed (2’s complement) number 10010110 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 128 64 32 16 8 4 2 1 32 16 8 4 2 1 Signed Bit weightings -128 64 Example 1010010 represents unsigned 160(128+32+2) Represents signed -96(-128+32+2) 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 Arithmetic instructions: ADD 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 00101011 11000001 011100111 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 The carry (C) and the overflow (OV) bits are affected by the ADD instruction ADD A,R1 R1 15 0 C 0 OV A 23 38 BeforeAdd Add After Example 1 Consider the addition of the following binary values ADD A,R1 R1 01100010 +01000111 10101001 A 01100010 10101001 01000111 After BeforeAdd Add Example 1 (Unsigned interpretation) Consider the addition of the following binary values 01100010 +01000111 10101001 This example illustrates dealing with unsigned numbers OV bit has no meaning in unsigned case R1 98 0 C 1 OV A 169 71 After BeforeAdd Add Example 1 (signed interpretation) Consider the addition of the following binary values 01100010 +01000111 10101001 This example illustrates dealing with signed numbers C bit has no meaning in signed case R1 98 0 C 1 OV A -87 71 After BeforeAdd Add Lecture 2 Example 2 Consider the addition of the following binary values ADD A,R1 R1 11101110 +00010111 100000101 A 11101110 00010111 00000101 BeforeAdd After Add Example 2 (Unsigned interpretation) Consider the addition of the following binary values 11101110 +00010111 100000101 This example illustrates dealing with unsigned numbers OV bit has no meaning in unsigned case R1 238 1 C 0 OV A 523 After BeforeAdd Add Example 2 (Signed interpretation) Consider the addition of the following binary values 11101110 +00010111 100000101 This example illustrates dealing with signed numbers C bit has no meaning in signed case R1 -18 1 C 0 OV A 523 After BeforeAdd Add Arithmetic instructions: ADD The allowable instructions for addition are ADD A,source ADD A,#data Where source can be any of Rn, direct or @Ri 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 ADD ADD ADD MOV A,R4 A,R5 A,R6 A,50H R7,A ; ; ; ; ; A = R4 A = A + R5 A = A + R6 A = A + contents of 50H 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 MOV MOV MOV R4,#-7 R5,#-37 R6,#15 50H,#32 Testing OV or C bit after the last addition only indicates if the last addition overflowed! Arithmetic instructions: ADDC A variation instruction. This on the ADD instruction is the ADDC 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 R1 R3 MOV R4,A R5 MOV A,R1 ADDC A,R3 MOV R5,A R0 R2 R4 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 1 0 0 1 0 11 11 1 0 0 0 1 0 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 This do this, use the following instruction CLR C 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 R0 23 R1 15 R2 8 A 23 0 C 0 OV 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) Appendix Week3 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 01001110101000111010101101010101010101000101110 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. To Program in high level language ===> Machine code 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 Proper learn to be patient 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 7 as follows i a 2 i 0 Where i a is 1 or 0 and represents the binary bits i For example, given the value 10010110, then its value is 1 2 7 0 2 6 0 25 1 2 4 0 23 1 2 2 1 2 0 2 128 0 0 16 0 4 2 0 150 1 0 Signed numbers An 8 bit unsiged (2’s complement) number has a value determined as follows 6 a 2 a 2 7 7 Where For i 0 i i a is 1 or 0 and represents the binary bits i example, given the value 10010110, then its value is 7 6 5 4 3 2 1 1 2 0 2 0 2 1 2 0 2 1 2 1 2 0 0 2 128 0 0 16 0 4 2 0 106 Exercises Exercises 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 1. 10111011 2. 10000110 4. 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. 7. 8. When is the carry bit used to indicate overflow? When is the OV bit used to indicate overflow? Given the following 8 bit value 6. 7. 10001001 What is its value if 6. 7. 9. Its interpreted as unsigned Its interpreted as signed 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 10001011 01111010 12. What is the SUBB instruction used for? 13. If a borrow out from the MSB is generated when using SUBB, where is it stored.