Programs now start up and can write to the console, but crash when reading.
This commit is contained in:
		
							parent
							
								
									48398b072a
								
							
						
					
					
						commit
						cc356b5c75
					
				
					 8 changed files with 111 additions and 43 deletions
				
			
		| 
						 | 
				
			
			@ -36,7 +36,7 @@ begtext:
 | 
			
		|||
    cseg mov (interrupt_ptr+0), esi
 | 
			
		||||
    cseg o16 mov (transfer_buffer_ptr), di
 | 
			
		||||
 | 
			
		||||
    mov eax, endbss
 | 
			
		||||
    mov eax, __end
 | 
			
		||||
    cseg callf (realloc_ptr)
 | 
			
		||||
 | 
			
		||||
    ! Clear BSS.
 | 
			
		||||
| 
						 | 
				
			
			@ -161,7 +161,7 @@ empty_environment:
 | 
			
		|||
    add esp, 5*4
 | 
			
		||||
 | 
			
		||||
    ! Bail out if something went wrong.
 | 
			
		||||
    test ax, ax
 | 
			
		||||
    test eax, eax
 | 
			
		||||
    jnz no_room
 | 
			
		||||
 | 
			
		||||
    ! argc, argv, and envp are now at the stack top.  Now go.
 | 
			
		||||
| 
						 | 
				
			
			@ -197,6 +197,7 @@ rmode:               .space 2
 | 
			
		|||
pmode_cs:            .space 2
 | 
			
		||||
pmode_ds:            .space 2
 | 
			
		||||
 | 
			
		||||
.define realloc_ptr
 | 
			
		||||
.define interrupt_ptr
 | 
			
		||||
.define rmode
 | 
			
		||||
.define pmode_cs
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										23
									
								
								plat/msdos386/libsys/brk.s
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								plat/msdos386/libsys/brk.s
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
! Declare segments (the order is important).
 | 
			
		||||
 | 
			
		||||
.sect .text
 | 
			
		||||
.sect .rom
 | 
			
		||||
.sect .data
 | 
			
		||||
.sect .bss
 | 
			
		||||
 | 
			
		||||
.sect .text
 | 
			
		||||
 | 
			
		||||
.extern realloc_ptr
 | 
			
		||||
 | 
			
		||||
.define _brk
 | 
			
		||||
_brk:
 | 
			
		||||
	enter 0, 0
 | 
			
		||||
newsize = 2*4
 | 
			
		||||
 | 
			
		||||
	mov eax, newsize(ebp)
 | 
			
		||||
	callf (realloc_ptr)
 | 
			
		||||
	xor eax, eax
 | 
			
		||||
	leave
 | 
			
		||||
	ret
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1,13 +1,14 @@
 | 
			
		|||
acklibrary {
 | 
			
		||||
    name = "lib",
 | 
			
		||||
    srcs = {
 | 
			
		||||
		"./brk.c",
 | 
			
		||||
		"./_hol0.s",
 | 
			
		||||
		"./brk.s",
 | 
			
		||||
		"./close.s",
 | 
			
		||||
		"./errno.s",
 | 
			
		||||
		"./getpid.s",
 | 
			
		||||
		"./_hol0.s",
 | 
			
		||||
		"./isatty.s",
 | 
			
		||||
		"./rename.s",
 | 
			
		||||
		"./sbrk.c",
 | 
			
		||||
		"./sys_exists.s",
 | 
			
		||||
		"./sys_getdate.s",
 | 
			
		||||
		"./sys_gettime.s",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,29 +8,9 @@
 | 
			
		|||
#include <unistd.h>
 | 
			
		||||
#include "libsys.h"
 | 
			
		||||
 | 
			
		||||
#define STACK_BUFFER 128 /* number of bytes to leave for stack */
 | 
			
		||||
 | 
			
		||||
extern char _end[1];
 | 
			
		||||
static char* current = _end;
 | 
			
		||||
 | 
			
		||||
int brk(void* newend)
 | 
			
		||||
{
 | 
			
		||||
	/* This variable is used to figure out the current stack pointer,
 | 
			
		||||
	 * by taking its address. */
 | 
			
		||||
	char dummy;
 | 
			
		||||
	char* p = newend;
 | 
			
		||||
	
 | 
			
		||||
	if ((p > (&dummy - STACK_BUFFER)) ||
 | 
			
		||||
	    (p < _end))	
 | 
			
		||||
	{
 | 
			
		||||
		errno = ENOMEM;
 | 
			
		||||
		return -1;
 | 
			
		||||
	}
 | 
			
		||||
		
 | 
			
		||||
	current = p;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* sbrk(int increment)
 | 
			
		||||
{
 | 
			
		||||
	char* old;
 | 
			
		||||
| 
						 | 
				
			
			@ -17,13 +17,13 @@
 | 
			
		|||
 | 
			
		||||
.define __sys_isreadyr
 | 
			
		||||
__sys_isreadyr:
 | 
			
		||||
	int 3
 | 
			
		||||
	mov bx, sp
 | 
			
		||||
	mov ax, 0x4406
 | 
			
		||||
	mov bx, 2(bx)
 | 
			
		||||
	mov ebx, sp
 | 
			
		||||
	mov eax, 0x4406
 | 
			
		||||
	mov ebx, 4(ebx)
 | 
			
		||||
	int 0x21
 | 
			
		||||
	jnc ok
 | 
			
		||||
	movb al, 0
 | 
			
		||||
ok:
 | 
			
		||||
	cbw
 | 
			
		||||
	cwd
 | 
			
		||||
	ret
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,5 +26,60 @@
 | 
			
		|||
 | 
			
		||||
.define __sys_rawread
 | 
			
		||||
__sys_rawread:
 | 
			
		||||
	int 3
 | 
			
		||||
	enter 4, 0
 | 
			
		||||
amount_transferred = -1*4
 | 
			
		||||
file_handle = 2*4
 | 
			
		||||
write_buffer = 3*4
 | 
			
		||||
amount_to_read = 4*4
 | 
			
		||||
 | 
			
		||||
	mov amount_transferred(ebp), 0
 | 
			
		||||
 | 
			
		||||
mainloop:
 | 
			
		||||
	mov eax, amount_to_read(ebp)
 | 
			
		||||
	test eax, eax
 | 
			
		||||
	jz exit
 | 
			
		||||
 | 
			
		||||
	mov ecx, 32*1024
 | 
			
		||||
	cmp eax, ecx
 | 
			
		||||
	jge 2f
 | 
			
		||||
	mov ecx, eax
 | 
			
		||||
2:
 | 
			
		||||
 | 
			
		||||
	! Read from DOS into the transfer buffer.
 | 
			
		||||
 | 
			
		||||
	movb ah, 0x3f
 | 
			
		||||
	o16 mov dx, (transfer_buffer_ptr)
 | 
			
		||||
	o16 mov bx, file_handle(ebp)
 | 
			
		||||
	mov ecx, 0x80
 | 
			
		||||
	or ebx, 0x210000
 | 
			
		||||
	callf (interrupt_ptr)
 | 
			
		||||
	jc exit
 | 
			
		||||
	test eax, eax
 | 
			
		||||
	jz exit
 | 
			
		||||
 | 
			
		||||
	! Copy eax bytes out of the transfer buffer.
 | 
			
		||||
 | 
			
		||||
	mov ecx, eax
 | 
			
		||||
	push eax
 | 
			
		||||
	movzx esi, (transfer_buffer_ptr)
 | 
			
		||||
	mov edi, write_buffer(ebp)
 | 
			
		||||
	mov es, (pmode_ds)
 | 
			
		||||
	cld
 | 
			
		||||
1:
 | 
			
		||||
	eseg lods
 | 
			
		||||
	movb (edi), al
 | 
			
		||||
	add edi, 4
 | 
			
		||||
	loop 1b
 | 
			
		||||
	pop eax
 | 
			
		||||
 | 
			
		||||
	add write_buffer(ebp), eax
 | 
			
		||||
	add amount_transferred(ebp), eax
 | 
			
		||||
	sub amount_to_read(ebp), eax
 | 
			
		||||
	jmp mainloop
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
	mov eax, amount_transferred(ebp)
 | 
			
		||||
	leave
 | 
			
		||||
	ret
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,31 +32,39 @@ file_handle = 2*4
 | 
			
		|||
read_buffer = 3*4
 | 
			
		||||
amount_to_write = 4*4
 | 
			
		||||
 | 
			
		||||
	mov es, (pmode_ds)
 | 
			
		||||
	mov amount_transferred(ebp), 0
 | 
			
		||||
 | 
			
		||||
mainloop:
 | 
			
		||||
	mov ecx, 32*1024
 | 
			
		||||
	cmp amount_to_write(ebp), ecx
 | 
			
		||||
	jge 2f
 | 
			
		||||
	mov ecx, amount_to_write(ebp)
 | 
			
		||||
	test ecx, ecx
 | 
			
		||||
	mov eax, amount_to_write(ebp)
 | 
			
		||||
	test eax, eax
 | 
			
		||||
	jz exit
 | 
			
		||||
 | 
			
		||||
	mov ecx, 32*1024
 | 
			
		||||
	cmp eax, ecx
 | 
			
		||||
	jge 2f
 | 
			
		||||
	mov ecx, eax
 | 
			
		||||
2:
 | 
			
		||||
 | 
			
		||||
	! Copy ecx bytes into the transfer buffer.
 | 
			
		||||
 | 
			
		||||
	push ecx
 | 
			
		||||
	mov esi, read_buffer(ebp)
 | 
			
		||||
	movzx edi, (transfer_buffer_ptr)
 | 
			
		||||
	mov es, (pmode_ds)
 | 
			
		||||
	cld
 | 
			
		||||
	rep movsb
 | 
			
		||||
	pop ecx
 | 
			
		||||
 | 
			
		||||
	! Write from the transfer buffer to DOS.
 | 
			
		||||
 | 
			
		||||
	movb ah, 0x40
 | 
			
		||||
	o16 mov dx, (transfer_buffer_ptr)
 | 
			
		||||
	o16 mov bx, file_handle(ebp)
 | 
			
		||||
	or ebx, 0x210000
 | 
			
		||||
	callf (interrupt_ptr)
 | 
			
		||||
	movzx eax, ax
 | 
			
		||||
	jc exit
 | 
			
		||||
 | 
			
		||||
	! Update counters and go again.
 | 
			
		||||
 | 
			
		||||
	add read_buffer(ebp), eax
 | 
			
		||||
	add amount_transferred(ebp), eax
 | 
			
		||||
| 
						 | 
				
			
			@ -64,7 +72,7 @@ mainloop:
 | 
			
		|||
	jmp mainloop
 | 
			
		||||
 | 
			
		||||
exit:
 | 
			
		||||
	mov eax, amount_to_write(ebp)
 | 
			
		||||
	mov eax, amount_transferred(ebp)
 | 
			
		||||
	leave
 | 
			
		||||
	ret
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -337,12 +337,12 @@ interrupt:
 | 
			
		|||
    pop ds
 | 
			
		||||
    pop es
 | 
			
		||||
    pushf
 | 
			
		||||
    mov ax, (dpmi_eax)
 | 
			
		||||
    mov bx, (dpmi_ebx)
 | 
			
		||||
    mov cx, (dpmi_ecx)
 | 
			
		||||
    mov dx, (dpmi_edx)
 | 
			
		||||
    mov si, (dpmi_esi)
 | 
			
		||||
    mov di, (dpmi_edi)
 | 
			
		||||
    o32 movzx eax, (dpmi_eax)
 | 
			
		||||
    o32 movzx ebx, (dpmi_ebx)
 | 
			
		||||
    o32 movzx ecx, (dpmi_ecx)
 | 
			
		||||
    o32 movzx edx, (dpmi_edx)
 | 
			
		||||
    o32 movzx esi, (dpmi_esi)
 | 
			
		||||
    o32 movzx edi, (dpmi_edi)
 | 
			
		||||
    popf
 | 
			
		||||
    ret
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue