paizaでkernel開発 その4

# 概要

paizaでkernel開発やってみる。
一連の動きを通しでやってみる。

# 環境

windows vista 32bit

# 手順
# １、paiza.ioでコンパイルしてbase64エンコードする。


## サンプルコード

```
import subprocess
import os

def gcc():
    p_file = 'test5.S'
    test_p = """
.code32    
.text
.globl _start
.globl multiboot_header

_start:
    movl    $0xb8000, %ebx
    movl    $12, %ecx
    movl    $msg, %esi
loop:
    movb    (%esi), %al
    movb    $2, %ah
    movw    %ax, (%ebx)
    addl    $2, %ebx
    addl    $1, %esi
    subl    $1, %ecx
    jne     loop
    jmp     .

.data
multiboot_header:
    .align 4
    .long   0x1badb002
    .long   0x0 
    .long   -0x1badb002-0x0
msg:
    .ascii "Hello world!"
"""
    with open(p_file, 'w') as f:
        f.write(test_p)
    os.system("gcc -c -m32 -nostdlib %s" % p_file)
    os.system("ld -melf_i386 -e_start -Ttext=0x100000 -otest5.elf test5.o")
    os.system("file test5.elf")
    os.system("cat test5.elf|base64")

if __name__ == '__main__':
  gcc()
```

## 結果


```
f0VMRgEBAQAAAAAAAAAAAAIAAwABAAAAAAAQADQAAABQEQAAAAAAADQAIAACACgABgAFAAEAAAAA
EAAAAAAQAAAAEAAjAAAAIwAAAAUAAAAAEAAAAQAAACQQAAAkEBAAJBAQABgAAAAYAAAABgAAAAAQ
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAQAAAAAAADAAEAAAAAACQQEAAAAAAAAwACAAEAAAAAAAAAAAAAAAQA
8f8JAAAAMBAQAAAAAAAAAAIADQAAAA8AEAAAAAAAAAABACgAAAAAABAAAAAAABAAAQASAAAAJBAQ
AAAAAAAQAAIAIwAAADwQEAAAAAAAEAACAC8AAAA8EBAAAAAAABAAAgA2AAAAPBAQAAAAAAAQAAIA
AHRlc3Q1Lm8AbXNnAGxvb3AAbXVsdGlib290X2hlYWRlcgBfX2Jzc19zdGFydABfZWRhdGEAX2Vu
ZAAALnN5bXRhYgAuc3RydGFiAC5zaHN0cnRhYgAudGV4dAAuZGF0YQAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAbAAAAAQAAAAYAAAAAABAAABAAACMAAAAAAAAAAAAA
AAEAAAAAAAAAIQAAAAEAAAADAAAAJBAQACQQAAAYAAAAAAAAAAAAAAAEAAAAAAAAAAEAAAACAAAA
AAAAAAAAAAA8EAAAsAAAAAQAAAAGAAAABAAAABAAAAAJAAAAAwAAAAAAAAAAAAAA7BAAADsAAAAA
AAAAAAAAAAEAAAAAAAAAEQAAAAMAAAAAAAAAAAAAACcRAAAnAAAAAAAAAAAAAAABAAAAAAAAAA==
```

## 成果物

https://paiza.io/projects/2R4cv-di91U3EL7_4FFzCQ


# ２、コピペして、elf0.txtを作る。

# ３、elf0.txtをvbsでbase64デコードして、test4.elfを作る。

## サンプルコード

```
Const adTypeBinary = 1
Const adTypeText = 2
Const adSaveCreateNotExist = 1
Const adSaveCreateOverWrite = 2
Dim st
Dim xmldom
Dim node
Dim inFile
Dim outFile
Dim cd
Dim sh
Dim str
Set sh = CreateObject("WScript.Shell")
cd = sh.CurrentDirectory
inFile = cd & "\elf0.txt"
outFile = cd & "\test5.elf"
Set xmldom = CreateObject("Microsoft.XMLDOM")
Set node = xmldom.CreateElement("work")
node.DataType = "bin.base64"
Set st = CreateObject("ADODB.Stream")
st.Charset = "ascii"
st.Open
st.LoadFromFile(inFile)
str = st.ReadText
msgbox str
node.Text = str
st.Close
bin = node.NodeTypedValue
st.Open
st.Charset = "Shift-JIS"
st.Type = adTypeBinary
st.Write bin
st.saveToFile outFile, adSaveCreateOverWrite
st.Close
Set st = Nothing
Set node = Nothing
Set xmldom = Nothing
msgbox "ok"


```

# ４、diskexplorerで、nano.imgにtest4.elfを入れる。

![image](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/34a09598-2a66-0e47-6718-0c94b7100f5d.png)


# ５、qemu-0.15.1で、test4.elfをgrubからbootする。

## qemu起動。

```
qemu -fda nano.img
```

## grub

```
root (fd0)
kernel /boot/test4.elf
boot

```

# 結果


![image](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/4e6e96f3-22ad-0296-b876-1a5c162ea82d.png)



以上。