小编Wos*_*ame的帖子

C宏 - 如何将整数值转换为字符串文字

是否可以将#defined 整数符号的值逐字插入到作为GCC(AVR Studio)中汇编部分一部分的字符串文字中?

我希望在下面的asm()块内的字符串文字中将"LEDS"替换为48.

#define LEDS 48 //I only want ONE mention of this number in the source
int x = LEDS;   //I'm using the value directly too

void DrawFrame()
{
    asm(
    "ldi        R27, 0x00       \n\t"
    "ldi        R26, 0x00       \n\t"
    "ldi        R18, LEDS       \n\t" //<-- substitution needed here
...
}
Run Code Online (Sandbox Code Playgroud)

但我希望编译器/汇编程序(在预处理器完成它的工作之后)看到这个......

#define LEDS 48 //I only want ONE mention of this number in the source
int x = LEDS;   //I'm using the value directly too

void DrawFrame()
{
    asm( …
Run Code Online (Sandbox Code Playgroud)

c assembly gcc avr c-preprocessor

31
推荐指数
3
解决办法
4311
查看次数

除以零不会导致Nvidia Jetson出现运行时异常

我对ARM处理器的内部细节不是很熟悉,但是我不了解Nvidia Jetson Nano开发板上的以下行为。

C代码示例...

//main.c

#include <stdio.h>

int main()
{
  int fred = 123;
  int i;

  for(i = -10 ; i <= 10 ; i++)
    printf("%d / %d == %d\n", fred, i, fred / i);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:

gcc main.c -ggdb
Run Code Online (Sandbox Code Playgroud)

运行生成的a.out可执行文件将产生以下输出...

123 / -10 == -12
123 / -9 == -13
123 / -8 == -15
123 / -7 == -17
123 / -6 == -20
123 / -5 == -24
123 / -4 == -30
123 / …
Run Code Online (Sandbox Code Playgroud)

c arm integer-division divide-by-zero armv8

8
推荐指数
1
解决办法
113
查看次数

Ada语言-如何存储函数返回的字符串值?

我正在尝试在Ada 2012中进行一些基本的命令行交互,但是我找不到一种方法来捕获从Ada.Command_Line.Command_Name()函数返回的字符串。

我在网上可以找到的所有示例都只需使用Put()打印字符串,而无需先将其存储在本地变量中。这是我尝试过的错误代码,虽然可以编译,但是CONSTRAINT_ERROR ... length check failed在我尝试将返回String值分配给String变量时抛出了一个错误代码...

with Ada.Command_Line; use Ada.Command_Line;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;

procedure Command_Line_Test is
  ArgC : Natural := 0;
  InvocationName : String (1 .. 80);
begin
  ArgC := Argument_Count;
  Put ("Number of arguments provided: ");
  Put (ArgC'Image);
  New_Line;

  InvocationName := Command_Name;  --  CONSTRAINT_ERROR here
  Put ("Name that the executable was invoked by: ");
  Put (InvocationName);
  New_Line;

end Command_Line_Test;
Run Code Online (Sandbox Code Playgroud)

我仅以Command_Name为例,但可以想象它是任何其他函数都可以返回字符串(也许在程序生命周期内可能会多次更改的字符串),我们应该如何声明局部变量才能存储返回的字符串串?

string declaration function ada

6
推荐指数
2
解决办法
129
查看次数

Ada-一维数组操作

参考约翰·巴恩斯(John Barnes)的《Ada 2012中的编程》(ISBN:978-1-107-42481-4),第138页(第8.6节)。

procedure ArrayOps is
  type Bit_Row is array (Positive range <>) of Boolean;

  A, B : Bit_Row (1 .. 4);

  T : constant Boolean := True;
  F : constant Boolean := False;
begin
  A := (T, T, F, F);
  B := (T, F, T, F);

  A := A and B;
end ArrayOps;
Run Code Online (Sandbox Code Playgroud)

我为作者提供的最小片段添加了一个简单的包装器,它似乎可以按预期进行编译和运行。

作者指出,这可能与“许多运营商的”来完成,这意味着算术比如+*-/

我试图使用Integer数据类型将其转换为加法运算符,但可惜它无法编译...

procedure ArrayOps is
  type Int_Row is array (Positive range <>) of Integer;

  A, …
Run Code Online (Sandbox Code Playgroud)

compiler-errors ada

4
推荐指数
1
解决办法
70
查看次数

Ada-SHA3实现的明智代码结构

我将SHA3规范用作Ada编程语言的学习示例。

规范包含称为“排列”的数据结构家族,其中有七个,它们的区别仅在于能够处理不同数量的数据。它们包含一个三维“状态数组”,其中前两个维始终是mod 5,第三个维是mod N,其中N限于以下几个值:25、50、100、200、400、800和1600。

我通常认为使用泛型来区分每个程序包变体,但是区别是数字而不是类型。

我如何明智地设计包裹/记录类型?

我能想到的唯一方法就是简单地创建一组显式类型。

package Perm_25 is...
package Perm_50 is...
package Perm_100 is...
package Perm_200 is...
package Perm_400 is...
package Perm_800 is...
package Perm_1600 is...
Run Code Online (Sandbox Code Playgroud)

显然,这很荒谬,因为它很费力,并且需要我重复很多代码,这会导致不一致。

我也不相信OOP在这里会有所帮助,因为除了某些数组维之外,类型实际上并没有什么不同。

我应该如何解决这个问题?


编辑:感谢用户@flyx的提示,以使用Static_Predicate子类型和区分的记录类型。使用该建议,我设法获得以下代码进行编译...

package body SHA3 is

  subtype Perm_w_Coeff_Type is Positive
    with Static_Predicate
      => Perm_w_Coeff_Type in 1 | 2 | 4 | 8 | 16 | 32 | 64;

  subtype Perm_l_Coeff_Type is Natural range 0 .. 6;

  type State_Array_Type is array (Natural range <>, Natural range <>, …
Run Code Online (Sandbox Code Playgroud)

ada

4
推荐指数
1
解决办法
82
查看次数

Ada-如何显式打包位字段记录类型?

请考虑以下实验性Ada程序,该程序尝试创建具有定义明确的位字段的32位记录,创建一个并将其输出到文件流...

with System;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Streams.Stream_Io; use Ada.Streams.Stream_Io;

procedure Main is

   type Bit is mod    (2 ** 1);

   type Opcode_Number is mod (2 ** 4);
   type Condition_Number is mod (2 ** 4);
   type Operand is mod (2 ** 9);

   type RAM_Register is
      record
         Opcode : Opcode_Number;
         Z      : Bit;
         C      : Bit;
         R      : Bit;
         I      : Bit;
         Cond   : Condition_Number;
         Rsvd_1 : Bit;
         Rsvd_2 : Bit;
         Dest   : Operand;
         Src    : Operand;
      end record;

   for RAM_Register …
Run Code Online (Sandbox Code Playgroud)

record ada stream bit-fields

3
推荐指数
1
解决办法
88
查看次数

Ada-如何初始化包含File_Type成员的有限标记类型?

在以下代码中,Mix_Card_Reader继承自Mix_IO_Device,后者是抽象的标记记录。

以前它包含一个Positive和两个Stream_Access成员。我想更改代码,以便File_Type改为使用成员。

这样做的原因是,我希望这种类型的每个实例都能够在需要时打开和关闭其文件,或者在需要时完全不打开。

问题是我无法初始化此继承类型,因为File_Type它是有限类型。如何编写我的Create_Mix_Card_Reader函数以允许这样做?

.ads ...

   type Mix_IO_Device is abstract tagged limited
      record
         Block_Size : Positive;
         Input_File : File_Type;
         Output_File : File_Type;
      end record;

   type Mix_Card_Reader is new Mix_IO_Device with null record;
Run Code Online (Sandbox Code Playgroud)

.adb ...

   function Create_Mix_Card_Reader return Mix_IO_Device_Access is
      Ret : Mix_IO_Device_Access := new Mix_Card_Reader'(16, null, null);
   begin
      return Ret;
   end Create_Mix_Card_Reader;
Run Code Online (Sandbox Code Playgroud)

GNAT抱怨我不能传递null, null给这对File_Type成员,因为它们当然是不兼容的,空值是以前拥有Stream_Access成员时的遗留物。似乎我必须在这里传递一些信息,但是我不想过早地打开文件只是为了使编译器满意。

该怎么办?

编辑:我有几个明显的选择:

  • 使用access File_Type代替(但我仍然必须在其他位置保持文件的打开/关闭)。
  • 将所有File_Type对象分别存储在一个数组中,并像以前一样使用Streams引用它们,但这似乎很麻烦。

oop syntax inheritance ada

3
推荐指数
1
解决办法
64
查看次数

Ada-关于“ mod 64”类型定义的编译器警告

我需要有效值介于0和63之间(含0和63)的模块化整数类型。如...

type Mix_Byte is mod 64;
Run Code Online (Sandbox Code Playgroud)

这确实可以按预期进行编译和工作,但是编译器有助于我将注意力吸引到我这一方面可能的监督上。

warning: 2 ** 64 may have been intended here
Run Code Online (Sandbox Code Playgroud)

碰巧的是我根本不打算这样做,但是很高兴知道编译器在监视:)

似乎只对值32或64发出此警告,而不对值8、16或128发出警告。我知道32和64是常见的整数大小,在这种情况下2 ** n会有意义。

如何针对此特定实例使该特定编译器警告静音(我想在整个项目中全局允许它,以防万一我在其他地方犯了真正的错误)。

我想我可以以不同的方式表达代码,以便更准确地表达我的意思?

syntax types ada

2
推荐指数
1
解决办法
64
查看次数

Ada-(Streams)如何在不事先知道字符串长度的情况下正确调用String'Read()

我正在尝试编写一个快速程序,以将AT命令发送到串行端口调制解调器。我已经使用正确的设置(B115200、8N1等)打开了端口,下面的代码示例中的String'Write调用实际上可以正常工作。

现在,我要添加代码以将调制解调器的响应读回字符串。但是,我无法事先知道响应的长度,因此out String除非我知道长度,否则无法创建String变量来传递给参数。

  package GSC renames GNAT.Serial_Communications;

  SP : aliased GSC.Serial_Port;

  function Send (Port : in GSC.Serial_Port; S : in String) return String is
  begin
    String'Write (SP'Access, S);
    delay 0.1;

    declare
      Retval : String;  -- NOT VALID - needs to be initialised
    begin
      String'Read (SP'Access, Retval);
      return Retval;
    end;
  end Send;
Run Code Online (Sandbox Code Playgroud)

我这里有鸡肉/鸡蛋的情况。

string serialization ada stream

2
推荐指数
1
解决办法
77
查看次数

VHDL - "Net pwr不断被驱动"

我正在尝试学习VHDL,作为练习,我正在尝试构建一个使用RS-232样式信令(8N1格式)的非常简单的串行端口.

这是小项目中两个vhdl文件的代码......

"glue.vhd"...(顶级模块)

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.std_logic_unsigned.all;
library MACHXO2;
use MACHXO2.components.all;

entity Glue is
    port(
        tx : out std_logic := '1' --idle high
        );
end entity Glue;

architecture behavioural of Glue is
    SIGNAL clk  : STD_LOGIC;
    signal divider : std_logic_vector(23 downto 0);

--internal oscillator
    COMPONENT OSCH
      GENERIC(
            NOM_FREQ: string
            );
      PORT( 
            STDBY    : IN  STD_LOGIC;
            OSC      : OUT STD_LOGIC;
            SEDSTDBY : OUT STD_LOGIC);
    END COMPONENT;
begin
--internal oscillator
   OSCInst0: OSCH
      GENERIC MAP (NOM_FREQ  => "3.69")
      PORT …
Run Code Online (Sandbox Code Playgroud)

compiler-errors vhdl lattice-diamond

1
推荐指数
1
解决办法
807
查看次数

VHDL(Xilinx工具链)我被"阵列修整"所打破

我有一个双文件VHDL项目,我有初学者的困难.

它需要系统时钟并使用30位时钟分频器(我只使用少量非连续位)驱动原始串行端口模块(仅输出TX)模块定期吐出8位字符.

似乎在合成过程中,优化器正在删除许多基本信号,这是我没想到的.

顶级文件"Glue.vhd"......

library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.numeric_std.all;
use ieee.std_logic_unsigned.all;

entity Glue is
    port(
        clk : in std_logic;
        tx  : out std_logic;
        LED : out std_logic_vector(1 downto 0)
        );
end entity Glue;

architecture behavioural of Glue is
    signal divider : unsigned(29 downto 0);
begin
    LED(1) <= '0';

    ser_tx : entity SerialTX
    port map (
            baud_clk => divider(12),
            byte_to_transmit => std_ulogic_vector(divider(29 downto 22)),
            poke => divider(20),
            busy => LED(0),
            serial_out => tx
            );

    clocker : process(clk)
    begin
        IF(rising_edge(clk)) then …
Run Code Online (Sandbox Code Playgroud)

synthesis vhdl compiler-warnings

1
推荐指数
1
解决办法
136
查看次数

Ada-如何实现允许主线程轮询的异步任务?

我想创建一个从文件读取几分钟的任务,而主线程执行其他操作。但是我希望主线程能够轮询任务以查看它是否为“忙”(布尔值)而不会阻塞主线程。

我在这里有一个幼稚的尝试,它确实起作用,但是它使Busy标志完全暴露出来,可以由主线程随意切换(这是不安全的)...

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is

  task type Non_Blocking_Reader_Task (Busy : access Boolean) is
    entry Read (Destination : in Natural);
  end Non_Blocking_Reader_Task;

  task body Non_Blocking_Reader_Task is
  begin
    loop
      select
        when not Busy.all =>
          accept Read (Destination : in Natural) do
            Busy.all := True;
          end Read;

          for i in 1 .. 50 loop
            Put ("."); --  pretend to do something useful
            delay 0.1; --  while wasting time
          end loop;

          Busy.all := False;
      end select;
    end loop;
  end Non_Blocking_Reader_Task; …
Run Code Online (Sandbox Code Playgroud)

ada task

1
推荐指数
1
解决办法
76
查看次数

Ada-Discrete_Random示例

John Barnes撰写的“ Ada 2012中的编程”的第53页共享了我无法使用的不完整代码片段。

我想出了一个完整的程序来扩展本书中的代码...

with Ada.Numerics; use Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
  type Coin is (Heads, Tails);
  package Random_Coin is new Discrete_Random(Coin);
  use Random_Coin;

  G : Generator;
  C : Coin;
begin

  for i in 1 .. 20 loop
    C := Random(G);
    Put (C'Image);
  end loop;  

end Main;
Run Code Online (Sandbox Code Playgroud)

我正在使用的“ GPS” IDE抱怨以下错误:

  • 第6行:“ Discrete_Random”未定义
  • 第7行:“ Random_Coin”未定义
  • 第9行:“ Generator”未定义
  • 第14行:“随机”未定义

IDE确实给了我“ intellisense”(使用Visual Studio中的术语),它指示Discrete_Random实际上是可见的,并且在添加了“ with”和“ use”语句的情况下可用。

有人可以引导我解决我犯下的愚蠢错误吗?

ada

0
推荐指数
1
解决办法
68
查看次数