Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;%define BOCHS_HD_IMAGE
  2.  
  3.     BITS    16
  4.  
  5.     [org    0x7C00]
  6. boot:
  7.     cli
  8.    
  9.     xor     ax, ax
  10.     mov     ds, ax
  11.     mov     gs, ax      
  12.    
  13.     mov     ax, 0B800h
  14.     mov     es, ax                              ;VGA color text data segment
  15.    
  16.     mov     ax, 07E0h
  17.     mov     ss, ax
  18.     mov     bp, ax
  19.     mov     sp, 9C00h
  20.    
  21.     mov     ax, word int70h_handler           ;setup 70h interrupt for RTC timer
  22.     mov     [gs:70h*4], ax
  23.     mov     [gs:70h*4+2], ds
  24.    
  25. ;    mov     ax, 64
  26. ;    mov     fs, ax
  27. ;    mov     [fs:160], byte 1         ;Trick BIOS into thinking RTC is in use, disable int 15h functions 83/86
  28.    
  29.     sti
  30.  
  31.    
  32.  ;   jmp     _debug
  33.    
  34.     xor     ax, ax    
  35.     xor     di, di
  36.     mov     cx, 80 * 25
  37.     rep     stosw               ;clear screen, mov [es:di], ax
  38.    
  39.  
  40. fizzbuzz:                       ;(!(i%5)^!!(i%3)*3)*6
  41.     xor     ax, ax
  42.     xor     bx, bx
  43.    
  44.     mov     al, byte [i]        ;i%5
  45.     mov     dl, 05h
  46.     div     byte dl          
  47.  
  48.     test    ah, 07h             ;!
  49.     pushf
  50.     pop     cx
  51.     and     cx, 40h
  52.     shr     cx, 06h
  53.    
  54.     xor     ah, ah
  55.     mov     al, byte [i]        ;i%3
  56.     mov     dl, 03h
  57.     div     byte dl
  58.  
  59.     test    byte ah, 03h        ;!!
  60.     push    cx
  61.     pushf
  62.     pop     cx
  63.     and     cx, 40h
  64.     shr     cx, 06h
  65.     xor     cx, 01h
  66.     mov     al, cl
  67.     pop     cx
  68.    
  69.     mov     dx, 03h             ;*3
  70.     mul     dx
  71.     xor     al, cl              ;^
  72.    
  73.     cmp     al, 0
  74.     jz      dofizz
  75.     cmp     al, 1
  76.     jz      dofizzbuzz
  77.     cmp     al, 2
  78.     jz      dobuzz
  79.  
  80.     xor     ah, ah
  81.     mov     al, byte [i]        ;int to ascii
  82.     mov     dl, 0Ah
  83.     div     byte dl
  84.     add     al, 30h
  85.     add     ah, 30h
  86.     mov     [istr], al
  87.     mov     [istr+2], ah
  88.     lea     si, [istr]
  89.     jmp     do_print
  90.    
  91. dofizz:
  92.     lea     si, [fizz]
  93.     jmp     do_print
  94.    
  95. dobuzz:
  96.     lea     si, [buzz]
  97.     jmp     do_print
  98.    
  99. dofizzbuzz:
  100.     lea     si, [fb]
  101.     jmp     do_print
  102.    
  103.    
  104. do_print:        
  105.     call    print_line    
  106.  
  107.     inc     byte [i]
  108.     cmp     byte [i], 65h
  109.    
  110.     jnz     fizzbuzz                    
  111.  
  112.  
  113. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;            
  114. _debug:
  115.     xor     ax, ax
  116.     cli
  117. ;;;;    RTC init
  118.     mov     al, 8Bh                     ;select reg B, disable NMI
  119.     out     70h, al
  120.    
  121.     in      al, 71h                     ;read B
  122.     or      al, 40h
  123.     push    ax
  124.    
  125.     mov     ax, 8Bh                     ;select B
  126.     out     70h, al                    
  127.     pop     ax
  128.     out     71h, al
  129.    
  130.     mov     al, 8Ah                     ;select reg A and disable NMI
  131.     out     70h, al
  132.    
  133.     in      al, 71h                     ;read reg A value
  134.     and     al, 11110000b               ;keep top nibble
  135.     or      al, 0Fh                     ;OR in divider in low nibble  
  136.     push    ax
  137.    
  138.     mov     al, 8Ah
  139.     out     70h, al                     ;write value to A
  140.     pop     ax
  141.     out     71h, al
  142.    
  143.     mov     al, 0Ch
  144.     in      al, 71h
  145.    
  146.     in      al, 0A1h                    ;clear PIC 2 mask to ensure that IRQ 8 fires
  147.     xor     al, al
  148.     out     0A1h, al
  149.    
  150.     sti
  151.  
  152. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  153.  
  154.     mov     byte [tmp], 0
  155.     jmp     $
  156.    
  157.    
  158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  159.  
  160.  
  161.     ;si - string
  162. print_line:
  163. .iter:
  164.     lodsw
  165.     cmp     al, 0
  166.     je      .newline
  167.     mov     cx, 1
  168.     call    print_char
  169.     call    vga_update_cursor
  170.     cmp     byte [cursorx], 80
  171.     jl      .iter
  172. .newline:
  173.     mov     byte [cursorx], 0
  174.     inc     byte [cursory]
  175.     call    vga_update_cursor
  176.     call    vga_scroll_screen
  177. .quit:
  178.     ret
  179.    
  180.     ;al - char, ah - attr, cl - rep
  181.     ;clobbers dx
  182. print_char:
  183.     mov     word [tmp], ax
  184.  
  185.     xor     ax, ax
  186.     xor     dx, dx
  187.     mov     al, byte [consolew]
  188.     sub     al, byte [cursorx]
  189.     cmp     al, cl
  190.     jge     .outc
  191.     sub     cl, al
  192. .outc:
  193.     mov     al, byte [cursory]
  194.     mul         byte [consolew]
  195.     mov     dl, byte [cursorx]
  196.     add     ax, dx
  197.     mov     di, ax
  198.     mov     word [coffset], di      ;store off the unshifted offset for cursor positioning
  199.     push    di
  200.     shl     di, 1                   ;shift to word offset
  201.     push    cx
  202.     mov     ax, word [tmp]
  203.     rep     stosw
  204.    
  205.     pop     cx
  206.     pop     di
  207.     push    cx
  208.     add     di, word [csize4]
  209.     shl     di, 1
  210.     rep     stosw                   ; copy 4 console screens down for loop
  211.    
  212.     pop     cx
  213.     add     byte [cursorx], cl
  214.  
  215.     ret
  216.    
  217. vga_update_cursor:
  218.     push    ax
  219.     push    bx
  220.     push    dx
  221.    
  222.     mov     bx, word [coffset]
  223.    
  224.     mov     dx, 3D4h        ;set VGA register index
  225.     mov     al, 0Fh
  226.     out     dx, al
  227.    
  228.     mov     dx, 3D5h        ;low cursor position
  229.     mov     ax, bx
  230.     out     dx, al
  231.    
  232.     mov     dx, 3D4h        ;set register index again
  233.     mov     al, 0Eh
  234.     out     dx, al
  235.    
  236.     mov     dx, 3D5h        ;high cursor position
  237.     mov     al, bh
  238.     out     dx, al
  239.    
  240.     pop     dx
  241.     pop     bx
  242.     pop     ax
  243.    
  244.     ret
  245.    
  246. vga_scroll_screen:
  247.     push    ax
  248.     push    bx
  249.     push    dx
  250.    
  251.     mov     al, byte [cursory]
  252.     mov     bl, byte [consolew]
  253.     mul     bl
  254.     xor     dx, dx
  255.     div     word [csize4]
  256.    
  257.     xchg    bx, dx
  258.    
  259.     mov     dx, 3D4h
  260.     mov     al, 0Ch
  261.     out     dx, al
  262.    
  263.     mov     dx, 3D5h
  264.     mov     al, bh
  265.     out     dx, al
  266.    
  267.     mov     dx, 3D4h
  268.     mov     al, 0Dh
  269.     out     dx, al
  270.    
  271.     mov     dx, 3D5h
  272.     mov     al, bl
  273.     out     dx, al
  274.    
  275.     pop     dx
  276.     pop     bx
  277.     pop     ax
  278.    
  279.     ret
  280.    
  281.    
  282. align 16
  283. int70h_handler:                     ;RTC timer interrupt
  284.     inc     byte [tmp]
  285.     cmp     byte [tmp], 5
  286.    
  287.     jnz     .noscroll
  288.    
  289.     mov     byte [tmp], 0
  290.     inc     byte [cursory]
  291.    
  292.     call    vga_scroll_screen
  293.    
  294. .noscroll:    
  295.     mov     al, 0Ch
  296.     out     70h, al                 ;read RTC reg C. We have to do this to keep receiving interrupts
  297.     in      al, 71h
  298.    
  299. ;    mov     [fs:09Bh], dword 0400h
  300.     mov     al, 0x20      ; non-specific EOI
  301.     out     0xa0, al      ; PIC 2 (slave)
  302.     out     0x20, al      ; PIC 1 (master)
  303.    
  304.     iret
  305.    
  306.  
  307.     i db 01h
  308.     fizz db 'F',0Ah,'i',0Dh,'z',0Ah,'z',0Dh,0,0
  309.     buzz db 'B',0Eh,'u',0Ch,'z',0Eh,'z',0Ch,0,0
  310.     fb   db 'F',0Dh,'i',0Ah,'z',0Dh,'z',0Ah,'B',0Eh,'u',0Ch,'z',0Eh,'z',0Ch,0,0
  311.     istr db  0,012h,0,012h,0,0
  312.  
  313.     consolew db 80  
  314.     consoleh db 25
  315.     cursorx  db 0
  316.     cursory  db 0
  317.     coffset  dw 0
  318.     csize4   dw 8000
  319.     tmp      dw 00
  320.    
  321.     times   510-($-$$)  db 0    ; Pad remainder of boot sector with 0s
  322.     dw      0xAA55              ; The standard PC boot signature
  323.    
  324. %ifdef BOCHS_HD_IMAGE
  325.     times   1024*1024   db 0    ; for Bochs disk boot, Cylinders/Heads/Sectors all set to 3
  326. %endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement