computer organization cpe 005b (tip reviewer)

Upload: james-lindo

Post on 05-Mar-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Computer Organization CPE 005B (TIP Reviewer)

TRANSCRIPT

  • Instructor: Rodelyn M. Aguinaldo*Announced quiz, normally every Friday.*Edmodo Account*Blue pen during Lab activities.

    Convert 18.6875D to binaryIntegral Part = 18D 18/2 => quotient=9 remainder=0 9/2 => quotient=4 remainder=1 4/2 => quotient=2 remainder=0 2/2 => quotient=1 remainder=0 1/2 => quotient=0 remainder=1 (quotient=0 stop) Hence, 18D = 10010BFractional Part = .6875D .6875*2=1.375 => whole number is 1 .375*2=0.75 => whole number is 0 .75*2=1.5 => whole number is 1 .5*2=1.0 => whole number is 1 Hence .6875D = .1011BTherefore, 18.6875D = 10010.1011B

    From

    Convert 18.6875D to hexadecimalIntegral Part = 18D 18/16 => quotient=1 remainder=2 1/16 => quotient=0 remainder=1 (quotient=0 stop) Hence, 18D = 12HFractional Part = .6875D .6875*16=11.0 => whole number is 11D (BH) Hence .6875D = .BHTherefore, 18.6875D = 12.BH

    From

    1.9 Exercises (Number Systems Conversion)Convert the following decimal numbers into binary and hexadecimal numbers:1.1082.48483.90004.

    Convert the following binary numbers into hexadecimal and decimal numbers:5.10000110006.100000007.1010101010108.

    Convert the following hexadecimal numbers into binary and decimal numbers:9.ABCDE10.

    Computer Organization (Lecture 1)Sunday, November 09, 2014 10:59 PM

    CPE 005B Page 1

  • ABCDE10.123411.80F12.

    Convert the following decimal numbers into binary equivalent:13.19.25D14.123.456D15.

    Answers: You could use the Windows' Calculator (calc.exe) to carry out number system

    conversion, by setting it to the scientific mode. (Run "calc" Select "View" menu Choose

    "Programmer" or "Scientific" mode.)

    1101100B, 1001011110000B, 10001100101000B, 6CH, 12F0H, 2328H.16.

    218H, 80H, AAAH, 536D, 128D, 2730D.17.

    10101011110011011110B, 1001000110100B, 100000001111B, 703710D, 4660D, 2063D.18.

    ??19.

    From

    CPE 005B Page 2

  • Registers: small areas that can store data temporarily* the areas are so small that it cannot store files, but the address of these files while the program is running.

    Ax contains the results of operations like 9+2.i.Divided into dual 8 bits (AH 15-8, AL 7-0)ii.Example: mov ahiii.

    Ax register - accumulator used to hold the first operand. a.

    Bx register - base addresses (bh, bl)b.Cx register - counter register (ch, cl)c.

    Example: mov dli.Dx register - data registerd.

    Data Register: AX,BX,CX,DX. 1.

    Segment Addresses: ES,DS,SS,CS2.Index Addressing: SP,BP,SI,DI3.Instruction Pointer: IP4.Flag Register: Flag5.

    16 bit registers (e.g. 8086, 8088, 80286) have 14 16-bit registers

    mov dl, 41 means:Dh = 00Dl = 41Dx = 0041

    32 bit registers: 80386*The architecture (16bit or 32 bit) determines the width of the data lines. The wider the faster!

    For arithmetic and data movement a.General Purpose Registers1.

    Segment Registers2.Other Registers3.

    Three Categories of Registers:

    General Purpose Registers: Used for data movement, arithmetic and logic operations.

    Addition:ADD destination, sourceADD destination, immediate dataADC, which means to add the two operands plus the carry flag.

    Subtraction:SUB destination, sourceSUB destination, immediate dataSBB which means to subtract minus the carry flag*Carry Flag is set to 1 if a larger number was subtracted from a smaller number and a borrow has occurred

    CPU RegistersSunday, November 30, 2014 9:45 PM

    CPE 005B Page 3

  • *Carry Flag is set to 1 if a larger number was subtracted from a smaller number and a borrow has occurred

    Multiplication and DivisionMUL / DIV = unsignedIMUL / IDIV = signed

    Default Operands for Multiplication

    No. of Bits Multiplicand Multiplier Product

    8 bits x 8 AL register or memory AX (16 bits)

    16 bits x 16 AX register or memory DX:AX (32 bits)

    32 bits x 32 EAX register or memory EDX:EAX (64 bits)

    Default Operands for Division

    No. of BIts Dividend Divisor Quotient Remainder

    16 bits / 8 AX register, memory (8-bit) AL AH

    32 bits / 16 DX:AX register, memory (16-bit) AX DX

    64 bits / 32 EDX:EAX register, memory (32-bit) EAX EDX

    *Note: Remainder sign will follow the dividend sign.

    Flags- are register consists of individual binary bits that control the operation of the CPU or reflect the result of arithmetic and logical instructions. - set when equals to 1 or reset / clear when it equals 0.

    AudioRecording

    Carry - No Carry NC (0) or Carry CY (1). Kung may carry!1.*sets when there is a carry on the Most Significant Digit*Set if the result of the instruction has gone beyond the limit of the data size (ie. a "carry" has occurred). *In addition, 255 +1 sets the carry flag, since 256 is beyond the upper limit of 255. *1111 + 0001 = 0000 (carry flag is turned on)*In subtraction, 02 - 04 also sets the carry flag since -2 is beyond the lower limit of 0.*0000 - 0001 = 1111 (carry flag is turned on)

    Sign - Sign Flag PL (0) or Sign Flag Negative NG (1). Kung negative!2.*sets when the most significant digit (leftmost bit) is equal to 1.

    Parity - Parity Odd (0) or Parity Even (1). Even ba yung number of ones!3.*Only changes AFTER AN ARITHMETIC OPERATION.*The parity flag indicates whether there are an even or odd number of bit set in the data.

    CPE 005B Page 4

  • *The parity flag indicates whether there are an even or odd number of bit set in the data.*Convert the result to binary to find out if it's parity even or parity odd.

    Result: 0000 --> #1s = 0. Parity even!1)Result: FC1F --> 1111 / 1100 / 0001 / 1111. #1s = 11. Parity odd!2)

    Example:

    Auxiliary Carry - No Auxiliary Carry NA (0) or Auxiliary Carry AC (1). Kung may carry sa gitna!4.*For 8 bits, sets if there's a carry from 4th to 5th bit.*For 16 bits, sets if there's a carry from 8th to 9th bit.*For 32 bits, sets if there's a carry from 16th to 17th bit.

    Example:AX = 1111 0101 +1111 1010 0110 1111*Turns on the carry flag, but no auxiliary carry.AX = 1111 1111 +0000 0001 0000 0000*Turns on the carry flag and the auxiliary carry.

    Overflow Flag - No Overflow NV (0) or Overflow OV (1). For signed numbers only!5.*Kailangang parehong 1 or 0 ang addends, tapos kabaliktaran ang result. 1 +1 = 0 or 0 + 0 =1

    If the sum of two numbers with the sign bits off yields a result number with the sign bit on, the "overflow" flag is turned on.1.*0100 + 0100 = 1000 (overflow flag is turned on)If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is turned on.2.*1000 + 1000 = 0000 (overflow flag is turned on)

    Rules for Overflow Flags:

    * Note that you only need to look at the sign bits (leftmost) of the three numbers to decide if the overflow flag is turned on or off.

    Zero: No Zero NZ(0) or Zero ZR (1). Kung equal to zero yung value!6.*Tells us if the result is equal to zero.*1000 - 1000 = 0000 (ZR)

    CPE 005B Page 5

  • *When we say basic structure, we refer to the computer's architecture.

    Before, data transmission is half duplex only, one goes north or one goes south, but now the data road is widening!

    a.Speed or Clock Rate1.

    Number of storage locationa.Memory2.

    Factors that affect Architecture:

    Super computers1.Mainframe computers2.Mini computers3.Micro computers4.

    Computers from most powerful to least powerful

    *We will talk about microcomputer!*It uses microprocessor as its central processing unit.

    Vacuum Tubes - bulky, needs airconditioning unit1.Transistor and Diodes - reduction of size but still big and expensive2.

    Small Scale Integration (SSI) - 1 - 50 transistorsa.Medium Scale Integration (MSI)b.Large Scale Integration (LSI)c.Very Large Scale Integration (VLSI)d.

    Integrated Circuit3.

    Microprocessor - used for single user computer4.Robotics and AI5.

    History (Generation of Computers)

    *Decrease in size and costs, yet increase in speed and capability*Silicon Valley in San Francisco, California.*Microns - Smallest wire on an IC. 100 microns is approximately as large as a strand of hair

    Input: keyboard, mouse, switch, joystick, keypad, mousepada.Output: Monitor, printer, speakerb.

    I / O (input and output)1.

    *monitor can also be both an input and outpuet

    CU and ALU are registers on the computer.a.This part houses the Arithmetic Logic Unit (ALU), which is responsible for arithmetic operations (MDAS) and logical operation (AND/OR, NOT)

    b.

    This part also houses the Central Unit, which controls the operation of the computer

    c.

    CPU (Central Processing Unit)2.

    What's inside a microcomputer?

    Basic Structure of a ComputerMonday, December 01, 2014 12:47 AM

    CPE 005B Page 6

  • Buses are modes of data transmission on computers.

    System bus1.Internal bus2.

    2 kinds of bus

    Data bus - contains the computer data (bi directional) 1.Address bus - contains address or location (unidirectional, from CPU to Memory / IO)2.Control bus - controls the syncing of data transmission3.

    3 Kinds of Bus

    CPE 005B Page 7

  • CPE 005B Page 8

  • Bit (Binary Digit) - 0, 1 (smallest unit that represents data inside the computer)* Most significant bit - left most bit* Least significant bit - right most bit*Signed bit = - (1) or + (0)*Unsigned bit = +

    Bytes: 8 bits, basic storage unit for IA-32-based computerWords: 16 bitsDouble Words: 32 bitsQuad Words: 64 bits

    Storage Type Unsigned Range Signed Range

    Byte 0 --> 28-1 -27 --> (27-1)

    Word 0 --> 216-1 -215 --> (215-1)

    Double Word 0 --> 232-1 -231 --> (231-1)

    Quad Word 0 --> 264-1 -263 --> (263-1)

    Measurements Number of Bytes

    Kilobyte 210

    Megabyte 220

    Gigabyte 230

    Terabyte 240

    Petabyte 250

    Exabyte 260

    Zettabyte 270

    Yottabyte 280

    Data RepresentationMonday, December 15, 2014 9:18 PM

    CPE 005B Page 9

  • *Converting Hexadecimal to 1s complement = Subtract 15 per digit*Negative signed hexadecimal = greater than 8 yung MSD or leftmost.*Kapag subtraction, convert to 2s complement. Tanggalin ang MSD and iadd.

    Number SystemMonday, December 15, 2014 11:26 PM

    CPE 005B Page 10

  • -T: Examine the contents of the registers.-R AX: Modify values of AX-R F: Modify values of flags

    Know the difference:* mov al,00* sub al,01=ax: 00FF

    *mov ax,0000* sub ax,0001= ax:FFFF

    *Setting ax or any register to 0000 doesn't trigger the or Parity flags.

    Lab ActivitiesMonday, December 15, 2014 11:48 PM

    CPE 005B Page 11

  • Debug has many limitations:* It does not let you create symbolic names, or insert and delete individual source code lines.

    Integer constant can end with Radix symbols: h (hexadecimal), q or o (octal), d or no radix (decimal) and b (binary)

    a.

    Upper case or lower case do not matter.b.When a hexadecimal constant begins with a letter, a leading zero must be placed.c.Constant expression consists of combinations of numeric literals, operators and defined symbolic constants. The expression value must be able to be determined at assembly time, and its value cannot change at the runtime. (e.g 4*20, -3*4/6)

    d.

    Symbolic constant created by assigning a constant expression to a name. (e.g. rows = 5, columns = 10, tablePOS= rows*columns). *Can only be evaluated at assembly time.

    e.

    Character or String Constant - a constant may also represent a string of characters in either single or double quotation marks. Embedded quotes are permitted. (e.g. 'ABC', "X"). Each characters is one byte long.

    f.

    Numeric Literal - combination of digits, signs, a decimal point and an exponent. (e.g. 5, 5.5, -5.5, 26. E+0.5)1.

    Statements 2.

    Elements of Assembly Language

    Statements are free form, meaning that they can be written in any column with any number of spaces between each operand.

    a.

    Statements should be written on a single line and cannot pass column 128, unless with the use of backslash.

    b.

    instructions - statements executed by the processor at runtime. i.

    Count DB 50 directive tells the assembler to create a storage for a byte variable named count and initialize it to 50.

    1)

    .Stack 4096 directive tells the assembler to reserve 4096 bytes of stack space.2)

    Directives - statements that provide information to tell the assembler how to generate executable code. A statement that affects either the program listing or the way machine code is generated.

    ii.

    Two classes of Statementsc.

    Names: identifies a label, variable, symbol or keyword. It may contain any of the following characters: 3.

    3.1 Basic Elements of Assembly Language

    CPE 005B Page 12

  • Max of 247 characters in MASMa.No distinction between uppercase and lower case letters. b.Can start with @, letter, _ or $, but avoid @ since it is associated with other predefined symbols. c.Variable - a location in a program's data area that has been assigned a name. (count1 db 50; a variable (memory allocation)

    d.

    Label - place markers when a program needs to jump or loop from one location to another. e.

    Keyword. A keyword always has some predefined meaning to the assembler. (mov, proc, title, add, ax, END)

    f.

    CPE 005B Page 13

  • Code Segments: where program instructions are stored1.Data Segments: contains all variables2.Stack Segments: contains the program's runtime stack3.

    Segments: building blocks of programs

    *.model small - a directive that indicates that uses a type of structure in which the program uses no more than 64K of memory for code, and 64K for data.*.stack 100h - a directive that sets aside 100h (256) bytes of stack space for the program*.data - a directive that marks the beginning of the data segment where variables are stored. The assembler allocates a block of memory to hold the string containing "Hello,world!" along with two bytes containing a newline character sequence (odh, 0ah). String terminator ($).*.code marks the beginning of the code segment where the executable instructions are located.*PROC directive declares the beginning of a procedure. Main is the procedure!*mov ax, @data / mov ds, ax --> To copy the address of the data segment into DS register. *mov ah,09 --> display a character string.*mov ax,4C00h / int 21h) --> halt the pgoram and return control of the operating system.z*MAIN ENDP --> to end main proc*end main --> ending

    3.2 Sample Hello Program

    CPE 005B Page 14

  • CPE 005B Page 15

  • *Assembler: a utility program that converts a source program into an object file*Linker: a program that converts object files into executable programs

    Can use text editor.1.Can use symbolic names for variables, rather than hard-coded numeric addresses.2.

    Advantages of an assembler than debug:

    Programs can take advantage of existing libraries full of useful subroutines.1.Advantage of a Linker:

    Listing file (optional) - produced by assembly. copy of the program's source file (suitable for printing) with line numbers and translated machine code. To create a listing file, use the coding below.

    Map file (optional) - produced by linker. Contains information about the program's code, data and stack segments. To create a map file:

    Link library - file containing subroutines that are already compiled into machine language

    Warning Messages: will assemble but the object file may have errors.

    3.3 Assembling, Linking and DebuggingSunday, January 11, 2015 11:15 PM

    CPE 005B Page 16

  • Variable - symbolic name for a location in memory where some data are stored.Offset of a variable - distance from the beginning of the segment to the variable.dataaList db "ABCD"

    *If the first letter is at offset 0,the next one is at offset 1.*the offset of aList is the same as the offset of its first letter.

    Data Allocation Directives

    Define Byte: For storage of one or more byte values.1.Single Initializers:

    *Question mark (?) assigned an undefined intitial contents.Multiple Initializers:

    *To know if it fits one byte, convert to hexadecimal!

    *_len = computes the length of the string!

    DUP Operator: duplicates or repeat one or more values when allocating space.

    3.4 Data Allocation Directives

    CPE 005B Page 17

  • DUP Operator: duplicates or repeat one or more values when allocating space.

    Define Word - for storage of 2byte-values.2.

    Pointer. The offset of a variable or subroutine can be stored in another variable, called a pointer.

    Defined Doubleword (DD) - for storage of 4byte values.3.*When storing word, the first offset is given on the rightmost side.

    CPE 005B Page 18

  • Equate Directives - allow constants and literals to be given symbolic names.

    Allocates no storage (unlike DB, DW)a.All occurrences of the name is replaced by the expression.b.

    Equal-Sign Directive (or redefinable equate) - the equal sign directive creates an absolute symbol by assigning the value of a number expression to a name. name = expression.

    1.

    EQU directive - assigns a symbolic name to a string or numeric constant. Same as equal sign directive, but cannot be redefined later in the program.

    2.

    TEXTEQU directive3.

    3.5 Symbolic ConstantsMonday, January 12, 2015 12:50 AM

    CPE 005B Page 19

  • Mov Instruction: copies data from one operand to another1.

    * reg should be any non-segment register, except that IP cannot be a target operand.* sizes of both operands must be the same.

    *CS cannot be a target operand.* mov ax, var1 / mov var2, ax --> to transfer contents from var1 to var2.

    Operands with Displacements2.*Direct Offset Addressing - lets you access memory values that do not have their own labels.a. When dealing with db, DB + 1 means next character.b. When dealing with dw, DW + 2 means next word.c. When dealing with DD, DB+4 means next doubleword.

    XCHG Instruction = exchange values.l3.

    INC / DEC = +1 / -14.

    Sizes of operands (source and destination) must be the same.1.Only one memory can be an operand. Not both.2.

    ADD / SUB Instruction5.

    3.6 - 3.7 Data Transfer and Arithmetic Instructions

    CPE 005B Page 20

  • Immediate - Constant1.Register - one of the CPU registers2.Memory - reference to a location in memory.3.

    Three Basic Types of Operands

    Displacement: either a number or the offset of a variableEffective Address of an operand refers to the offset (distance) of the data from the beginning of its segment.Addressing mode - used by an instruction refers to the type of memory operand in use.

    Address: 121 Maple Street.Maple Street - house's base location121 - offset from the beginning of the street.

    Register Operands - CPU register and no memory access is required.1.

    Immediate Operands - is a constant expression2.

    Direct Operands: refers to the contents of memory at a location identified by a label in the data segment.

    3.

    3.8 Basic Operand Types

    CPE 005B Page 21

  • Offset Operator: returns the 16bit offset of a variable.

    Direct-Offset Operands: the +/- adds or subtracts the offset.4.

    CPE 005B Page 22

  • CMP (CMP destination, source) CMP reg, regCMP reg, imm/mem*used to compare integers and character codes.*implied subtraction between source and destination operand but neither of them is modified*changes the Overflow, Sign, Zero, Carry, Auxiliary Carry and Parity Flags as if the subtraction really happened.

    Example:*mov ax,5cmp ax,10 --> ZF = 0, CF = 1

    *mov ax,1000 mov cx,1000 cmp cx,ax --> ZF = 1, CF = 0

    6.2.8 CMP InstructionWednesday, January 21, 2015 1:17 AM

    CPE 005B Page 23

  • *AND (destination, source)- performs a boolean (bitwise) AND operation between each pair of matching bits in two operands and places the result in the destination operand.

    AND InstructionWednesday, January 21, 2015 1:39 AM

    CPE 005B Page 24

  • Jcond (Jcond destination) or conditional jump*refers to a flag condition identifying the state of one or more flags. *these flags are commonly set by arithmetic, boolean and comparison instructions.

    Jumps based on specific flag values1.4 types:

    N,

    Jumps based on equality2.

    Jcond InstructionWednesday, January 21, 2015 1:49 AM

    CPE 005B Page 25

  • Jumps based on Unsigned Comparisons3.

    CPE 005B Page 26

  • Jumps based on Signed Comparisons4.

    CPE 005B Page 27

  • Answer: No (8109h is negative)

    CPE 005B Page 28

  • Logical Instructions (AND, OR, XOR, TEST)- performs bitwise operation and places the result on the destination operand

    reg, reg1.reg, mem2.reg, imm3.mem, reg4.mem, imm5.

    *Syntax:

    * memory to memory and immediate destination is not allowed!* operands must be of the same size!

    *Truth Tables:

    X Y AND / TEST OR XOR

    0 0 0 0 0

    0 1 0 1 1

    1 0 0 1 1

    1 1 1 1 0

    *AND / TEST = both operands should be 1*OR = either operands or both should be 1*XOR = exactly one should be 1

    Flags:*clears overflow and carry flags and modifies sign, zero and parity depending on the results of the instruction in the destination operand

    AND Instruction (AND destination, source) - Intersection1. can be used to clear out or zero (called bit masking) certain bits in a byte or wordExample 1:mov al, 33h ; 33H --> 0011 0011AND al, 0CFh; 0CFh --> 1100 1111Result: 0000 0011

    Example 2:Suppose, for example, that a control byte is about to be copied from the AL register to a hardware device. Further, we will assume that the device resets itself when bits 0 and 3 are cleared in the control byte. Assuming that we want to reset the device without modifying any other bits in AL, we can write the following:*AND AL,11110110b ; clear bits 0 and 3, leave others unchanged.

    Example 3:

    R

    M - Logical InstructionsSaturday, January 24, 2015 3:18 PM

    CPE 005B Page 29

  • 2. provides an easy way to translate a letter from lower case to upper case. To convert a lower case character to an upper case letter, use AND x, 11011111bExample:0110 0001 - a0100 0001 - A

    OR Instruction (OR destination, source) - Union1. can be used to set certain bit to 1 in a byte or wordExample 1:mov dl,03 ; 03 --> 0011OR dl, 20h ; 20h --> 10 0000Result: 10 0011

    Example 2:Suppose, for example, that your computer is attached to a servo motor, which is activated by setting bit 2 in its control byte. Assuming that the AL register contains a control byte in which each bit contains some important information, the following code only sets the bit in position 2.OR AL,00000100b ; set bit 2, leave others unchanged

    Example 3:

    2.Flag statuses: If you OR the register itself (e.g OR AL,AL), you will know the value based on the zero flag and sign flag.

    XOR Instruction (XOR destination, source)1. Ideal tool for simple data encryption since it reverses itself when applied twice to the same operand!Example:Step 1: XOR AL, 1010 0001 --> If al = 1011 1010, then al becomes 0001 1011Step 2: XOR AL, 1010 0001--> Since al is 0001 1001, it becomes 1011 1010. Al goes back to the initial value!

    2. Any register / memory XORed to itself is equal to zero. (e.g XOR al,al)3. To initialize zero in a register, XOR AL, AL is faster in 8086 / 8088 processors than using mov al,00h.3. To check parity, you can XOR a number with a zero. (XOR X,0)

    CPE 005B Page 30

  • TEST instruction 1. similar to AND instruction except that destination value does not change, but flags status are changed.2. valuable in finding out if individual bits in an operand are setExample:Testing Multiple Bits The TEST instruction can check several bits at once. Suppose we want to know whether bit 0 or bit 3 is set in the AL register. We can use the following instruction to find this out:Test al, 0000 1000b --> tests bits zero and 3 if they are set or clear0 0 1 0 0 1 0 1
  • *Shifting - to move bits right and left inside an operand. *It affects both Carry and Overflow Flags.

    Two Ways to Shift an operand's bits:1. Logical Shift - fills the newly created bit position with zero. Maglalagay ng zero sa MSD at uusog to the right. Yung last digit mapupunta sa Carry Flag.

    Example: 11001111 --> 01100111

    2. Arithmetic Shift - the newly created bit position is filled with a copy of the original number's sign bit. Si MSD will be repeated twice sa unahan.

    Example: 11001111 --> 11100111

    Syntax:1. reg, imm

    F - Shift and Rotate InstructionsMonday, February 02, 2015 10:04 PM

    CPE 005B Page 32

  • 1. reg, imm2. mem, imm3. reg, CL register4. mem, CL register*where imm is any integer between 0 and 255.

    1. SHL Instruction*The SHL (shift left) instruction performs a logical shift on the destination operand, filling the lowest bit with 0. The highest bit is moved to the Carry flag and the bit that was initially in the Carry flag is discarded.*MSD becomes the new Carry Flag.*LSD becomes zero.*If it is shifted more than once, the last MSD becomes the CF.

    Example: SHL 11001111 by 1 bit --> 10011110

    Example:

    Bitwise Multiplication:*SHL can perform multiplication by powers of 2. Shifting any operand left by n bits multiplies the operand by 2n.Example 1: When we SHL 5 by one bit, 5 * 2 = 10

    Example 2: When we SHL 10 by 2 bits, 10*22 = 40

    2. SHR Instruction:*performs a logical right shift on the destination operand, replacing the highest bit with a 0. The lowest bit is copied into the Carry flag, and the bit that was previously in the Carry flag is lost.*MSD becomes zero.*LSD becomes the Carry Flag.*In a multiple shift operation, the last bit to be shifted out of position 0 (the LSB) ends up in the Carry flag.

    Example 1:

    Bitwise Division: Logically shifting an unsigned integer to the right by n bits divides the operand by 2n.

    Example 1: 32 was shifted once. 32 / 2 = 16.

    CPE 005B Page 33

  • Example 2:

    3. SAL and SAR Instructions:a. SAL: Shift arithmetic left*works the same as the SHL instruction. *for each shift count, SAL shifts each bit in the destination operand to the next highest bit position. The lowest bit is assigned 0.The highest bit is moved to the Carry flag, and the bit that was in the Carry flag is discarded.

    Example: Shifting 11001111 by 1 bit to the left --> 10011110

    b. SAR: Shift Arithmetic Right*different than SHR*performs a right arithmetic, repeats MSD on the left.

    Example 1: When -16 is SAR by 1, -16/2 = -8

    Signed Division: You can divide a signed operand by a power of 2, using the SAR instruction.

    Example 1: When you SAR -128 by 3 bits, -128 / 23 = -16

    Sign-Extend AX into EAX: Suppose AX contains a signed integer and you want to extend its sign into EAX. First shift EAX 16 bits to the left, then shift it arithmetically 16 bits to the right.

    4. ROL Instruction:*The ROL (rotate left) instruction shifts each bit to the left. The highest bit is copied into the Carry flag and the lowest bit position.The instruction format is the same as for SHL.

    Example 1:

    CPE 005B Page 34

  • Example 2:

    Exchanging Groups of Bits: Use ROL to exchange the upper bits (bits 4-7) and lower bits (0-3) halves of a byte.Example 1: 26h rotated 4 times becomes 62h

    Example 2: When ROL is applied by 4 bits, the characters goes to the left.

    5. ROR Instruction*ROR (rotate right) instruction shifts each bit to n right and copies the lowest bit into the Carry flag and the highest bit position.

    Example 1:

    6. RCL and RCR Instructions:*RCL: shifts each bit to the left, copies the carry flag to the LSB and copies the MSB into the Carry flag.

    Example 1:

    Example 2:

    RCR Instruction:*shifts each bit to the right, copies the Carry flag into the MSB and copies the LSB into the Carry flag.

    CPE 005B Page 35

  • CPE 005B Page 36

  • *Screen has 80 columns (0 - 79 ) and 25 (0-24) rows.

    Int 10h / Function 06h - handles the processing of clearing the screen* mov ah,06h = function* al = number of blank lines at the bottom of the window* cl = column number start* dl = column number end* ch = row number start* dh = row number end* bh = display attributes for blank lines

    Int 10h / Function 02h*mov ah, 02h = function*dl = column position (y)*dh = row position (x)*bh = page number

    F - Screen Attributes

    CPE 005B Page 37

  • CPE 005B Page 38

  • CPE 005B Page 39