Repository navigation
Expand file tree
/
Copy pathbyte_strings.c
More file actions
82 lines (73 loc) · 2.25 KB
/
Copy pathbyte_strings.c
File metadata and controls
82 lines (73 loc) · 2.25 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* byte_strings.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eblackbu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/05/03 13:56:35 by eblackbu #+# #+# */
/* Updated: 2020/05/03 13:56:43 by eblackbu ### ########.fr */
/* */
/* ************************************************************************** */
#include "asm.h"
void value_to_bytes(char **arg, int value, int len)
{
if (len == 1)
value = (char)value;
else if (len == 2)
value = (short)value;
*arg = ft_memcpy(*arg, &value, len);
}
int get_arg_len(t_instr **instr, t_arg arg)
{
if (arg.type == REG_CODE)
return (REG_BYTE);
if (arg.type == IND_CODE)
return (IND_BYTE);
if (arg.type == DIR_CODE && \
(*instr)->type.number > 8 && (*instr)->type.number != 13)
return (IND_BYTE);
return (DIR_BYTE);
}
void set_arg_to_string(t_instr **instr, int *i, t_arg arg)
{
char *tmp_arg;
int len;
int j;
len = get_arg_len(instr, arg);
if (!(tmp_arg = (char*)malloc(sizeof(char) * len)))
exit(-1);
value_to_bytes(&tmp_arg, arg.value, len);
j = len - 1;
while (j > -1)
(*instr)->bytestr[(*i)++] = tmp_arg[j--];
ft_strdel(&tmp_arg);
}
void make_one_string(t_instr **instr)
{
int i;
int num_arg;
i = 0;
num_arg = 0;
if (!(((*instr)->bytestr = (char*)malloc(sizeof(char) \
* (*instr)->full_size))))
exit(-1);
(*instr)->bytestr[i++] = (*instr)->type.number;
if ((*instr)->instr_byte)
(*instr)->bytestr[i++] = (*instr)->instr_byte;
while (num_arg < (*instr)->type.count_args)
{
set_arg_to_string(instr, &i, (*instr)->args[num_arg]);
num_arg++;
}
}
void make_byte_strings(t_champion **champ)
{
t_instr *tmp;
tmp = (*champ)->code;
while (tmp)
{
make_one_string(&tmp);
tmp = tmp->next;
}
}