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
Chapter 2 Summary Overview • Numbers – Decimal, Binary, Octal, Hexadecimal – Their relationship • • • • Sign-magnitude One’s complement Two’s complement Arithmetic operation Positive Numbers • A computer represents positive integers in binary • Three methods for representing negative numbers (signed numbers) are – Sign-magnitude – One’s complement – Two’s complement Sign Magnitude • Representation the number’s sign and magnitude (value) • Sign -- Positive numbers 0 and negative numbers 1 • e.g. 01000000 = 64 and 11000000 = -64 • Easy for human to understand but it requires some special logic for arithmetic operations (addition, subtraction) One’s Complement • The negative number is represented by flipping the number’s bits • E.g., 01001001 becomes 10110110 • E.g. consider 103 + -97 01100111 + (-01100001) = 01100111 + 10011110 = 100000101 (9 bits) 00000101 + 1 = 00000110 (8 bits) 103 – 97 = 6 = 00000110 Try 113 + -42 = ?? and –75 + 13 = ?? Example: Convert –5 into ones complement representation (8 bit) Solution: • First, obtain +5 representation in 8 bits 00000101 • Change every bit in the number from 0 to 1 and vice-versa. • –510 in ones complement is 111110102 Exercise: Get the representation of ones complement (6 bit) for the following numbers: i) +710 ii) –1010 Solution: Solution: (+7) = 0001112 (+10)10 = 0010102 So, (-10)10 = 1101012 Twos complement • Similar to ones complement, its positive number is same as signand-magnitude • Representation of its negative number is obtained by adding 1 to the ones complement of the number. Example: Convert –5 into twos complement representation and give the answer in 8 bits. Solution: First, obtain +5 representation in 8 bits 000001012 Obtain ones complement for –5 111110102 Add 1 to the ones complement number: 111110102 + 12 = 111110112 –5 in twos complement is 111110112 Exercise: • Obtain representation of twos complement (6 bit) for the following numbers i) +710 ii)–1010 Solution: Solution: (+7) = 0001112 (+10) 10 = 0010102 (same as sign-magnitude) (-10) 10 = 1101012 + 12 = 1101102 So, twos compliment for –10 is 1101102 Two’s Complement • Rules – – – – Just add all the bits Throw away EAC (“end around carry”) if a – b becomes a + (-b) e.g. 111111 (-1) 10110 (-10) + 001000 (8) + 11101 (-3) 1000111 (7) 110011 (-13) • try again –75 + 13 in 2’s complement Chapter 3 Summary Relationship Between Basic Operation of Boolean and Basic Logic Gate • The basic construction of a logical circuit is gates • Gate is an electronic circuit that emits an output signal as a result of a simple Boolean operation on its inputs • Logical function is presented through the combination of gates • The basic gates used in digital logic is the same as the basic Boolean algebra operations (e.g., AND, OR, NOT,…) • The package Truth Tables and Boolean Algebra set out the basic principles of logic. Name Graphic Symbol AND A B F OR A B F NOT NAND NOR A A B A B Boolean Algebra F =A. B Or F = AB F =A+ B _ F =A F ____ F=A.B Or F = AB F F _____ F=A+B Truth Table A 0 0 1 1 A 0 0 1 1 B 0 1 0 1 B 0 1 0 1 B 0 1 F 0 0 0 1 F 0 1 1 1 F 1 0 A B 0 0 0 1 1 0 1 1 F 0 1 1 1 A B 0 0 0 1 1 0 1 1 F 0 0 0 1 the symbols, algebra signs and the truth table for the gates Basic Theorems of Boolean Algebra 1. Identity Elements 1.A=A 0+A=A 2. Inverse Elements A.A=0 A+A=1 3. Idempotent Laws A+A=A A.A=A 4. Boundess Laws A+1=1 A.0=0 5. Distributive Laws A . (B + C) = A.B + A.C A + (B . C) = (A+B) . (A+C) 6. Order Exchange Laws A.B=B.A A+B=B+A 7. Absorption Laws A + (A . B) = A A . (A + B) = A 9. Elimination Laws A + (A . B) = A + B A . (A + B) = A . B 8. Associative Laws A + (B + C) = (A + B) + C A . (B . C) = (A . B) . C 10. De Morgan Theorem (A + B) = A . B (A . B) = A + B Relationship Between Boolean Function and Logic Circuit Boolean function Q = AB + B = (NOT A AND B) OR B Logic circuit A A AB B B Q = AB + B Relationship Between Boolean Function and Logic Circuit • Any Boolean function can be implemented in electronic form as a network of gates called logic circuit A B A.B = AB F = AB + C + D C D C+D G = A . (B + C + D) A G = A . (B + C + D) B C D B+C+D C+D Truth Table A A AB B B Produce a truth table from the logic circuit A B A AB Q 0 0 0 1 1 1 0 1 0 1 1 0 0 0 0 1 1 0 0 1 Q = AB + B Karnaugh Map • A graphical way of depicting the content of a truth table where the adjacent expressions differ by only one variable • For the purposes simplification, the Karnaugh map is a convenient way of representing a Boolean function of a small number (up to four) of variables • The map is an array of 2n squares, representing all possible combination of values of n binary variables • Example: 2 variables, A and B B B B A AB AB A AB AB A B A 0 1 1 0 00 01 10 11 4 variables, A, B, C, D 24 = 16 squares AB CD CD CD AB 0000 0001 AB 0100 AB 1100 AB 1000 CD CD • List combinations in the order 00, 01, 11, 10 00 01 11 C AB AB AB AB 0 C 000 010 110 100 1 C 001 011 111 101 C 0 C 1 C 00 AB 000 001 01 AB 010 011 11 AB 110 111 10 AB 100 101 AB AB 10 How to create Karnaugh Map Truth Table A B C F 0 0 0 1 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 0 1 1 1 0 1. Place 1 in the corresponding square Karnaugh Map BC BC 00 A 0 1 A A 1 1 BC 01 BC 11 1 1 BC 10 Karnaugh Maps to Represent Boolean Functions AB 00 AB 01 AB 1 11 AB 10 AB 1 F = AB + AB 2. Group the adjacent squares: Begin grouping square with 2n-1 for n variables • e.g. 3 variables, A, B, and C 23-1 = 22 = 4 = 21 = 2 = 20 = 1 BC BC 00 A 0 1 A 1 A 1 BC BC 01 BC 11 BC 10 1 1 AB ABC F = BC + AB + ABC BC A BC 00 BC 11 BC 10 1 A 0 A 1 BC 01 1 1 A F = A + BC BC 1 1 3 variables: 23-1 = 22 = 4 22-1 = 21 = 2 21-1 = 20 = 1 4 variables, A, B, C, D 24-1 = 23 = 8 (maximum); 22 = 4; 21 = 2; 20 = 1 (minimum); AB CD 00 00 1 01 1 11 1 10 1 F= 01 11 10 1 1 CD + BD + ABC 1 Karnaugh Map Boolean Function Logic Circuit Chapter 5 Summary Contains the beginning address of each segment SS Register Address Stack segment DS Register Address Data segment CS Register Address Code segment Segment register (in CPU) memory (MM) 32 bits 8 bit 8 bit AH AL AX EAX Value in AX = FFFFH, what is the value in AX after the following instruction is executed? MOV AL, 09H AX = ???? MOV AX,0009H AX = ???? 32 bits 8 bit 8 bit CH CL CX ECX Value in CX = FFFFH, what is the value in CX after the following instruction is executed? MOV CX, 25H CX = ???? MOV CL, 0 CX = ???? How many bytes/ bits for the following instructions? MOV AX, 0123 MOV AH, 09 What are the values in CS register and IP register? What is the value in IP after MOV CL,42 is executed? CHAPTER 6 ASSEMBLY LANGUAGE PROGRAM FORMAT AND DATA DEFINITION 6.1) Assembly Language Program Format 6.2) Features of Assembly Language 6.3) Data Definition PAGE directive to establish 60 lines and 132 columns per page TITLE directive to identify the program’s name as A04ASM1 CODESEG to define the code segment ; symbol is for comment STACK to define the stack segment DATASEG to define the data segment ASSUME directive is used to tell the assembler the starting address of segments with the segment registers Procedure MAIN END directive to tell the assembler that this is the end of the source program Initialize the address of data segment in DS Request to end the program and return to the OS Ending Program Execution After executing an assembly language program, the programmer must tell the system to terminate the executing program with the help of DOS interrupt services. INT 21H is the commonly used interrupt service. It used the function code in the AH register to determine the next course of action. INT 21H can also be used to control input from the keyboard, control the screen, disk I/O and output to the printer. INT 21H with function code 4CH is used to terminate program execution. The function code 4CH must be priory entered into AH. Example: Data Definition Assembler offers a few directives that enable programmers to define data according to its type and length. Format for data definition: [name] Data names are optional because in assembly language programming, data is not necessarily reference by its name. Dn Directive Next slide are the common directives to define data and also directives used in MASM 6.0 The following are some examples of numeric and character data definition Page 60, 132 TITLE A04DEFIN (EXE) Define data directives .MODEL SMALL .DATA ;DB – Define Bytes: ;----------------------BYTE1 DB ? ; Uninitialized BYTE2 DB 48 ; Decimal constant BYTE3 DB 30H ; Hex constant BYTE4 DB 01111010B ; Binary constant BYTE5 DB 10 DUP (0) ; Ten zeros BYTE6 DB ‘PC FAIR’ ; Character string BYTE7 DB ‘12345’ ; Number as characters BYTE8 DB 01, ‘Jan’, 02, ‘Feb’ ; Table of months ;DW – Define Words: ;------------------------WORD1 DW 0FFF0H WORD2 DW 01111010B WORD3 DW BYTE8 WORD4 DW 2, 4, 6, 7, 9 WORD5 DW 6 DUP (0) ;DQ – Define Doublewords: ;--------------------------------DWORD1 DD ? DWORD2 DD 41562 DWORD3 DD 24, 48 DWORD4 DD BYTE3 – BYTE2 END ; Hex constant ; Binary constant ; Address constant ; Table of 5 constants ; Six zeros ; Uninitialized ; Decimal value ; Two constants ; Difference between addresses DB or BYTE -to define item with the size of one byte. The range of its value is stated in the table before. DW or WORD -to define item with the size of one word or 2 bytes. The range of its value is stated in the table before. Assembler will change numeric constants into binary object code (presented in hexadecimal) and kept in the memory in reverse bytes. For instance, if the real value is 3039H it will be kept as 3930H in the data segment DD or DWORD - to define item with the size of 4 bytes. Its range of values is stated in the table above. As usual data is kept in reverse byte or reverse sequence. For example, if data is 00BC614EH it will be kept as 4E61BC00H. Expressions Expressions in operand may specify an uninitialized value or a constant value. Example: DATAX DATAYDB DB 25 ? ; Uninitialized item, size of 1 bait ; Initialized item, DATAY with value 25 Uninitialized item is used to store a value which size is defined. The value of a data can be used and edited to suit the program’s needs. Expressions can contain a few constants that is separated by the sign ‘,’ and the quantity is limited to the row length. Example: DATAZ DB 21, 22, 23, 24, 25, 26, … The assembler defines the above constant byte by byte , from left to right. DATAZ or DATAZ+0 contains the value 21, DATAZ+1 contains 22, DATAZ+2 contains 23 and so forth. Example of instruction MOV AL, DATAZ+3 will enter the value 24 into the CL register Expressions also allows duplication of constants using the format below: Example: DW 10 DB 5 0C0C0C0C0C DB 3 DUP(?) DUP(12) ; Ten words, uninitialized ; Five bytes containing DUP(5 DUP(4)) ; Fifteen 4s CHAPTER 7 ASSEMBLY LANGUAGE INSTRUCTIONS 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 Introduction Data Transfer Instructions Arithmetic Instructions Bit Shifting Instructions Looping Instructions Unconditional Transfer Instructions Conditional Jump Instructions Other Instructions Data Transfer Instructions Some examples on MOV: BYTEFLD WORDFLD DB DW … MOV MOV MOV MOV MOV EDX , ECX BYTEFLD , DH WORDFLD , 1234 CH , BYTEFLD CX , 40H MOV AX , DS ? ? ; define byte ; define word ; ; ; ; ; register to register register to memory immediate to memory memory to register immediate to register ; segment register to register 7.3.1 Addition and Subtraction Of Binary Data Example of the ADD and SUB instructions: BYTE1 WORD1 MOV MOV ADD SUB ADD MOV MOV SUB SUB ADD DB DW 24H 4000H . . . CL , BYTE1 DL , 40H CL , DL CL , 20H BYTE1 , BL CX , WORD1 DX , 2000H CX , DX CX , 124H WORD1 , DX ;Data elements ; byte processing ; ; ; ; register to register Immediate from register register to memory word processing ; register from register ; Immediate from memory ; register to memory MUL is used for unsigned data Examples on the usage of the MUL instructions using the data as defined below: BYTE1 BYTE2 WORD1 WORD2 DWORD1 DWORD2 DB DB DW DW DD DD 80H 40H 8000H 2000H 00018402H 00012501H (a) MOV MUL AL, BYTE1 BYTE2 ; AL (multiplicand)=80H ; byte x byte, product in AX ; 80H x 40H, AX= 2000H (b) MOV MUL AX, WORD1 WORD2 ; ; ; ; AX (multiplicand)=8000H word x word, product in DX:AX 80000H x 2000H, DX:AX= 1000 0000H The following are a few examples of the IDIV instruction using the data definition below: BYTE1 BYTE2 WORD1 WORD2 WORD3 DB DB DW DW DW 80H 16H 2000H 0010H 1000H (a) MOV AX, WORD1 IDIV BYTE1 ; ; ; ; (b) MOV DX, WORD2 MOV AX, WORD3 ;DX=0010H IDIV WORD1 ; Byte value ; Word value AX=2000H 2000H(+ve)/80H (-ve), quotient=C0H (-ve), remainder=00H AL=C0H, AH=00H ;AX=1000H,dividend in DX:AX (WORD2:WORD3) ;DX:AX = 0010 1000H (+ve) ;00101000H (+ve)/2000H (+ve) ;remainder:quotient in DX:AX ;1000H:0080H Example of the SHR instruction As in the example above, the SHR instruction will enter the value 0 to the leftmost bit after the shift. Carry flag will contain the last bit shifted out after the shift C BH 1 0 1 1 0 1 1 1 0 1 0 1 1 0 1 1 1 0 0 0 1 0 1 1 0 1 SHR BH, 01 0 SHR BH, CL 00 SHR BH, 02 00 0 0 0 0 0 1 0 1 1 Example of the SAR instruction The SAR instruction is used on signed number. SAR will enter the sign bit (whether 0 (+ve) or 1 (–ve)) into the leftmost bit after every shift. Examples of SAR instruction BH 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 0 1 1 0 1 SAR BH, 01 1 SAR BH, CL 11 SAR BH, 02 11 1 1 1 1 1 1 0 1 1 0 0 0 0 0 1 0 1 BH 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 00 00 A few examples on ROR: 1 2 3 The difference between ROR and RCR is only the way of operation. In RCR, every bit that is rotated will enter the carry flag before entering the leftmost bit 1 BH 1 0 1 1 0 1 1 1 1 2 1 1 0 1 1 0 1 1 1 011 3 0 011 1 1 1 1 0 1 1 0 Below are instances of the ROL instruction : 1 2 3 1 2 1 0 1 1 0 1 1 1 0 1 1 0 1 1 1 1 BH 1 1 1 011 3 1 0 1 1 1 1 0 1 1 011 LOOP Instruction – example using DEBUG -A 100 4A66:0100 4A66:0103 4A66:0106 4A66:0108 MOV CX,5 MOV AX,0 ADD AX,CX LOOP 106 ;LOOP COUNTER=5 ;LOOP TO LOCATION 0106