Download code

Jump to: navigation, search

Back to Hello_World_(IBM_PC_bootstrap)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

build.bat

 1 @REM The authors of this work have released all rights to it and placed it
 2 @REM in the public domain under the Creative Commons CC0 1.0 waiver
 3 @REM (http://creativecommons.org/publicdomain/zero/1.0/).
 4 @REM 
 5 @REM THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 @REM EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 @REM MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 @REM IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 @REM CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 @REM TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 @REM SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 @REM 
13 @REM Retrieved from: http://en.literateprograms.org/Hello_World_(IBM_PC_bootstrap)?oldid=18712
14 
15 set OBJS=a.exe a.bin
16 set SRCS=crt0.s hello.c screen.c
17 set ARGS=-nostdlib -Wl,-Ttext -Wl,0x7e00 -Wl,-Tdata -Wl,0x7f00
18 del *.bin
19 gcc %ARGS% %SRCS%
20 objcopy -O binary a.exe a.bin
21 if not exist a.bin goto err
22 debug < boot.src
23 del %OBJS%
24 if not exist tail.img goto end
25 
26 copy /b boot.bin + tail.img 144.img
27 del boot.bin
28 
29 if not exist bochsrc.bxrc goto end
30 
31 bochsrc.bxrc
32 goto end
33 
34 :ERR
35 @echo off
36 echo -
37 echo -
38 echo gcc and objcopy failed or missing from PATH
39 pause
40 :END


hijacker
hijacker
hijacker
hijacker

screen.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Hello_World_(IBM_PC_bootstrap)?oldid=18712
14 */
15 
16 #define VIDEOMEM	(char *)0xb8000
17 #define SCRX            80
18 #define SCRY            25
19 #define ATTRIB          0x71    /* blue on grey */
20 
21 void cls()
22 {
23         char *p = VIDEOMEM;
24         int n;
25         for(n = 0; n < SCRX*SCRY; ++n) {
26                 *p++ = ' ';
27                 *p++ = ATTRIB;
28         }
29 }
30 
31 void at(int x, int y, char *m)
32 {
33         char *p = VIDEOMEM + 2*(x+SCRX*y);
34         while(*m)       {
35 		*p++ = *m++;
36                 *p++ = ATTRIB;
37 	}
38 }


hijacker
hijacker
hijacker
hijacker

boot.src

 1 f 100,700 0
 2 a
 3 ;;;; first we bring in our C program (300-700)	;;;;
 4 
 5 n a.bin
 6 l 300
 7 
 8 a
 9 ;;;; then the floppy parameter table (103-13F)	;;;;
10 ; parameters taken from a FREEDOS formatted 1.44M  ;
11 
12 e 103          46 52 44 4F 53
13 e 108 34 2E 31 00 02 01 01 00
14 e 110 02 E0 00 40 0B f0 09 00
15 e 118 12 00 02 00 00 00 00 00
16 e 120 00 00 00 00 00 00 29 12
17 e 128 15 5D 33 45 4D 50 54 59
18 e 130 44 49 53 4B 20 20 46 41
19 e 138 54 31 32 20 20 20 31 C0
20 a
21 ;;;; now we assemble the boot sector		;;;;
22 
23 a 100
24 jmp 140                          ; skip over the floppy parameters
25 
26 a 140
27 ; == floppy I/O ==
28 xor dx,dx
29 mov es,dx
30 mov cx,02
31 mov bx,7e00
32 mov ax,0202
33 int 13				; read the next (al=2) sectors...
34 mov dx,03f2
35 xor ax,ax
36 out dx,al			; ... then turn off the floppy
37 jmp 0:7c80
38 
39 a 180
40 ; == enter protected mode ==
41 cli				; turn off interrupts
42 				; load GDT (using CS override)
43 db 2e, 0f, 01, 16, 00, 7d
44 				; set pmode bit in cr0
45 db 0f, 20, c0
46 or al, 1
47 db 0f, 22, c0
48 				; load 32-bit data segment registers, and...
49 mov dx, 10
50 mov es, dx
51 mov ds, dx
52 mov ss, dx
53 				; ...far jump to load 32-bit code segment	
54 jmp 18:7e00
55 
56 a 200
57 ; // GDT //
58 db 1f, 00, 00, 7d, 00, 00, 00, 00
59 db 00, 00, 00, 00, 00, 00, 00, 00
60 db ff, ff, 00, 00, 00, 92, cf, 00
61 db ff, ff, 00, 00, 00, 9a, cf, 00
62 
63 a 2fe
64 ; // signature for boot record //
65 db 55, aa
66 
67 a 300
68 ;;;; finally, we produce the binary (3 sectors)	;;;;
69 
70 n boot.bin
71 r cx
72 600
73 w
74 q
75 


hijacker
hijacker
hijacker
hijacker

hello.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Hello_World_(IBM_PC_bootstrap)?oldid=18712
14 */
15 
16 extern void cls(), at();
17 void rawmain()
18 {
19         cls();
20         at(32,12,"Hello, world!");
21 }
22 


hijacker
hijacker
hijacker
hijacker

crt0.s

 1 	xor %eax, %eax		# boot sets CS,DS,ES,SS
 2 	mov %eax, %fs
 3 	mov %eax, %gs
 4 
 5 	mov $0x20000, %esp	# set up a stack
 6 	call _rawmain		# and enter the C code
 7 wkbd:	mov $0x64, %edx		# if it returns, wait for any keypress
 8 	inb %dx, %al
 9 	andl $1, %eax
10 	jz wkbd
11 
12 	mov $0x64, %edx		# then reboot the machine
13 	mov $0xfe, %eax
14 	outb %al, %dx
15 
16 	cli
17 loop:	jmp loop		# (or at least hang)


hijacker
hijacker
hijacker
hijacker