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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
JakTheFifth
Posts: 4
Joined: Tue Sep 15, 2015 1:19 pm

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

Post 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')
Last edited by JakTheFifth on Tue Feb 23, 2016 9:22 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
JakTheFifth
Posts: 4
Joined: Tue Sep 15, 2015 1:19 pm

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

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

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

Post 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

JakTheFifth
Posts: 4
Joined: Tue Sep 15, 2015 1:19 pm

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

Post 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?
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

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

Post by Octocontrabass »

JakTheFifth wrote:Doesn't moving [ebp+16] directly to edi corrupt data?
What's the difference between edi and [edi]?
Post Reply