Title: Instructions That May Be Used for Assignment 1
1Instructions That May Be Used for Assignment 1
- Department of Computer Science
- National Tsing Hua University
2CMP Instruction (1/2)
- Compare destination operand to source operand
- Nondestructive subtraction of source from
destination (destination operand is not changed) - Syntax CMP destination, source
- Example destination lt source
- Example destination gt source
mov al,4 cmp al,5 Carry flag set
mov al,6 cmp al,5 ZF 0, CF 0
(both the Zero and Carry flags are clear)
3CMP Instruction (2/2)
- The comparisons shown here are performed with
signed integers. - Example destination gt source
- Example destination lt source
mov al,5 cmp al,-2 Sign flag Overflow flag
mov al,-1 cmp al,5 Sign flag ! Overflow flag
4CMP and Jcond Instruction
- The IF statement in C and PASCAL is converted
into CMP and Jcond instructions in x86 Assembly
CMP X, op1 JNG EndIf ltgt EndIf
If (X gt op1) Then ltgt End If
5Jcond Instruction
- A conditional jump instruction branches to a
label when specific register or flag conditions
are met - Examples
- 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 jumps to a label if the Sign flag is set
- JNE, JNZ jump to a label if the Zero flag is
clear - JECXZ jumps to a label if ECX equals 0
6More Frequently Used Jcond
- JE (Equal)
- JNE (Not Equal)
- JG or JGE (Greater Than or Equal)
- JL or JLE (Less Than or Equal)
- Note JGJNLE, JGEJNL, etc.
7Simple IF
- If (op1op2) then ltgt end if
- Two different approaches
CMP op1, op2 JE True JMP EndIf True ltgt End
If
CMP op1, op2 JNE False ltgt False