Transcript HW-Inst

CS2422 Assembly Language and System Programming
Instructions That May Be
Used for Assignment 1
Department of Computer Science
National Tsing Hua University
CMP 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 < source
mov al,4
cmp al,5

; Carry flag set
Example: destination > source
mov al,6
cmp al,5
; ZF = 0, CF = 0
(both the Zero and Carry flags are clear)
1
CMP Instruction (2/2)


The comparisons shown here are performed with
signed integers.
Example: destination > source
mov al,5
cmp al,-2
; Sign flag == Overflow flag
• Example: destination < source
mov al,-1
cmp al,5
; Sign flag != Overflow flag
2
CMP and Jcond Instruction

The IF statement in C and PASCAL is converted
into CMP and Jcond instructions in x86 Assembly:
If (X > op1)
Then
<…>
End If
CMP X, op1
JNG EndIf
<…>
EndIf:
3
Jcond 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
4
More Frequently Used Jcond




JE (Equal)
JNE (Not Equal)
JG or JGE (Greater Than or Equal)
JL or JLE (Less Than or Equal)
Note: JG=JNLE, JGE=JNL, …etc.
5
Simple IF


If (op1=op2) then <…> end if
Two different approaches:
CMP op1, op2
JE True
JMP EndIf
True:
<…>
EndIf
CMP op1, op2
JNE False
<…>
False:
6