<commit_before>
; boot assembly file

; multiboot header constants
mbh.magic: equ 0x1badb002
mbh.flags: equ (1 << 0) | (1 << 1)
mbh.checksum: equ -(mbh.flags + mbh.magic)

; multiboot header
section .multiboot
align 4
dd mbh.magic
dd mbh.flags
dd mbh.checksum

; boot strap stack
section .bsStack, nobits
align 4
bsStackBottom:
resb 0x4000
bsStackTop:

; startup code
section .text
global _start
_start:
	; set stack to bootstrap stack
	mov esp, bsStackTop
	mov ebp, esp

	; allocate screen
	sub esp, 0x10
	mov dword [esp + 0xc], vid.addr
	mov dword [esp + 0x8], vid.h
	mov dword [esp + 0x4], vid.w
	mov dword [esp], 0
	mov edi, esp

	; clear screen
	call clear

	; write string to screen
	mov edi, eax
	mov esi, msg
	mov edx, msg.len
	call write

	pop ebp
	; turn off interrupts, hang
	cli
.hang:
	hlt
	jmp .hang

; display {
;     u32 addr;
;     u32 h;
;     u32 w;
;     u32 off;
; }

; clear(display *s);
; clear screen
clear:
	; grab parameters from display
	mov ebx, edi
	mov edi, [ebx + 0xc]
	mov ecx, [ebx + 0x8]
	mov eax, [ebx + 0x4]
	mul ecx
	mov ecx, eax
	mov eax, 0xf0 | ' '
	cld ; clear direction flag
	rep stosw

	mov eax, ebx ; return display
	ret

; write(display *s, char *msg, size_t len);
; write to screen
write:
	; parameters
	mov eax, edi ; first param, display
	mov ecx, edx ; third param, len

	; TODO: outer loop for fitting
	; in height & width constraints

	; write string to screen
	mov edi, [eax + 0xc]
.lp:
	xor eax, eax
	mov al, [esi]
	lea esi, [esi + 1]
	or ax, vid.col << 8 ; or with color byte
	mov [edi], ax
	lea edi, [edi + 2]
	loop .lp
	ret

; greeting msg
msg db "Hello, World!"
msg.len equ $-msg

vid.addr equ 0xb8000
vid.col equ 0x0f
vid.w equ 80
vid.h equ 25<commit_msg>String.foreach and wrapper print function<commit_after>
; boot assembly file

; multiboot header constants
mbh.magic: equ 0x1badb002
mbh.flags: equ (1 << 0) | (1 << 1)
mbh.checksum: equ -(mbh.flags + mbh.magic)

; multiboot header
section .multiboot
align 4
dd mbh.magic
dd mbh.flags
dd mbh.checksum

; boot strap stack
section .bsStack, nobits
align 4
bsStackBottom:
resb 0x4000
bsStackTop:

; startup code
section .text

; "anonymous" functions
anon:
	; doPrintCall(void *o, char c)
	; wraps call to putcColor
	.doPrintCall:
		mov edx, 0x7
		jmp Display.putcColor

global _start
_start:
	; set stack to bootstrap stack
	mov esp, bsStackTop
	mov ebp, esp

	; allocate screen
	sub esp, 0x10
	mov dword [esp + 0xc], 0xb8000 ; address
	mov dword [esp + 0x8], 25 ; height
	mov dword [esp + 0x4], 80 ; width
	mov dword [esp], 0
	mov edi, esp
	push edi

	; clear screen
	call Display.clear

	; write string to screen
	mov ecx, 10
.lp:
	push ecx
	mov edi, msg
	mov esi, anon.doPrintCall
	mov edx, [esp + 0x4] ; saved edi
	call String.foreach
	pop ecx
	loop .lp

	pop edi
	pop ebp
	; turn off interrupts, hang
	cli
.hang:
	hlt
	jmp .hang

; Display {
;     dword addr
;     dword h
;     dword w
;     dword off
; }

; clearDisplay(display *s);
; clear screen
Display.clear:
	; grab parameters from display
	mov ebx, edi
	mov edi, [ebx + 0xc]
	mov ecx, [ebx + 0x8]
	mov eax, [ebx + 0x4]
	mul ecx
	mov ecx, eax
	mov eax, 0xf0 | ' '
	cld ; clear direction flag
	rep stosw

	mov eax, ebx ; return display
	ret

; printDisplay(display *s, char c, byte color);
; write to screen
Display.putcColor:
	; parameters
	mov eax, edi ; display
	mov ecx, edx ; color
	shl ecx, 8

	; calculate di
	mov edi, [eax + 0xc] ; addr
	add edi, [eax] ; + offset

	; or with color byte
	or esi, ecx
	mov [edi], si
	add dword [eax], 2 ; increment offset
	ret

; String.foreach(char *s, func f(void *o, char c), void *o)
String.foreach:
	push ebp
	mov ebp, esp

; loop over characters
.lp:
	; get current char
	xor eax, eax
	mov al, [edi]
	inc edi
	; quit on null
	test al, al
	jz .break

	; save registers, return eip; jmp to func
	push edi
	push esi
	push edx
	push .r
	mov ebx, esi
	mov edi, edx
	mov esi, eax
	jmp ebx
.r:
	pop edx
	pop esi
	pop edi

	jmp .lp
.break:
	pop ebp
	ret

; greeting msg
msg db "Hello, World!", 0
{'fraction_non_alphanumeric': 0.09815184815184815, 'fraction_numerical': 0.026473526473526472, 'mean_word_length': 3.0825688073394497, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2022656', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1652, 'n_words': 608}