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:
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:
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
The following is an example of destination operand = source operand:
; ZF CF
mov ax, 1000
mov cx, 1000
cmp cx, ax ; 1 0
The following is an example of destination operand > source operand:
;ZF CF
mov ax, 105
cmp ax, 0 ;0 0
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
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
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
Jump based on equality
Jump based on signed comparison
Jumps based on carry and zero flags
Previous article:Assembly language pushf and popf instructions
Next article:Assembly language 8086+8255A simulation interrupt control
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- threadx is now open source
- How do I remove the small SMD components from the circuit board?
- Let me ask, what are the most cutting-edge energy storage technologies currently?
- Review summary: Free review: The cheapest Hongmeng development board Neptune is here
- Has anyone used the software every circuit?
- Excuse me, is there any tutorial for assembling MCS-51 microcontroller?
- [Hardcore Science] What is a bipolar four-quadrant power supply? Is the power amplifier also a bipolar four-quadrant power supply?
- Nonvolatile MRAM and its cell structure
- FPGA controls DSP power-on reset procedure
- Sampling period