Assembly 條件

SN

1

2

CMP 指令

CMP指令比較兩個操作數。它通常用於在條件執行。該指令基本上減去一個操作數進行比較的操作數是否等於或不從其他。它不干擾源或目的操作數。它是用來爲決策的條件跳轉指令。

語法

CMP destination, source

CMP比較兩個數字數據字段。目的操作數可以是寄存器或內存中。源操作數可以是一個常數（立即）數據，寄存器或內存。

例子:

CMP DX, 00 ; Compare the DX value with zero
JE L7 ; If yes, then jump to label L7 . . L7: ...

CMP往往是用於比較的計數器值是否已經達到了一個循環的時間的數量需要運行。考慮以下典型條件：

INC EDX
CMP EDX, 10 ; Compares whether the counter has reached 10 JLE LP1 ; If it is less than or equal to 10, then jump to LP1

無條件跳轉

語法:

JMP指令立即傳送控制流提供了一個標籤名稱。 JMP指令的語法是：

JMP label

實例:

MOV AX, 00 ; Initializing AX to 0 MOV BX, 00 ; Initializing BX to 0 MOV CX, 01 ; Initializing CX to 1 L20: ADD AX, 01 ; Increment AX
SHL CX, 1 ; shift left CX, this in turn doubles the CX value
JMP L20 ; repeats the statements

有條件跳轉

Instruction

Description

Flags tested

JE/JZ

Jump Equal or Jump Zero

ZF

JNE/JNZ

Jump not Equal or Jump Not Zero

ZF

JG/JNLE

Jump Greater or Jump Not Less/Equal

OF, SF, ZF

JGE/JNL

Jump Greater or Jump Not Less

OF, SF

JL/JNGE

Jump Less or Jump Not Greater/Equal

OF, SF

JLE/JNG

Jump Less/Equal or Jump Not Greater

OF, SF, ZF

Instruction

Description

Flags tested

JE/JZ

Jump Equal or Jump Zero

ZF

JNE/JNZ

Jump not Equal or Jump Not Zero

ZF

JA/JNBE

Jump Above or Jump Not Below/Equal

CF, ZF

JAE/JNB

Jump Above/Equal or Jump Not Below

CF

JB/JNAE

Jump Below or Jump Not Above/Equal

CF

JBE/JNA

Jump Below/Equal or Jump Not Above

AF, CF

Instruction

Description

Flags tested

JXCZ

Jump if CX is Zero

none

JC

Jump If Carry

CF

JNC

Jump If No Carry

CF

JO

Jump If Overflow

OF

JNO

Jump If No Overflow

OF

JP/JPE

Jump Parity or Jump Parity Even

PF

JNP/JPO

Jump No Parity or Jump Parity Odd

PF

JS

Jump Sign (negative value)

SF

JNS

Jump No Sign (positive value)

SF

CMP AL, BL
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL: ... EQUAL: ...

實例:

section .text global _start ;must be declared for using gcc

mov ecx, [num1] cmp ecx, [num2] jg check_third_num
mov ecx, [num3] check_third_num: cmp ecx, [num3] jg _exit
mov ecx, [num3] _exit: mov [largest], ecx
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel
mov ecx,largest
mov edx, 2 mov ebx,1 ;file descriptor (stdout) mov eax,4 ;system call number (sys_write) int 0x80 ;call kernel

``````    mov   eax, 1 int 80h section .data
msg db "The largest digit is: ", 0xA,0xD len equ \$\- msg
num1 dd '47' num2 dd '22' num3 dd '31' segment .bss``````

largest resb 2

The largest digit is:
47