Clean up more and add getchar and gets

This commit is contained in:
mintsuki 2020-01-22 05:02:12 +01:00
parent fdfc83abfb
commit 5082a450a0
10 changed files with 75 additions and 51 deletions

View File

@ -3,6 +3,7 @@ bits 16
start:
cli
cld
jmp 0x0000:.initialise_cs
.initialise_cs:
xor ax, ax

View File

@ -28,9 +28,8 @@ read_sector:
mov si, .da_struct
mov ah, 0x42
clc ; Clear carry for int 0x13 because some BIOSes may not clear it on success
int 0x13 ; Call int 0x13
clc
int 0x13
.done:
pop edi
@ -39,7 +38,7 @@ read_sector:
pop ecx
pop ebx
pop eax
ret ; Exit routine
ret
align 4
.da_struct:
@ -65,6 +64,9 @@ align 4
; OUT:
; Carry if error
%define TEMP_BUFFER_SEG 0x7000
%define BYTES_PER_SECT 512
read_sectors:
push eax ; Save GPRs
push ebx
@ -77,41 +79,41 @@ read_sectors:
push es
push ebx
mov bx, 0x7000 ; Load in a temp buffer
mov bx, TEMP_BUFFER_SEG
mov es, bx
xor bx, bx
call read_sector ; Read sector
call read_sector
pop ebx
pop es
jc .done ; If carry exit with flag
jc .done
push ds
mov si, 0x7000
mov si, TEMP_BUFFER_SEG
mov ds, si
mov edi, ebx
xor esi, esi
push ecx
mov ecx, 512
mov ecx, BYTES_PER_SECT
a32 o32 rep movsb
pop ecx
pop ds
inc eax ; Increment sector
add ebx, 512 ; Add 512 to the buffer
inc eax
add ebx, BYTES_PER_SECT
loop .loop ; Loop!
loop .loop
.done:
pop edi
pop esi
pop edx
pop ecx ; Restore GPRs
pop ecx
pop ebx
pop eax
ret ; Exit routine
ret

View File

@ -6,16 +6,17 @@
; SI = points to a 0x00 terminated string
simple_print:
push ax ; Save registers
push ax
push si
mov ah, 0x0E ; int 0x10, function 0x0E (print character)
; int 0x10, function 0x0e (print character)
mov ah, 0x0e
.loop:
lodsb ; Load character from string
test al, al ; Is is the 0x00 terminator?
jz .done ; If it is, exit routine
int 0x10 ; Call BIOS
jmp .loop ; Repeat!
lodsb
test al, al
jz .done
int 0x10
jmp .loop
.done:
pop si ; Restore registers
pop ax
ret ; Exit routine
pop si
pop ax
ret

View File

@ -3,7 +3,7 @@
#include <lib/libc.h>
#include <drivers/disk.h>
#include <lib/real.h>
#include <lib/print.h>
#include <lib/blib.h>
#include <lib/mbr.h>
#define SECTOR_SIZE 512

View File

@ -1,9 +1,8 @@
#include <fs/echfs.h>
#include <stdint.h>
#include <lib/libc.h>
#include <lib/print.h>
#include <lib/blib.h>
#include <drivers/disk.h>
#include <lib/types.h>
struct echfs_identity_table {
uint8_t jmp[4];

View File

@ -1,8 +1,37 @@
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#include <lib/print.h>
#include <lib/blib.h>
#include <drivers/vga_textmode.h>
#include <lib/real.h>
char getchar(void) {
struct rm_regs r = {0};
rm_int(0x16, &r, &r);
return (char)(r.eax & 0xff);
}
void gets(char *buf, size_t limit) {
for (size_t i = 0; ; ) {
char c = getchar();
switch (c) {
case '\b':
if (i) {
i--;
text_write(&c, 1);
}
continue;
case '\n':
buf[i] = 0;
text_write(&c, 1);
return;
}
if (i < limit-1) {
buf[i++] = c;
text_write(&c, 1);
}
}
}
static const char *base_digits = "0123456789abcdef";

14
src/lib/blib.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef __LIB__BLIB_H__
#define __LIB__BLIB_H__
#include <stddef.h>
void print(const char *fmt, ...);
char getchar(void);
void gets(char *buf, size_t limit);
#define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
typedef void *symbol[];
#endif

View File

@ -1,6 +0,0 @@
#ifndef __PRINT_H__
#define __PRINT_H__
void print(const char *, ...);
#endif

View File

@ -1,8 +0,0 @@
#ifndef __LIB_TYPES_H__
#define __LIB_TYPES_H__
typedef void *symbol[];
#define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
#endif

View File

@ -7,8 +7,7 @@ asm (
#include <drivers/vga_textmode.h>
#include <lib/real.h>
#include <lib/print.h>
#include <lib/types.h>
#include <lib/blib.h>
#include <lib/mbr.h>
#include <fs/echfs.h>
@ -51,11 +50,4 @@ void main(int boot_drive) {
: "b" ("")
: "memory"
);
/*for (;;) {
struct rm_regs r = {0};
rm_int(0x16, &r, &r); // Real mode interrupt 16h
char c = (char)(r.eax & 0xff);
text_write(&c, 1);
}*/
}