-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.snippets
149 lines (129 loc) · 4.52 KB
/
asm.snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
priority -40
snippet dischar "Display a character in the video memory"
; Display a character in the video memory
; video memory physical 0x000b8000
mov ah, 0x0fh ; set the display attribute
mov al, 'c' ; set the character to display
mov [es: (80 * 1 + 39) * 2)], ax ; display 'c' in screen row 1, 39 column,
; note the display address will multiply 2
endsnippet
snippet int103 "Read Cursor Position and size"
; int 0x10, 3 - read the cursor position
; input:
; AH = 03
; BH = video page (0)
; output:
; CH = cursor starting scan line (low order 5 bits)
; CL = cursor ending scan line (low order 5 bits)
; DH = row
; DL = column
endsnippet
snippet int1013 "Write String"
; int 0x10,13 - write string
; input:
; AH = 13h
; AL = write mode (see bit settings below)
; = 0 string is chars only, attribute in BL, cursor not moved
; = 1 string is chard only, attribute in BL, cursor moved
; = 2 string contains chars and attributes, cursor not moved
; = 3 string contains chars and attributes, cursor moved
; BH = video page number
; BL = attribute if mode 0 or 1 (AL bit 1=0)
; CX = length of string (ignoring attributes)
; DH = row coordinate
; DL = column coordinate
; ES:BP = pointer to string
; output:
endsnippet
snippet int132 "Read Disk Sectors"
; int 0x13,2 - read disk sectors
; input:
; AH = 02
; AL = number of sectors to read (1-128 dec.)
; CH = track/cylinder number (0-1023 dec., see below)
; CL = sector number (1-17 dec.)
; DH = head number (0-15 dec.)
; DL = drive number (0=A:, 1=2nd floppy, 80h=drive 0, 81h=drive 1)
; ES:BX = pointer to buffer
; output
; AH = status
; AL = number of sectors read
; CF = 0 if successful
; = 1 if error
endsnippet
snippet int130 "Reset Disk System"
; int 0x13,0 - reset disk system
; input:
; AH = 00
; DL = drive number (0=A:, 1=2nd floppy, 80h=drive 0, 81h=drive 1)
; output:
; AH = disk operation status
; CF = 0 if successful
; = 1 if error
endsnippet
snippet ent_pro "Enter into Protected Mode"
; Enter into the Protected Mode
; 1. Disable interrupts
; 2. Set up the gd (Global Descriptor) and gdt (Global Descriptor Table)
; 3. Load the gdt descriptor
; 4. Set 32-bit mode bit in cr0
; 5. Switch on the 20th address
; 6. Flush the CPU pipeline by issuing a carefully crafted far jump
; 7. Update all the segment registers and the stack
; 8. Now, you can call the 32-bit code
endsnippet
snippet set_paging "Set up Paging"
; Set up paging
; 1. Set up page directory according to the memory
; 2. Set up page table
; 3. Set cr3 pointing to the page directory
; 4. Start up paging bit in cr0
endsnippet
snippet set_idt "Set up Interrupt"
; Set up Interrupt
; 1. set up 8259A
; 2. set up idt (interrupt descriptor table) according to the interrupt
; handling function address
; 3. set up the interrupt handling function for the specific interrupt
endsnippet
snippet ring0to1 "Initial switch from kernel to process ring0 to ring1"
; Switch from ring0(kernel) to ring1(process)
; 1. construct the stack including EIP, CS, EFLAGS, ESP, SS
; 2. set the ESP to point the well-constructed stack top
; 3. just use 'iret(d)(iret and iretd interchangeably)' instruction
endsnippet
snippet set_tss "Setup TSS"
; Set up TSS
; TSS is used for switching from low privilege to high privilege
; 1. set up the TSS according to the TSS structure
; 2. set up the TSS descriptor into the GDT
; 3. load the TSS selector
endsnippet
snippet multi_process "Realize multiply process"
; Realize multiply process
; 1. switch from ring0 to ring1 to run process 0
; 2. set up IDT, especially time interrupt to realize task switching
; 3. set up process 0 and process 1 TSS and LDT
; 4. set up process 0 and process 1 LDT descriptor and TSS descriptor
endsnippet
snippet protected-mode "protected-mode summary"
; How to write real mode and protected mode code?
; 1. mbr (master boot sector) in real mode code using the basic assembly code
; 2. Protected mode:
; 1. stop interrupt, because in protected-mode, the real-mode interrupt code is abnormal (nessary)
; 2. set and load gdt, because in protected-mode, the segment register will use segment selector, instead of the address, note: the separation of code and data (nessary)
; 3. set PE bit (nessary)
; 4. start 21th address line (nessary)
; 5. jump to 32bits code to flush the CPU pipeline (nessary)
; 6. reset the ds, es, fs, gs, ss to be used in protected mode (nessary)
; 7. set up paging (optional)
; 8. set up interrupt (optional)
; 9. set up LDT (optional)
; 10. set up privelege (optional)
endsnippet
snippet asmheader "asm file name page title header" b
name $1
page ${2:55,132}
title $3
$0
endsnippet