-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfi.asm
144 lines (118 loc) · 3.86 KB
/
bfi.asm
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
global main
section .data
i dd 0 ;mi servirà per salvare lo stato di edi
lenfile dd 0
addr dd 0
erroropen db "Error to open file"
usage db "Usage: <./name> <file.bf>\n"
section .bss
buff resb 3000 ;alloco 3000 byte sul seg bss
mem resb 6000 ;alloco 6000 byte sul seg bss
section .text
main :
pop eax ;prendo argc dallo stack
pop esi ;prendo ProgramName
pop ebx ;infine il filename
cmp eax,2
jl errorarg ;se argc < 2 jmp errorarg
mov eax, 5 ;open
mov ecx, 0
mov edx, 0
int 0x80
cmp eax,0
jl erroropen ;se eax < 0 jmp erroropen
mov ebx, eax
mov eax, 3 ;read file
mov ecx, buff
mov edx, 3000
int 0x80
mov [lenfile],eax
mov eax, 6 ;close file
int 0x80
xor edi, edi ; edi contatore per buff
xor esi, esi ; esi contatore per mem
xor eax, eax ; eax conterrà di volta in volta il byte del file
interpret :
mov byte al, [buff+edi] ;metto in al il byte buff[edi]
cmp edi, [lenfile] ;controllo se edi è fuori file
jg exit
inc edi
;inizio switch-case
cmp al, '+'
je plus
cmp al, '-'
je minus
cmp al, '>'
je major
cmp al, '<'
je lower
cmp al, '.'
je putch
cmp al, ','
je getch
cmp al, '['
je squareop
cmp al, ']'
je squarecl
jmp interpret ;default è un commento
plus : ;mem[esi]++
inc byte [mem+esi]
jmp interpret
minus : ;mem[esi]--
dec byte [mem+esi]
jmp interpret
major : ;esi++
inc esi
jmp interpret
lower :
dec esi ;esi--
jmp interpret
putch : ;putchar(mem[esi])
mov ecx, esi
add ecx, mem ;mem[esi]
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
jmp interpret
getch : ;mem[esi]=getchar()
mov ecx, esi
add ecx, mem ;mem[esi]
mov eax, 3
mov ebx, 0
mov edx, 1
int 0x80
jmp interpret
squareop : ;while(*ptr){
cmp byte [mem+esi],0 ;se mem(esi) == 0 va avanti fino a ']'
je squarecl
mov [i], edi ;else salva edi in i
jmp interpret ;interpreta byte nel ciclo
squarecl : ;}
cmp byte [mem+esi], 0 ;se mem[esi] !=0
jne editk ; vai all'istruzione dopo [
jmp interpret
editk :
mov edi, [i] ;reimposta in edi [i] così da ricominciare il byte dopo '['
jmp interpret
errorarg : ;mostra errore ed esce
mov eax,4
mov ebx,1
mov ecx,usage
mov edx,29
int 0x80
mov eax,1
mov ebx,0
int 0x80
erropen : ;mostra errore ed esce
mov eax, 4
mov ebx, 1
mov ecx, erroropen
mov edx, 20
int 0x80
mov eax, 1
mov ebx, 0
int 0x80
exit : ;end
mov eax, 1
int 0x80