Added stage 2 of the assessment with stack management

Changed Stage 1 to use full registers and the si register instead of splitting them
This commit is contained in:
iDunnoDev
2022-11-05 23:34:34 +00:00
committed by iDunnoDev
parent d7f3c20324
commit fe2e14921a
19 changed files with 585 additions and 60 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.img filter=lfs diff=lfs merge=lfs -text

View File

@ -39,7 +39,6 @@ cons_writeline:
ret
# Added Write Hex and Int functions to help with debugging
HexChars: .ascii "0123456789ABCDEF"
cons_write_hex:
@ -62,7 +61,7 @@ cons_write_int:
cmpw $0, %ax
jge getdigit
xor %ax, %ax
xor %ax, %ax # Added to handle signed numbers, it adds the minus and then neg's the number
movb $0x0E, %ah
movb $0x2D, %al
int $0x10
@ -85,13 +84,14 @@ getdigit:
IntBuffer: .string " "
# Draw Line function
cons_draw_line:
movw $0, err # Make sure that err starts at 0
movw %ax, (x0) # Move the initial vars from the registers to static memory
movb %cl, (y0)
movw %bx, (x1)
movb %ch, (y1)
movb %dl, (color)
movw %bx, (y0)
movw %cx, (x1)
movw %dx, (y1)
movw %si, (color)
movw x1, %cx # Load x1 and y1 into the registers
movw y1, %dx
@ -102,16 +102,17 @@ cons_draw_line:
cons_line_check_x:
movw x0, %cx # Load x0 into register cx so we can manipulate this value to plot each pixel
movw $1, (sx) # Preload x slope with 1
cmp x1, %cx # Check if x1 is less than x0 and we can move to y
movw $1, (sx) # Preload the x slope with 1
cmp x1, %cx # Check if x0 is less than x1 and we can move to y
jl cons_line_check_y
negw sx # if x1 is greater than we need to flip the slope and the x delta values
negw deltax
negw sx # If x1 is greater than we need to flip the slope and the x delta values
negw deltax # Flipping the deltax here saves us from having to have an abs function because if
# x0 was greater than x1 we know we have a negative delta value and we flip it to pos
cons_line_check_y:
movw y0, %dx # Load y0 into register dx so we can manipulate this value to plot each pixel
movw $1, (sy) # Preload y slope with 1
cmpw y1, %dx # Check if y1 is less than y0 and we can start our plotting
movw $1, (sy) # Preload the y slope with 1
cmpw y1, %dx # Check if y0 is less than y1 and we can start our plotting
jl cons_line_prep_loop
negw sy # if y1 is greater than we need to flip the slope y and delta y values
negw deltay
@ -136,7 +137,7 @@ cons_line_loop_start:
jmp cons_line_loop_end # if both x's and y's are equal then we can end the function
cons_line_loop_next_point:
movw err, %ax # Load err into ax so that we can multiply it
movw err, %ax # Load err into ax so that we can change it
add %ax, %ax # e2 is 2 * err, so we can just add err to itself
cons_line_loop_move_y_point:
@ -170,75 +171,75 @@ draw_start:
int $0x10
xor %ax, %ax
movw $50, %ax # Plot a line, ax = x0, bx = x1, cl = y0, ch = y1, dl = color
movb $50, %cl # the X values require a full register but the y's only require a byte since the max value is 200
movw $160, %bx
movb $55, %ch
movb $12, %dl # The Color also only requires a byte
movw $50, %ax # Plot a line, ax = x0, bx = y0, cx = x1, dx = y1, si = color
movw $50, %bx
movw $160, %cx
movw $55, %dx
movw $12, %si # The Color using the si counter because we dont have enough normal registers
call cons_draw_line
# Draw the rest of the lines
movw $160, %ax
movb $55, %cl
movw $90, %bx
movb $150, %ch
movb $11, %dl
movw $55, %bx
movw $90, %cx
movw $150, %dx
movw $11, %si
call cons_draw_line
movw $90, %ax
movb $150, %cl
movw $50, %bx
movb $50, %ch
movb $14, %dl
movw $150, %bx
movw $50, %cx
movw $50, %dx
movw $14, %si
call cons_draw_line
movw $10, %ax
movb $10, %cl
movw $310, %bx
movb $10, %ch
movb $9, %dl
call cons_draw_line
movw $310, %ax
movb $10, %cl
movw $310, %bx
movb $190, %ch
movb $10, %dl
call cons_draw_line
movw $310, %ax
movb $190, %cl
movw $10, %bx
movb $190, %ch
movb $13, %dl
movw $310, %cx
movw $10, %dx
movw $9, %si
call cons_draw_line
movw $310, %ax
movw $10, %bx
movw $310, %cx
movw $190, %dx
movw $10, %si
call cons_draw_line
movw $310, %ax
movw $190, %bx
movw $10, %cx
movw $190, %dx
movw $13, %si
call cons_draw_line
movw $10, %ax
movb $190, %cl
movw $10, %bx
movb $10, %ch
movb $15, %dl
movw $190, %bx
movw $10, %cx
movw $10, %dx
movw $15, %si
call cons_draw_line
movw $75, %ax
movb $150, %cl
movw $250, %bx
movb $110, %ch
movb $1, %dl
movw $150, %bx
movw $250, %cx
movw $110, %dx
movw $1, %si
call cons_draw_line
movw $210, %ax
movb $30, %cl
movw $300, %bx
movb $150, %ch
movb $6, %dl
movw $30, %bx
movw $300, %cx
movw $150, %dx
movw $6, %si
call cons_draw_line
movw $180, %ax
movb $180, %cl
movw $170, %bx
movb $20, %ch
movb $3, %dl
movw $180, %bx
movw $170, %cx
movw $20, %dx
movw $3, %si
call cons_draw_line
endless_loop: # Loop forever more
@ -258,4 +259,4 @@ sx: .word 0
sy: .word 0
err: .word 0
e2: .word 0
color: .byte 0
color: .word 0

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

13
stage2/.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build and QEMU",
"type": "shell",
"command": "make qemu",
"problemMatcher": []
}
]
}

102
stage2/Makefile Normal file
View File

@ -0,0 +1,102 @@
# Try to infer the correct TOOLPREFIX if not set
ifndef TOOLPREFIX
TOOLPREFIX := $(shell if i386-jos-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
then echo 'i386-jos-elf-'; \
elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2; \
echo "*** Error: Couldn't find an i386-*-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with i386-jos-elf-gcc in your PATH?" 1>&2; \
echo "*** If your i386-*-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'i386-jos-elf-', set your TOOLPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake TOOLPREFIX= ...'." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
# If the makefile can't find QEMU, specify its path here
# QEMU = qemu-system-i386
# Try to infer the correct QEMU
ifndef QEMU
QEMU = $(shell if which qemu > /dev/null; \
then echo qemu; exit; \
elif which qemu-system-i386 > /dev/null; \
then echo qemu-system-i386; exit; \
elif which qemu-system-x86_64 > /dev/null; \
then echo qemu-system-x86_64; exit; \
else \
qemu=/Applications/Q.app/Contents/MacOS/i386-softmmu.app/Contents/MacOS/i386-softmmu; \
if test -x $$qemu; then echo $$qemu; exit; fi; fi; \
echo "***" 1>&2; \
echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
echo "*** or have you tried setting the QEMU variable in Makefile?" 1>&2; \
echo "***" 1>&2; exit 1)
endif
CC = $(TOOLPREFIX)gcc
AS = $(TOOLPREFIX)gas
LD = $(TOOLPREFIX)ld
OBJCOPY = $(TOOLPREFIX)objcopy
OBJDUMP = $(TOOLPREFIX)objdump
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
# FreeBSD ld wants ``elf_i386_fbsd''
LDFLAGS += -m $(shell $(LD) -V | grep elf_i386 2>/dev/null | head -n 1)
# Disable PIE when possible (for Ubuntu 16.10 toolchain)
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
CFLAGS += -fno-pie -no-pie
endif
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
CFLAGS += -fno-pie -nopie
endif
xv6.img: bootblock bootblock2
dd if=/dev/zero of=xv6.img count=10000
dd if=bootblock of=xv6.img conv=notrunc
dd if=bootblock2 of=xv6.img seek=1 conv=notrunc
bootblock: bootasm.S
$(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c bootasm.S
$(LD) $(LDFLAGS) -N -e start -Ttext 0x7C00 -o bootblock.o bootasm.o
$(OBJCOPY) -S -O binary -j .text bootblock.o bootblock
./sign.pl bootblock
bootblock2: bootasm2.S
$(CC) $(CFLAGS) -fno-pic -nostdinc -I. -c bootasm2.S
$(LD) $(LDFLAGS) -N -e start -Ttext 0x9000 -o bootblock2.o bootasm2.o
$(OBJCOPY) -S -O binary -j .text bootblock2.o bootblock2
.PRECIOUS: %.o
clean:
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
*.o *.d *.asm *.sym vectors.S bootblock bootblock2 entryother \
initcode initcode.out kernel xv6.img fs.img kernelmemfs \
xv6memfs.img mkfs \
syscall.h syscalltable.h usys.S
# run in emulators
# try to generate a unique GDB port
GDBPORT = $(shell expr `id -u` % 5000 + 25000)
# QEMU's gdb stub command line changed in 0.11
QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
then echo "-gdb tcp::$(GDBPORT)"; \
else echo "-s -p $(GDBPORT)"; fi)
ifndef CPUS
CPUS := 1
endif
QEMUOPTS = -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA)
qemu: xv6.img
$(QEMU) -vga std -serial mon:stdio $(QEMUOPTS)
qemu-gdb: xv6.img
@echo "*** Now run 'gdb'." 1>&2
$(QEMU) -vga std -serial mon:stdio $(QEMUOPTS) -S -gdb tcp::1234

101
stage2/bootasm.S Normal file
View File

@ -0,0 +1,101 @@
# When the PC starts, the processor is essentially emulating an 8086 processor, i.e.
# a 16-bit processor. So our initial boot loader code is 16-bit code that will
# eventually switch the processor into 32-bit mode.
# This code is linked to assume a starting address of 0x7C00 which is where the BIOS
# will load a boot segment.
.code16 # Assemble for 16-bit mode
.globl start
start:
jmp real_start
# Write to the console using BIOS.
#
# Input: SI contains the address of the null-terminated string to be displayed
cons_write:
movb $0x0e, %ah # 0x0e is the INT 10h BIOS call to output the value contained in AL to screen
cons_write_rpt:
movb (%si), %al # Load the byte at the location contained in the SI register into AL
inc %si # Add 1 to the value in SI
cmp $0, %al # Compare the value in AL with 0
jz cons_write_done # If it is zero, then we are done
int $0x10 # Output the character in AL to the screen
jmp cons_write_rpt # and continue
cons_write_done: # Something that is called will never return
ret # until a 'ret' instruction is encountered. Labels do
# not give a program any structure. They just give a
# memory location a name that we can use in our code.
cons_write_crlf:
movb $0x0e, %ah # Output CR
movb $0x0d, %al
int $0x10
movb $0x0a, %al # Output LF
int $0x10
ret
cons_writeline:
call cons_write
call cons_write_crlf
ret
real_start:
cli # BIOS enabled interrupts; disable
# Zero data segment registers DS, ES, and SS.
xorw %ax, %ax # Set %ax to zero
movw %ax, %ds # -> Data Segment
movw %ax, %es # -> Extra Segment
movw %ax, %ss # -> Stack Segment
movw $0, %sp # Set the stack to the top of the segment
movb %dl, (boot_device) # Boot device number is passed in DL from BIOS. Save it hear since DL might get trashed
movw $boot_message, %si # Display our boot message
call cons_writeline
movb $2, %ah # BIOS function 13h, sub-function 2 is read sectors
movb $7, %al # Number of sectors to read = 7
movw $0x9000, %bx # The 7 sectors will be loaded into memory at ES:BX (0000:9000h)
movb $0, %ch # Use cylinder 0
movb $0, %dh # Use head 0
movb (boot_device), %dl # Retrieve the ID of our boot device
movb $2, %cl # Start reading at sector 2 (i.e. one after the boot sector)
int $0x13
cmpb $7, %al # AL returns the number of sectors read. If this is not 7, report an error
jne read_failed
movb (0x9000), %al # Check that what we loaded is not empty
cmpb $0, %al
je read_failed
movb (boot_device), %dl # Pass boot device ID to second stage
movw $0x9000, %ax # Jump to stage 2
jmp *%ax
read_failed: # Display error messages
movw $read_failed_msg, %si
call cons_writeline
mov $cannot_continue, %si
call cons_writeline
endless_loop: # Loop forever more
jmp endless_loop
# Program data
boot_device:
.byte 0
boot_message:
.string "Boot Loader V1.0"
read_failed_msg:
.string "Unable to read stage 2 of the boot process"
cannot_continue:
.string "Cannot continue boot process"

1
stage2/bootasm.d Normal file
View File

@ -0,0 +1 @@
bootasm.o: bootasm.S

BIN
stage2/bootasm.o Normal file

Binary file not shown.

283
stage2/bootasm2.S Normal file
View File

@ -0,0 +1,283 @@
# Second stage of the boot loader
.code16 # Assemble for 16-bit mode
.globl start
start:
jmp real_start
# Write to the console using BIOS.
#
# Input: SI contains the address of the null-terminated string to be displayed
cons_write:
movb $0x0e, %ah # 0x0e is the INT 10h BIOS call to output the value contained in AL to screen
cons_write_rpt:
movb (%si), %al # Load the byte at the location contained in the SI register into AL
inc %si # Add 1 to the value in SI
cmp $0, %al # Compare the value in AL with 0
jz cons_write_done # If it is zero, then we are done
int $0x10 # Output the character in AL to the screen
jmp cons_write_rpt # and continue
cons_write_done: # Something that is called will never return
ret # until a 'ret' instruction is encountered. Labels do
# not give a program any structure. They just give a
# memory location a name that we can use in our code.
cons_write_crlf:
movb $0x0e, %ah # Output CR
movb $0x0d, %al
int $0x10
movb $0x0a, %al # Output LF
int $0x10
ret
cons_writeline:
call cons_write
call cons_write_crlf
ret
# Added Write Hex and Int functions to help with debugging
HexChars: .ascii "0123456789ABCDEF"
cons_write_hex:
movw $4, %cx
movb $0x0E, %ah
hexloop:
rol $4, %bx
movw %bx, %si
and $0x000F, %si
movb HexChars(%si), %al
int $0x10
loop hexloop
ret
cons_write_int:
movw $IntBuffer + 4, %si
movw %bx, %ax
cmpw $0, %ax
jge getdigit
xor %ax, %ax # Added to handle signed numbers, it adds the minus and then neg's the number
movb $0x0E, %ah
movb $0x2D, %al
int $0x10
movw %bx, %ax
negw %ax
getdigit:
xor %dx, %dx
movw $10, %cx
idiv %cx
addb $48, %dl
movb %dl, (%si)
dec %si
cmp $0, %ax
jne getdigit
inc %si
call cons_write
ret
IntBuffer: .string " "
# Draw Line function DrawLine(x0 (4), y0 (6), x1 (8), y1 (10), color (12))
# Define parameter address positions in stack
#define color 12
#define y1 10
#define x1 8
#define y0 6
#define x0 4
# Define local variable positions in stack
#define deltax -2
#define deltay -4
#define sx -6
#define sy -8
#define err -10
#define e2 -12
cons_draw_line:
pushw %bp
movw %sp, %bp
subw $12, %sp # Make room for our local variables in the stack
# Store existing register values to the stack so we can restore later
pushw %ax
pushw %bx
pushw %cx
pushw %dx
pushw %si
pushw %di
movw $0, err(%bp) # Make sure that err starts at 0
movw x1(%bp), %cx # Load x1 and y1 into the registers
movw y1(%bp), %dx
sub x0(%bp), %cx # Remove x/y0 from x/y1 to get the delta values
sub y0(%bp), %dx
movw %cx, deltax(%bp) # Store the delta values
movw %dx, deltay(%bp)
cons_line_check_x:
movw x0(%bp), %cx # Load x0 into register cx so we can manipulate this value to plot each pixel
movw $1, sx(%bp) # Preload the x slope with 1
cmp x1(%bp), %cx # Check if x0 is less than x1 and we can move to y
jl cons_line_check_y
negw sx(%bp) # If x1 is greater than we need to flip the slope and the x delta values
negw deltax(%bp) # Flipping the deltax here saves us from having to have an abs function because if
# x0 was greater than x1 we know we have a negative delta value and we flip it to pos
cons_line_check_y:
movw y0(%bp), %dx # Load y0 into register dx so we can manipulate this value to plot each pixel
movw $1, sy(%bp) # Preload the y slope with 1
cmpw y1(%bp), %dx # Check if y0 is less than y1 and we can start our plotting
jl cons_line_prep_loop
negw sy(%bp) # if y1 is greater than we need to flip the slope y and delta y values
negw deltay(%bp)
cons_line_prep_loop:
movw deltax(%bp), %ax # Calculate the err variable by subtracting delta y from delta x
sub deltay(%bp), %ax
movw %ax, err(%bp)
cons_line_loop_start:
xor %ax, %ax # Clear ax and bx for use with the draw function
xor %bx, %bx
movb $0x0c, %ah # Set the function byte to plot the pixel
movb $0, %bh # Set the video page number
movb color(%bp), %al # Set the color of the pixel
int $0x10 # Call the int
cmpw x1(%bp), %cx # Check if x0 and x1 are equal, if not then we are still plotting
jne cons_line_loop_next_point
cmpw y1(%bp), %dx # Check if y0 and y1 are equal, if not then we are still plotting
jne cons_line_loop_next_point
jmp cons_line_loop_end # if both x's and y's are equal then we can end the function
cons_line_loop_next_point:
movw err(%bp), %ax # Load err into ax so that we can change it
add %ax, %ax # e2 is 2 * err, so we can just add err to itself
cons_line_loop_move_y_point:
movw deltay(%bp), %bx
negw %bx # We need negative deltay to compare
cmpw %bx, %ax # Check if we need to apply the slope value to y
jle cons_line_loop_move_x_point
negw %bx # Change deltay back to normal
subw %bx, err(%bp) # Remove the deltay from the err check
addw sx(%bp), %cx # Add the slope value to the current y value
cons_line_loop_move_x_point:
movw deltax(%bp), %bx
cmpw %bx, %ax # Check if we need to apply the x slope value
jge cons_line_loop_start
addw %bx, err(%bp) # Add the deltax to the err value
addw sy(%bp), %dx # Add the slope value to the current x value
jmp cons_line_loop_start # Go back to the start of the loop
cons_line_loop_end:
# Return all the original values to each register before we return back
popw %di
popw %si
popw %dx
popw %cx
popw %bx
popw %ax
movw %bp, %sp
popw %bp
ret $10 # Finish the loop and return to the call address
# we also tell it to free the 10 bytes in the stack for the paramters
# 5 x Word (2 bytes)
real_start:
movw $boot_message, %si # Display our boot message
call cons_writeline
draw_start:
# Set the Video mode to VGA 320 x 200 x 8
movb $0, %ah
movb $0x13, %al
int $0x10
xor %ax, %ax
# Plot a line, we add the parameters to the stack in reverse order
pushw $12 # Color
pushw $55 # y1
pushw $160 # x1
pushw $50 # y0
pushw $50 # x0
call cons_draw_line
# Draw the rest of the lines
pushw $11 # Color
pushw $150 # y1
pushw $90 # x1
pushw $55 # y0
pushw $160 # x0
call cons_draw_line
pushw $14 # Color
pushw $50 # y1
pushw $50 # x1
pushw $150 # y0
pushw $90 # x0
call cons_draw_line
pushw $9 # Color
pushw $10 # y1
pushw $310 # x1
pushw $10 # y0
pushw $10 # x0
call cons_draw_line
pushw $10 # Color
pushw $190 # y1
pushw $310 # x1
pushw $10 # y0
pushw $310 # x0
call cons_draw_line
pushw $13 # Color
pushw $190 # y1
pushw $10 # x1
pushw $190 # y0
pushw $310 # x0
call cons_draw_line
pushw $15 # Color
pushw $10 # y1
pushw $10 # x1
pushw $190 # y0
pushw $10 # x0
call cons_draw_line
pushw $1 # Color
pushw $110 # y1
pushw $250 # x1
pushw $150 # y0
pushw $75 # x0
call cons_draw_line
pushw $6 # Color
pushw $150 # y1
pushw $300 # x1
pushw $30 # y0
pushw $210 # x0
call cons_draw_line
pushw $3 # Color
pushw $20 # y1
pushw $170 # x1
pushw $180 # y0
pushw $180 # x0
call cons_draw_line
endless_loop: # Loop forever more
jmp endless_loop
# Program data
boot_message:
.string "Boot Loader Stage 2 loaded"

1
stage2/bootasm2.d Normal file
View File

@ -0,0 +1 @@
bootasm2.o: bootasm2.S

BIN
stage2/bootblock Normal file

Binary file not shown.

BIN
stage2/bootblock.o Normal file

Binary file not shown.

BIN
stage2/bootblock2 Normal file

Binary file not shown.

BIN
stage2/bootblock2.o Normal file

Binary file not shown.

19
stage2/sign.pl Normal file
View File

@ -0,0 +1,19 @@
#!/usr/bin/perl
open(SIG, $ARGV[0]) || die "open $ARGV[0]: $!";
$n = sysread(SIG, $buf, 1000);
if($n > 510){
print STDERR "boot block too large: $n bytes (max 510)\n";
exit 1;
}
print STDERR "boot block is $n bytes (max 510)\n";
$buf .= "\0" x (510-$n);
$buf .= "\x55\xAA";
open(SIG, ">$ARGV[0]") || die "open >$ARGV[0]: $!";
print SIG $buf;
close SIG;

BIN
stage2/xv6.img (Stored with Git LFS) Normal file

Binary file not shown.