Documente Academic
Documente Profesional
Documente Cultură
(c) Pearson Education, 2002. 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. Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 2
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 3 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 4
00111011
00111011 OR 00001111
AND 00001111
unchanged 00111111 set
cleared 00001011 unchanged
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 5 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 6
1
XOR Instruction NOT Instruction
• Performs a Boolean exclusive-OR operation between • Performs a Boolean NOT operation on a single
each pair of matching bits in two operands destination operand
• Syntax: • Syntax:
XOR destination, source XOR NOT destination NOT
NOT 00111011
00111011
XOR 00001111 11000100 inverted
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 7 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 8
Applications (1 of 5) Applications (2 of 5)
• Task: Convert the character in AL to upper case. • Task: Convert a binary decimal byte into its equivalent
ASCII decimal digit.
• Solution: Use the AND instruction to clear bit 5.
• Solution: Use the OR instruction to set bits 4 and 5.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 9 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 10
Applications (3 of 5) Applications (4 of 5)
• Task: Turn on the keyboard CapsLock key • Task: Jump to a label if an integer is even.
• Solution: Use the OR instruction to set bit 6 in the keyboard • Solution: AND the lowest bit with a 1. If the result is Zero,
flag byte at 0040:0017h in the BIOS data area. the number was even.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 11 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 12
2
Applications (5 of 5) TEST Instruction
• Task: Jump to a label if the value in AL is not zero. • Performs a nondestructive AND operation between each pair of
matching bits in two operands
• Solution: OR the byte with itself, then use the JNZ (jump
• No operands are modified, but the Zero flag is affected.
if not zero) instruction.
• Example: jump to a label if either bit 0 or bit 1 in AL is set.
test al,00000011b
jz ValueNotFound
ORing any number with itself does not change its value.
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 13 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 14
mov al,5
cmp al,5 ; Zero flag set (both the Zero and Carry flags are clear)
mov al,4
cmp al,5 ; Carry flag set
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 15 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 16
CMP Instruction (3 of 3)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 18
3
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 19 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 20
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 21 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 22
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 23 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 24
4
Jumps Based on Equality Jumps Based on Unsigned Comparisons
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 25 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 26
cmp eax,ebx
ja Larger
cmp eax,ebx
jg Greater
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 27 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 28
Applications (2 of 5) Applications (3 of 5)
• Jump to label L1 if signed EAX is less than or equal to Val1 • Compare signed AX to BX, and copy the smaller of the two
into a variable named Small
cmp eax,Val1
jle L1 mov Small,ax
cmp bx,ax
jnl Next
mov Small,bx
Next:
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 29 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 30
5
Applications (4 of 5) Applications (5 of 5)
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 31 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 32
Your turn . . .
write a complete program that does the following:
• Write code that jumps to label L1 if either bit 4, 5, or 6 1. Input gradeAverage and credits from the user. If the
is set in the BL register. user enters zero for either value, halt the program.
• Write code that jumps to label L1 if bits 4, 5, and 6 2. Perform range checking on both credits and GradeAverage.
are all set in the BL register. The latter must be between 1 and 400. If either value
• Write code that jumps to label L2 if AL has even is out of range, display an appropriate error message.
parity. (Note: the book says 0 to 400, but 1 to 400 is more logical.)
• Write code that jumps to label L3 if EAX is negative. 3. Determine whether or not the person can register (using
• Write code that jumps to label L4 if the expression the existing example) and display an appropriate message.
(EBX – ECX) is greater than zero.
4. Repeat steps 1 through 3 until the user decides to quit.
Code
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 33 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 34
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 35 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 36
6
BT (Bit Test) Instruction
• Copies bit n from an operand into the Carry flag
• Syntax: BT bitBase, n
• bitBase may be r/m16 or r/m32
• n may be r16, r32, or imm8
• Example: jump to label L1 if bit 9 is set in the AX
register:
bt AX,9 ; CF = bit 9
jc L1 ; jump if Carry
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 37 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 38
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 39 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 40
Code
Code
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 41 Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 42