55
Conditional Processing COE 205 Computer Organization and Assembly Language Computer Engineering Department King Fahd University of Petroleum and Minerals

Conditional Processing

  • Upload
    cedric

  • View
    25

  • Download
    0

Embed Size (px)

DESCRIPTION

Conditional Processing. COE 205 Computer Organization and Assembly Language Computer Engineering Department King Fahd University of Petroleum and Minerals. Presentation Outline. Boolean and Comparison Instructions Conditional Jumps Conditional Loop Instructions - PowerPoint PPT Presentation

Citation preview

  • Conditional ProcessingCOE 205Computer Organization and Assembly LanguageComputer Engineering DepartmentKing Fahd University of Petroleum and Minerals

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Presentation OutlineBoolean and Comparison InstructionsConditional JumpsConditional Loop InstructionsTranslating Conditional StructuresIndirect Jump and Table-Driven SelectionApplication: Sorting an Integer Array

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    AND InstructionBitwise AND between each pair of matching bitsAND destination, sourceFollowing operand combinations are allowedAND reg, regAND reg, memAND reg, immAND mem, regAND mem, immAND instruction isoften used to clear selected bitsOperands can be 8, 16, or 32 bits and they must be of the same size

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Converting Characters to UppercaseAND instruction can convert characters to uppercase'a'= 0 1 1 0 0 0 0 1'b'= 0 1 1 0 0 0 1 0'A'= 0 1 0 0 0 0 0 1'B'= 0 1 0 0 0 0 1 0Solution: Use the AND instruction to clear bit 5mov ecx, LENGTHOF mystringmov esi, OFFSET mystringL1:and BYTE PTR [esi], 11011111b; clear bit 5inc esiloop L1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    OR InstructionBitwise OR operation between each pair of matching bitsOR destination, sourceFollowing operand combinations are allowedOR reg, regOR reg, memOR reg, immOR mem, regOR mem, immOR instruction isoften used to set selected bitsOperands can be 8, 16, or 32 bits and they must be of the same size

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Converting Characters to LowercaseOR instruction can convert characters to lowercase'A'= 0 1 0 0 0 0 0 1'B'= 0 1 0 0 0 0 1 0'a'= 0 1 1 0 0 0 0 1'b'= 0 1 1 0 0 0 1 0Solution: Use the OR instruction to set bit 5mov ecx, LENGTHOF mystringmov esi, OFFSET mystringL1:or BYTE PTR [esi], 20h; set bit 5inc esiloop L1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Converting Binary Digits to ASCIIOR instruction can convert a binary digit to ASCII0= 0 0 0 0 0 0 0 01= 0 0 0 0 0 0 0 1'0'= 0 0 1 1 0 0 0 0'1'= 0 0 1 1 0 0 0 1Solution: Use the OR instruction to set bits 4 and 5or al,30h ; Convert binary digit 0 to 9 to ASCIIWhat if we want to convert an ASCII digit to binary?Solution: Use the AND instruction to clear bits 4 to 7and al,0Fh ; Convert ASCII '0' to '9' to binary

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    XOR InstructionBitwise XOR between each pair of matching bitsXOR destination, sourceFollowing operand combinations are allowedXOR reg, regXOR reg, memXOR reg, immXOR mem, regXOR mem, immXOR instruction isoften used to invert selected bitsOperands can be 8, 16, or 32 bits and they must be of the same size

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Affected Status FlagsThe six status flags are affectedCarry Flag: Cleared by AND, OR, and XOROverflow Flag: Cleared by AND, OR, and XORSign Flag: Copy of the sign bit in resultZero Flag: Set when result is zeroParity Flag: Set when parity in least-significant byte is evenAuxiliary Flag: Undefined by AND, OR, and XOR

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    String Encryption ProgramTasks:Input a message (string) from the userEncrypt the messageDisplay the encrypted messageDecrypt the messageDisplay the decrypted messageSample OutputEnter the plain text: Attack at dawn.Cipher text: ---GsDecrypted: Attack at dawn.

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Encrypting a StringKEY = 239; Can be any byte valueBUFMAX = 128.databuffer BYTE BUFMAX+1 DUP(0)bufSize DWORD BUFMAXThe following loop uses the XOR instruction to transform every character in a string into a new valuemov ecx, bufSize; loop countermov esi, 0; index 0 in bufferL1:xor buffer[esi], KEY; translate a byteinc esi; point to next byteloop L1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    TEST InstructionBitwise AND operation between each pair of bitsTEST destination, sourceThe flags are affected similar to the AND InstructionHowever, TEST does NOT modify the destination operandTEST instruction can check several bits at onceExample:Test whether bit 0 or bit 3 is set in ALSolution:test al, 00001001b ; test bits 0 & 3We only need to check the zero flag; If zero flag => both bits 0 and 3 are clear; If Not zero => either bit 0 or 3 is set

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    NOT InstructionInverts all the bits in a destination operandNOT destinationResult is called the 1's complementDestination can be a register or memoryNOT regNOT mem

    None of the Flags is affected by the NOT instruction

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    CMP InstructionCMP (Compare) instruction performs a subtractionSyntax:CMP destination, sourceComputes:destination sourceDestination operand is NOT modifiedAll six flags: OF, CF, SF, ZF, AF, and PF are affectedCMP uses the same operand combinations as ANDOperands can be 8, 16, or 32 bits and must be of the same sizeExamples: assume EAX = 5, EBX = 10, and ECX = 5

    cmp eax, ebxcmp eax, ecx; OF=0, CF=1, SF=1, ZF=0; OF=0, CF=0, SF=0, ZF=1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Unsigned ComparisonCMP can perform unsigned and signed comparisonsThe destination and source operands can be unsigned or signedFor unsigned comparison, we examine ZF and CF flags

    CMP does a subtraction and CF is the borrow flagCF = 1 if and only if unsigned destination < unsigned sourceAssume AL = 5 and BL = -1 = FFhcmp al, bl ; Sets carry flag CF = 1To check for equality, it is enough to check ZF flag

    Unsigned ComparisonZFCFunsigned destination < unsigned source01unsigned destination > unsigned source00destination = source10

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Signed ComparisonFor signed comparison, we examine SF, OF, and ZF

    Recall for subtraction, the overflow flag is set when Operands have different signs and result sign destination signCMP AL, BL (consider the four cases shown below)OF = 0SF = 0AL > BLOF = 0SF = 1AL < BLOF = 1SF = 1AL > BLOF = 1SF = 0AL < BL

    Signed ComparisonFlagssigned destination < signed sourceSF OFsigned destination > signed sourceSF = OF, ZF = 0destination = sourceZF = 1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Next . . .Boolean and Comparison InstructionsConditional JumpsConditional Loop InstructionsTranslating Conditional StructuresIndirect Jump and Table-Driven SelectionApplication: Sorting an Integer Array

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Conditional StructuresNo high-level control structures in assembly languageComparisons and conditional jumps are used to Implement conditional structures such as IF statementsImplement conditional loopsTypes of Conditional Jump InstructionsJumps based on specific flagsJumps based on equalityJumps based on the value of CX or ECXJumps based on unsigned comparisonsJumps based on signed comparisons

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Jumps Based on Specific FlagsConditional Jump Instruction has the following syntax:Jcond destination; cond is the jump condition DestinationDestination LabelPrior to 386Jump must be within 128 to +127 bytes from current locationIA-3232-bit offset permits jump anywhere in memory

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Jumps Based on Equalityjecxz L2; exit loopL1: . . .; loop bodyloop L1L2:JE is equivalent to JZJNE is equivalent to JNZJECXZChecked once at the beginningTerminate a loop if ECX is zero

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Examples of Jump on ZeroTask: Check whether integer value in EAX is evenSolution: TEST whether the least significant bit is 0If zero, then EAX is even, otherwise it is odd

    Task: Jump to label L1 if bits 0, 1, and 3 in AL are all setSolution:test eax, 1; test bit 0 of eaxjz EvenVal; jump if Zero flag is setand al,00001011b; clear bits except 0,1,3cmp al,00001011b; check bits 0,1,3je L1; all set? jump to L1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Jumps Based on Unsigned Comparisoncmp eax, ebxjb IsBelowTask: Jump to a label if unsigned EAX is less than EBXSolution:JB conditionCF = 1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Jumps Based on Signed Comparisonscmp eax, ebxjl IsLessTask: Jump to a label if signed EAX is less than EBXSolution:JL conditionOF SF

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Compare and Jump ExamplesJump to L1 if unsigned EAX is greater than Var1Solution:cmp eax, Var1ja L1JA conditionCF = 0, ZF = 0Jump to L1 if signed EAX is greater than Var1Solution:cmp eax, Var1jg L1JG conditionOF = SF, ZF = 0Jump to L1 if signed EAX is greater than or equal to Var1Solution:cmp eax, Var1jge L1JGE conditionOF = SF

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Computing the Max and Minmov Max, eax; assume Max = eaxcmp Max, ebxjae donemov Max, ebx; Max = ebxdone:Compute the Max of unsigned EAX and EBXSolution:mov Min, eax; assume Min = eaxcmp Min, ebxjle donemov Min, ebx; Min = ebxdone:Compute the Min of signed EAX and EBXSolution:

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Application: Sequential Search; Receives:esi = array address;ecx = array size;eax = search value; Returns:esi = address of found elementsearch PROC USES ecxjecxz notfoundL1:cmp [esi], eax; array element = search value?je found; yes? found elementadd esi, 4; no? point to next array elementloop L1notfound:mov esi, 0; if not found then esi = 0found:ret; if found, esi = element addresssearch ENDP

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    BT InstructionBT = Bit Test InstructionSyntax:BT r/m16, r16BT r/m32, r32BT r/m16, imm8BT r/m32, imm8Copies bit n from an operand into the Carry flagExample: jump to label L1 if bit 9 is set in AX registerbt AX, 9; CF = bit 9jc L1; jump if Carry to L1

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Next . . .Boolean and Comparison InstructionsConditional JumpsConditional Loop InstructionsTranslating Conditional StructuresIndirect Jump and Table-Driven SelectionApplication: Sorting an Integer Array

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    LOOPZ and LOOPESyntax: LOOPE destinationLOOPZ destinationLogic: ECX = ECX 1if ECX > 0 and ZF=1, jump to destinationUseful when scanning an array for the first element that does not match a given value.

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    LOOPNZ and LOOPNESyntax: LOOPNZ destinationLOOPNE destinationLogic: ECX ECX 1; if ECX > 0 and ZF=0, jump to destinationUseful when scanning an array for the first element that matches a given value.

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    LOOPZ ExampleThe following code finds the first negative value in an array.dataarray SWORD 17,10,30,40,4,-5,8.codemov esi, OFFSET array 2; start before firstmov ecx, LENGTHOF array; loop counterL1:add esi, 2; point to next elementtest WORD PTR [esi], 8000h; test sign bitloopz L1; ZF = 1 if value >= 0jnz found; found negative valuenotfound:. . .; ESI points to last array elementfound:. . .; ESI points to first negative value

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Your Turn . . .Locate the first zero value in an arrayIf none is found, let ESI point to last array element.dataarray SWORD -3,7,20,-50,10,0,40,4.codemov esi, OFFSET array 2; start before firstmov ecx, LENGTHOF array; loop counterL1:add esi, 2; point to next elementcmp WORD PTR [esi], 0; check for zeroloopne L1; continue if not zerojz found; found zeronotfound:. . .; ESI points to last array valuefound:. . .; ESI points to first zero value

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Next . . .Boolean and Comparison InstructionsConditional JumpsConditional Loop InstructionsTranslating Conditional StructuresIndirect Jump and Table-Driven SelectionApplication: Sorting an Integer Array

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Block-Structured IF StatementsIF statement in high-level languages (such as C or Java)Boolean expression (evaluates to true or false)List of statements performed when the expression is trueOptional list of statements performed when expression is falseTask: Translate IF statements into assembly languageExample:mov eax,var1cmp eax,var2jne elsepartmov X,1jmp nextelsepart:mov X,2next:if( var1 == var2 ) X = 1;else X = 2;

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Your Turn . . .Translate the IF statement to assembly languageAll values are unsignedcmp ebx,ecxja nextmov eax,5mov edx,6next:if( ebx cl)) {X = 1;}; One Possible Implementation ...cmp al, bl; first expression ...ja L1; unsigned comparisonjmp nextL1:cmp bl,cl; second expression ...ja L2; unsigned comparisonjmp nextL2:mov X,1 ; both are truenext:

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Better Implementation for ANDcmp al,bl; first expression...jbe next; quit if falsecmp bl,cl; second expression...jbe next; quit if falsemov X,1; both are truenext:The following implementation uses less codeBy reversing the relational operator, We allow the program to fall through to the second expressionNumber of instructions is reduced from 7 to 5if ((al > bl) && (bl > cl)) {X = 1;}

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Your Turn . . .Implement the following IF in assembly languageAll values are unsignedcmp ebx,ecxja nextcmp ecx,edxjbe nextmov eax,5mov edx,6next:if ((ebx edx)){ eax = 5; edx = 6;}

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Application: IsDigit ProcedureIsDigit PROC cmp al,'0'; AL < '0' ? jb L1; yes? ZF=0, return cmp al,'9'; AL > '9' ? ja L1; yes? ZF=0, return test al, 0 ; ZF = 1L1: retIsDigit ENDPReceives a character in ALSets the Zero flag if the character is a decimal digitif (al >= '0' && al bl) || (bl > cl)) {X = 1;}cmp al,bl; is AL > BL?ja L1; yes, execute if partcmp bl,cl; no: is BL > CL?jbe next; no: skip if partL1:mov X,1; set X to 1next:

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    WHILE Loopswhile( eax < ebx) { eax = eax + 1; }A WHILE loop can be viewed asIF statement followed by The body of the loop, followed byUnconditional jump to the top of the loop

    Conditional ProcessingCOE 205 KFUPM Muhamed Mudawar slide *

    Your Turn . . .top:cmp ebx,var1; ebx