Day2

Assembly language and Makefile

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
; source of helloos.nas

org 0x7c00 ; these code should be loaded into memory 0x7c00

; FAT12 requirement code
jmp entry
db 0x90
...

; main program
entry:
mov ax,0
mov ss,ax
mov sp,0x7c00
mov ds,ax
mov es,ax
mov si,msg
putloop:
mov al,[si]
add si,1
cmp al,0
je fin
mov ah,0x0e
mov bx,15
int 0x10 ; use bios to show one character
jmp putloop
fin:
hlt ; put cpu into sleep, waiting for interrupt
jmp fin

msg:
db 0xa,0xa ; '\n', '\n'
db "hello world"
db 0xa
db 0

Rebuild the IPL

Only the first 512 bytes is needed

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
; source of ipl.nas
ORG 0x7c00

JMP entry
DB 0x90
DB "HELLOIPL"
DW 512
DB 1
DW 1
DB 2
DW 224
DW 2880
DB 0xf0
DW 9
DW 18
DW 2
DD 0
DD 2880
DB 0,0,0x29
DD 0xffffffff
DB "HELLO-OS "
DB "FAT12 "
RESB 18

entry:
MOV AX,0
MOV SS,AX
MOV SP,0x7c00
MOV DS,AX
MOV ES,AX

MOV SI,msg
putloop:
MOV AL,[SI]
ADD SI,1
CMP AL,0
JE fin
MOV AH,0x0e
MOV BX,15
INT 0x10
JMP putloop
fin:
HLT
JMP fin

msg:
DB 0x0a, 0x0a
DB "hello, world"
DB 0x0a
DB 0

RESB 0x7dfe-$

DB 0x55, 0xaa
1
2
3
4
5
% nask.exe ipl.nas ipl.bin ipl.lst
% edimg.exe imgin:fdimg0at.tek wbinimg src:ipl.bin len:512 from:0 to:0 imgout:helloos.img
# equivlent to `qemu-img create` and `dd` under Unix-like system
% copy helloos.img qemu/fdimage0.bin
% make.exe -C qemu/

Makefile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
default:
make.exe img

ipl.bin: ipl.nas
nask.exe ipl.nas ipl.bin

helloos.img: ipl.bin
dimg.exe imgin:fdimg0at.tek wbinimg src:ipl.bin len:512 from:0 to:0 imgout:helloos.img

asm:
make.exe -r ipl.bin

img:
make.exe -r helloos.img

run:
make.exe img
copy helloos.img qemu/fdimage0.bin
make.exe -C qemu/

clean:
del ipl.bin
del helloos.img