Guest User

Untitled

a guest
Jul 1st, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 5.27 KB | Source Code | 0 0
  1. [global Start]
  2. [BITS 16]
  3. [ORG 0x7C00]
  4.  
  5. section .text
  6. Start:
  7.     ; DL stores the current drive number, save in variable
  8.     mov [BOOT_DRIVE], dl
  9.  
  10.     ; Initialize the stack
  11.     mov bp, 0x7C00
  12.     mov sp, bp
  13.  
  14.     jmp EnterProtectedMode
  15.  
  16. EnterProtectedMode:       ; Enter protected mode
  17.     cli
  18.     lgdt [GDT_Descriptor] ; Load GDT
  19.     mov eax, cr0
  20.     or eax, 0x1 ; Set protected mode bit
  21.     mov cr0, eax
  22.     jmp CODE_SEG:StartProtectedMode ; Jump to code segment in protected mode
  23. GDT_Start:          ; Create a global descriptor table
  24.     null_descriptor:
  25.         dd 0x0 ; 8 bits of zeros
  26.         dd 0x0
  27.     code_descriptor:
  28.         dw 0xFFFF ; Limit (16 bits)
  29.         dw 0x0 ; Base (24 bits in total) (16 bits)
  30.         db 0x0 ; Base (8 bits)
  31.         db 10011010b ; First 4 bits: present, priviledge, type. Last 4 bits: Type flags
  32.         db 11001111b ; Other flags (4 bits) + Limit (4 bits)
  33.         db 0x0 ; Base (8 bits)
  34.     data_descriptor:
  35.         dw 0xFFFF ; Limit (16 bits)
  36.         dw 0x0 ; Base (24 bits in total) (16 bits)
  37.         db 0x0 ; Base (8 bits)
  38.         db 10010010b ; First 4 bits: present, priviledge, type. Last 4 bits: Type flags
  39.         db 11001111b ; Other flags (4 bits) + Limit (4 bits)
  40.         db 0x0 ; Base (8 bits)
  41. GDT_End:
  42. GDT_Descriptor:
  43.     dw GDT_End - GDT_Start - 1 ; Size of GDT
  44.     dd GDT_Start ; Start address of GDT
  45. Print4Hex:
  46.     ; Input AX register, BL register (optional)
  47.     ; Output: None
  48.     ; Prints the hex value of AX register (4 nibbles). Example: AX=0x1234 will print: 0x1234
  49.     ; If you want to print prefix '0x' then set BL=0, else set BL=1. Example: AX=0x1234, BL=1 will print: 1234
  50.     push ax
  51.  
  52.     shr ax, 8
  53.     mov ah, bl ; Print prefix according to BL input for first byte
  54.     call Print2Hex
  55.  
  56.     ; Print low byte
  57.     pop ax
  58.     mov ah, 1 ; Here we don't need to print prefix
  59.     call Print2Hex
  60.  
  61.     ret
  62.  
  63. Print2Hex:
  64.     ; Input: AL register, AH register (optional)
  65.     ; Output: None
  66.     ; Print the hex value of AL register (2 nibbles). Example: AL=0x12 will print: 0x12
  67.     ; If you want to print prefix '0x' then set AH=0, else set AH=1. Example: AL=0x12, AH=1 will print: 12
  68.     cmp ah, 1
  69.     je .no_prefix
  70.     ; Print hex prefix
  71.     push ax
  72.     mov al, '0'
  73.     call PrintCharacter
  74.     mov al, 'x'
  75.     call PrintCharacter
  76.     pop ax ; Get the argument
  77.     .no_prefix:
  78.  
  79.     ; Print high nibble
  80.     call ALToHex
  81.     push ax ; Store for low nibble printing later on
  82.     mov al, ah ; Move high nibble to AL, since the PrintCharacter procedure expects the character in AL
  83.     ; Check if nibble is greater than 0x9. If it does, then we need offset of 0x41 to get 'A' in ASCII. Else, we need offset of 0x30 to get '0' in ASCII.
  84.     cmp al, 0xA
  85.     jl .finish
  86.     add al, 0x7
  87.     .finish:
  88.     add al, 0x30
  89.     call PrintCharacter
  90.  
  91.     ; Print low nibble
  92.     pop ax
  93.     cmp al, 0xA
  94.     jl .finish2
  95.     add al, 0x7
  96.     .finish2:
  97.     add al, 0x30
  98.     call PrintCharacter
  99.  
  100.     ret
  101.  
  102. ALToHex:
  103.     ; Input: AL register
  104.     ; Output: AX register
  105.     ; Convert a number in AL to hex nibbles. Example: 256 -> 0xAB. The high nibble (0xA) is stored in AH and the low nibble (0xB) in AL
  106.     push ax ; Save AL
  107.     ; Get high nibble of AL, store in DH for later retrieval
  108.     and al, 0xF0
  109.     shr al, 4
  110.     mov dh, al
  111.    
  112.     pop ax
  113.     ; Get low nibble of AL, store in AL
  114.     and al, 0x0F
  115.    
  116.     mov ah, dh ; Retrieve high nibble from DH to AH
  117.     ret
  118.  
  119.  
  120.  
  121. PrintCharacter:                         ;Procedure to print character on screen
  122.                                         ;Assume that ASCII value is in register AL
  123.     mov ah, 0x0E                        ;Tell BIOS that we need to print one charater on screen.
  124.     mov bh, 0x00                        ;Page no.
  125.     mov bl, 0x07                        ;Text attribute 0x07 is lightgrey font on black background
  126.     int 0x10                            ;Call video interrupt
  127.     ret                                 ;Return to calling procedure
  128. PrintString:                            ;Procedure to print string on screen
  129.                                         ;Assume that string starting pointer is in register SI
  130.     .next_character:                     ;Lable to fetch next character from string
  131.         mov al, [SI]                    ;Get a byte from string and store in AL register
  132.         inc SI                          ;Increment SI pointer
  133.         or AL, AL                       ;Check if value in AL is zero (end of string)
  134.         jz .exit_function                ;If end then return
  135.         call PrintCharacter             ;Else print the character which is in AL register
  136.         jmp .next_character              ;Fetch next character from string
  137.         .exit_function:                  ;End label
  138.         ret                             ;Return from procedure
  139. PrintNewLine:
  140.     ; Print new line
  141.     mov al, 0x0D
  142.     call PrintCharacter
  143.     mov al, 0x0A
  144.     call PrintCharacter
  145.     ret
  146.  
  147. CODE_SEG equ code_descriptor - GDT_Start
  148. DATA_SEG equ data_descriptor - GDT_Start
  149. BOOT_DRIVE db 0
  150. error_msg db 'Error reading from disk, error code: ', 0
  151. success_msg db 'Successfully read from disk', 0
  152.  
  153. [BITS 32]
  154. StartProtectedMode:
  155.     mov al, 'A'
  156.     mov ah, 0x0f
  157.     mov [0xb8000], ax
  158.     cli
  159.     hlt
  160.  
  161. times 510 - ($ - $$) db 0
  162. dw 0xAA55
Advertisement
Add Comment
Please, Sign In to add comment