Assembly language CMP instructions

Publisher:翩翩轻舞Latest update time:2022-10-18 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The CMP (Compare) instruction performs an implicit subtraction operation of subtracting the source operand from the destination operand without modifying any operands.


Instruction format:


CMP destination operand, source operand


Flags When the actual subtraction occurs, the CMP instruction modifies the overflow, sign, zero, carry, auxiliary carry, and parity flags according to the calculation result. If two unsigned numbers are compared, the relationship between the two operands represented by the zero flag and the carry flag is as shown in the following table:

image.png

If two signed numbers are compared, the relationship between the two operands represented by the sign flag, zero flag and overflow flag is as shown in the following table:

image.png

The CMP instruction is an important tool for creating conditional logic structures. When using CMP in a conditional jump instruction, the execution result of the assembly language is the same as that of the IF statement.

The following is an example of destination operand < source operand:


; ZF CF

mov ax, 5

cmp ax, 10 ; 0 1

Insert image description here

The following is an example of destination operand = source operand:


                        ; ZF CF

mov ax, 1000

mov cx, 1000

cmp cx, ax ; 1 0

Insert image description here

The following is an example of destination operand > source operand:


   ;ZF CF

mov ax, 105

cmp ax, 0 ;0 0

Insert image description here

Use the CMP instruction to count the number of bytes with a value of 8 in the data segment, and use ax to save the statistical results.


; 1. Program to count the number of bytes with a value of 8 in the data segment, and use ax to save the statistical results

assume cs:code, ds:data, ss:stack


data segment

db 8, 11, 8, 1, 8, 5, 63, 38

data ends


stack segment stack

db 128 dup(0)

stack ends


code segment

start: mov ax, stack

mov ss,ax

mov sp, 128

call init_reg

call get_eight

mov ax, 4C00H

int 21H

;============================================

get_eight:

movsi,0

mov cx, 8

mov ax, 0

getEight: cmp byte ptr ds:[si], 8

jne nextNumber

inc ax

nextNumber: inc si

loop getEight

ret

;================================================

init_reg:

mov bx, data

mov ds, bx

ret

code ends


end start


Insert image description here

Use the CMP instruction to count the number of bytes in the data segment with a value greater than 8, and use ax to save the statistical results.


; 2. Program to count the number of bytes in the data segment with a value greater than 8, and use ax to save the statistical results.

assume cs:code, ds:data, ss:stack


data segment

db 8, 11, 8, 12, 8, 5, 63, 38

data ends


stack segment stack

db 128 dup(0)

stack ends


code segment

start: mov ax, stack

mov ss,ax

mov sp, 128

call init_reg

call get_eight

mov ax, 4C00H

int 21H

;============================================

get_eight:

movsi,0

mov cx, 8

mov ax, 0

getEight: cmp byte ptr ds:[si], 8

jna nextNumber; ja > na <=

inc ax

nextNumber: inc si

loop getEight

ret

;================================================

init_reg:

mov bx, data

mov ds, bx

ret

code ends


end start

Insert image description here

image.png

Based on signed examples:


mov al, 1

movbl, 2

cmp al, bl → infer al < bl

sub al, bl → influence flag 1 - 2 = -1 SF symbol flag position 1


mov al, 22H → 34 34 - (-96) = 140 -128 ~ 127

mov bl, A0H -96

sub al, bl → OF flag bit and SF flag bit, overflow flag position 1, symbol flag position 1


mov al, 8AH -118 - 112 = - 230 overflow 1A positive number

movbl, 70H

cmp al, bl OF = 1 SF = 0 Overflow flag position 1 Symbol flag position 0


Summary: based on signed comparison


Let cmp al, bl

if SF = 1 OF = 0

Then al < bl


If SF = 1 OF =1

Then al > bl


if SF = 0 OF = 1     

Because SF = 0

The inequality should be al - bl > 0 al > bl        

When OF = 1 holds, then

al < bl


if SF = 0 OF = 0    

Then al - bl > 0 

get al > bl


If the actual result is negative due to overflow, then logically the real result must be positive.


If the actual result is positive due to overflow, then logically the real result must be negative.


Jump based on unsigned comparison

image.png

Jump based on equality

image.png

Jump based on signed comparison

image.png

Jumps based on carry and zero flags

image.png

Reference address:Assembly language CMP instructions

Previous article:Assembly language pushf and popf instructions
Next article:Assembly language 8086+8255A simulation interrupt control

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号