loadwarka/src/x86_stage1.S

280 lines
4.2 KiB
ArmAsm
Raw Normal View History

2022-01-16 08:26:54 +00:00
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2022-01-26 21:46:30 +00:00
2022-01-27 01:46:04 +00:00
#define STACK_BASE 0x2000
#define STAGE2_BASE 0x7e00
#define DIST(x) (x - _start)
#define STAGE2_ADDR(x) (DIST(x) + STAGE2_BASE)
2022-01-26 21:46:30 +00:00
2022-01-26 20:19:39 +00:00
.code16
2022-01-16 08:26:54 +00:00
.global _start
.section .text
2022-01-26 23:16:14 +00:00
_start:
2022-01-27 01:46:04 +00:00
ljmp $0, $main
2022-01-27 00:30:53 +00:00
.align 2
2022-01-27 02:20:10 +00:00
size: .word 0
2022-01-27 00:21:22 +00:00
2022-01-26 22:43:18 +00:00
disk: .byte 0
2022-01-27 00:21:22 +00:00
newline: .string "\r\n"
2022-01-27 00:29:34 +00:00
hello: .string "Hello from stage 1!\r\n"
2022-01-27 02:20:10 +00:00
size_str: .string "Stage 2 size: "
2022-01-27 00:47:11 +00:00
number_of_sectors: .string "Number of sectors: "
reading_sector: .string "Reading sector "
2022-01-27 02:17:59 +00:00
parenth: .string " ("
of: .string " of "
to_address: .string ") to address "
2022-01-26 21:33:27 +00:00
2022-01-26 23:03:47 +00:00
main:
cli
xor %ax, %ax
mov %ax, %ss
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
2022-01-26 23:03:47 +00:00
mov $STACK_BASE, %sp
2022-01-27 01:46:04 +00:00
mov %dl, disk
2022-01-26 21:33:27 +00:00
2022-01-27 01:46:04 +00:00
mov $hello, %si
2022-01-27 00:21:22 +00:00
call print_str
2022-01-27 02:17:59 +00:00
mov $1, %ax
mov $STAGE2_BASE, %bx
xor %cx, %cx
call print_reading_state
2022-01-26 22:58:11 +00:00
mov $0x02, %ah
2022-01-27 00:34:06 +00:00
mov $1, %al // Sectors count
mov $0x80, %dl // Drive
mov $0, %ch // Cylinder
mov $0, %dh // Head
mov $2, %cl // Cylinder and sector
mov $STAGE2_BASE, %bx // Address
2022-01-26 20:19:39 +00:00
int $0x13
2022-01-27 00:21:22 +00:00
mov $STAGE2_ADDR(size), %bx
mov (%bx), %ax
2022-01-27 02:20:10 +00:00
call print_stage2_size
2022-01-26 23:16:14 +00:00
2022-01-27 00:47:11 +00:00
// DX - dividend high (always zero)
xor %dx, %dx
// AX - dividend low
mov $STAGE2_ADDR(size), %bx
mov (%bx), %ax
// BX - divisor (always 512)
mov $512, %bx
// AX - quotient
// DX - remainder
div %bx
test %dx, %dx
jz no_rem
inc %ax
no_rem:
2022-01-27 00:51:37 +00:00
2022-01-27 00:47:11 +00:00
push %ax
call print_number_of_sectors
2022-01-27 01:26:59 +00:00
pop %cx
2022-01-27 02:17:59 +00:00
2022-01-27 00:53:49 +00:00
mov $STAGE2_BASE, %bx
2022-01-27 02:17:59 +00:00
mov $1, %ax
2022-01-27 00:47:11 +00:00
2022-01-27 02:17:59 +00:00
// AX - current sector index
// BX - current address
2022-01-27 01:26:59 +00:00
// CX - total number of sectors
2022-01-27 00:51:37 +00:00
read_loop:
2022-01-27 02:17:59 +00:00
cmp %ax, %cx
je finish
2022-01-27 00:53:49 +00:00
add $512, %bx
2022-01-27 01:26:59 +00:00
inc %ax
2022-01-27 00:51:37 +00:00
push %ax
2022-01-27 00:53:49 +00:00
push %bx
2022-01-27 01:26:59 +00:00
push %cx
2022-01-27 01:59:21 +00:00
call print_reading_state
2022-01-27 01:26:59 +00:00
pop %cx
2022-01-27 00:57:11 +00:00
pop %bx
pop %ax
push %ax
push %bx
2022-01-27 01:26:59 +00:00
push %cx
2022-01-27 02:17:59 +00:00
add $1, %ax
2022-01-27 00:57:11 +00:00
mov %al, %cl // Cylinder and sector
mov $0x02, %ah
mov $1, %al // Sectors count
mov $0x80, %dl // Drive
mov $0, %ch // Cylinder
mov $0, %dh // Head
int $0x13
2022-01-27 01:26:59 +00:00
pop %cx
2022-01-27 00:53:49 +00:00
pop %bx
2022-01-27 00:51:37 +00:00
pop %ax
jmp read_loop
finish:
2022-01-26 23:03:47 +00:00
ljmp $0, $STAGE2_BASE
2022-01-26 20:19:39 +00:00
hang:
2022-01-26 20:19:39 +00:00
cli
hlt
jmp hang
2022-01-26 23:16:14 +00:00
do_ret:
ret
2022-01-27 00:26:54 +00:00
// AL - char
2022-01-26 23:16:14 +00:00
print_char:
2022-01-27 00:21:22 +00:00
mov $0x0e, %ah
2022-01-26 23:16:14 +00:00
mov $0x0001, %bx
int $0x10
2022-01-27 00:36:10 +00:00
ret
2022-01-26 23:16:14 +00:00
2022-01-27 00:26:54 +00:00
// SI - string pointer
2022-01-26 23:16:14 +00:00
print_str:
lodsb
test %al, %al
jz do_ret
call print_char
jmp print_str
2022-01-27 00:21:22 +00:00
// AX - number
print_number:
test %ax, %ax
jnz print_number_notnull
mov $0x0e, %ah
mov $'0', %al
int $0x10
ret
// AX - number
print_number_notnull:
test %ax, %ax
jz do_ret
// DX - dividend high (always zero)
// AX - dividend low
xor %dx, %dx
// BX - divisor (always 10)
mov $10, %bx
// AX - quotient
// DX - remainder
div %bx
push %dx
call print_number_notnull
pop %dx
mov $0x0e, %ah
mov %dl, %al
add $'0', %al
int $0x10
ret
2022-01-27 00:33:11 +00:00
// AX - size
2022-01-27 02:20:10 +00:00
print_stage2_size:
2022-01-27 00:33:11 +00:00
push %ax
2022-01-27 02:17:59 +00:00
mov $size_str, %si
2022-01-27 00:33:11 +00:00
call print_str
pop %ax
call print_number
2022-01-27 01:46:04 +00:00
mov $newline, %si
2022-01-27 00:33:11 +00:00
call print_str
ret
2022-01-27 00:47:11 +00:00
// AX - number of sectors
print_number_of_sectors:
push %ax
2022-01-27 01:46:04 +00:00
mov $number_of_sectors, %si
2022-01-27 00:47:11 +00:00
call print_str
pop %ax
call print_number
2022-01-27 01:46:04 +00:00
mov $newline, %si
2022-01-27 00:47:11 +00:00
call print_str
ret
2022-01-27 02:17:59 +00:00
// AX - current sector index
2022-01-27 01:59:21 +00:00
// BX - current address
2022-01-27 02:17:59 +00:00
// CX - total number of sectors
2022-01-27 01:59:21 +00:00
print_reading_state:
push %ax
push %bx
2022-01-27 02:17:59 +00:00
push %cx
2022-01-27 01:59:21 +00:00
mov $reading_sector, %si
call print_str
2022-01-27 02:17:59 +00:00
pop %cx
2022-01-27 01:59:21 +00:00
pop %bx
pop %ax
push %ax
push %bx
2022-01-27 02:17:59 +00:00
push %cx
2022-01-27 01:59:21 +00:00
2022-01-27 02:17:59 +00:00
inc %ax
call print_number
mov $parenth, %si
call print_str
pop %cx
pop %bx
pop %ax
push %ax
push %bx
push %cx
call print_number
mov $of, %si
call print_str
pop %cx
// pop %bx
// pop %ax
// push %ax
// push %bx
push %cx
mov %cx, %ax
2022-01-27 01:59:21 +00:00
call print_number
mov $to_address, %si
call print_str
2022-01-27 02:17:59 +00:00
pop %cx
2022-01-27 01:59:21 +00:00
pop %bx
// pop %ax
// push %ax
push %bx
2022-01-27 02:17:59 +00:00
push %cx
2022-01-27 01:59:21 +00:00
mov %bx, %ax
call print_number
mov $newline, %si
call print_str
2022-01-27 02:17:59 +00:00
pop %cx
2022-01-27 01:59:21 +00:00
pop %bx
pop %ax
ret