简体   繁体   English

在C程序中创建数据危害

[英]Create a Data Hazard in a C Program

I am working on a problem where I am attempting to create different scenarios in different C programs such as 我正在尝试在不同的C程序中创建不同方案的问题,例如

  • Data Hazard 资料危害
  • Branch Evaluation 分行评估
  • Procedure Call 程序调用

This is in an attempt at learning pipelining and the different hazards that come up. 这是在尝试了解流水线以及出现的各种危险的尝试。

So I am writing simple C programs and disassembling to assembly language to see if a hazard gets created. 因此,我正在编写简单的C程序,并反汇编为汇编语言,以查看是否会造成危险。 But I cannot figure out how to create these hazards. 但我不知道如何制造这些危险。 Do yall have any idea how I could do this? 你们都知道我该怎么做吗? Here is some of the simple code I have written. 这是我编写的一些简单代码。

I compile using. 我编译使用。

gcc -g -c programName.c -o programName.o
gcc programName.o -o programName
objdump -d programName.o > programName.asm

Code: 码:

#include <stdio.h>
int main()
{
    int i = 0;
    int size = 5;
    int num[5] = {1,2,3,4,5};
    int sum=0;
    int average = 0;

    for(i = 0; i < size; i++)
    {
        sum += num[i];
    }

    average=sum/size;

    return 0;
}

...and here is the assembly for that. ...这是该程序集。

average.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   c7 45 f0 00 00 00 00    movl   $0x0,0xfffffffffffffff0(%rbp)
   b:   c7 45 f4 05 00 00 00    movl   $0x5,0xfffffffffffffff4(%rbp)
  12:   c7 45 d0 01 00 00 00    movl   $0x1,0xffffffffffffffd0(%rbp)
  19:   c7 45 d4 02 00 00 00    movl   $0x2,0xffffffffffffffd4(%rbp)
  20:   c7 45 d8 03 00 00 00    movl   $0x3,0xffffffffffffffd8(%rbp)
  27:   c7 45 dc 04 00 00 00    movl   $0x4,0xffffffffffffffdc(%rbp)
  2e:   c7 45 e0 05 00 00 00    movl   $0x5,0xffffffffffffffe0(%rbp)
  35:   c7 45 f8 00 00 00 00    movl   $0x0,0xfffffffffffffff8(%rbp)
  3c:   c7 45 fc 00 00 00 00    movl   $0x0,0xfffffffffffffffc(%rbp)
  43:   c7 45 f0 00 00 00 00    movl   $0x0,0xfffffffffffffff0(%rbp)
  4a:   eb 10                   jmp    5c <main+0x5c>
  4c:   8b 45 f0                mov    0xfffffffffffffff0(%rbp),%eax
  4f:   48 98                   cltq   
  51:   8b 44 85 d0             mov    0xffffffffffffffd0(%rbp,%rax,4),%eax
  55:   01 45 f8                add    %eax,0xfffffffffffffff8(%rbp)
  58:   83 45 f0 01             addl   $0x1,0xfffffffffffffff0(%rbp)
  5c:   8b 45 f0                mov    0xfffffffffffffff0(%rbp),%eax
  5f:   3b 45 f4                cmp    0xfffffffffffffff4(%rbp),%eax
  62:   7c e8                   jl     4c <main+0x4c>
  64:   8b 55 f8                mov    0xfffffffffffffff8(%rbp),%edx
  67:   89 d0                   mov    %edx,%eax
  69:   c1 fa 1f                sar    $0x1f,%edx
  6c:   f7 7d f4                idivl  0xfffffffffffffff4(%rbp)
  6f:   89 45 fc                mov    %eax,0xfffffffffffffffc(%rbp)
  72:   b8 00 00 00 00          mov    $0x0,%eax
  77:   c9                      leaveq 
  78:   c3                      retq   

Would appreciate any insight or help. 将不胜感激或帮助。 Thanks! 谢谢!

Since this is homework, I'm not going to give you a straight answer, but some food for thought to push you in the right direction. 由于这是家庭作业,因此我不会给您一个直截了当的答案,但是请您多加一些思考,以帮助您朝正确的方向发展。

x86 is a terrible ISA to be using to try and comprehend pipelining. x86是一种可怕的ISA,用于尝试理解流水线。 A single x86 instruction can hide two or three side-effects, making it difficult to tease out how a given instruction would perform in even the simplest of pipelines. 一条x86指令可以隐藏两个或三个副作用,即使是最简单的流水线也很难弄清给定指令的执行方式。 Are you sure you're not provided a RISC ISA to use for this problem? 您确定没有提供用于此问题的RISC ISA吗?

Put your loop/hazard code into a function and preferably randomize the creation of the array. 将循环/危险代码放入函数中,最好随机化数组的创建。 Make the array much longer. 使阵列更长。 A good compiler will basically figure out the answer otherwise and remove most of the code you wrote! 一个好的编译器将基本上找出答案,否则将删除您编写的大多数代码! For reasons I don't understand it's putting your variables in memory. 由于某些原因,我不明白这是将变量存储在内存中。

A good compiler will also do things such as loop unrolling in attempt to hide data hazards and get better code scheduling. 一个好的编译器还会做一些事情,例如展开循环,以试图隐藏数据危险并获得更好的代码调度。 Learn how to defeat that (or if you can, give the flag the compiler telling it to NOT do those things if messing around with the compiler is allowed). 了解如何克服这种情况(或者,如果可以的话,给标志赋予编译器告诉它如果允许编译器乱搞,则不要执行这些操作)。

The keyword "volatile" can be very helpful in telling the compiler to not optimize around/away certain variables (it tells the compiler this value can change at any moment, so don't be clever and optimize code with it and also don't keep the variable inside the register file). 关键字“ volatile”在告诉编译器不要对某些变量进行优化时非常有用(它告诉编译器此值随时可能发生变化,因此请不要聪明地使用它来优化代码,也不要这样做)将变量保留在寄存器文件中)。

A data hazard means the pipeline will stall waiting on data. 数据危险意味着管道将暂停等待数据。 Normally instructions get bypassed just in time, so no stalling occurs. 通常,指令会及时被绕开,因此不会发生停顿。 Think about which types of instructions may not be able to be bypassed and could cause a stall on a data hazard. 考虑一下可能无法绕过哪些类型的指令,并且可能导致数据危险停滞。 This is dependent on the pipeline, so code that stalls for a specific processor may not stall for another. 这取决于流水线,因此停顿在特定处理器上的代码可能不会停顿在另一个处理器上。 Modern out-of-order Intel processors are excellent at avoiding these stalls and compilers are great at re-scheduling code so they won't occur even on an in-order core. 现代乱序的英特尔处理器非常擅长避免这些停顿,而编译器则擅长重新调度代码,因此即使在有序内核上也不会出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM