Advertisement
Guest User

_OS _code_

a guest
Aug 26th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. [bits 16]           ; tell assembler that working in real mode(16 bit mode)
  3. [org 0x7c00]        ; organize from 0x7C00 memory location where BIOS will load us
  4.  
  5. start:              ; start label from where our code starts
  6.  
  7.     xor ax,ax           ; set ax register to 0
  8.     mov ds,ax           ; set data segment(ds) to 0
  9.     mov es,ax           ; set extra segment(es) to 0
  10.     mov bx,0x8000
  11.  
  12.     mov ax,0x13         ;clears the screen
  13.     int 0x10            ;call bios video interrupt
  14.  
  15.     mov ah,02           ;clear the screen with big font
  16.     int 0x10            ;interrupt display
  17.  
  18.     ;set cursor to specific position on screen
  19.     mov ah,0x02         ; set value for change to cursor position
  20.     mov bh,0x00         ; page
  21.     mov dh,0x06         ; y cordinate/row
  22.     mov dl,0x09         ; x cordinate/col
  23.     int 0x10
  24.  
  25.     mov si, start_os_intro              ; point start_os_intro string to source index
  26.     call _print_DiffColor_String        ; call print different color string function
  27.  
  28.     ;set cursor to specific position on screen
  29.     mov ah,0x02
  30.     mov bh,0x00
  31.     mov dh,0x10
  32.     mov dl,0x06
  33.     int 0x10
  34.  
  35.     mov si,press_key                    ; point press_key string to source index
  36.     call _print_GreenColor_String       ; call print green color string function
  37.  
  38.     mov ax,0x00         ; get keyboard input
  39.     int 0x16            ; interrupt for hold & read input
  40.  
  41.     ;/////////////////////////////////////////////////////////////
  42.     ; load second sector into memory
  43.  
  44.     mov ah, 0x02                    ; load second stage to memory
  45.     mov al, 1                       ; numbers of sectors to read into memory
  46.     mov dl, 0x80                    ; sector read from fixed/usb disk
  47.     mov ch, 0                       ; cylinder number
  48.     mov dh, 0                       ; head number
  49.     mov cl, 2                       ; sector number
  50.     mov bx, _OS_Stage_2             ; load into es:bx segment :offset of buffer
  51.     int 0x13                        ; disk I/O interrupt
  52.  
  53.     jmp _OS_Stage_2                 ; jump to second stage
  54.  
  55.  
  56.     ;/////////////////////////////////////////////////////////////
  57.     ; declaring string datas here
  58.     start_os_intro db 'Welcome to My OS!',0
  59.     press_key db '>>>> Press any key <<<<',0
  60.  
  61.     login_username db 'Username : ',0
  62.     login_password db 'Password : ',0
  63.    
  64.     display_text db '! Welcome to my Operating System !', 0
  65.  
  66.     os_info db 10, 'My Operating System, 16-Bit, version=1.0.0',13,0
  67.  
  68.     press_key_2 db 10,'Press any key to go to graphics view',0
  69.  
  70.     window_text db 10,'Graphics in OS......', 0
  71.     hello_world_text db 10,10, '    Hello World!',0
  72.     login_label db '#] Login please....(ESC to skip login)', 0
  73.  
  74.  
  75.     ;/////////////////////////////////////////////////////////////
  76.     ; defining printing string functions here
  77.  
  78.     ;****** print string without color
  79.  
  80. print_string:
  81.     mov ah, 0x0E            ; value to tell interrupt handler that take value from al & print it
  82.  
  83. .repeat_next_char:
  84.     lodsb                ; get character from string
  85.     cmp al, 0                    ; cmp al with end of string
  86.     je .done_print               ; if char is zero, end of string
  87.     int 0x10                     ; otherwise, print it
  88.     jmp .repeat_next_char        ; jmp to .repeat_next_char if not 0
  89.  
  90. .done_print:
  91.     ret                         ;return
  92.  
  93. ;****** print string with different colors
  94.  
  95. _print_DiffColor_String:
  96.         mov bl,1                ;color value
  97.     mov ah, 0x0E
  98.  
  99. .repeat_next_char:
  100.     lodsb
  101.     cmp al, 0
  102.     je .done_print
  103.     add bl,6               ;increase color value by 6
  104.     int 0x10
  105.     jmp .repeat_next_char
  106.  
  107. .done_print:
  108.     ret
  109.  
  110. ;****** print string with green color
  111.  
  112. _print_GreenColor_String:
  113.     mov bl,10
  114.     mov ah, 0x0E
  115.  
  116. .repeat_next_char:
  117.     lodsb
  118.     cmp al, 0
  119.     je .done_print
  120.     int 0x10
  121.     jmp .repeat_next_char
  122.  
  123. .done_print:
  124.     ret
  125.  
  126. ;****** print string with white color
  127.  
  128. _print_WhiteColor_String:
  129.     mov bl,15
  130.     mov ah, 0x0E
  131.  
  132. .repeat_next_char:
  133.     lodsb
  134.     cmp al, 0
  135.     je .done_print
  136.     int 0x10
  137.     jmp .repeat_next_char
  138.  
  139. .done_print:
  140.     ret
  141.  
  142. ;****** print string with yellow color
  143.  
  144. _print_YellowColor_String:
  145.     mov bl,14
  146.     mov ah, 0x0E
  147.  
  148. .repeat_next_char:
  149.     lodsb
  150.     cmp al, 0
  151.     je .done_print
  152.     int 0x10
  153.     jmp .repeat_next_char
  154.  
  155. .done_print:
  156.     ret
  157.  
  158.  
  159.     ;///////////////////////////////////////////
  160.     ; boot loader magic number
  161.     times ((0x200 - 2) - ($ - $$)) db 0x00     ;set 512 bytes for boot sector which are necessary
  162.     dw 0xAA55                                  ; boot signature 0xAA & 0x55
  163.  
  164.  
  165. ;////////////////////////////////////////////////////////////////////////////////////////
  166.  
  167. _OS_Stage_2 :
  168.  
  169.     mov al,2                    ; set font to normal mode
  170.     mov ah,0                    ; clear the screen
  171.     int 0x10                    ; call video interrupt
  172.  
  173.     mov cx,0                    ; initialize counter(cx) to get input
  174.  
  175.     ;***** print login_label on screen
  176.     ;set cursor to specific position on screen
  177.     mov ah,0x02
  178.     mov bh,0x00
  179.     mov dh,0x00
  180.     mov dl,0x00
  181.     int 0x10
  182.  
  183.     mov si,login_label              ; point si to login_username
  184.     call print_string               ; display it on screen
  185.  
  186.     ;****** read username
  187.  
  188.     ;set cursor to specific position on screen
  189.     mov ah,0x02
  190.     mov bh,0x00
  191.     mov dh,0x02
  192.     mov dl,0x00
  193.     int 0x10
  194.  
  195.     mov si,login_username          ; point si to login_username
  196.     call print_string              ; display it on screen
  197.  
  198. _getUsernameinput:
  199.  
  200.     mov ax,0x00             ; get keyboard input
  201.     int 0x16                ; hold for input
  202.  
  203.     cmp ah,0x1C             ; compare input is enter(1C) or not
  204.     je .exitinput           ; if enter then jump to exitinput
  205.  
  206.     cmp ah,0x01             ; compare input is escape(01) or not
  207.     je _skipLogin           ; jump to _skipLogin
  208.  
  209.     mov ah,0x0E             ;display input char
  210.     int 0x10
  211.  
  212.     inc cx                  ; increase counter
  213.     cmp cx,5                ; compare counter reached to 5
  214.     jbe _getUsernameinput   ; yes jump to _getUsernameinput
  215.     jmp .inputdone          ; else jump to inputdone
  216.  
  217. .inputdone:
  218.     mov cx,0                ; set counter to 0
  219.     jmp _getUsernameinput   ; jump to _getUsernameinput
  220.     ret                     ; return
  221.  
  222. .exitinput:
  223.     hlt
  224.  
  225.  
  226.     ;****** read password
  227.  
  228.     ;set x y position to text
  229.     mov ah,0x02
  230.     mov bh,0x00
  231.     mov dh,0x03
  232.     mov dl,0x00
  233.     int 0x10
  234.  
  235.     mov si,login_password               ; point si to login_username
  236.     call print_string                   ; display it on screen
  237.  
  238. _getPasswordinput:
  239.  
  240.     mov ax,0x00
  241.     int 0x16
  242.  
  243.     cmp ah,0x1C
  244.     je .exitinput
  245.    
  246.     cmp ah,0x01
  247.     je _skipLogin
  248.  
  249.     inc cx
  250.  
  251.     cmp cx,5
  252.     jbe _getPasswordinput
  253.    
  254.     jmp .inputdone
  255.  
  256. .inputdone:
  257.     mov cx,0
  258.     jmp _getPasswordinput
  259.     ret
  260. .exitinput:
  261.     hlt
  262.  
  263. ;****** display display_text on screen
  264.  
  265.     ;set x y position to text
  266.     mov ah,0x02
  267.     mov bh,0x00
  268.     mov dh,0x08
  269.     mov dl,0x12
  270.     int 0x10
  271.  
  272.     mov si, display_text        ;display display_text on screen
  273.     call print_string
  274.  
  275.     ;set x y position to text
  276.     mov ah,0x02
  277.     mov bh,0x00
  278.     mov dh,0x9
  279.     mov dl,0x10
  280.     int 0x10
  281.  
  282.     mov si, os_info     ;display os_info on screen
  283.     call print_string
  284.  
  285.     ;set x y position to text
  286.     mov ah,0x02
  287.     mov bh,0x00
  288.     mov dh,0x11
  289.     mov dl,0x11
  290.     int 0x10
  291.  
  292.     mov si, press_key_2     ;display press_key_2 on screen
  293.     call print_string
  294.  
  295.     mov ah,0x00
  296.     int 0x16
  297.  
  298.  
  299. ;//////////////////////////////////////////////////////////////////
  300.  
  301. _skipLogin:
  302.  
  303.     ;/////////////////////////////////////////////////////////////
  304.     ; load third sector into memory
  305.  
  306.     mov ah, 0x03                    ; load third stage to memory
  307.     mov al, 1
  308.     mov dl, 0x80
  309.     mov ch, 0
  310.     mov dh, 0
  311.     mov cl, 3                       ; sector number 3
  312.     mov bx, _OS_Stage_3
  313.     int 0x13
  314.  
  315.     jmp _OS_Stage_3
  316.  
  317. ;////////////////////////////////////////////////////////////////////////////////////////
  318.  
  319. _OS_Stage_3:
  320.  
  321.     mov ax,0x13              ; clears the screen
  322.     int 0x10
  323.  
  324. ;//////////////////////////////////////////////////////////
  325. ; drawing window with lines
  326.  
  327.     push 0x0A000                ; video memory graphics segment
  328.     pop es                      ; pop any extar segments from stack
  329.     xor di,di                   ; set destination index to 0
  330.     xor ax,ax                   ; set color register to zero
  331.  
  332.     ;//////////////////////////////////////////////
  333.     ;******drawing top line of our window
  334.     mov ax,0x02                 ; set color to green
  335.  
  336.     mov dx,0                    ; initialize counter(dx) to 0
  337.  
  338.     add di,320                  ; add di to 320(next line)
  339.     imul di,10                  ;multiply by 10 to di to set y cordinate from where we need to start drawing
  340.  
  341.     add di,10                   ;set x cordinate of line from where to be drawn
  342.  
  343. _topLine_perPixel_Loop:
  344.  
  345.     mov [es:di],ax              ; move value ax to memory location es:di
  346.  
  347.     inc di                      ; increment di for next pixel
  348.     inc dx                      ; increment our counter
  349.     cmp dx,300                  ; comprae counter value with 300
  350.     jbe _topLine_perPixel_Loop  ; if <= 300 jump to _topLine_perPixel_Loop
  351.  
  352.     hlt                         ; halt process after drawing
  353.  
  354.     ;//////////////////////////////////////////////
  355.     ;******drawing bottm line of our window
  356.     xor dx,dx
  357.     xor di,di
  358.     add di,320
  359.     imul di,190         ; set y cordinate for line to be drawn
  360.     add di,10           ;set x cordinate of line to be drawn
  361.  
  362.     mov ax,0x01         ; blue color
  363.  
  364. _bottmLine_perPixel_Loop:
  365.  
  366.     mov [es:di],ax
  367.  
  368.     inc di
  369.     inc dx
  370.     cmp dx,300
  371.     jbe _bottmLine_perPixel_Loop
  372.     hlt
  373.  
  374.     ;//////////////////////////////////////////////
  375.     ;******drawing left line of our window
  376.     xor dx,dx
  377.     xor di,di
  378.     add di,320
  379.     imul di,10           ; set y cordinate for line to be drawn
  380.  
  381.     add di,10            ; set x cordinate for line to be drawn
  382.  
  383.     mov ax,0x03          ; cyan color
  384.  
  385. _leftLine_perPixel_Loop:
  386.  
  387.     mov [es:di],ax
  388.  
  389.     inc dx
  390.     add di,320
  391.     cmp dx,180
  392.     jbe _leftLine_perPixel_Loop
  393.  
  394.     hlt
  395.  
  396.     ;//////////////////////////////////////////////
  397.     ;******drawing right line of our window
  398.     xor dx,dx
  399.     xor di,di
  400.     add di,320
  401.     imul di,10           ; set y cordinate for line to be drawn
  402.  
  403.     add di,310           ; set x cordinate for line to be drawn
  404.  
  405.     mov ax,0x06          ; orange color
  406.  
  407. _rightLine_perPixel_Loop:
  408.  
  409.     mov [es:di],ax
  410.  
  411.     inc dx
  412.     add di,320
  413.     cmp dx,180
  414.     jbe _rightLine_perPixel_Loop
  415.  
  416.     hlt
  417.  
  418.     ;//////////////////////////////////////////////
  419.     ;******drawing line below top line of our window
  420.     xor dx,dx
  421.     xor di,di
  422.  
  423.     add di,320
  424.     imul di,27           ; set y cordinate for line to be drawn
  425.  
  426.     add di,11            ; set x cordinate for line to be drawn
  427.  
  428.     mov ax,0x05         ; pink color
  429.  
  430. _belowLineTopLine_perPixel_Loop:
  431.  
  432.     mov [es:di],ax
  433.  
  434.     inc di
  435.     inc dx
  436.     cmp dx,298
  437.     jbe _belowLineTopLine_perPixel_Loop
  438.  
  439.     hlt
  440.  
  441.     ;***** print window_text & X char
  442.  
  443.     ;set cursor to specific position
  444.     mov ah,0x02
  445.     mov bh,0x00
  446.     mov dh,0x01         ; y cordinate
  447.     mov dl,0x02         ; x cordinate
  448.     int 0x10
  449.  
  450.     mov si,window_text              ; point si to window_text
  451.     call _print_YellowColor_String
  452.  
  453.     hlt
  454.  
  455.     ;set cursor to specific position
  456.     mov ah,0x02
  457.     mov bh,0x00
  458.     mov dh,0x02           ; y cordinate
  459.     mov dl,0x25           ; x cordinate
  460.     int 0x10
  461.  
  462.     mov ah,0x0E
  463.     mov al,0x58           ; 0x58=X
  464.     mov bh,0x00
  465.     mov bl,4              ; red color
  466.     int 0x10
  467.  
  468.     hlt
  469.  
  470.     ;set cursor to specific position
  471.     mov ah,0x02
  472.     mov bh,0x00
  473.     mov dh,0x02           ; y cordinate
  474.     mov dl,0x23           ; x cordinate
  475.     int 0x10
  476.  
  477.     mov ah,0x0E
  478.     mov al,0x5F           ; 0x58=X
  479.     mov bh,0x00
  480.     mov bl,9              ; red color
  481.     int 0x10
  482.  
  483.     hlt
  484.  
  485.     ;set cursor to specific position
  486.     mov ah,0x02
  487.     mov bh,0x00
  488.     mov dh,0x05   ; y cordinate
  489.     mov dl,0x09    ; x cordinate
  490.     int 0x10
  491.  
  492.     mov si,hello_world_text
  493.     call _print_DiffColor_String
  494.  
  495.     hlt
  496.  
  497.     ;set cursor to specific position
  498.     mov ah,0x02
  499.     mov bh,0x00
  500.     mov dh,0x12   ; y cordinate
  501.     mov dl,0x03  ; x cordinate
  502.     int 0x10
  503.  
  504.     mov si,display_text
  505.     call _print_WhiteColor_String
  506.  
  507.     hlt
  508.  
  509.  
  510.     ; add how much memory we need
  511.     times (1024 - ($-$$)) db 0x00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement