CS 2422 Assembly Language and System Programming Instructions
CS 2422 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) u Compare destination operand to source operand l u u Nondestructive subtraction of source from destination (destination operand is not changed) Syntax: CMP destination, source Example: destination < source mov al, 4 cmp al, 5 u ; 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) u u 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 u The IF statement in C and PASCAL is converted into CMP and Jcond instructions in x 86 Assembly: If (X > op 1) Then <…> End If CMP X, op 1 JNG End. If <…> End. If: 3
Jcond Instruction u u A conditional jump instruction branches to a label when specific register or flag conditions are met Examples: l l l 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 u u 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 u u If (op 1=op 2) then <…> end if Two different approaches: CMP op 1, op 2 JE True JMP End. If True: <…> End. If CMP op 1, op 2 JNE False <…> False: 6
- Slides: 7