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:
283
stage2/bootasm2.S
Normal file
283
stage2/bootasm2.S
Normal 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"
|
Reference in New Issue
Block a user