ZhenjaMax

Untitled

Sep 27th, 2021
840
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ASSUME CS:CODE, ds:DATA, SS:PROGRAM_CALLER_STACK
  2.  
  3. PROGRAM_CALLER_STACK SEGMENT STACK
  4.     DW 128 DUP (0)
  5. PROGRAM_CALLER_STACK ENDS
  6.  
  7. DATA SEGMENT
  8.     PARAMETERS_BLOCK DW 0   ; сегментный адрес среды
  9.                      DD 0   ; сегмент и смещение командной строки
  10.                      DD 0   ; сегмент и смещение первого FCB
  11.                      DD 0   ; сегмент и смещение второго FCB
  12.     KEEP_SS DW 0
  13.     KEEP_SP DW 0
  14.     KEEP_PSP DW 0
  15.    
  16.     ERROR_MEMORY_MSG DB "Memory error! Code:  ?", 0Dh, 0Ah, '$'
  17.     EXIT_CODE_MSG DB 0Dh, 0Ah, 0Dh, 0Ah, "Program was closed with ", '$'
  18.     EXIT_CODE_NUMBER_MSG DB "code   ?.", 0Dh, 0Ah, '$'
  19.     EXIT_CODE_CTRL_C_MSG DB "Ctrl+C.", 0Dh, 0Ah, '$'
  20.     EXIT_CODE_OTHER_MSG DB "other reason.", '$'
  21.    
  22.     CMD_STRING DW 010Dh
  23.     PATH_STACK DB 128 dup (0)
  24. DATA ENDS
  25.  
  26. CODE SEGMENT
  27. WRD_TO_DEC PROC near
  28.     push cx
  29.     push dx
  30.     mov dx, 0
  31.     mov cx, 10
  32. wloop_bd:  
  33.     div cx
  34.     or dl, 30h
  35.     mov [si], dl
  36.     dec si
  37.     xor dx, dx
  38.     cmp ax, 10
  39.     jae wloop_bd
  40.     cmp al, 00h
  41.     je wend_l
  42.     or al, 30h
  43.     mov [si], al
  44. wend_l:      
  45.     pop dx
  46.     pop cx
  47.     ret
  48. WRD_TO_DEC ENDP
  49.  
  50. PRINT_PROC PROC near
  51.     push ax
  52.     mov ah, 09h
  53.     int 21h
  54.     pop ax
  55.     ret
  56. PRINT_PROC ENDP
  57.  
  58. CHECK_ERROR_FLAG PROC near
  59.     push dx
  60.     push si
  61.     jnc CHECK_ERROR_FLAG_END
  62.    
  63.     mov si, offset ERROR_MEMORY_MSG + 21
  64.     call WRD_TO_DEC
  65.     mov dx, offset ERROR_MEMORY_MSG
  66.     call PRINT_PROC
  67.     mov al, 0
  68.     mov ah, 4Ch
  69.     int 21h
  70. CHECK_ERROR_FLAG_END:
  71.     pop si
  72.     pop dx
  73. CHECK_ERROR_FLAG ENDP
  74.  
  75. CHECK_EXIT_CODE PROC near
  76.     push dx
  77.     push si
  78.    
  79.     mov dx, 0
  80.     cmp ah, 0
  81.     jne CHECK_EXIT_CODE_1
  82.     mov si, offset EXIT_CODE_NUMBER_MSG + 7
  83.     call WRD_TO_DEC
  84.     mov dx, offset EXIT_CODE_NUMBER_MSG
  85.     call PRINT_PROC
  86.     jmp CHECK_EXIT_CODE_END
  87.    
  88. CHECK_EXIT_CODE_1:
  89.     cmp ah, 1
  90.     jne CHECK_EXIT_CODE_OTHER
  91.     mov dx, offset EXIT_CODE_CTRL_C_MSG
  92.     call PRINT_PROC
  93.     jmp CHECK_EXIT_CODE_END
  94.    
  95. CHECK_EXIT_CODE_OTHER:
  96.     mov dx, offset EXIT_CODE_OTHER_MSG
  97.     call PRINT_PROC
  98.    
  99. CHECK_EXIT_CODE_END:
  100.     pop si
  101.     pop dx
  102. CHECK_EXIT_CODE ENDP
  103.  
  104.  
  105. FREE_MEMORY PROC near
  106.     push ax
  107.     push bx
  108.    
  109.     mov bx, offset PROGRAM_CALLER_CODE_TAG
  110.     shr bx, 4
  111.     add bx, 100h    ; +data
  112.     mov ah, 4Ah
  113.     int 21h
  114.  
  115.     jnc FREE_MEMORY_END
  116.     call CHECK_ERROR_FLAG
  117. FREE_MEMORY_END:
  118.     pop bx 
  119.     pop ax
  120.     ret
  121. FREE_MEMORY ENDP
  122.  
  123.  
  124.  
  125. SET_PROGRAM_PATH PROC near
  126.     push ax
  127.     push bx
  128.     push si
  129.     push di
  130.     push ds
  131.     push es
  132.    
  133.     mov ax, ds
  134.     mov es, ax
  135.     mov ax, KEEP_PSP
  136.     mov ds, ax
  137.     mov ax, ds:[2Ch]
  138.     mov ds, ax
  139.     mov di, offset PATH_STACK
  140.     mov si, 0
  141.    
  142. loop_find_path_start:
  143.     mov al, ds:[si]
  144.     inc si
  145.     cmp al, 0h
  146.     jne loop_find_path_start
  147.     mov al, ds:[si]
  148.     cmp al, 0h
  149.     jne loop_find_path_start
  150.     jmp loop_find_path_end
  151.    
  152. loop_find_path_end:
  153.     add si, 3h
  154.     xor bx, bx
  155.     push si
  156.    
  157. loop_find_end_path_1:
  158.     mov al, ds:[si]
  159.     cmp al, 0h
  160.     je loop_find_end_path_2
  161.     inc si
  162.     inc bx
  163.     jmp loop_find_end_path_1
  164.    
  165. loop_find_end_path_2:
  166.     mov al, ds:[si]
  167.     cmp al, '\'
  168.     je loop_copy_path_start_1
  169.     dec si
  170.     dec bx
  171.     jmp loop_find_end_path_2
  172.  
  173. loop_copy_path_start_1:
  174.     inc bx
  175.     pop si
  176. loop_copy_path_start_2:
  177.     mov al, ds:[si]
  178.     mov es:[di], al
  179.     inc di
  180.     inc si
  181.     dec bx
  182.     cmp bx, 0h
  183.     jne loop_copy_path_start_2
  184.    
  185.     mov byte ptr es:[di + 0], 'L'   ; PROGRAM_NAME: "LB2.COM", 0
  186.     mov byte ptr es:[di + 1], 'B'
  187.     mov byte ptr es:[di + 2], '2'
  188.     mov byte ptr es:[di + 3], '.'
  189.     mov byte ptr es:[di + 4], 'C'
  190.     mov byte ptr es:[di + 5], 'O'
  191.     mov byte ptr es:[di + 6], 'M'
  192.     mov byte ptr es:[di + 7],  0
  193.    
  194.     pop es
  195.     pop ds
  196.     pop di
  197.     pop si
  198.     pop bx
  199.     pop ax
  200.     ret
  201. SET_PROGRAM_PATH ENDP
  202.  
  203.  
  204.  
  205. CALL_PROGRAM PROC near
  206.     push ax
  207.     push bx
  208.     push dx
  209.    
  210.     push ds
  211.     push es
  212.     mov KEEP_SP, sp
  213.     mov KEEP_SS, ss
  214.    
  215.     mov ax, DATA
  216.     mov es, ax
  217.     mov dx, offset CMD_STRING
  218.     mov bx, offset PARAMETERS_BLOCK
  219.     mov [bx + 2], dx
  220.     mov [bx + 4], ds
  221.     mov dx, offset PATH_STACK   ; ds:DX путь
  222.     mov ax, 4B00h               ; AL=0 => EXEC
  223.     int 21h                     ; запуск
  224.    
  225.     mov ss, KEEP_SS
  226.     mov sp, KEEP_SP
  227.     pop es
  228.     pop ds  
  229.    
  230.     jnc CALL_PROGRAM_RESULT
  231.     call CHECK_ERROR_FLAG
  232. CALL_PROGRAM_RESULT:
  233.     mov dx, offset EXIT_CODE_MSG
  234.     call PRINT_PROC
  235.     mov ax, 4D00h
  236.     int 21h
  237.     call CHECK_EXIT_CODE
  238.    
  239.     pop dx
  240.     pop bx
  241.     pop ax
  242.     ret
  243. CALL_PROGRAM ENDP
  244.  
  245.  
  246.  
  247. PROGRAM_CALLER PROC far
  248.     mov ax, DATA
  249.     mov ds, ax
  250.     mov KEEP_PSP, es
  251.    
  252.     call FREE_MEMORY
  253.     call SET_PROGRAM_PATH
  254.     call CALL_PROGRAM
  255.    
  256.     mov al, 0
  257.     mov ah, 4Ch
  258.     int 21h
  259. PROGRAM_CALLER ENDP
  260.  
  261. PROGRAM_CALLER_CODE_TAG:
  262. CODE ENDS
  263.  
  264. END PROGRAM_CALLER
  265.  
RAW Paste Data