Page 1 of 1

Buffer doesn't return the right value to [ebp+16]

Posted: Tue Feb 23, 2016 6:23 am
by JakTheFifth
I'm writing in the third sector an integer value 104. I debugged my kernel and when I'm writing esi the last parameter of my writedisk function, has the right value, however when I'm reading my parameter isn't receiving the right values, eax is receiving "random" values, printing random characters each time I compile my OS. I saved [ebp+16] in the memory with the register eax and passed the values inside my buffer to eax.
Why is it returning me "random" values?

Code: Select all

_readsector:
 mov ebx, ecx
 mov eax, [ebp+12] ;second parameter
 cmp eax, 0
 je _readsector_fail

 push eax ;sector number
 mov dx, 0x1f2
 mov al, cl
 out dx, al
 inc dx ;dx = 0x1f3
 pop eax
 out dx, al
 inc dx ;dx = 0x1f4
 shr eax, 8
 out dx, al
 inc dx ;dx = 0x1f5
 shr eax, 8
 out dx, al
 inc dx ;dx = 0x1f6
 shr eax, 8
 and al, 00001111b
 or al, 01000000b
 out dx, al
 inc dx ;dx = 0x1f7
 mov al, 0x20
 out dx, al

 mov ecx, 4
_readsector_wait:
 in al, dx
 test al, 0x80 ; BSY
 jne _readsector_retry
 
 test al, 0x08 ; DRQ
 jne _readsector_ready

_readsector_retry:
 dec ecx
 jg _readsector_wait

_readsector_nextsector:
 in al, dx
 test al, 0x80
 jne _readsector_nextsector

 test al, 0x21
 jne _readsector_fail

_readsector_ready:
 sub dx, 7 ;0x1f0
 mov ecx, 256
 mov edi, buffer
 rep insw

(...)
push eax
 mov eax, [ebp+16]
_readdisk_eax:
 mov [eax], long word buffer
 pop eax

(...)
buffer: times 512 db ('V')

Re: Buffer doesn't return the right value to [ebp+16]

Posted: Tue Feb 23, 2016 8:27 am
by Combuster
While you have REP INSW write to the bytes at label buffer directly from the disk controller, the code you provided doesn't actually read those bytes.

Re: Buffer doesn't return the right value to [ebp+16]

Posted: Tue Feb 23, 2016 9:23 am
by JakTheFifth
Combuster wrote:While you have REP INSW write to the bytes at label buffer directly from the disk controller, the code you provided doesn't actually read those bytes.
I updated the original post's code

Re: Buffer doesn't return the right value to [ebp+16]

Posted: Tue Feb 23, 2016 1:56 pm
by Combuster
That's just added bloat - the likely error I was looking at still exists. The contents of variable buffer is still never read in this code. You're doing something else with it, look carefully.

Re: Buffer doesn't return the right value to [ebp+16]

Posted: Tue Feb 23, 2016 6:01 pm
by ~
You should pass a parameter value to [ebp+16] BEFORE calling _readsector, otherwise that parameter will simply NOT exist in the stack and you will effectively read whatever value it was from any other kernel call.

Code: Select all

push eax
mov eax, [ebp+16]
_readdisk_eax:
mov [eax], long word buffer
pop eax

It looks like you want [ebp+16] to contain the end buffer the user wants to place the sector data at. If that's the case, you probably need to use that parameter before the rep insw instruction since it will be needing ES:EDI to find the buffer:

Code: Select all

;NOTE: Remember to push some value for [b][ebp+16][/b] BEFORE
;      calling the following routine:
;;
_readsector_ready:
sub dx, 7 ;0x1f0
mov ecx, 256
mov edi, [ebp+16]    ;...Make sure that [ebp+16] contains exactly the end buffer address
rep insw                   ;...Using ES:EDI, ES:DI or ES:RDI for the final buffer data


Re: Buffer doesn't return the right value to [ebp+16]

Posted: Wed Feb 24, 2016 7:50 am
by JakTheFifth
~ wrote:You should pass a parameter value to [ebp+16] BEFORE calling _readsector, otherwise that parameter will simply NOT exist in the stack and you will effectively read whatever value it was from any other kernel call.

Code: Select all

push eax
mov eax, [ebp+16]
_readdisk_eax:
mov [eax], long word buffer
pop eax

It looks like you want [ebp+16] to contain the end buffer the user wants to place the sector data at. If that's the case, you probably need to use that parameter before the rep insw instruction since it will be needing ES:EDI to find the buffer:

Code: Select all

;NOTE: Remember to push some value for [b][ebp+16][/b] BEFORE
;      calling the following routine:
;;
_readsector_ready:
sub dx, 7 ;0x1f0
mov ecx, 256
mov edi, [ebp+16]    ;...Make sure that [ebp+16] contains exactly the end buffer address
rep insw                   ;...Using ES:EDI, ES:DI or ES:RDI for the final buffer data

Doesn't moving [ebp+16] directly to edi corrupt data?

Re: Buffer doesn't return the right value to [ebp+16]

Posted: Wed Feb 24, 2016 10:32 am
by Octocontrabass
JakTheFifth wrote:Doesn't moving [ebp+16] directly to edi corrupt data?
What's the difference between edi and [edi]?