CPSC 240 L9V3-Conditional Processing(Chpt 6)-2 PDF

Title CPSC 240 L9V3-Conditional Processing(Chpt 6)-2
Course Computer Organization and Assembly Language
Institution California State University Fullerton
Pages 28
File Size 1.1 MB
File Type PDF
Total Downloads 51
Total Views 140

Summary

Professor's name was Eric Shulman....


Description

CPSC 240_L9V3-Conditional Processing(Chpt 6).docx 6-18-2016 This lecture is from Chapter 6 in Kip Irvine’s Assembly Language of X86 Processors 6th Ed, his supplemental instruction material, and other sources. V3 adds in Logic Operator Implementations Table of Contents 1

2

3

4

5

6

Boolean and Comparison Instructions ..................................................................................................................... 2 1.1 Status Flags ................................................................................................................................................................. 2 1.2 AND Instruction ......................................................................................................................................................... 2 1.2.1 Example of Converting lowercase to uppercase .................................................................................. 3 1.3 OR Instruction ............................................................................................................................................................ 3 1.4 XOR Instruction ......................................................................................................................................................... 4 1.5 NOT Instruction ......................................................................................................................................................... 4 1.5.1 Logic Operator Implementation ................................................................................................................. 5 1.6 Bit-Mapped Sets ........................................................................................................................................................ 6 1.6.1 Bit-Mapped Set Operations ........................................................................................................................... 6 1.6.2 Applications of Bit-Mapped Set Operations ........................................................................................... 7 1.7 TEST Instruction ....................................................................................................................................................... 7 1.8 CMP Instruction ......................................................................................................................................................... 7 Conditional Jumps ............................................................................................................................................................. 9 2.1 Jumps Based on Specific Flags ........................................................................................................................... 10 2.2 Jumps Based on Equality .....................................................................................................................................10 2.3 Jumps Based on Unsigned Comparisons........................................................................................................10 2.4 Jumps Based on Signed Comparisons ............................................................................................................. 11 2.5 Applications .............................................................................................................................................................. 11 2.5.1 IsDigit Procedure ............................................................................................................................................ 12 2.6 Encrypting a String.................................................................................................................................................12 2.7 BT (Bit Test) Instruction ...................................................................................................................................... 15 Conditional Loop Instructions ...................................................................................................................................15 3.1 LOOPZ and LOOPE .................................................................................................................................................. 15 3.2 LOOPNZ and LOOPNE ........................................................................................................................................... 15 3.2.1 LOOPNZ Example............................................................................................................................................ 16 Conditional Structures .................................................................................................................................................. 16 4.1 Block-Structured IF Statements ........................................................................................................................ 16 4.2 Compound Expressions with AND ................................................................................................................... 17 4.3 Compound Expressions with OR ...................................................................................................................... 17 4.4 WHILE Loops ............................................................................................................................................................ 18 4.4.1 Loop Containing IF Statement ................................................................................................................... 18 4.5 Table-Driven Selection ......................................................................................................................................... 20 Conditional Control Flow Directives........................................................................................................................ 23 5.1 Runtime Expressions (.IF, .ELSE, ELSEIF) ..................................................................................................... 23 5.2 Relational and Logical Operators ..................................................................................................................... 24 5.2.1 Signed and Unsigned Comparisons ......................................................................................................... 24 5.2.2 Comparing Registers ..................................................................................................................................... 25 5.2.3 Quick Example of using size directive .................................................................................................... 26 5.3 .REPEAT Directive .................................................................................................................................................. 27 5.4 .WHILE Directive..................................................................................................................................................... 27 Summary.............................................................................................................................................................................27 1 T t

1 Boolean and Comparison Instructions • • • • • • • •

CPU Status Flags AND Instruction OR Instruction XOR Instruction NOT Instruction Applications TEST Instruction CMP Instruction

1.1 Status Flags • The Zero flag is set when the result of an operation equals zero. • The Carry flag is set when an instruction generates a result that is too large (or too small) for the destination operand. • The Sign flag is set if the destination operand is negative, and it is clear if the destination operand is positive. • The Overflow flag is set when an instruction generates an invalid signed result (bit 7 carry is XORed with bit 6 Carry). • The Parity flag is set when an instruction generates an even number of 1 bits in the low byte of the destination operand. • The Auxiliary Carry flag is set when an operation produces a carry out from bit 3 to bit 4 1.2 AND Instruction • Performs a Boolean AND operation between each pair of matching bits in two operands • Syntax: AND destination, source (same operand types as the MOV instruction) The AND Logic Table

Then just like we did in the binary Arithmetic Portion of this class

00111011 AND 0 0 0 0 1 1 1 1 cleared

00001011

unchanged

2

1.2.1 Example of Converting lowercase to uppercase Using the AND instruction to provide an easy way to convert from lowercase to uppercase. Bit ‘a’ ‘A’

7 6 5 4 3 2 1 0 0 1 1 0 0 0 0 1 or 61h 0 1 0 0 0 0 0 1 or 41h

From C++ you might remember that uppercase to lowercase letters only differ by 32 decimal. That is 25 power or 32. It becomes clear that the only difference between ‘a’ and ‘A’ is bit 5 So if we write a short program that clears bit 5, you are converting from ‘a’ to ‘A’ .data array BYTE 50 DUP (?) .code mov mov L1: and inc esi loop

ecx, LENGTHOF array esi, OFFSET array BYTE PTR [esi], 11011111b

; clear bit 5

L1

1.3 OR Instruction • Performs a Boolean OR operation between each pair of matching bits in two operands • Syntax: OR destination, source The OR Logic Table

Then just like we did in the binary Arithmetic Portion of this class

00111011 OR 0 0 0 0 1 1 1 1 unchanged

00111111

set

3

1.4 XOR Instruction • Performs a Boolean exclusive-OR operation between each pair of matching bits in two operands • Syntax: XOR destination, source The XOR Logic Table

Then just like we did in the binary Arithmetic Portion of this class

00111011 XOR 0 0 0 0 1 1 1 1 unchanged

00110100

inverted

XOR is a useful way to toggle (invert) the bits in an operand. It is also a great way to do a compare, if the same, logic 0, else a logic 1 1.5 NOT Instruction • Performs a Boolean NOT operation on a single destination operand • Syntax: NOT destination The NOT Logic Table

Then again, just like we did in the binary Arithmetic Portion of this class NOT

00111011 11000100

inverted

4

1.5.1 Logic Operator Implementation AND, OR, & XOR Noting that XOR is a half adder, an adder without a carry XOR destination, source

; result in destination

XOR reg, reg ; also for AND & OR XOR mem, reg XOR reg, mem XOR reg, imm XOR mem, imm Note: For XOR only, clears the overflow and carry flags. Set the sign, zero, and parity flags appropriately. But remember that there is no mem to mem operations allowed

Example Code 1 (All in Assembly) INCLUDE Irvine32.inc .data val1 dword 5ah ; 0101 1010 val2 dword 0ach ; 1010 1100, XOR of two is 1111 0110 .code main proc xor val2, 05ah mov eax, val2 call WriteBin call CrLf call WaitMsg exit main ENDP END main

Example Code 2 (Using Inline Assembly) #include using namespace std; int main() { short val1 = 0x5a; short val2 = 0xac; short Result = 0;

// Remember that short is 16-bit

__asm { mov ax, val1 xor ax, val2 mov Result, ax } cout b lower byte X = 1; One possible solution would be cmp al,bl ; first expression... (al – bl) ja L1 ; jump if above (if leftOp > rightOp) jmp next L1: cmp bl,cl ; second expression... (bl – cl) ja L2 ; jump if above (if leftOp > rightOp) jmp next L2: ; both are true mov X,1 ; set X to 1 next: Another possible solution that uses 29% less code would reversing the first relational operator. Thus we would allow the program to “fall through” to the second expression as follows: cmp al,bl ; first expression... (al – bl) jbe next ; quit if false, jump if below or equal (if leftOp cl) X = 1; In the following implementation, the code branches to L1 if the first expression is true, otherwise it falls through to the second CMP instruction. The second expression reverses the > operator and uses the JBE instruction instead.

L1: next:

cmp ja cmp jbe mov

al,bl L1 bl,cl next X,1

; is AL > BL? (al – bl) ; yes, jump if above (if leftOp > rightOp) ; no: is BL > CL? (bl – cl) ; no: skip next statement, jump if below or equal (if leftOp = rightOp) ; body of loop ; repeat the loop

next: 4.4.1 Loop Containing IF Statement The easiest way to generate assembly code from a flowchart is to implement separate code for each flowchart shape. Note the direct correlation between the flowchart labels below and the labels used in the following source code.

18

TITLE Flowchart Example

(Flowchart.asm)

; Implements the flowchart in Figure 6-2, ; Calculates the sum of all array elements larger than ; a given value (sample). INCLUDE Irvine32.inc .data sum DWORD 0 sample DWORD 50 array DWORD 10,60,20,33,72,89,45,65,72,18 ArraySize = ($ - Array) / TYPE array

19

.code main PROC mov mov mov mov

eax,0 ; sum edx,sample esi,0 ; index ecx,ArraySize

L1: cmp jl L2 jmp

esi,ecx

L2: cmp jg L3 jmp L3: add

array[esi*4], edx L4 eax,array[esi*4]

L4: inc jmp

esi L1

L5: mov

sum,eax

call call

L5

Writeint Crlf

exit main ENDP END main

4.5 • • • •

Table-Driven Selection

Table-driven selection uses a table lookup to replace a multiway selection structure Create a table containing lookup values and the offsets of labels or procedures Use a loop to search the table Suited to a large number of comparisons

For example, a C++ switch statement selects from among pieces of code based on the value of an integral expression. Its general form is: switch (selector) { // The selector can not be either type float or string case integral-value1 : statement1; statement2; break; case integral-value2 : statement; break; case integral-value3 : statement; break; ... default: statement; } // semicolon not required after case stmt

Step 1: create a table containing lookup values and procedure offsets: 20

.data CaseTable BYTE 'A' ; lookup value DWORD Process_A ; address of procedure EntrySize = ($ - CaseTable) BYTE 'B' DWORD Process_B BYTE 'C' DWORD Process_C BYTE 'D' DWORD Process_D NumberOfEntries = ($ - CaseTable) / EntrySize Now let’s assume that Process_A, Process_B, Process_C, and Process_D are located at addresses 120h, 130h, 140h, and 150h, respectively. The table of Procedure Offsets would look like

Step 2: Use a loop to search the table. When a match is found, call the procedure offset stored in the current table entry: The use of a NEAR PTR operator is required in the following program. PTR lets you override an operand’s default size The type can be NEAR (in same segment) or FAR (in a different segment) Refer to CPSC 240_L8 titled Procedures, Section 4.2 for more detail. TITLE Table of Procedure Offsets

(ProcTble.asm)

; This progam contains a table with offsets of procedures. ; It uses the table to execute indirect procedure calls. INCLUDE Irvine32.inc .data CaseTable BYTE 'A' ; lookup value DWORD Process_A ; address of procedure BYTE 'B' DWORD Process_B BYTE 'C' DWORD Process_C BYTE 'D' DWORD Process_D NumberOfEntries = 4 ; We are defining a separate message string for each procedure prompt BYTE "Press capital A,B,C,or D: ",0

21

msgA BYTE "Process_A",0 msgB BYTE "Process_B",0 msgC BYTE "Process_C",0 msgD BYTE "Process_D",0 .code main PROC mov edx,OFFSET prompt call WriteString call ReadChar mov ebx,OFFSET CaseTable mov ecx,NumberOfEntries L1: cmp al,[ebx] jne L2 call NEAR PTR [ebx + 1]

; ask user for input ; read one character ; point EBX to the table ; loop counter ; match found? ; no: continue ; yes: call the procedure

; The call instruction calls the procedure whose address is stored ; in the memory location referenced by EBX+1. An indirect call ; such as this requires the NEAR PTR operator. call WriteString call Crlf jmp L3

; display message ; exit the search

add ebx,5 loop L1

; point to the next entry ; repeat until ECX = 0

L2:

L3: exit main ENDP ; Each fo the following procedures moves a different string ; offset to EDX Process_A PROC mov edx,OFFSET msgA ret Process_A ENDP Process_B PROC mov edx,OFFSET msgB ret Process_B ENDP Process_C PROC mov edx,OFFSET msgC ret Process_C ENDP Process_D PROC mov edx,OFFSET msgD ret Process_D ENDP END main

22

The table-driven selection method involves some initial overhead, but it can reduce the amount of code that you have to write. A table can handle a large number of comparisons, and it can be more easily modified than a long series of compare, jump, and CALL instructions. Later in this course, we will learn how we can insert assembly code within our C or C++ code to take advantage of assemblies ability to make our code more compact and quicker.

5 Conditional Control Flow Directives 5.1

Runtime Expressions (.IF, .ELSE, ELSEIF)

MASM includes a number of high-level conditional control flow directives that help to simplify the coding of conditional statements. These are called Runtime Expressions and start with a period. .IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate runtime expressions and create blockstructured IF statements. For example: .IF eax > ebx mov edx,1 .ELSE mov edx,2 .ENDIF

.IF eax > ebx && eax > ecx mov edx,1 .ELSE mov edx,2 .ENDIF

When using one of these runtime expressions, MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump instructions.

INCLUDE Irvine32.inc .data .code main PROC mov EAX, 3 mov EBX, 4 mov ECX, 5 .IF eax > ebx mov eax,1 .ELSEIF ECX > EBX mov eax, 2 .ENDIF call WriteDec call CrLf call waitmsg

; tell program to wait

23

exit main ENDP END main

; exit program ; end of main ; end main PROC

5.2 Relational and Logical Operators Relation and Logical Operators

5.2.1 Signed and Unsigned Comparisons When you use the .IF directive to compare values, you need to be aware of how MASM generates conditional jumps.

If the comparison involves an unsigned variable, an unsigned conditional jump instruction is Inserted in the generated code. See below 24

MASM automatically generates an unsigned jump (JBE) (jump if below or equal) when both operands are registers . . .

Now if an .IF directive compares a signed variable, a signed conditional jump instruction is Inserted into the generated code. In the example below, result is a signed doubleword:

Now the assembler generates code using the JLE instruction (jump if less than or equal), a jump based on signed comparisons.

5.2.2 Comparing Registers If two registers are compared, what do you think will happen? 25

It is clear that the assembler can not determine whether the values are signed or unsigned. mov eax, 6 mov ebx, val2 .IF eax > ebx mov result, 1 .ENDIF The following code would be generated by the assembler, showing that the assembler would default to an unsigned comparison (note the use of the JBE instruction) mov eax, 6 mov ebx, val2 cmp eax, ebx jbe @C0001 mov result, 1 @C0001: If necessary, you can prefix one of the register operands with the SDWORD PTR operator (This is called a size directive). Then a signed jump is generated.

" mov byte ptr [var], 5 ; store the value 5 into the byte at location var mov BYTE PTR [ESI], 5 ; Store 8-bit value in mem location pointed by ESI mov WORD PTR [ESI], 5 ; Store 16-bit value mov DWORD PTR [ESI], 5 ; Store 32-bit value 5.2.3 Quick Example of using size directive TITLE Little Demo INCLUDE Irvine32.inc .data Rval DWORD 5 .code main PROC ;mov eax, Rval ;call WriteDec

; This will work

mov eax, DWORD PTR [Rval] ; This is demo of using size directive call WriteDe...


Similar Free PDFs