Partie IPartie IIPartie IIICréation d'un traducteur de langage
brainfuck sur TurboAssembler'e.
Ajoutez la sortie du tableau
data_arr (la «bande» de la machine Turing) à l'écran.
Écrivons un programme qui affiche les éléments d'un tableau arbitraire au moyen de la fonction d'interruption
09h 21h.model tiny ; ascii-decoder.asm jumps .data data_arr DB 1,0,2,0,3,0,4,0,5,0,6,0,7,'$' ; .code ORG 100h start: ; mov AX, @data ; mov DS,AX ;;;;;;;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah INT 21h mov dx,offset data_arr ; mov ah,09h ; int 21h ;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah INT 21h mov AX, 4c00h ; int 21h end start
Sur l'écran, nous verrons les codes ascii des éléments du
tableau DB data_arr 1,0,2,0,3,0,4,0,5,0,6,0,7, '$'
Afin de représenter les éléments du tableau sous forme de nombres, nous utiliserons l'opérateur
div .
La commande
div NUMBER divise le registre
AX par
NUMBER et place la partie entière de la division dans
AL , et le reste de la division dans
AH (le
NUMBER peut être soit une zone mémoire soit un registre général)
Imprimer les premier et deuxième éléments du tableau
.model tiny ; ascii-decoder.asm jumps .data data_arr DB 10,12,0,0,0,0,0,0,0,0,'$' ; .code ORG 100h start: ; mov AX, @data ; mov DS,AX ;;;;;;;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah INT 21h ;mov dx,offset data_arr ; ;mov ah,09h ; ;int 21h ;; sub AH, AH ; AH mov AL, data_arr ; mov BL, 10 ; div BL ; AL=, AH= mov BX,AX add BX,3030h mov AH,2 ; 21h mov DL,BL ; int 21h mov DL, BH ; int 21h ; sub AH, AH ; AH mov AL, data_arr+1 ; mov BL, 10 ; div BL ; AL=, AH= mov BX,AX add BX,3030h mov AH,2 ; 21h mov DL,BL ; int 21h mov DL, BH ; int 21h ;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah INT 21h mov AX, 4c00h ; int 21h end start
Afin d'afficher tous les éléments du tableau, nous utiliserons la commande
loop .
Nous mettons dans le registre
CX le nombre de mesures égal au nombre d'éléments du tableau et sur chaque mesure nous en ajouterons un à l'index du tableau
i .
.model tiny ; ascii-decoder1.asm jumps .data data_arr DB 3,5,6,7,0,11,12,13,0,20,'$' ; i DB 0,'$' .code ORG 100h start: ; mov AX, @data ; mov DS,AX ;;;;;;;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah INT 21h ;mov dx,offset data_arr ; ;mov ah,09h ; ;int 21h mov CX, 0Ah _prev: ;; ; mov BL,i sub AH, AH ; AH mov AL, data_arr[BX] ; mov BL, 10 ; div BL ; AL=, AH= mov BX,AX add BX,3030h mov AH,2 ; 21h mov DL,BL ; int 21h mov DL, BH ; int 21h ; sub DL, DL int 21h ;;; sub BX,BX inc i ; mov BL, i loop _prev ;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah INT 21h mov AX, 4c00h ; int 21h end start
Ensuite, ajoutez une boucle qui affiche les éléments du tableau sous forme de nombres dans le programme principal.
.model tiny jumps .data str_arr DB 256h DUP('$') data_arr DB 0,0,0,0,0,0,0,0,0,0,'$' i DB 0,'$' j DB 0,'$' i_stor DB 0,'$' .code ORG 100h start: mov AX, @data mov DS,AX ;;; mov ah, 3fh mov cx, 100h mov dx,OFFSET str_arr int 21h ;;; mov DL, str_arr prev: cmp DL, 24h je exit_loop cmp DL, 2Bh jne next mov BL, j inc data_arr[BX] next: cmp DL, 2Dh jne next1 mov BL, j dec data_arr[BX] next1: cmp DL, 3Eh jne next2 inc j next2: cmp DL, 3Ch jne next3 dec j next3: cmp DL, 2Eh jne next4 mov AH,2 mov BL, j mov DL, data_arr[BX] int 21h next4: cmp DL, 5Bh jne next5 ;mov BL, j ;mov DL, data_arr[BX] ;cmp DL, 00 ;jz next5 mov DL, i mov i_stor, Dl next5: cmp DL, 5Dh jne next6 mov BL, j mov DL, data_arr[BX] cmp DL, 00 jz next6 mov DL, i_stor mov i, DL next6: inc i mov BL, i mov DL, str_arr[BX] ; loop prev jmp prev exit_loop: ;;;;;;;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah ; INT 21h ; ; output data_arr mov CX, 0Ah ; 10 sub AL,AL ; AL mov i, AL ; sub BX,BX ; BX _prev: ; incorrect 1st element sub AH, AH ; AH mov AL, data_arr[BX] ; ;mov AL, data_arr+1 mov BL, 10 ; div BL ; AL= AH= mov BX,AX add BX,3030h mov AH,2 ; 2 21h mov DL,BL ; int 21h mov DL, BH ; int 21h ; ( ) sub DL, DL int 21h ;;; sub BX,BX inc i ; mov BL, i loop _prev ;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah ; INT 21h ; mov AX, 4c00h ; int 21h end start
HelloWorld ressemble maintenant à ceci

Étant donné que nous ne traitons pas les nombres supérieurs à
99 , le nombre
100 ne s'affiche pas correctement, les nombres restants sont affichés correctement.
Crochets imbriqués
Pour traiter les parenthèses imbriquées, nous mettrons les parenthèses ouvrantes sur la pile, et les parenthèses fermantes sur la pile.
Nous allons écrire un programme simple pour travailler avec la pile en Pascal.
var a : array[1..10] of integer; size : integer; procedure push(c : integer); begin size := size + 1; a[size] := c; end; procedure pop; begin size := size - 1; end; begin size := 0; Push(1); writeln(a[size]); Push(2); writeln(a[size]); Push(3); writeln(a[size]); Pop(); writeln(a[size]); Pop(); writeln(a[size]); end.
A pris
d'ici .
Vous pouvez vérifier
ici ou
ici .
Modifiez la procédure
push de sorte que lorsque la
taille est nulle, nous obtenons un lien vers le premier élément.
procedure push(c : integer); begin a[size+1] := c; size := size + 1; end;
Ajoutez une «pile» au programme principal.
Program bf5_stack; LABEL prev,next; var a : array[1..10] of integer; size : integer; data_arr:array[1..10] of integer; // str_arr: string; // i,j,k: integer; // i_stor: integer; //Stack procedure push(c : integer); begin a[size+1] := c; size := size + 1; end; procedure pop; begin size := size - 1; end; {---------------------------------------------------} begin j:=1; // i:=1; size := 0; { } //readln(str_arr); // //str_arr:='+++[>+++[>+<-]<-]'; // 3*3=9 str_arr:='+++[> +++[>+++[>+<-]<-] <-]'; //3^3=27; prev: if i>length(str_arr) then goto next; if (str_arr[i]='+') then data_arr[j]:= data_arr[j]+1; if (str_arr[i]='-') then data_arr[j]:= data_arr[j]-1; if (str_arr[i]='>') then j:=j+1; if (str_arr[i]='<') then j:=j-1; if (str_arr[i]='.') then write(chr(data_arr[j])); // if (str_arr[i]='[') then Push(i); if (str_arr[i]=']') then begin Pop(); if (data_arr[j]>0) then begin i := a[size+1]; goto prev; end; end; i:=i+1; goto prev; next: for k:=1 to 10 do begin write(data_arr[k]); write(' '); end; end.
ideone.comSi nous rencontrons une parenthèse ouvrante, nous mettons simplement son adresse sur la pile, lorsque nous rencontrons une parenthèse fermante, puis nous extrayons son adresse de la pile, si la valeur dans la cellule actuelle est supérieure à zéro, nous retournons à la parenthèse ouvrante.
Un exemple d'utilisation d'une pile normale / «standard» est montré dans le programme
bf51_stack.pas«Ajouter» une pile au programme assembleur principal
.model tiny ; bf7_stack_decoder.asm jumps .data str_arr DB 256h DUP('$') ; 256 data_arr DB 0,0,0,0,0,0,0,0,0,0,'$' ; i DB 0,'$' ; j DB 0,'$' ; i_stor DB 0,'$' .code ORG 100h start: ; mov AX,@data ; mov DS,AX ;;; mov ah, 3fh ; mov cx, 100h ; 256 mov dx,OFFSET str_arr int 21h ;;; mov DL, str_arr ; DL 1 ;mov CX, 100h ; 256 prev: cmp DL, 24h ; '$' je exit_loop cmp DL, 2Bh ; + jne next ; , next mov BL, j ; BL inc data_arr[BX] ; , 1 next: cmp DL, 2Dh ; - jne next1 ; , next1 mov BL, j dec data_arr[BX] ;BX, Bl next1: cmp DL, 3Eh ; > jne next2 ; , next2 inc j ; , data_arr next2: cmp DL, 3Ch ; < jne next3 ; , next3 dec j ; , data_arr next3: cmp DL, 2Eh ; . jne next4 ; , next4 mov AH,2 ; , mov BL, j mov DL, data_arr[BX] int 21h next4: cmp DL, 5Bh ; [ jne next5 ; , next5 ;sub DX,DX mov AL, i ; push AX next5: cmp DL, 5Dh ; ] jne next6 ; , next6 sub AX,AX pop AX mov BL, j mov DL, data_arr[BX] cmp DL, 00 ; , data_arr jz next6 ; , mov i, AL ; i_stor i mov BL, i mov DL, str_arr[BX] jmp prev next6: inc i ; mov BL, i mov DL, str_arr[BX] jmp prev exit_loop: ; ascii- MOV AH,2 ; MOV DL,0Ah ; INT 21h ; ; output data_arr mov CX, 0Ah ; 10 sub AL,AL ; AL mov i, AL ; sub BX,BX ; BX _prev: ; incorrect 1st element sub AH, AH ; AH mov AL, data_arr[BX] ; ;mov AL, data_arr+1 mov BL, 10 ; div BL ; AL= AH= mov BX,AX add BX,3030h mov AH,2 ; 2 21h mov DL,BL ; int 21h mov DL, BH ; int 21h ; ( ) sub DL, DL int 21h ;;; sub BX,BX inc i ; mov BL, i loop _prev ;;;;;;;;;; MOV AH,2 ; MOV DL,0Ah ; INT 21h ; ;;;;;;;;;;;;;;; mov AX, 4c00h ; int 21h END start
Lien vers github.