Chapt 06
Chapt 06
6th Edition
Kip R. Irvine
(c) Pearson Education, 2010. All rights reserved. You may modify and copy this slide show for your personal use, or
for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
Chapter Overview
00111011
XOR 0 0 0 0 1 1 1 1
NOT 00111011
11000100 inverted
• Set Intersection
mov eax,setX
and eax,setY
• Set Union
mov eax,setX
or eax,setY
mov ax,wordVal
and ax,1 ; low bit set?
jz EvenValue ; jump if Zero flag set
or al,al
jnz IsNotZero ; jump if not zero
ORing any number with itself does not change its value.
test al,00000011b
jnz ValueFound
test al,00000011b
jz ValueNotFound
mov al,5
cmp al,5 ; Zero flag set
mov al,4
cmp al,5 ; Carry flag set
mov al,6
cmp al,5 ; ZF = 0, CF = 0
• Jumps Based On . . .
• Specific flags
• Equality
• Unsigned comparisons
• Signed Comparisons
• Applications
• Encrypting a String
• Bit Test (BT) Instruction
• Specific jumps:
JB, JC - jump to a label if the Carry flag is set
JE, JZ - jump to a label if the Zero flag is set
JS - jump to a label if the Sign flag is set
JNE, JNZ - jump to a label if the Zero flag is clear
JECXZ - jump to a label if ECX = 0
cmp eax,ebx
ja Larger
cmp eax,ebx
jg Greater
cmp eax,Val1
jbe L1 ; below or equal
cmp eax,Val1
jle L1
.code
mov ecx,bufSize ; loop counter
mov esi,0 ; index 0 in buffer
L1:
xor buffer[esi],KEY ; translate a byte
inc esi ; point to next byte
loop L1
bt AX,9 ; CF = bit 9
jc L1 ; jump if Carry
.data
array SWORD -3,-6,-1,-10,10,30,40,4
sentinel SWORD 0
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
next:
test WORD PTR [esi],8000h ; test sign bit
pushfd ; push flags on stack
add esi,TYPE array
popfd ; pop flags from stack
loopnz next ; continue loop
jnz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:
.data
array SWORD 50 DUP(?)
sentinel SWORD 0FFFFh
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
L1: cmp WORD PTR [esi],0 ; check for zero
quit:
.data
array SWORD 50 DUP(?)
sentinel SWORD 0FFFFh
.code
mov esi,OFFSET array
mov ecx,LENGTHOF array
L1: cmp WORD PTR [esi],0 ; check for zero
pushfd ; push flags on stack
add esi,TYPE array
popfd ; pop flags from stack
loope L1 ; continue loop
jz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:
• Block-Structured IF Statements
• Compound Expressions with AND
• Compound Expressions with OR
• WHILE Loops
• Table-Driven Selection
.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
digit
digit
start +,-
A B
StateA:
call Getnext ; read next char into AL
cmp al,'+' ; leading + sign?
je StateB ; go to State B
cmp al,'-' ; leading - sign?
je StateB ; go to State B
call IsDigit ; ZF = 1 if AL = digit
jz StateC ; go to State C
call DisplayErrorMsg ; invalid input found
jmp Quit
IsDigit PROC
cmp al,'0' ; ZF = 0
jb ID1
cmp al,'9' ; ZF = 0
ja ID1
test ax,0 ; ZF = 1
ID1: ret
IsDigit ENDP
• Runtime Expressions
• Relational and Logical Operators
• MASM-Generated Code
• .REPEAT Directive
• .WHILE Directive
.data
val1 DWORD 5
result DWORD ? Generated code:
.code
mov eax,6
mov eax,6 cmp eax,val1
.IF eax > val1 jbe @C0001
mov result,1 mov result,1
@C0001:
.ENDIF
.data
val1 SDWORD 5
result SDWORD ? Generated code:
.code
mov eax,6
mov eax,6 cmp eax,val1
.IF eax > val1 jle @C0001
mov result,1 mov result,1
@C0001:
.ENDIF
.data
result DWORD ? Generated code:
.code
mov ebx,5
mov ebx,5 mov eax,6
mov eax,6 cmp eax,ebx
.IF eax > ebx jbe @C0001
mov result,1
mov result,1
@C0001:
.ENDIF
.data
result SDWORD ? Generated code:
.code
mov ebx,5
mov ebx,5 mov eax,6
mov eax,6 cmp eax,ebx
.IF SDWORD PTR eax > ebx jle @C0001
mov result,1
mov result,1
@C0001:
.ENDIF
mov eax,0
.REPEAT
inc eax
call WriteDec
call Crlf
.UNTIL eax == 10
mov eax,0
.WHILE eax < 10
inc eax
call WriteDec
call Crlf
.ENDW