-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSectionVar.cs
More file actions
62 lines (55 loc) · 1.42 KB
/
DataSectionVar.cs
File metadata and controls
62 lines (55 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Text;
namespace Compiler
{
enum DataSize
{
BYTE = 'b',
WORD = 'w',
DWORD = 'd',
QWORD = 'q'
}
class DataSectionVar
{
public string Name;
public string Value;
public char Size;
private static int floatConstantIndex = 0;
// Constructor
// input: variable's name
// variable's size in bytes
// variable's initial value
public DataSectionVar(string name, DataSize size, string value)
{
Name = name;
Value = value;
Size = (char)size;
}
// Method return variable as assembly code for it's declaration
// input: none
// return: none
public string ToAssembly()
{
return String.Format("{0}: d{1} {2}\n", Name, Size, Value);
}
// Method creates a data section variable for a float with incrementing index
// input: value
// return: none
public static DataSectionVar FloatConstant(float value)
{
string numString = value.ToString();
if (value == (int)value)
// is int, add decimal so nasm would treat it as float const
numString += ".0";
return new DataSectionVar("__f" + floatConstantIndex++, DataSize.DWORD, numString);
}
// Method creates a variable for a const string
public static DataSectionVar StringConstant(string name, string value, bool newline = true)
{
return new DataSectionVar(name, DataSize.BYTE,
String.Format("\"{0}\"{1}, 0", value, newline ? ", 0xa" : "")
);
}
}
}