Page 1 of 1

Strange problem with SI register!

Posted: Sat Nov 01, 2008 1:35 pm
by Pitchu
Hi
i don't know what to do other than posting here to get some expert opinion after a week long try to make my simple real mode code work.
[Following code works fine both on qemu and when booted]

[ORG 0800H]
[BITS 16]

;Clear the screen
pusha
mov ax,03
int 10h
popa


;Setup new stack
mov ax,0800h
mov sp,ax

mov si,welcome
call print
jmp $

print:
pusha
;next 3 instructions show me the ascii value of LSbyte
;The value is same on qemu as when i boot it using grub
mov ax,si
mov ah,0eh
int 10h

.next
mov al,byte [si]
inc si
cmp al,0
je .exit
mov ah,0eh
int 10h
jmp .next
.exit
popa
ret
welcome db 'Second Stage Boot Loader...',13,10,0
-------------------------------------------------------------------------------------------------------------------
As my program size grows to 600 bytes it does not work like follows

[ORG 0800H]
[BITS 16]

;Clear the screen
pusha
mov ax,03
int 10h
popa


;Setup new stack
mov ax,0800h
mov sp,ax

mov si,welcome
call print
jmp $

print:
pusha
;next 3 instructions show me the ascii value of LSbyte
;The value is same on qemu as when i boot it using grub using rootnoverify(hd0,1) and other instructions
mov ax,si
mov ah,0eh
int 10h

.next
mov al,byte [si]
inc si
cmp al,0
je .exit
mov ah,0eh
int 10h
jmp .next
.exit
popa
ret

dump times 1000 db 'x'
welcome db 'Second Stage Boot Loader...',13,10,0

More precisely it shows the same ascii value on qemu as when booted but the string displayed when booted is
xxxxxxxxSecond Stage Boot Loader...
i mean 8x's followed by message.

what i am confused with is that if the least significant byte of SI is same as per ascii, why mov al,byte [si] loads x.
I used lodsb,cld,what not.
please help i am stuck.
thanks in advance

Re: Strange problem with SI register!

Posted: Sat Nov 01, 2008 4:33 pm
by bewing
You are certain that the DS segement register is set to 0x800? You are certain that the first stage bootloader loaded at least 2 sectors, and that they are correct?

Re: Strange problem with SI register!

Posted: Sat Nov 01, 2008 4:37 pm
by ~
bewing wrote:You are certain that the DS segement register is set to 0x800?....
Maybe DS should point, NOT to 0x800, but either to 0x00 (and SI to 0x800) or to 0x80?


What assembler are you using?

I remember having a problem with NASM where the GDT entries turned 6 bytes for no reason I know about. Then I used YASM and it was corrected. I also fixed the source code to use 4 dw's, instead of 2 dd's and that also fixed the problem with NASM.

If you work under Windows, I would recommend you to try to get HIEW.EXE or HIEW32.EXE to analyze your binary machine code files to make sure they are well formed and all instructions point to the data where they should.

Could you post your program to see if it's the case and discard this possibility at once?

Re: Strange problem with SI register!

Posted: Sat Nov 01, 2008 5:31 pm
by Love4Boobies
The origin doesn't fit in a byte (0800H) and Welcome is at 08xxH. You can't MOV AL,BYTE [SI] and expect it to work right, as AL is a byte.

Re: Strange problem with SI register!

Posted: Sat Nov 01, 2008 8:22 pm
by ~
Love4Boobies wrote:The origin doesn't fit in a byte (0800H) and Welcome is at 08xxH. You can't MOV AL,BYTE [SI] and expect it to work right, as AL is a byte.
What do you mean?

Isn't SI along with DS supposed to be given here the value of 800h+Welcome_String? So that instruction just puts the ASCII byte to be written into AL for writing it to the screen by the BIOS function later.

Pitchu, I insist that it would be very good if you could post your compiled binary, an image of your disk for us to boot it and testi t.

Re: Strange problem with SI register!

Posted: Sat Nov 01, 2008 9:33 pm
by Troy Martin
Above all that you could use lodsb to do the same as mov al, byte [SI]. Same thing, except it increments si on its own.

Methinks you should go to the wiki.

Re: Strange problem with SI register!

Posted: Sun Nov 02, 2008 1:32 am
by Brendan
Hi,
Pitchu wrote:As my program size grows to 600 bytes it does not work like follows
If it works when the size is less than 600 bytes (512 bytes?) but doesn't work when the size if larger than 600 bytes, then I'd be looking at the boot loader to make sure it loaded the second, third, fourth, etc sectors at the right spot; and then I'd be looking to see if anything could have moved some data incorrectly (e.g. "rep movsd" with ECX set wrong, where some data is moved correctly but data after this is moved when it shouldn't be).

It'd also be interesting to see if your 1000 'x' characters are correct, of if there's (for e.g.) 400 'x' characters, then 8 bytes of garbage, then the remaining 600 'x' characters; where the 8 bytes of garbage are inserted into the binary by something.

The segment registers aren't the problem - if the segment registers were wrong then you wouldn't get eight 'x' characters and then the string. For example, if DS was wrong by 1, then you might get 16 'x' characters, but not eight of them.


Cheers,

Brendan

Re: Strange problem with SI register!

Posted: Sun Nov 02, 2008 11:31 am
by Pitchu
Hi
Thanks a lot for all your replies it gave me real kick to do more research and after spending my whole Sunday on it I think I finally got it as it is now working! :D
Well I am using Nasm 0.90 and my DS is set correctly to 0x0 in First Stage and i used SI to traverse the rest of segment in Second stage. I am using it under windows and used Hex Workshop utility to check the operand value at the instruction
mov si,welcome and found that equal to the sum of 800h and the offset of 'Second Stage Bootloader... ' string. #-o
I manually edited that operand and incremented it by 8(BIN file) and it resulted in correct string while booting but not on qemu. :shock:
Well I planned to print the memory map of the code by the code to compare with the hex dump of bin file, but before doing so i reviewed my First stage bootloader in which i did PIO to read sectors. #-o
Here is the code:
when i was supposed to read SSBootloader it used to set count=BPB_SecPerClus to read whole cluster at a time and used to set di=800h
READSECTOR:
;Reads a Sector(if count=1) or a complete cluster(if count=BPB_SecPerClus) by its LBA address into DI offset
;<sector=LBA sector and DI=Offset are Inputs>
PUSHA
MOV AL,BYTE [sector+3]
OR AL,11100000b
MOV DX,0X1F6
OUT DX,AL

MOV AL,BYTE [sector+2]
MOV DX,0X1F5
OUT DX,AL

MOV AL,BYTE [sector+1]
MOV DX,0X1F4
OUT DX,AL

MOV AL,BYTE [sector]
MOV DX,0X1F3
OUT DX,AL

MOV AL,[count] ;One Sector at-a-time by default
MOV DX,0X1F2
OUT DX,AL

MOV AL,20H ;Read with Retry
MOV DX,0X1F7
OUT DX,AL

.TRANSFER:
IN AL,DX
TEST AL,8
JZ .TRANSFER

XOR AX,AX
MOV AL,BYTE [count]
MOV CX,256
MUL CX

MOV CX,AX
MOV DX,0X1F0
REP INSW
POPA
RET

It seemed to me as if sector boundaries are creating problems and hence changed my code to read only one sector at a time as follows with count=1: [-X
------------
MOV AL,1
MOV DX,0X1F2
OUT DX,AL

MOV CX,256

MOV DX,0X1F0
REP INSW
POPA
-------------
MOV DI,0800H
XOR CX,CX
MOV CL,BYTE [BPB_SecPerClus]

.NEXT
CALL READSECTOR
INC DWORD [sector]
ADD DI,200H
LOOP .NEXT
jmp 0000:0800h

To my surprise it worked on both bare machine and qemu. =D>
I further tested it to more size even upto whole cluster( I am making an assumption in First stage that SSBootloader will be under 1 cluster) and till now it is working fine.
Now atleast I can sleep BUT still the question remained there, what did i miss?i mean is not the controller able to load full cluster at a time?
Why was it adding 8 last bytes of previous sector to the new sector? I am not able to put my questions in proper words here but I know all u so gooooooooooooood people will get it?

Once more thanks a lot for all your replies!
Lots of love for all u there!