Advertisement
rnort

LAB5-AVMIS

Dec 25th, 2012
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .386P
  2. ;; Segment descriptor structure
  3. descr struc
  4. lim dw 0
  5. base_l dw 0
  6. base_m db 0
  7. attr_1 db 0
  8. attr_2 db 0
  9. base_h db 0
  10. descr ends
  11. ;; Trap gate structure
  12. trap struc
  13. offs_l dw 0
  14. sel dw 16
  15. cntr db 0
  16. dtype db 8Fh
  17. offs_h dw 0
  18. trap ends
  19.  
  20. data segment use16
  21. ;; Global descriptors table
  22. gdt_null descr <>
  23. gdt_data descr <data_size-1,,,92h>
  24. gdt_code descr <code_size-1,,,98h>
  25. gdt_stack descr <255,,,92h,,>
  26. gdt_screen descr <3999,8000h,0Bh,92h>
  27. gdt_size = $-gdt_null
  28. ;; IDT
  29. idt label word          ;; label for exception table start
  30. trap 13 dup (<dummy>)   ;; 0-12  exceptions
  31. trap <exc13>            ;; exception 13
  32. trap 18 dup (<dummy>)   ;; exceptions 14-31
  33. trap <new_08,,8Eh>      ;; timer interrupt
  34. trap <new_09,,8Eh>      ;; keyboard interrupt
  35. idt_size=$-idt          ;; idt size label
  36.  
  37. ;; Other data definition
  38. pdescr df 0         ;; Descriptor for lgdt and lidt
  39. sym db 1
  40. attr db 1Eh
  41. msg db 27,'We returned to real mode!',27,'$'
  42. master db 0
  43. slave db 0
  44. time dw 0
  45. delay_str   db 30, ?, 30 dup('$')
  46. intsNum dw ?
  47. delay_prompt   db  "Set delay to: ", '$'
  48. data_size=$-gdt_null
  49. data ends
  50.  
  51. ;; Command segment
  52.  
  53. text segment use16
  54. assume CS:text, DS:data
  55.  
  56. textseg label word
  57. exc13 proc
  58.     pop eax
  59.     pop eax
  60.     iret
  61. exc13 endp
  62.  
  63. dummy proc
  64.     iret
  65. dummy endp
  66.  
  67. new_08 proc
  68.      push ax
  69.      push bx
  70.      push di        
  71.      inc edx
  72.      cmp edx, 50
  73.      jb continue_count      ; Less than 50 hits
  74.      
  75. second_dec:
  76.      mov di, 40h                ; start position to print remaining time
  77.      mov edx, 0f200f20h         ; overrride old value with spaces
  78.      mov dword ptr es:[di], edx
  79.      mov dword ptr es:[di+4], edx
  80.      mov ax, time       ; remaining time( in seconds )
  81.      mov bx, 10         ; base
  82.      call print_number
  83.      dec time           ; decrease remaining time
  84.      mov edx, 0
  85. continue_count:  
  86.      mov al, 020h      
  87.      out 020h, al       ;; 20h - > master
  88.      out 0A0h, al       ;; 20h -> slave
  89.      pop di
  90.      pop bx
  91.      pop ax
  92.      iretd
  93. new_08 endp
  94.  
  95. new_09 proc
  96.  
  97.     push ax
  98.     in al, 60h
  99.     in al, 61h
  100.     or al, 80h
  101.     out 61h, al
  102.     and al, 7Fh
  103.     out 61h, al
  104.     mov al, 20h
  105.     out 20h, al
  106.     pop ax
  107.         ;;db 66h
  108.         iretd
  109. new_09 endp
  110.  
  111. ;;---------------------
  112. ;; SI - string address
  113. ;; BL - number base
  114. ;; CL - string len
  115. ;; AX - result
  116. ;; BP - error code
  117. ;; -------------------
  118. to_number proc near
  119.     push si
  120.     push bx
  121.     push dx
  122.     push cx  
  123.     xor bh, bh    
  124.     xor ax, ax
  125.     xor bp, bp    
  126.     xor dh, dh
  127.     xor ch, ch      
  128. next_char:
  129.     mul bx      ; dx:ax=ax*bx
  130.     jb error_   ; Overflow means error
  131.     mov dl,[si] ; Get next number
  132.     cmp dl,48   ; Less than 48 is not number
  133.     jb error_
  134.     cmp dl,71           ; Letter bigger "F"
  135.     jae testLittle      ; Lowercase a-f letters checking
  136.     jmp test_           ; 0-9 digits and A-F letters checking
  137.      
  138. testLittle:
  139.     cmp dl,97  
  140.     jb error_  
  141.     cmp dl,103  
  142.     jae error_
  143.     jmp m_ab  
  144.      
  145. test_:    
  146.     cmp dl,58   ;; Less than 58 means 0-9 digits
  147.     jb m_1    
  148.     cmp dl,65   ;; Bigger than 65 means A-F letters
  149.     jae m_A
  150.  
  151. ;; Value between 58 and 64 prohibited
  152. error_:
  153.     mov ax,0        ;; Zero is error
  154.     mov bp,1        
  155.     jmp to_exit
  156. m_1:
  157.     sub dl,48
  158.     jmp next_
  159. m_A:
  160.     sub dl,55
  161.     jmp next_
  162. m_ab:
  163.     sub dl,87
  164. next_:
  165.     cmp dl,bl   ;; Next number in dl, base in bl
  166.     jae error_  ;; number bigger than base
  167.     add ax,dx  
  168.     jb error_   ;; overflow means error
  169.     inc si
  170.     loop next_char        
  171. to_exit:    
  172.     pop cx
  173.     pop dx
  174.     pop bx  
  175.     pop si
  176.     ret
  177. to_number endp
  178.  
  179. ;; -------------
  180. ;; AX - target number
  181. ;; -------------
  182. print_number proc
  183.     push cx
  184.     push dx
  185.     push ax
  186.     xor cx,cx
  187. again2:
  188.     sub dx,dx
  189.     div bx      ; ax/bx, remainder in dx
  190.     inc cx      ; Count digits
  191.     push dx    
  192.     cmp ax,0    
  193.     jne again2  ; While number not zero
  194. toBuffer:
  195.     pop dx      ; Get digit from stack
  196.     add dx,30h  ; Add 30h to get its ASCII code
  197.     mov dh, 00001110b   ;; color code
  198.     mov word ptr es:[di], dx    
  199.     add di, 2
  200.     loop toBuffer
  201.     pop ax
  202.     pop dx
  203.     pop cx  
  204.     ret
  205. print_number endp
  206.  
  207. ;; Init timer
  208. timer_init proc near
  209.     push ax
  210.     mov al, 00110110b
  211.     out 43h, al  
  212.     mov ax, 05D2Eh ;; set 50Hz frequence
  213.     out 40h, al
  214.     shr ax, 8  
  215.     out 40h, al
  216.     pop ax
  217.     ret
  218. timer_init ENDP
  219.  
  220.  
  221. enter_delay proc near
  222.  
  223.     push ax
  224.     push dx
  225.     push si
  226.     push bx
  227.     push cx
  228. ;; Clear screen
  229.     mov ax,3
  230.     int 10h
  231. ;;
  232.     lea dx, delay_prompt  
  233.     mov ah, 09h
  234.     int 21h
  235.     lea dx, delay_str;
  236.     mov ah,0Ah          
  237.     int 21h              
  238.     mov si, offset delay_str+2
  239.     mov bl, 10
  240.     mov cl, delay_str[1]
  241.     call to_number  
  242.     mov time, ax
  243. ;; Clear screen
  244.     mov ax,3
  245.     int 10h
  246. ;;
  247.     pop cx
  248.     pop bx
  249.     pop SI
  250.     pop dx
  251.     pop ax
  252.     ret
  253. enter_delay endp
  254.  
  255. load_gdt_addr proc near
  256.  
  257.     shl eax, 4
  258.     mov [bx].base_l, ax
  259.     shr eax, 16
  260.     mov [bx].base_m, al
  261.     ret
  262. load_gdt_addr endp
  263.  
  264.  
  265. save_mask proc near
  266.     push ax
  267.     in al, 21h
  268.     mov master, al
  269.     in al, 0A1h
  270.     mov slave, al
  271.     pop ax
  272.     ret
  273. save_mask endp
  274.  
  275.  
  276. init_master proc near
  277.    
  278.     mov al, 11h
  279.     out 20h, al
  280.     mov al, dl
  281.     out 21h, al
  282.     mov al, 4
  283.     out 21h, al
  284.     mov al, 1
  285.     out 21h, al
  286.     ret
  287. init_master endp
  288.  
  289. init_segments_pr proc near
  290.     ;; Make data available
  291.     mov ax, 8
  292.     mov ds, ax
  293.     ;; Make stack available
  294.     mov ax, 24
  295.     mov ss, ax
  296.     ;; Initialize ES
  297.     mov ax, 32
  298.     mov es, ax
  299.     ret
  300. init_segments_pr endp
  301.  
  302. return_masks proc near
  303.     push ax
  304.     mov al, master
  305.     out 21h, al
  306.     mov al, slave
  307.     out 0A1h, al
  308.     pop ax
  309.     ret
  310. return_masks  endp
  311.  
  312. ;; --------------------------------------------------------
  313. ;; --------------------------------------------------------
  314. main proc
  315.     xor eax, eax
  316.     mov ax, data
  317.     mov ds, ax
  318.     call timer_init
  319.     call enter_delay
  320.  
  321.     shl eax, 4                
  322.     mov ebp, eax
  323. ;; Calculate and load in GDT linear address of data segment
  324.     mov ax, data
  325.     mov bx, offset gdt_data
  326.     call load_gdt_addr
  327. ;; Calculate and load in GDT linear address of command segment
  328.     xor eax, eax
  329.     mov ax, cs
  330.     mov bx, offset gdt_code
  331.     call load_gdt_addr
  332. ;; Calculate and load in GDT linear address of stack segment
  333.     xor eax, eax
  334.     mov ax, ss
  335.     mov bx, offset gdt_stack
  336.     call load_gdt_addr
  337. ;; Prepare pdescr and load GDTR
  338.     mov dword ptr pdescr+2, ebp        ;; GDT base
  339.     mov word ptr pdescr, gdt_size-1
  340.     lgdt pdescr                        ;; load GDTR
  341.     cli
  342. ;; Save interrupt masks
  343.     call save_mask
  344. ;; init master ( base vector now 32)
  345.     push dx
  346.     mov dl, 32
  347.     call init_master
  348.     pop dx
  349.     mov al, 0FCh   ;; New mask
  350.     out 21h, al
  351.  
  352. ;; LOad IDTR
  353.  
  354.     mov word ptr pdescr, idt_size-1
  355.     xor eax, eax
  356.     mov ax, offset idt
  357.     add eax, ebp    ;; IDT linear address
  358.     mov dword ptr pdescr+2, eax
  359.     lidt pdescr
  360.  
  361. ;; Switch mode to protected
  362.     mov eax, cr0
  363.     or eax, 1          ;; set protected mode bit
  364.     mov cr0, eax
  365.  
  366. ;; Now in protected mode
  367. ;; Load in CS:IP address of 'continue' label
  368.     db 0EAh
  369.     dw offset continue
  370.     dw 16
  371. continue:
  372.     call init_segments_pr
  373. home:
  374.     xor ecx, ecx    
  375.     sti
  376. wait_:              ;; wait for delay end
  377.     cmp time, -1  
  378.     jne wait_
  379. toRealMode:    
  380. ;; Return to real mode
  381.     cli
  382. ;; Made and load descriptors for real mode
  383.     mov gdt_data.lim, 0FFFFh
  384.     mov gdt_code.lim, 0FFFFh
  385.     mov gdt_stack.lim, 0FFFFh
  386.     mov gdt_screen.lim, 0FFFFh
  387.     push ds ;; data segment register
  388.     pop ds
  389.     push ss ;; load stack segment register
  390.     pop ss
  391.     push es ;; load extended segment register
  392.     pop es
  393.  
  394. ;; Do far jmp
  395.     db 0EAh
  396.     dw offset go
  397.     dw 16
  398. go:
  399. ;; Switch processor mode
  400.     mov eax, cr0
  401.     and eax, 0FFFFFFFEh
  402.     mov cr0, eax
  403. ;; Do far jmp to return label
  404.     db 0eah
  405.     dw offset return
  406.     dw text
  407. ;; Now in real mode
  408. return:
  409.     mov ax, data
  410.     mov ds, ax
  411.     mov ax, stk
  412.     mov ss, ax
  413.     mov sp, 256
  414. ;; Reload state of IDTR
  415.     mov ax, 3FFh
  416.     mov word ptr pdescr, ax
  417.     mov eax, 0
  418.     mov dword ptr pdescr+2, eax
  419.     lidt pdescr
  420.     sti
  421. ;; Reinit master PIC and set base vector 08h
  422.     push dx
  423.     mov dl, 8
  424.     call init_master
  425.     pop dx
  426. ;; Return mask for master and slave
  427.     call return_masks
  428.     sti
  429.  
  430. ;; Write msg
  431.    
  432.     mov ah, 09h
  433.     mov dx, offset msg
  434.     int 21h
  435.     mov ax, 4c00h
  436.     int 21h
  437. main endp
  438.  
  439. code_size=$-textseg
  440. text ends
  441.  
  442. stk segment stack use16
  443.     db 256 dup('^')
  444. stk ends
  445.  
  446. end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement