Page 1 of 1

Help debugging assembly code (Was: "Interesting problem")

Posted: Wed Aug 08, 2012 9:36 am
by lkurusa
Hello:)

In the following lines of code if I uncomment the lines, the program just hangs in bochs, no error message whatsoever....:/
The code is loaded via the bootloader to the address of 0x2000...
If the lines are commented, everything works fine as intended.
I just don't seem to understand why the emulator hangs when I uncomment any of those lines
Please help me, thanks in advance :)

Code: Select all

org 0x2000
entry:
mov si, msg
call putstr
mov byte [0x298], 0x01
jmp 0x1000
;mov bx, 0x0000    ; LINES FROM HERE 
;mov es, bx
;mov bx, 0x2300
;mov byte [0x0297], 4
;mov ax, 0 ; move code segment to ax
;mov ds, ax ; move ax to data segment
;mov es, ax ; move ax to extra segment
;mov		ah, 0					; reset floppy disk function
;mov		dl, 80h				; drive number
;int		0x13					; call BIOS
;jc		fail				

;mov byte cl, [0x297]

;mov		ah, 0x02				; read floppy sector function
;mov		al, 1					; read 1 sector
;mov		ch, 0					; track number
;;mov		cl, 2					; sector to read
;mov		dh, 0					; head number
;mov		dl, 80h					; drive number.
;int		0x13					; call BIOS - Read the sector
;;jc fail
hlt
putstr:
        lodsb           ; AL = [DS:SI]
        or al, al       ; Set zero flag if al=0
        jz putstrd      ; jump to putstrd if zero flag is set
        mov ah, 0x0e    ; video function 0Eh (print char)
        mov bx, 0x0007  ; color
        int 0x10
        jmp putstr
putstrd:
        retn
        
msg db 'Sector 3 loaded. Pure awesomenuzz!', 0
	
size    equ     $ - entry
%if size > 512
  %error "util > 512"
%endif
        times   (512 - size) db 0
Thanks again :)

Re: Interesting problem

Posted: Wed Aug 08, 2012 10:22 am
by GAT
It looks like you are messing with the segment registers without saving/restoring them. And possibly setting the wrong drive number. (Hint - first floppy is drive 0 IIRC)
Try uncommenting it a few lines at a tine til you hit a problem.

Re: Interesting problem

Posted: Wed Aug 08, 2012 10:27 am
by lkurusa
I'm reading from the HDD so I assume that 80H is alright.
If I just uncomment a line it just hangs just like this would:

Code: Select all

hang:
 jmp hang

Re: Interesting problem

Posted: Wed Aug 08, 2012 11:00 am
by GAT
Try this:

Code: Select all

;mov bx, 0x0000    ; LINES FROM HERE
push es
push ds
;mov es, bx
;mov bx, 0x2300
;mov byte [0x0297], 4
;mov ax, 0 ; move code segment to ax
;mov ds, ax ; move ax to data segment
;mov es, ax ; move ax to extra segment
;mov      ah, 0               ; reset floppy disk function
;mov      dl, 80h            ; drive number
;int      0x13               ; call BIOS
pop ds
pop es
;jc      fail            

;mov byte cl, [0x297]

mov bx, 0x0000
push es
push ds
mov es, bx
mov bx, 0x2300
;mov      ah, 0x02            ; read floppy sector function
;mov      al, 1               ; read 1 sector
;mov      ch, 0               ; track number
;;mov      cl, 2               ; sector to read
;mov      dh, 0               ; head number
;mov      dl, 80h               ; drive number.
;int      0x13               ; call BIOS - Read the sector
pop ds
pop es
;;jc fail
Also, what is with the jmp 0x1000 near the top of the code?

Re: Interesting problem

Posted: Wed Aug 08, 2012 11:09 am
by lkurusa
Still hangs, 0x1000 is an another sector which is properly loaded and executed.
I have absolutely no idea why from a single piece of code it just hangs, not even printing the strings...
Probably NASM optimizing too much..

Re: Interesting problem

Posted: Wed Aug 08, 2012 11:29 am
by GAT
no, nasm don't optimize.
Where is the fail that you jc to after int 13h?

Re: Interesting problem

Posted: Wed Aug 08, 2012 11:37 am
by Congdm
You jump to 1000h but how did you know the exact address to return?
And what is the purpose of hlt before putstr?

How about using Bochs debugger if you can not find the bug?

Re: Help debugging assembly code (Was: "Interesting problem"

Posted: Wed Aug 08, 2012 11:54 am
by lkurusa
Fail is just a simple call to putstr, so I know if the load sector sets an error.
This sector's job is to load a filesystem (later), and then pass control back to RAM adress 1000h, so I know the return adress.