Page 1 of 1

String printing routine broken?

Posted: Mon Mar 26, 2012 6:05 pm
by Ziddia
Hello,

Sorry if the title's a bit vague. I've recently been struggling with the iso9660 file system, in an attempt to make a second stage bootloader. However, I recently (read: an hour ago) got it to read and execute the second stage.

It seems to work fine - when I try to print a single character to the screen ('H' for hello) it works. However, when I try to use the same string printing routine I've been using since I started working on my hobby OS, it either prints a collection of 'smiley faces' or doesn't print anything at all. This doesn't make sense to me, since I don't think I've changed anything to break the method.

For reference, I compile with NASM, create my ISOs with MagicISO, and use VirtualBox for my virtual machine.

Any help is appreciated.

Bootloader:

Code: Select all

BITS 16 ; 16-bit mode
ORG 0x7C00 ; Bootloader loads at 0x7C00 in memory

%include "iso9660fs.inc" ; Filesystem data

JMP start ; Jumps to main method

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Bootloader entry point.                                      ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start:
	MOV SI, msg_booting
	CALL print
	CLI ; CLears Interrupts
	XOR AX, AX ; Reset AX
	MOV DS, AX ; Set DS to 0Ah (AX default?)
	MOV SP, AX ; Same with SP
	MOV AX, 9000h ; Set AX to 9000h
	MOV SS, AX ; Setting our stack
	MOV AX, 1000h ; Set AX to 1000h
	MOV ES, AX ; Setting a different segment
	STI ; STarts Interrupts
	MOV BYTE[bootdrv], DL ; Our bootdrive
	CALL load_vol_desc ; Loads the volume data
	TEST AX, AX ; Test AX
	JZ .err_read ; Jumps to error code if AX == 0
	CALL load_root_dir ; Loads root directory of CD
	TEST AX, AX ; Test AX
	JZ .err_read ; Jumps to error code if AX == 0
	CALL load_loader ; Loads the loader image
	TEST AX, AX ; Test AX
	JZ .err_no_loader ; Jumps to error code if AX == 0
	MOV DL, BYTE[bootdrv] ; Restores our bootdrive
	MOV SI, msg_jumping ; Sets jumping msg to print
	CALL print ; Prints message.
	JMP 1000h:0000h ; Jumps to the loaded boot image

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Called if the image file couldn't be found.                  ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.err_no_loader:
	MOV SI, msg_no_loader ; Set up to print error
	CALL print ; Prints the error
	JMP hang ; Hangs the system

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Called if an error occured while reading the disc.           ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.err_read:
	MOV SI, msg_read ; Set up to print error
	CALL print ; Prints the error
	JMP hang ; Hangs the system

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the volume's data, in preparation for image loading.   ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_vol_desc:

.loop:
	MOV SI, dap ; Loads disc data from dap function
	MOV AH, 42h ; Size of the currently looped file
	MOV DL, [bootdrv] ; Bootdrive loaded
	INT 13h ; Interrupt for LBA

	JC .end_err ; Error if carry flag is set by INT 13h

	CMP BYTE[ES:0],1 ; Primary descriptor?
	JE .end ; If yes, end the search
	CMP BYTE[ES:0], 0ffh ; Last descriptor?
	JE .end_err ; If yes, call an error.

	INC DWORD[dap.lba] ; Grabs the next LBA to check.
	JMP .loop ; Try again!


.end:
	MOV AX, 1 ; Ensures that AX checks will fail
	RET ; Return

.end_err:
	XOR AX, AX ; Resets AX (checks will succeed)
	RET ; Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the root dir. Uses the data in iso9660fs.inc for this. ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_root_dir:
	MOV SI, iso9660vol_desc.root_dir_rec ; Loads root data
	MOV EAX, DWORD[ES:SI+iso9660dir_desc.loc_data]
	MOV ECX, DWORD[ES:SI+iso9660dir_desc.data_len] ; Loads root dir length
	SHR ECX, 11 ; Length of root dir / 2048 to get block # (right bit shift) TODO
	ADD CX, 1 ; Not entirely sure what's happening here. TODO
	CALL load_lba ; Gets the LBA for the root block

.end:
	RET ; Return on ending (hopefully with AX == 1)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the LBA for the root dir.                              ;
; Note: EAX is the LBA we are on.                              ;
; CX is the number of LBAs to load for the root directory.     ;
; Defined in load_root_dir.                                    ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_lba:
	PUSHA ; Save AH register
	MOV [dap.blocks], CX ; Saves the number of blocks to load.
	MOV [dap.lba], EAX ; Saves the LBA we will load.
	MOV SI, dap ; Loads the data
	MOV AH, 42h ; Size of the currently looped block/lba
	MOV DL, [bootdrv] ; Creates boot drive data
	INT 13h ; Interrupt, we now have the LBA.
	JC .end_err ; Error if carry flag is set

.end:
	POPA ; Restores AH register
	MOV AX, 1 ; FUQ DA ERRORS
	RET ; Return

.end_err:
	POPA ; Restores AH register
	XOR AX, AX ; CONFOUND THESE BUGS
	RET ; Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Loads the image file.                                        ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
load_loader:
	XOR SI, SI ; Restore SI
	SHL ECX, 11 ; Gets size of root dir

.loop_search:
	CMP BYTE[ES:SI], 0 ; Checks if is root dir entry
	JE .end_err ; If not, error.
	CMP SI, CX ; Checks if SI == root dir size
	JE .end_err ; If yes, error.
	MOV DI, SI ; Saves DI
	ADD DI, iso9660dir_desc.file_ident ; Points DI to file name/address
	PUSH CX ; Save CX
	PUSH SI ; Save SI
	MOVZX CX, BYTE[ES:SI+iso9660dir_desc.len_file_ident] ; Size of file ID
	MOV SI, os_img ; Load our image name
	REP CMPSB ; Compares string bytes until CX == 0
	MOV AL, [SI] ; If strings equal, AL == 0
	POP SI ; Restore SI
	POP CX ; Restore CX
	TEST AL, AL ; Tests if AL is 0
	JZ .load ; If so, load the file
	MOVZX AX, BYTE[ES:SI+iso9660dir_desc.len_dir_rec] ; Size of directory
	ADD SI, AX ; Make SI point to next entry
	JMP .loop_search ; Search again

.load:
	MOV EAX, DWORD[ES:SI+iso9660dir_desc.loc_data] ; LBA of file
	MOV ECX, DWORD[ES:SI+iso9660dir_desc.data_len] ; Length of file
	SHR ECX, 11 ; Switch back to 16-bit
	INC CX ; Increment in case we miss a block
	CALL load_lba ; Reload all LBAs

.end:
	RET ; File loaded into memory - return

.end_err:
	XOR AX, AX ; Null AX
	RET ; Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; DAP stuff.                                                   ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
align 4 ; 4 bits

dap:
	.size DB 10h ; Disc size I believe
	.reserved DB 0 ; Reserved sectors/blocks
	.blocks DW 1 ; Number of blocks
	.offset DW 0 ; Offset at beginning of search
	.segment DW 1000h ; Segments in disc
	.lba DD 10h, 0 ; Number of LBAs

bootdrv DB 0 ; Our drive number

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Stage1 subroutines.                                          ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                                      

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Prints a string to the screen.                               ;
; Takes the value of SI to do this.                            ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
print:			; Routine: output string in SI to screen
	PUSHA ; Saves value of AH
	MOV AH, 0Eh		; int 10h 'print char' function

.repeat:
	LODSB			; Get character from string
	CMP AL, 0
	JE .done		; If char is zero, end of string
	INT 10h			; Otherwise, print it
	JMP .repeat

.done:
	POPA ; Loads AH back into memory
	RET

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Hangs the system.                                            ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hang:
 CLI
 HLT

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Stage1 declarations.                                         ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
msg_booting DB 'ZidOS is booting...',13,10,0
msg_jumping DB 'Preparing to jump to Stage2.',13,10,0
msg_no_loader DB 'Could not find the Stage2 image.',13,10,0
msg_read DB 'Could not read the disc.',13,10,0
os_img DB 'BOOTTWO.BIN',0

TIMES 510-($-$$) DB 0
DW 0AA55h
Stage 2:

Code: Select all

[BITS 16]       ; We need 16-bit intructions for Real mode

mov si, msg_hello
call print
jmp hang

hang:
        jmp hang        ; Loop, self-jump

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Prints a string to the screen.                               ;
; Takes the value of SI to do this.                            ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
print:			; Routine: output string in SI to screen
	MOV AH, 0Eh		; int 10h 'print char' function

.repeat:
	LODSB			; Get character from string
	CMP AL, 0
	JE .done		; If char is zero, end of string
	INT 10h			; Otherwise, print it
	JMP .repeat

.done:
	RET


msg_hello db 'Stage Two',0
iso9660fs.inc, for reference:

Code: Select all

struc iso9660vol_desc
	.reserved1						resb 8
	.sys_ident						resb 32
	.vol_ident						resb 32
	.reserved2						resb 8
	.total_sect						resb 8
	.reserved3						resb 32
	.vol_set_size					resb 4
	.vol_seq_nmbr					resb 4
	.sect_size						resb 4
	.path_table_size				resb 8
	.1st_sect_little_1st_table		resb 4
	.1st_sect_little_2nd_table		resb 4
	.1st_sect_big_1st_table			resb 4
	.1st_sect_big_2nd_table			resb 4
	.root_dir_rec					resb 34
	.vol_set_ident					resb 128
	.publish_ident					resb 128
	.data_preparer_ident			resb 128
	.app_ident						resb 128
	.copyright_file_ident			resb 37
	.abstract_file_ident			resb 37
	.bibliographical_file_ident		resb 37
	.date_time_vol_create			resb 17
	.date_time_mod					resb 17
	.date_time_vol_expires			resb 17
	.date_time_vol_effective		resb 17
	.reserved4						resb 2
	.reserved_app					resb 512
	.reserved5						resb 653
endstruc

struc iso9660dir_desc
	.len_dir_rec					resb 1
	.ext_attr_rec_len				resb 1
	.loc_data						resb 8
	.data_len						resb 8
	.date_time						resb 7
	.file_flags						resb 1
	.file_unit_size					resb 1
	.interleave_gap_size			resb 1
	.vol_seq_nmbr					resb 4
	.len_file_ident					resb 1
	.file_ident						resb 0
endstruc
(Apologies if any formatting of comments is lost on the forums, and for the potential size)

Thankyou to anyone who can help!

Re: String printing routine broken?

Posted: Mon Mar 26, 2012 6:07 pm
by VolTeK
Try-

Code: Select all

push cs
pop ds
Assuming the segments to your loaded file are set correctly.


Before every print, it will prove your data segment is out of order.

Check to see that you are setting your kernel you are loading to its correct segments as well, especially DS.

Re: String printing routine broken?

Posted: Mon Mar 26, 2012 6:10 pm
by Ziddia
VolTeK wrote:Try-

Code: Select all

push cs
pop ds
Assuming the segments to your loaded file are set correctly.


Before every print, it will prove your data segment is out of order.

Check to see that you are setting your kernel you are loading to its correct segments as well, especially DS.
Okidoke, I'll have a go at that.

Re: String printing routine broken?

Posted: Mon Mar 26, 2012 6:15 pm
by VolTeK
Make sure your segments are correct, i would say do a push cs pop ds at the beginning of your kernel file, assuming the org is automatically set to 0x100.

Re: String printing routine broken?

Posted: Mon Mar 26, 2012 6:17 pm
by Ziddia
VolTeK wrote:Try-

Code: Select all

push cs
pop ds
Assuming the segments to your loaded file are set correctly.


Before every print, it will prove your data segment is out of order.

Check to see that you are setting your kernel you are loading to its correct segments as well, especially DS.
Thankyou very much! Seems that they were out of order. Everything's working again now (I did have to mess with one thing, but it fixed.)

Re: String printing routine broken?

Posted: Mon Mar 26, 2012 6:18 pm
by VolTeK
Ziddia wrote:Thankyou very much! Seems that they were out of order. Everything's working again now (I did have to mess with one thing, but it fixed.)
No problem

Re: String printing routine broken?

Posted: Tue Mar 27, 2012 3:04 pm
by Ziddia
VolTeK wrote:
Ziddia wrote:Thankyou very much! Seems that they were out of order. Everything's working again now (I did have to mess with one thing, but it fixed.)
No problem
I've encountered another problem. I've managed to get to my second stage file. It enables the A20 line, loads the GDT, and sets the bit on cr0 to enter Protected Mode. However, a small piece of code I used to write to the screen in Protected Mode in a previous attempt is no longer working. Since it depends on DS, I think that maybe my segments are a little messed up. I'm not sure where I tripped up, though. I've Pastebinned the code for my Stage2, because it's a bit longer than it was.

http://pastebin.com/JDKeAndS

I'm not sure if it's a problem with the registers or with the code. Any help would be appreciated.

EDIT: The code I was using to print got cut out, this was at the end of clear_pipe:

mov byte [ds:0B8000h], 'P'
mov byte [ds:0B8001h], 1Bh

Re: String printing routine broken?

Posted: Tue Mar 27, 2012 4:47 pm
by VolTeK
bits 32 as the first thing at the jump to clearpipe. I do not do much protected mode programming, if at all. I would guess that's what your missing.

Re: String printing routine broken?

Posted: Tue Mar 27, 2012 4:53 pm
by Ziddia
VolTeK wrote:bits 32 as the first thing at the jump to clearpipe. I do not do much protected mode programming, if at all. I would guess that's what your missing.
Doesn't seem to have worked. I might start a new thread, since this one's deviated from its original purpose.