Browse Source

Merge remote-tracking branch 'origin/refactor32'

pull/4/head
Jakob Kjær-Kammersgaard 7 years ago
parent
commit
823955c556
  1. 74
      IntToString.nasm
  2. 160
      Strings32.nasm
  3. 2
      bootloader.nasm
  4. BIN
      demo
  5. 162
      go32bit.nasm
  6. 4
      kernel.nasm

74
IntToString.nasm

@ -0,0 +1,74 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Puts the stringified version of eax on the stack
; IN: eax = number to write
; IN: edi = base of where to write string
; OUT: ecx = number of characters written to buffer
IntToString: ; (eax = number, edi = buffer base) -> ecx = number of characters
push eax
push edi
;push ecx
push edx
push esi
push ebp
mov ecx, edi ; store negative original buffer base
neg ecx ; (used for counting amount of characters)
test eax, eax
jns .not_negative
mov BYTE [edi], '-'
neg eax
add edi, 1
.not_negative:
mov esi, edi ; store pointer after potential negative sign character
mov ebp, 0xcccccccd ; ebp = 32 bit multiplicative reciprocal of 10
.divide_loop:
mov ebx, eax ; save number in ebx
mul ebp
shr edx, 3 ; edx = quotient
lea eax, [edx + edx*4] ; eax = quotient * 10 = (number - remainder)
add eax, eax ; |
sub ebx, eax ; ebx = number - (number - remainder) = remainder
add ebx, '0' ; remainder is our digit, turn it ASCII
mov BYTE [edi], bl ; store ASCII digit to buffer
lea edi, [edi+1]
mov eax, edx ; We need quotient in eax for 'mul'
test eax, eax
jne .divide_loop
add ecx, edi ; Update amount of characters
mov byte [edi], 0 ; Null terminator
lea edi, [edi-1] ; We incremented the buffer pointer once too much
.swap_loop:
cmp esi, edi
jae .done_swapping
movzx eax, BYTE [edi]
movzx edx, BYTE [esi]
mov BYTE [edi], dl
mov BYTE [esi], al
sub edi, 1
add esi, 1
jmp .swap_loop
.done_swapping:
pop ebp
pop esi
pop edx
;pop ecx
pop edi
pop eax
ret

160
Strings32.nasm

@ -0,0 +1,160 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Converts integer to string
; IN: eax = number to convert
; IN: edi = base of string buffer
; OUT: ecx = number of characters written to buffer
IntToString:
push eax
push esi
push edx
push edi
push ebp
mov ecx, esi ; store negative original buffer base
neg ecx ; (used for counting amount of characters)
test eax, eax
jns .not_negative
mov BYTE [esi], '-'
neg eax
add esi, 1
.not_negative:
mov edi, esi ; store pointer after potential negative sign character
mov ebp, 0xcccccccd ; ebp = 32 bit multiplicative reciprocal of 10
.divide_loop:
mov ebx, eax ; save number in ebx
mul ebp
shr edx, 3 ; edx = quotient
lea eax, [edx + edx*4] ; eax = quotient * 10 = (number - remainder)
add eax, eax ; |
sub ebx, eax ; ebx = number - (number - remainder) = remainder
add ebx, '0' ; remainder is our digit, turn it ASCII
mov BYTE [esi], bl ; store ASCII digit to buffer
lea esi, [esi+1]
mov eax, edx ; We need quotient in eax for 'mul'
test eax, eax
jne .divide_loop
add ecx, esi ; Update amount of characters
mov byte [esi], 0x00 ; Add null terminator to end of string
lea esi, [esi-1] ; We incremented the buffer pointer once too much
.swap_loop:
cmp edi, esi
jae .done_swapping
movzx eax, BYTE [esi]
movzx edx, BYTE [edi]
mov BYTE [esi], dl
mov BYTE [edi], al
sub esi, 1
add edi, 1
jmp .swap_loop
.done_swapping:
pop ebp
pop edi
pop edx
pop esi
pop eax
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Right justifies source string within the given justification width
; IN: esi = base of string buffer
; IN: edx = justification width
; IN: ecx = string length
; IN: al = padding char
; ASSUMES: that the justification width > string length
; ASSUMES: that the size of the string buffer > justification width
RightJustifyString:
pushad
pushfd
lea edi, [esi + edx-1]
lea esi, [esi + ecx-1]
sub edx, ecx
std ; Move string from the end
rep movsb ; |
mov ecx, edx
rep stosb ; Pad with AL
popfd
popad
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Copies ecx bytes
; IN: esi = source
; IN: edi = destination
; IN: ecx = number of bytes to copy
CopyData:
pushad
mov edx, ecx
and edx, 11b
shr ecx, 2
; ecx = quotient, edx = remainder
rep movsd
mov ecx, edx
rep movsb
popad
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Puts the stringified version of eax on the stack
; IN: esi = base of string
; IN: ecx = string length
; IN: ah = character attributes
PrintString:
push ecx
push eax
push edi
push esi
.print_loop:
mov al, byte [esi]
mov word [edi], ax
lea esi, [esi+1]
lea edi, [edi+2]
sub ecx, 1
jnz .print_loop
pop esi
pop edi
pop eax
pop ecx
ret

2
bootloader.nasm

@ -31,7 +31,7 @@ start:
mov si, enter_debug_mode
call print
call printCRLF
%IF 1
%IF 0
; here goes wait call, for the user to enter debug mode.
; Wating for 2 seconds:
mov ah, 0x86 ; code for waiting interupt call

BIN
demo

Binary file not shown.

162
go32bit.nasm

@ -12,6 +12,9 @@
%define GDT_DataSeg16Selector GDT_Selector(GDT_DataSeg16Index, 0, 0)
%define VIDEO 0xB8000
%define VIDEO_X_RES 80
%define VIDEO_Y_RES 25
%define ScreenCoord(x, y) (2*(VIDEO_X_RES*(y)+(x)))
; GDT Stuff
%define Data 0000b
@ -247,12 +250,16 @@ ProtectedModeBaby:
mov fs, ax
mov gs, ax
; Setup stack
mov esp, 0x8000000
mov esp, (100 << 20) - 1 ; 100M Addressable
mov ebp, esp
; Space for dynamic variables
sub esp, 200*4 ; 200 32-bit integers
push ebp
DrawStuff:
xor ecx, ecx
@ -296,15 +303,15 @@ DrawStuff:
stosw
inc ecx
cmp ecx, 80
cmp ecx, VIDEO_X_RES
jne .DrawLoop
xor ecx, ecx
inc edx
cmp edx, 25-1
cmp edx, VIDEO_Y_RES-2
jne .DrawLoop
pop ebp
cld
@ -377,9 +384,8 @@ DrawStuff:
.PrintKeyLegend:
lea esi, [ThereWasSomethingStr]
lea edi, [VIDEO+(80*24 + (80-11))*2]
; lea edi, [VIDEO+(80*2)*12+30*2]
mov ah, 0x0f
lea edi, [VIDEO + ScreenCoord((80-11), 24)]
mov ah, 0x2f
.print_loop:
mov al, BYTE [esi]
test al, al
@ -389,104 +395,71 @@ DrawStuff:
jmp .print_loop
.break_print_loop:
lea edi, [VIDEO + (80*24 + 1)*2]
push edi
mov ecx, 34
xor eax, eax
rep stosd
pop edi
lea esi, [VarLabels]
lea edi, [ConversionBuffer]
mov ecx, VarLabelsLength
call CopyData
mov eax, dword [Px]
sar eax, 4
call IntToString
lea esi, [ConversionBuffer]
lea edi, [VIDEO + ScreenCoord(0, 24)]
mov ah, 0x0f
call PrintString
add edi, 12*2
mov eax, dword [Py]
; lea esi, [ConversionBuffer]
mov eax, dword [Px]
sar eax, 4
call IntToString
add edi, 12*2
mov eax, [Zoom]
sar eax, 9
call IntToString
add [ColorOffset], dword 1
jmp DrawStuff
HALT:
cli
hlt
jmp HALT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
IntToString:
; Puts the stringified version of eax on the stack
; IN: eax = number to write
; IN: edi = base of where to write string
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pushad
; lea esi, [ConversionBuffer]
; ecx set by IntToString
mov edx, 7
mov al, ' '
call RightJustifyString
mov ebx, 10 ; Divisor
; esi still ConversionBuffer
mov ecx, edx
lea edi, [VIDEO + ScreenCoord(StrXStart, 24)]
mov ah, 0x70
call PrintString
xor ecx, ecx ; Digit count starts at 0
mov [.sign], byte ' '
; lea esi, [ConversionBuffer]
mov eax, dword [Py]
sar eax, 4
call IntToString
test eax, eax
jns .loop_divide
; lea esi, [ConversionBuffer]
; ecx set by IntToString
mov edx, 7
mov al, ' '
call RightJustifyString
neg eax
mov [.sign], byte '-'
; esi still ConversionBuffer
mov ecx, edx
lea edi, [VIDEO + ScreenCoord(StrYStart, 24)]
mov ah, 0x70
call PrintString
.loop_divide: ; finds digits and pushes them to stack
test eax, eax
jz .break_loop_divide
xor edx, edx
idiv ebx ; edx = (edx:eax)%ebx, eax = (edx:eax)/ebx
inc ecx ; Increase digit count
mov byte [.buffer + ecx], dl
jmp .loop_divide
.break_loop_divide:
add edi, 20
test ecx, ecx
jnz .NotZero
add [ColorOffset], dword 1
mov ax, (0x0f << 8) | '0'
stosw
popad
ret
jmp DrawStuff
.NotZero:
sub edi, ecx
sub edi, ecx
mov ah, 0x0f
mov al, byte [.sign]
stosw
.loop_print:
test ecx, ecx
jz .break_loop_print
mov al, byte [.buffer + ecx]
add al, '0' ; Convert to ascii
mov ah, 0x0f
stosw
dec ecx
jmp .loop_print
.break_loop_print:
popad
ret
.buffer times 40 db 0
.sign db ' '
%include "Strings32.nasm"
Halt:
cli
hlt
jmp Halt
; ------------------------------------------------------------------------------
Reboot:
in al, 0x64
test al, 0x2 ; Wait for an empty Input Buffer
@ -498,11 +471,22 @@ Reboot:
;;;;;;;;;;;;;;;;;;;;;;;;
;; Strings
;;;;;;;;;;;;;;;;;;;;;;;;
ProtectedWelcomeStr: db " Placeholder for SingOS - 32 bit edition! ", 0
ThereWasSomethingStr: db " KEY = '"
Key: db " ' ", 0
Px: dd 0
Py: dd 0
ColorOffset: dd 0
Zoom: dd (4<<9)
VarLabels: db "X:"
StrXStart equ $-VarLabels
db " "
db "Y:"
StrYStart equ $-VarLabels
db " "
VarLabelsLength equ $-VarLabels
ProtectedWelcomeStr: db " Placeholder for SingOS - 32 bit edition! ", 0
ProtectedWelcomeStrLength equ $-ProtectedWelcomeStr
TestStr: db "hello, world"
TestStrLength equ $-TestStr
ThereWasSomethingStr: db " KEY = '"
Key: db " ' ", 0
Px: dd 0
Py: dd 0
ColorOffset: dd 0
Zoom: dd (4<<9)
ConversionBuffer: times 40 db 0
align 512

4
kernel.nasm

@ -24,6 +24,10 @@ sing_loaded: ; SingOS is ready for the user:
mov bp, sp
sing_ready: ; SingOS is ready for the user:
%if 1
call Go32Bit
%endif
call os_clear_screen
mov si, welcome ; Put address of the null-terminated string to output into 'si'
call print ; Call our string-printing routine

Loading…
Cancel
Save