1 / 46

Week 3

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 memory and register resources Benefits are significant

penney
Download Presentation

Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 3

  2. 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

  3. Using Assembly Language Linker (opt) Editor Assembler Executable Program Source Code Object Code test.a51 test.obj test.hex test.lst

  4. Editor Assembler Linker Using Assembly Language • What happens after creating executable file? • Must be verified and tested!! • How is this done? • There are 2 basic strategies • Use real hardware to run and test code • Use simulation software to simulate the operation of a real hardware • This is the approach that we will take

  5. Testing and Debugging Using Assembly Language

  6. 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

  7. 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 0 0 1 1 0 1 0 0 1 1 1 Carry bits

  8. 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

  9. Another example (215) 11010111 (127) 01111111 (342) 101010110 The 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????? Arithmetic instructions: ADD

  10. Unsigned number 10010110 Has decimal value 150 Signed (2’s complement) number 10010110 Has decimal value –106 Number Representations How can this be???? ANS: The MSB of a signed number (using 2’s complement) has a negative weighting

  11. Number Representations • UnSigned • Bit weightings • 128 64 32 16 8 4 2 1 • Signed • Bit weightings • -128 64 32 16 8 4 2 1 • Example • 1010010 • represents unsigned 160(128+32+2) • Represents signed -96(-128+32+2)

  12. 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

  13. 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) 00101011 (-68) 11000001 (-25) 011100111 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

  14. 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 Arithmetic instructions: ADD 0 15 0 23 Before Add 38 After Add

  15. Example 1 • Consider the addition of the following binary values 01100010 +01000111 ADD A,R1 01100010 10101001 After Add 01000111 Before Add 10101001

  16. 0 1 169 After Add Example 1 (Unsigned interpretation) • Consider the addition of the following binary values 01100010 +01000111 • This example illustrates dealing with unsigned numbers • OV bit has no meaning in unsigned case 98 10101001 71 Before Add

  17. 0 1 -87 After Add Example 1 (signed interpretation) • Consider the addition of the following binary values 01100010 +01000111 • This example illustrates dealing with signed numbers • C bit has no meaning in signed case 98 10101001 71 Before Add

  18. Lecture 2

  19. Example 2 • Consider the addition of the following binary values 11101110 +00010111 ADD A,R1 11101110 00010111 Before Add 00000101 After Add 100000101

  20. 1 0 5 After Add Example 2 (Unsigned interpretation) • This example illustrates dealing with unsigned numbers • OV bit has no meaning in unsigned case • Consider the addition of the following binary values 11101110 +00010111 238 100000101 23 Before Add

  21. 1 0 5 After Add Example 2 (Signed interpretation) • This example illustrates dealing with signed numbers • C bit has no meaning in signed case • Consider the addition of the following binary values 11101110 +00010111 -18 100000101 23 Before Add

  22. 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

  23. 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

  24. 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!

  25. 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

  26. 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

  27. 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 1 1 1 0 0 0 1 0 1 1 Borrow bits

  28. 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

  29. 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 23 15 0 8 0 23

  30. 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)

  31. Appendix Week3

  32. 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.

  33. Types of Programming Languages • Machine Code 01001110101000111010101101010101010101000101110

  34. Types of Programming Languages • Assembly Language MOV A,R0 ADD A,R2 MOV R4,A MOV A,R1 ADDC A,R3 MOV R5,A

  35. 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’);

  36. 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.

  37. 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

  38. 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.

  39. 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!

  40. 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 10010110, then its value is

  41. 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 10010110, then its value is

  42. Exercises

  43. Exercises • What is the process in going from coding an assembly language program to executable machine code? • What is the ALU? • Perform the binary addition of the following 2 bytes. Give the result in binary, decimal and hexadecimal • 10111011 • 10000110 • How are signed numbers represented in binary? • Does the ADD instruction need to know whether signed or unsigned numbers are being added?

  44. Exercises • When is the carry bit used to indicate overflow? • When is the OV bit used to indicate overflow? • Given the following 8 bit value • 10001001 • What is its value if • 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.

  45. Exercises • What does the ADDC instruction do? Give an example use. • Detail how the following 2 binary values are subtracted 10001011 01111010 • What is the SUBB instruction used for? • If a borrow out from the MSB is generated when using SUBB, where is it stored.

More Related