本文介绍了什么是cmpq指令呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读下面定义的系统调用:

I was reading the following definition for syscall:

.text
.globl syscall
.type   syscall,%function
.align 16
syscall:
    movq %rdi, %rax     /* Syscall number -> rax.  */
    movq %rsi, %rdi     /* shift arg1 - arg5.  */
    movq %rdx, %rsi
    movq %rcx, %rdx
    movq %r8, %r10
    movq %r9, %r8
    movq 8(%rsp),%r9    /* arg6 is on the stack.  */
    syscall         /* Do the system call.  */
    cmpq $-4095, %rax   /* Check %rax for error.  */
    jae __syscall_error     /* Branch forward if it failed.  */
    ret         /* Return to caller.  */

.size syscall,.-syscall

我看到它解释说,该行 cmpq $ -4095%RAX 确定%RAX是否包含-1和-4095之间的值。它是如何做到的?究竟该cmpq指令做什么呢?

I saw it explained that the line cmpq $-4095 %rax determines whether %rax contains a value between -1 and -4095. How does it do that? What exactly does the cmpq instruction do?

推荐答案

cmpq $ -4095,RAX% 64位寄存器%RAX与即时值进行比较 -4095 - 值是符号扩展为64位,用于比较的目的。即 -4095 有64位2的补再presentation: FFFF FFFF FFFF F001

cmpq $-4095, %rax compares the 64-bit register %rax with the immediate value -4095 - the value is sign-extended to 64-bits for the purposes of the comparison. i.e., -4095 has the 64-bit 2's complement representation: ffff ffff ffff f001

CMP 指令设置的标志从第二个操作数的注册,因为它会为(减)第一 - '第二'和'第一'被扭转了AT& T公司的语法。 或(RAX + 4095)( - - (4095)RAX):在效果标志根据结果集C>,是在2的补数是相同的。

cmp instructions set the flags register as it would for a sub (subtract) of the second operand from the first - 'second' and 'first' being reversed in AT&T syntax. In effect the flags are set according to the result of: (RAX - (- 4095)) or (RAX + 4095), being the same in 2's complement.

一组标志是的执行的标志(CF),这是在(符号)溢出设置。在指令(跳,如果在上面的或相等)实际上是 JNC (跳一个别名 - 如果 - 不进位)。换句话说,该分支机构采取了(RAX + 4095)做的的携带。在2的补数,这将是在范围 RAX 的价值观真: [ - 4095,-1] 。 (牢记2的补数算术环绕方式)。

One of the flags set is the carry flag (CF), which is set on (unsigned) overflow. The jae instruction (jump-if-above-or-equal) is actually an 'alias' for jnc (jump-if-not-carry). In other words, the branch is taken if (RAX + 4095) does not carry. In 2's complement, this will be true for values of RAX in the range: [-4095, -1]. (Keeping in mind how 2's complement arithmetic wraps).

该指令,包括 CMP (或 J< COND> )的描述:Intel® 64和IA-32架构软件开发人员手册,第2卷。

The instructions, including cmp and jae (or j<cond>) are described in: Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 2.

在[E]标志位寄存器(什么算术标志表示)在Intel® 64和IA-32架构软件开发人员手册,第1卷。

The [E]FLAGS register (and what the arithmetic flags denote) are described in section 3.4.3 of Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 1.

这篇关于什么是cmpq指令呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:18