📜 ⬆️ ⬇️

Low Level Brainfuck. Continuing ...

Part I
Part II
Part III

We write brainfuck on TurboAssembler'e.

Add the output of the data_arr array (“tape” of the Turing machine) to the screen.
')
Write a program that displays the elements of an arbitrary array through the function 09h interrupt 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 

On the screen we will see the ascii codes of the elements of the data_arr DB array 1,0,2,0,3,0,4,0,0,0,0,0,0,7, '$'



In order to represent the elements of the array in the form of numbers, we will use the div operator.

The div command NUMBER divides the register AX into NUMBER and places the integer part of the division in AL , and the remainder of the division in AH ( NUMBER can be either a memory area or a general register)

Let's output the 1st and 2nd elements of the array

 .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 

In order to display all the elements of the array, we will use the loop command.
Put in the register CX the number of clock cycles equal to the number of array elements and at each clock cycle we add one to the index of the array 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 

Next, add a loop that displays the elements of the array as numbers in the main program.

 .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 

Now HelloWorld looks like this



Since we do not process numbers greater than 99 , the number 100 is displayed incorrectly, the remaining numbers are displayed correctly.

Nested brackets


To handle nested brackets, we will place the opening brackets in the stack, and the closing brackets will be removed from the stack.

Let's write a simple program for working with a stack in 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. 

Took from here .

You can check here or here .

Modify the push procedure so that when size equals zero, we get a reference to the first element.

 procedure push(c : integer); begin a[size+1] := c; size := size + 1; end; 

Add a "stack" to the main program.

 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.com
If we meet the bracket, then we simply put its address in the stack, when we meet the closing bracket, then we extract its address from the stack, and if the value in the current cell is greater than zero, then we return to the opening bracket.

An example of using the normal / “standard” stack is shown in the bf51_stack.pas program .

"Add" a stack to the main assembler program

 .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 



Link to github.

Source: https://habr.com/ru/post/426649/


All Articles