-
Notifications
You must be signed in to change notification settings - Fork 50
/
add_bin.cpp
161 lines (129 loc) · 3.23 KB
/
add_bin.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* naken_asm MSP430 assembler.
* Author: Michael Kohn
* Email: [email protected]
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2023 by Michael Kohn
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "asm/common.h"
#include "common/Memory.h"
void add_bin8(AsmContext *asm_context, uint8_t b, int flags)
{
int line = DL_NO_CG;
if (asm_context->pass == 2 && flags == IS_OPCODE)
{
line = asm_context->tokens.line;
}
if (asm_context->pass == 1 && asm_context->pass_1_write_disable == 1)
{
asm_context->address++;
return;
}
asm_context->memory_write_inc(b, line);
}
void add_bin16(AsmContext *asm_context, uint16_t b, int flags)
{
int line = DL_NO_CG;
if (asm_context->pass == 2 && flags == IS_OPCODE)
{
line = asm_context->tokens.line;
}
if (asm_context->pass == 1 && asm_context->pass_1_write_disable == 1)
{
asm_context->address += 2;
return;
}
if (asm_context->memory.endian == ENDIAN_LITTLE)
{
// 1 little, 2 little, 3 little endian
asm_context->memory_write_inc(b & 0xff, line);
asm_context->memory_write_inc(b >> 8, DL_NO_CG);
}
else
{
asm_context->memory_write_inc(b >> 8, DL_NO_CG);
asm_context->memory_write_inc(b & 0xff, line);
}
}
void add_bin32(AsmContext *asm_context, uint32_t b, int flags)
{
int line = asm_context->tokens.line;
if (asm_context->pass == 1 && asm_context->pass_1_write_disable == 1)
{
asm_context->address += 4;
return;
}
if (asm_context->memory.endian == ENDIAN_LITTLE)
{
asm_context->memory_write_inc(b & 0xff, line);
asm_context->memory_write_inc((b >> 8) & 0xff, line);
asm_context->memory_write_inc((b >> 16) & 0xff, line);
asm_context->memory_write_inc((b >> 24) & 0xff, line);
}
else
{
asm_context->memory_write_inc((b >> 24) & 0xff, line);
asm_context->memory_write_inc((b >> 16) & 0xff, line);
asm_context->memory_write_inc((b >> 8) & 0xff, line);
asm_context->memory_write_inc(b & 0xff, line);
}
}
int add_bin_varuint(AsmContext *asm_context, uint64_t b, int fixed_size)
{
uint32_t num;
int count = 0;
int line = asm_context->tokens.line;
if (asm_context->memory.endian == ENDIAN_BIG)
{
printf("Warning: varuint only works with little endian at %s:%d\n",
asm_context->tokens.filename,
asm_context->tokens.line);
}
while (1)
{
fixed_size--;
num = b & 0x7f;
b = b >> 7;
if (b != 0 || fixed_size > 0)
{
num |= 0x80;
}
asm_context->memory_write_inc(num, line);
count++;
if (b == 0 && fixed_size <= 0) { break; }
}
return count;
}
int add_bin_varint(AsmContext *asm_context, uint64_t b, int fixed_size)
{
uint32_t num;
int count = 0;
int line = asm_context->tokens.line;
if (asm_context->memory.endian == ENDIAN_BIG)
{
printf("Warning: varint only works with little endian at %s:%d\n",
asm_context->tokens.filename,
asm_context->tokens.line);
}
while (1)
{
fixed_size--;
num = b & 0x7f;
b = b >> 7;
if (b != 0 || fixed_size > 0)
{
num |= 0x80;
}
asm_context->memory_write_inc(num, line);
count++;
if (b == 0 && fixed_size <= 0) { break; }
}
return count;
}