x86-64 addq指令仅具有一个操作数是什么意思? (摘自CSAPP第三版) [英] What does this x86-64 addq instruction mean, which only have one operand? (From CSAPP book 3rd Edition)

查看:1248
本文介绍了x86-64 addq指令仅具有一个操作数是什么意思? (摘自CSAPP第三版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下说明中,addq如何工作?它只有一个操作数,该书声称它递增%rdx,但%rdx不在此指令中。我很困惑...

In the following instructions, how does the addq work? It only has one operand, the book claims that it increments %rdx, but %rdx is not in this instruction. I am so confused...

这是《计算机系统程序员概论》第三版的书。

This is from the book Computer Systems A Programmers Perspective, 3rd Edition.

推荐答案

@Jester在评论中指出。确实是一个错误。我实际上输入了
程序,并在Linux上使用gcc对其进行了编译。下面是结果。

As @Jester pointed out in the comment. It is indeed an error. I actually typed in the program and compiled it using gcc on linux. Below is the results.

C程序:badcnt.c

C program: badcnt.c

/*
 * badcnt.c - An improperly synchronized counter program
 */
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>

void *thread(void *vargp);  /* Thread routine prototype */

/* Global shared variable */
volatile int cnt = 0; /* Counter */

int main(int argc, char **argv)
{
  int niters;
  pthread_t tid1, tid2;

  /* Check input argument */
  if (argc != 2) {
    printf("usage: %s <niters>\n", argv[0]);
    exit(0);
  }
  niters = atoi(argv[1]);

  /* Create threads and wait for them to finish */
  pthread_create(&tid1, NULL, thread, &niters);
  pthread_create(&tid2, NULL, thread, &niters);
  pthread_join(tid1, NULL);
  pthread_join(tid2, NULL);

  /* Check result */
  if (cnt != (2 * niters))
    printf("BOOM! cnt=%d\n", cnt);
  else
    printf("OK cnt=%d\n", cnt);
  exit(0);
}

/* Thread routine */
void *thread(void *vargp)
{
  int i, niters = *((int *)vargp);

  for (i = 0; i < niters; i++)
    cnt++;

  return NULL;
}

使用gcc 6.3.0编译

Compile using gcc 6.3.0

$ gcc -pthread -Og -S badcnt.c

$ gcc -pthread -Og -S badcnt.c

下面是badcnt.s中的内容

Below is the contents in badcnt.s

        .file   "badcnt.c"
        .text
        .globl  thread
        .type   thread, @function
thread:
.LFB20:
        .cfi_startproc
        movl    (%rdi), %ecx
        movl    $0, %edx
        jmp     .L2
.L3:
        movl    cnt(%rip), %eax
        addl    $1, %eax
        movl    %eax, cnt(%rip)
        addl    $1, %edx
.L2:
        cmpl    %ecx, %edx
        jl      .L3
        movl    $0, %eax
        ret
        .cfi_endproc
.LFE20:
        .size   thread, .-thread
        .section        .rodata.str1.1,"aMS",@progbits,1
.LC0:
        .string "usage: %s <niters>\n"
.LC1:
        .string "BOOM! cnt=%d\n"
.LC2:
        .string "OK cnt=%d\n"
        .text
        .globl  main
        .type   main, @function
main:
.LFB19:
        .cfi_startproc
        subq    $40, %rsp
        .cfi_def_cfa_offset 48
        cmpl    $2, %edi
        je      .L5
        movq    (%rsi), %rsi
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        movl    $0, %edi
        call    exit
.L5:
        movq    8(%rsi), %rdi
        movl    $10, %edx
        movl    $0, %esi
        call    strtol
        movl    %eax, 28(%rsp)
        leaq    28(%rsp), %rcx
        movl    $thread, %edx
        movl    $0, %esi
        leaq    16(%rsp), %rdi
        call    pthread_create
        leaq    28(%rsp), %rcx
        movl    $thread, %edx
        movl    $0, %esi
        leaq    8(%rsp), %rdi
        call    pthread_create
        movl    $0, %esi
        movq    16(%rsp), %rdi
        call    pthread_join
        movl    $0, %esi
        movq    8(%rsp), %rdi
        call    pthread_join
        movl    28(%rsp), %eax
        addl    %eax, %eax
        movl    cnt(%rip), %edx
        cmpl    %edx, %eax
        je      .L6
        movl    cnt(%rip), %esi
        movl    $.LC1, %edi
        movl    $0, %eax
        call    printf
.L7:
        movl    $0, %edi
        call    exit
.L6:
        movl    cnt(%rip), %esi
        movl    $.LC2, %edi
        movl    $0, %eax
        call    printf
        jmp     .L7
        .cfi_endproc
.LFE19:
        .size   main, .-main
        .globl  cnt
        .bss
        .align 4
        .type   cnt, @object
        .size   cnt, 4
cnt:
        .zero   4
        .ident  "GCC: (GNU) 6.3.0"
        .section        .note.GNU-stack,"",@progbits

因此它确认了这本书的错误。

这篇关于x86-64 addq指令仅具有一个操作数是什么意思? (摘自CSAPP第三版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆