MBOOT_PAGE_ALIGN equ 1<<0 MBOOT_MEM_INFO equ 1<<1 MBOOT_HEADER_MAGIC equ 0x1BADB002 MBOOT_HEADER_FLAGS equ MBOOT_PAGE_ALIGN | MBOOT_MEM_INFO MBOOT_CHECKSUM equ -(MBOOT_HEADER_MAGIC + MBOOT_HEADER_FLAGS) [BITS 32] [GLOBAL mboot] [EXTERN code] [EXTERN bss] [EXTERN end] mboot: dd MBOOT_HEADER_MAGIC dd MBOOT_HEADER_FLAGS dd MBOOT_CHECKSUM dd mboot dd code dd bss dd end dd start [GLOBAL start] extern go.kernel.Load ; , Go start: push ebx cli call go.kernel.Load ; , jmp $
package kernel func Load(){ // }
ENTRY(start) SECTIONS { .text 0x100000 : { code = .; _code = .; __code = .; *(.text) . = ALIGN(4096); } .data : { data = .; _data = .; __data = .; *(.data) *(.rodata) . = ALIGN(4096); } .bss : { bss = .; _bss = .; __bss = .; *(.bss) . = ALIGN(4096); } end = .; _end = .; __end = .; }
SOURCES=multiboot.o kernel.go.o GOFLAGS= -nostdlib -nostdinc -fno-stack-protector -fno-split-stack -static -m32 -g -I. GO=gccgo ASFLAGS= -felf NASM= nasm $(ASFLAGS) OBJCOPY=objcopy LDFLAGS=-T link.ld -m elf_i386 all: $(SOURCES) link clean: rm *.o kernel link: ld $(LDFLAGS) -o kernel $(SOURCES) %.go.o: %.go $(GO) $(GOFLAGS) -o $@ -c $< %.o: %.s $(NASM) $<
global __go_runtime_error global __go_register_gc_roots global __unsafe_get_addr __unsafe_get_addr: push ebp mov ebp, esp mov eax, [ebp+8] mov esp, ebp pop ebp ret __go_register_gc_roots: __go_runtime_error: ret
package screen var ( frameBuffer *[totalMax]uint16 // 8 - , - cursorX, cursorY uint8 ) const ( frameBufferAddr = 0xB8000 maxX = 80 maxY = 25 totalMax = maxX * maxY whiteOnBlack = 0x07 ) // Go __unsafe_get_addr //extern __unsafe_get_addr func getAddr(addr uint32) *[totalMax]uint16 func Init() { cursorX = 0 cursorY = 0 frameBuffer = getAddr(frameBufferAddr) // } // , func Clear() { for i := 0; i < totalMax; i++ { frameBuffer[i] = 0 } cursorX = 0 cursorY = 0 } // func SetCursor(x, y uint8) { cursorX = x cursorY = y } // func scroll() { if cursorY >= maxY { for i := 0; i < 24*maxX; i++ { frameBuffer[i] = frameBuffer[i+80] // } for i := 24 * 80; i < totalMax; i++ { // frameBuffer[i] = 0x20 | (((0 << 4) | (15 & 0x0F)) << 8) frameBuffer[i] = 0 } cursorY = 24 cursorX = 0 } } // func putChar(c byte) { switch c { case 0x08: //backspace if cursorX > 0 { cursorX-- } case 0x09: //tab cursorX = (cursorX + 8) & (8 - 1) case '\r': //return cursorX = 0 case '\n': //new line cursorX = 0 cursorY++ default: if c >= 0x20 { // frameBuffer[cursorY*80+cursorX] = uint16(c) | (((0 << 4) | (15 & 0x0F)) << 8) cursorX++ } } if cursorX >= 80 { // cursorX = 0 cursorY++ } scroll() } // func PrintStr(s string) { for i := 0; i < len(s); i++ { putChar(s[i]) } }
screen.Init() screen.Clear() screen.PrintStr("Hello Habrahar!")
%.gox: %.go.o $(OBJCOPY) -j .go_export $< $@
Source: https://habr.com/ru/post/259719/
All Articles