Reputation: 2025
I'm writing a simple compiler for Brainf*** in Ada, but I'm in serious trouble in the code generation, therefore I am unable to generate a binary file with opcode instructions, correctly because whenever I write the file, it saves hexa decimal value like an integer. ( Integer size )
I made a simple sample of wat I try to make:
With Ada, Ada.Sequential_IO, Ada.Integer_Text_IO;
Use Ada;
procedure main is
type Byte is range -128 .. 127;
for Byte'Size use 16;
package Bin_IO is new Sequential_IO(Integer);
File : Bin_IO.File_Type;
FileName : constant String := "teste.bin";
Num : Byte;
begin
Bin_IO.Create(File => File,
Name => FileName);
Num := 16#FF#;
Bin_IO.Write(File => File,
Item => Num);
Bin_IO.Close(File => File);
end main;
The spected result in file is just FF but, when I open the file in hex editor I have FF00 0000
How I can save the opcode instructions correctly correctly??
Upvotes: 2
Views: 3187
Reputation: 2025
I found functional solution...
I changed the type to Character type, and, I just convert the machine opcode to char type and write in file, that work's very well..., I don't know if that is the best way, but works
With Ada, Ada.Sequential_IO, Ada.Integer_Text_IO;
Use Ada;
procedure main is
package Bin_IO is new Sequential_IO(Character);
File : Bin_IO.File_Type;
FileName : constant String := "teste.bin";
Num : Character;
begin
Bin_IO.Create(File => File,
Name => FileName);
Num := Character'Val(16#97#);
Bin_IO.Write(File => File,
Item => Num);
Bin_IO.Close(File => File);
end main;
Upvotes: 0
Reputation: 24086
Try changing line 9 to:
package Bin_IO is new Sequential_IO(Byte);
It changes the generic package to sequence of bytes. Bin_IO.Write should now write Bytes instead.
Upvotes: 1