From 7fe9b061ba7556ca9617cd061d16e2bc8e040f63 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sat, 14 Apr 2018 20:33:57 +0300 Subject: [PATCH] cleanup and getchs --- src/boot/boot.s | 170 ++++++++++++++++++++++++++++++------------------------ src/boot/boot2.s | 59 +++++++++++++++---- 2 files changed, 142 insertions(+), 87 deletions(-) diff --git a/src/boot/boot.s b/src/boot/boot.s index b9e1a76..2202800 100644 --- a/src/boot/boot.s +++ b/src/boot/boot.s @@ -1,7 +1,8 @@ .code16 .section .boot,"a" - .set scratchbuf, 0x7b00 + .set scratchbuf, 0x7bf0 + .set scratchbuf_size, 16 boot: cli @@ -9,7 +10,7 @@ boot: # move stack to just below the code xor %ax, %ax mov %ax, %ss - mov $0x7b00, %sp + mov $0x7bf0, %sp # use the code segment for data access mov %cs, %ax mov %ax, %ds @@ -19,8 +20,15 @@ boot: call setup_serial + mov $loading_msg, %si + call print_str + # load the second stage boot loader and jump to it mov $_boot2_size, %eax + call print_num + mov $loading_msg2, %si + call print_str + mov %eax, %ebx shr $9, %eax and $0x1ff, %ebx @@ -42,6 +50,37 @@ boot: .set ARG_NSECT, 6 .set ARG_SIDX, 4 +loading_msg: .asciz "Loading " +loading_msg2: .asciz " bytes\n" + +sect_per_track: .short 18 +num_cylinders: .short 80 +num_heads: .short 2 +heads_mask: .byte 1 + +get_drive_chs: + movb drive_number, %dl + mov $8, %ah + int $0x13 + jnc .Lok + ret + +.Lok: mov %ch, %al + mov %cl, %ah + rol $2, %ah + and $0x3ff, %ax + mov %ax, num_cylinders + + mov %cx, sect_per_track + andw $0x3f, sect_per_track + + shr $8, %dx + mov %dx, num_heads + dec %dl + mov %dl, heads_mask + + ret + # read_sectors(first, num) read_sectors: push %bp @@ -63,7 +102,6 @@ read_sectors: ret .set VAR_ATTEMPTS, -2 -#str_rdsec_msg: .asciz "rdsec: " # read_sector(sidx) read_sector: @@ -78,23 +116,28 @@ read_sector: .Lread_try: # calculate the track (sidx / sectors_per_track) mov 4(%bp), %ax -# mov $str_rdsec_msg, %si -# call print_str_num16 xor %dx, %dx - mov $SECT_PER_TRACK, %cx + mov sect_per_track, %cx div %cx mov %ax, %cx - # save the remainder in ax - mov %dx, %ax + # save the remainder + push %dx # head in dh mov %cl, %dh - and $1, %dh - # cylinder (track/2) in ch [0-7] and cl[6,7]<-[8,9] - rol $7, %cx + and heads_mask, %dh + # cylinder (track/heads) in ch [0-7] and cl[6,7]<-[8,9] + push %dx + xor %dx, %dx + movw num_heads, %cx + div %cx + pop %dx + mov %ax, %cx + rol $8, %cx ror $2, %cl and $0xc0, %cl - # sector num cl[0-5] is sidx % sectors_per_track (saved in ax) + # sector num cl[0-5] is sidx % sectors_per_track + 1 + pop %ax inc %al or %al, %cl @@ -118,10 +161,8 @@ read_sector: jmp abort_read .Lread_ok: - # DBG print first dword -# mov $str_read_error + 4, %si -# mov %es:(%bx), %eax -# call print_str_num + mov $46, %ax + call print_char # increment es:bx accordingly (advance es if bx overflows) add $512, %bx @@ -136,23 +177,19 @@ read_sector: pop %bp ret -str_read_error: .asciz "err read sect: " +str_read_error: .asciz "read error, sector: " abort_read: mov $str_read_error, %si call print_str_num16 hlt -cursor_x: .byte 0 - # prints a string (ds:si) followed by a number (eax) print_str_num: push %eax call print_str pop %eax call print_num - mov $13, %al - call ser_putchar mov $10, %al call ser_putchar ret @@ -166,74 +203,58 @@ print_str_num16: # expects string pointer in ds:si print_str: - push %es pusha +0: mov (%si), %al + cmp $0, %al + jz .Lend + call print_char + inc %si + jmp 0b + +.Lend: popa + ret + + # expects character in al +print_char: + push %es pushw $0xb800 pop %es - xor %di, %di - movb $0, cursor_x - - push %si + movw cursor_x, %di + shl $1, %di -0: mov (%si), %al mov %al, %es:(%di) movb $7, %es:1(%di) - inc %si - add $2, %di - incb cursor_x - cmp $0, %al - jnz 0b - - pop %si - call ser_putstr + incw cursor_x - popa + call ser_putchar pop %es ret +cursor_x: .short 0 + # expects number in eax .global print_num print_num: # save registers - push %es pusha - xor %cx, %cx - movw $scratchbuf, %si + movw $scratchbuf + scratchbuf_size, %si + movb $0, (%si) mov $10, %ebx - -0: xor %edx, %edx +.Lconvloop: + xor %edx, %edx div %ebx add $48, %dl + dec %si mov %dl, (%si) - inc %si - inc %cx cmp $0, %eax - jnz 0b + jnz .Lconvloop - # print the backwards string - pushw $0xb800 - pop %es - movb cursor_x, %al - xor %ah, %ah - shl $1, %ax - mov %ax, %di - -0: dec %si - mov (%si), %al - movb %al, %es:(%di) - call ser_putchar - inc %di - mov $7, %al - movb %al, %es:(%di) - inc %di - dec %cx - jnz 0b + call print_str # restore regs popa - pop %es ret .set UART_DATA, 0x3f8 @@ -288,28 +309,25 @@ setup_serial: ser_putchar: push %dx - mov %al, %ah + cmp $10, %al + jnz 0f + push %ax + mov $13, %al + call ser_putchar + pop %ax + +0: mov %al, %ah # wait until the transmit register is empty mov $UART_LSTAT, %dx -0: in %dx, %al +.Lwait: in %dx, %al and $LST_TREG_EMPTY, %al - jz 0b + jz .Lwait mov $UART_DATA, %dx mov %ah, %al out %al, %dx pop %dx ret - - # expects a string in ds:si -ser_putstr: - mov (%si), %al - cmp $0, %al - jz 0f - call ser_putchar - inc %si - jmp ser_putstr -0: ret drive_number: .byte 0 diff --git a/src/boot/boot2.s b/src/boot/boot2.s index 86ee611..838af46 100644 --- a/src/boot/boot2.s +++ b/src/boot/boot2.s @@ -2,7 +2,6 @@ .code16 .section .boot2,"a" - .extern print_num .extern ser_putchar mov $0x13, %ax @@ -16,8 +15,8 @@ mov $0x3c8, %dx movb %cl, %al outb %al, %dx - #DBG - call print_num + #### DBG + call ser_print_num mov $58, %al call ser_putchar mov $32, %al @@ -30,8 +29,8 @@ inc %si shr $2, %al outb %al, %dx - #DBG - call print_num + #### DBG + call ser_print_num mov $32, %al call ser_putchar xor %eax, %eax @@ -41,8 +40,8 @@ inc %si shr $2, %al outb %al, %dx - #DBG - call print_num + #### DBG + call ser_print_num mov $32, %al call ser_putchar xor %eax, %eax @@ -52,12 +51,10 @@ inc %si shr $2, %al outb %al, %dx - #DBG - call print_num + #### DBG + call ser_print_num mov $32, %al call ser_putchar - mov $13, %al - call ser_putchar mov $10, %al call ser_putchar xor %eax, %eax @@ -79,6 +76,46 @@ cli hlt + # expects string pointer in ds:si +ser_print_str: + pusha + +0: mov (%si), %al + cmp $0, %al + jz .Lend + call ser_putchar + inc %si + jmp 0b + +.Lend: popa + ret + + + # expects number in eax +ser_print_num: + # save registers + pusha + + movw $numbuf + 16, %si + movb $0, (%si) + mov $10, %ebx +.Lconvloop: + xor %edx, %edx + div %ebx + add $48, %dl + dec %si + mov %dl, (%si) + cmp $0, %eax + jnz .Lconvloop + + call ser_print_str + + # restore regs + popa + ret + +numbuf: .space 16 + logo_pal: .incbin "logo.pal" -- 1.7.10.4