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 = 64 and = -64 Easy for human to understand but it requires some special logic for arithmetic operations (addition, subtraction)
Arithmetic Operation Computers mostly storing information, particularly integers, in byte form In this section we look how addition and subtraction are performed in computers To perform mathematical functions, a CPU contains a component called ALU (arithmetic logic unit) – to perform arithmetic operations (addition & subtraction) and logic operation (in next chapter)
One’s Complement The negative number is represented by flipping the number’s bits E.g., becomes E.g. consider ( ) = = (9 bits) = (8 bits) 103 – 97 = 6 = Try = ?? and – = ??
Ones Complement In the ones complement representation, positive numbers are same as that of sign-and-magnitude Example: +5 = (8 bit) as in sign-and-magnitude representation Sign-and-magnitude and ones complement use the same representation above for +5 with 8 bits and all positive numbers. For negative numbers, their representation are obtained by changing bit 0 → 1 and 1 → 0 from their positive numbers
Example: Convert –5 into ones complement representation (8 bit) Solution: First, obtain +5 representation in 8 bits Change every bit in the number from 0 to 1 and vice-versa. –5 10 in ones complement is
Exercise: Get the representation of ones complement (6 bit) for the following numbers: i)+7 10 ii) –10 10 Solution: (+7) = Solution: (+10) 10 = So, (-10) 10 =
Twos complement Similar to ones complement, its positive number is same as sign- and-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 Obtain ones complement for –5 Add 1 to the ones complement number: = –5 in twos complement is
Exercise: Obtain representation of twos complement (6 bit) for the following numbers i)+7 10 ii)–10 10 Solution: (+7) = (same as sign-magnitude) Solution: (+10) 10 = (-10) 10 = = So, twos compliment for –10 is
Two’s Complement Makes addition and subtraction simple The fact that all numbers are stored in 2’s complement form allows the ALU to use just one circuit for all basic mathematical operations Visual Basic uses 2’s complement representations of numbers
Two’s Complement Rules –Just add all the bits –Throw away EAC (“end around carry”) –if a – b becomes a + (-b) –e.g (-1)10110(-10) (8) (-3) (7) (-13) try again – 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. A B F A B F A B F A B F F F F F Name Graphic Symbol Boolean Algebra Truth Table ABAB ABAB ABAB ABAB A F AND OR NOT NAND NOR F = A. B Or F = AB F = A + B _____ F = A + B ____ F = A. B Or F = AB _ F = A B F the symbols, algebra signs and the truth table for the gates
1. Identity Elements 2. Inverse Elements 1. A = A A. A = A = A A + A = 1 3. Idempotent Laws 4. Boundess Laws A + A = A A + 1 = 1 A. A = A A. 0 = 0 5. Distributive Laws 6. Order Exchange Laws A. (B + C) = A.B + A.C A. B = B. A A + (B. C) = (A+B). (A+C) A + B = B + A 7. Absorption Laws 8. Associative Laws A + (A. B) = AA + (B + C) = (A + B) + C A. (A + B) = AA. (B. C) = (A. B). C 9. Elimination Laws 10. De Morgan Theorem A + (A. B) = A + B (A + B) = A. B A. (A + B) = A. B (A. B) = A + B Basic Theorems of Boolean Algebra
Relationship Between Boolean Function and Logic Circuit A B Q Boolean function Q = AB + B = (NOT A AND B) OR B Logic circuit A AB B = 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 F A.B = AB C D C + D = AB + C + D
G = A. (B + C + D) A B C D C + D B + C + D
Truth Table
A B Q A AB B = AB + B Produce a truth table from the logic circuit ABAABQ
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 2 n squares, representing all possible combination of values of n binary variables Example: 2 variables, A and B A B B A B A B A B A
AB C D A B C D A B CD A B C D 4 variables, A, B, C, D 2 4 = 16 squares
AB C C C ABC C List combinations in the order 00, 01, 11, 10 C
ABCF Truth Table Karnaugh Map BC A 0 1 A A How to create Karnaugh Map 1.Place 1 in the corresponding square
AB F = AB + AB A B Karnaugh Maps to Represent Boolean Functions
2.Group the adjacent squares: Begin grouping square with 2 n-1 for n variables e.g. 3 variables, A, B, and C = 2 2 = 4 = 2 1 = 2 = 2 0 = BC A 0 1 A A AB BC ABC F = BCABABC + +
BC A 0 1 A A 3 variables: = 2 2 = = 2 1 = = 2 0 = 1 A BC F = A + BC
AB CD variables, A, B, C, D = 2 3 = 8 ( maximum ); 2 2 = 4; 2 1 = 2; 2 0 = 1 ( minimum ); CD +BDABC+F =
Karnaugh Map Boolean Function Logic Circuit
Chapter 5 Summary
Address Stack segment Data segment Code segment Contains the beginning address of each segment Segment register (in CPU) memory (MM) SS Register DS Register CS Register
MOV AL, 09H AX = ???? 8 bit 32 bits AHAL AX EAX MOV AX,0009H AX = ???? Value in AX = FFFFH, what is the value in AX after the following instruction is executed?
8 bit 32 bits CX CHCL ECX MOV CX, 25H CX = ???? MOV CL, 0 CX = ???? Value in CX = FFFFH, what is the value in CX after the following instruction is executed?
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?
6.1) Assembly Language Program Format 6.2) Features of Assembly Language 6.3) Data Definition CHAPTER 6 ASSEMBLY LANGUAGE PROGRAM FORMAT AND DATA DEFINITION
PAGE directive to establish 60 lines and 132 columns per page TITLE directive to identify the program’s name as A04ASM1 ; symbol is for comment STACK to define the stack segment DATASEG to define the data segment CODESEG to define the code segment
ASSUME directive is used to tell the assembler the starting address of segments with the segment registers Initialize the address of data segment in DS Procedure MAINEND directive to tell the assembler that this is the end of the source program 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 TITLEA04DEFIN (EXE)Define data directives.MODELSMALL.DATA ;DB – Define Bytes: ; BYTE1DB?; Uninitialized BYTE2DB48; Decimal constant BYTE3DB30H; Hex constant BYTE4DB B; Binary constant BYTE5DB10 DUP (0); Ten zeros BYTE6DB‘PC FAIR’; Character string BYTE7DB‘12345’; Number as characters BYTE8DB01, ‘Jan’, 02, ‘Feb’; Table of months
;DW – Define Words: ; WORD1DW0FFF0H; Hex constant WORD2DW B; Binary constant WORD3DWBYTE8; Address constant WORD4DW2, 4, 6, 7, 9; Table of 5 constants WORD5DW6 DUP (0); Six zeros ;DQ – Define Doublewords: ; DWORD1DD?; Uninitialized DWORD2DD41562; Decimal value DWORD3DD24, 48; Two constants DWORD4DD BYTE3 – BYTE2; Difference between addresses END
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 DB?; Uninitialized item, size of 1 bait DATAYDB25; 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: DATAZDB21, 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: DW10DUP(?); Ten words, uninitialized DB5DUP(12); Five bytes containing 0C0C0C0C0C DB3DUP(5 DUP(4)); Fifteen 4s
CHAPTER 7 ASSEMBLY LANGUAGE INSTRUCTIONS 7.1Introduction 7.2Data Transfer Instructions 7.3Arithmetic Instructions 7.4Bit Shifting Instructions 7.5Looping Instructions 7.6Unconditional Transfer Instructions 7.7Conditional Jump Instructions 7.8Other Instructions
Data Transfer Instructions Some examples on MOV: BYTEFLDDB?; define byte WORDFLDDW?; define word … MOV EDX, ECX; register to register MOV BYTEFLD, DH; register to memory MOV WORDFLD, 1234; immediate to memory MOV CH, BYTEFLD; memory to register MOV CX, 40H; immediate to register MOV AX, DS; segment register to register
Example of the ADD and SUB instructions: BYTE1DB24H;Data elements WORD1DW4000H... MOVCL, BYTE1; byte processing MOVDL, 40H ADDCL, DL; register to register SUBCL, 20H; Immediate from register ADD BYTE1, BL; register to memory MOVCX, WORD1; word processing MOV DX, 2000H SUBCX, DX; register from register SUBCX, 124H; Immediate from memory ADDWORD1, DX; register to memory Addition and Subtraction Of Binary Data
MUL is used for unsigned data Examples on the usage of the MUL instructions using the data as defined below: BYTE1DB80H BYTE2DB40H WORD1DW8000H WORD2DW2000H DWORD1DD H DWORD2DD H (a) MOV AL, BYTE1 ; AL (multiplicand)=80H MULBYTE2 ; byte x byte, product in AX ; 80H x 40H, AX= 2000H (b) MOVAX, WORD1 ; AX (multiplicand)=8000H MULWORD2 ; word x word, product in DX:AX ; 80000H x 2000H, ; DX:AX= H
The following are a few examples of the IDIV instruction using the data definition below: BYTE1DB80H; Byte value BYTE2DB16H WORD1DW2000H ; Word value WORD2DW0010H WORD3DW1000H (a) MOV AX, WORD1 ; AX=2000H IDIV BYTE1 ; 2000H(+ve)/80H (-ve), ; quotient=C0H (-ve), remainder=00H ; AL=C0H, AH=00H (b) MOV DX, WORD2 ;DX=0010H MOV AX, WORD3 ;AX=1000H,dividend in DX:AX (WORD2:WORD3) ;DX:AX = H (+ve) IDIV WORD1 ; H (+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 BH C SHR BH, SHR BH, CL SHR BH,
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 SAR BH, SAR BH, CL SAR BH,
BH
A few examples on ROR: 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 BH
Below are instances of the ROL instruction : BH
LOOP Instruction – example using DEBUG -A 100 4A66:0100MOV CX,5;LOOP COUNTER=5 4A66:0103MOV AX,0 4A66:0106ADD AX,CX 4A66:0108LOOP 106;LOOP TO LOCATION 0106