Help in writing to a sector

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
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Help in writing to a sector

Post by Zenith »

Why does the following code fail to display the character A?

What am I missing?

Any help would be appreciated

Code: Select all

main:


	mov	ah, 03h				; write function. 
	mov	al, 1  				; # of sectors to write = 1

	mov	cl, 4   			; sector = 4
	mov	ch, 0   			; cylinder = 0
	mov	dh, 0   			; head = 0

	mov 	bx,[Data]
	mov	es,bx
	mov	bx,0

	int	13h



	mov	ah, 02h				; read function. 
	mov	al, 1  				; # of sectors to read = 1

	mov	cl, 4   			; sector = 4
	mov	ch, 0   			; cylinder = 0
	mov	dh, 0   			; head = 0

	mov 	bx,508h
	mov 	es,bx
	mov 	bx,0

	int	13h

	mov	al,35h
	cmp	al,[es:bx]
	je	equals

hang:
	jmp hang

equals:
	mov ah,0Eh
	mov al,'A'
	int 10h
	jmp hang


Data db 35h
Adan
Posts: 5
Joined: Fri Apr 13, 2007 8:37 pm

Post by Adan »

mov bx,[Data]
I think it should be:

Code: Select all

  mov bx, Data 
Watch out also your segment selector.

Good luck.
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Post by Zenith »

Sorry, but it doesn't seem to work.
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Actually, ES is a segment register. It must be loaded with the address /16... so i advise changing it to.

Code: Select all

main: 


   mov   ah, 03h            ; write function. 
   mov   al, 1              ; # of sectors to write = 1 

   mov   cl, 4            ; sector = 4 
   mov   ch, 0            ; cylinder = 0 
   mov   dh, 0            ; head = 0 

   xor   es,es ;Clears ES 
   mov   bx, Data

   int   13h 



   mov   ah, 02h            ; read function. 
   mov   al, 1              ; # of sectors to read = 1 

   mov   cl, 4            ; sector = 4 
   mov   ch, 0            ; cylinder = 0 
   mov   dh, 0            ; head = 0 

   mov    bx,508h ;Where were you getting this from?
   mov    es,bx  ;It should work anyway
   mov    bx,0

   int   13h 

   mov   al,35h 
   cmp   al,byte [es:bx] 
   je   equals 

hang: 
   jmp hang 

equals: 
   mov ah,0Eh 
   mov al,'A' 
   int 10h 
   jmp hang 


Data db 35h 
Ztane
Posts: 9
Joined: Sun Jan 14, 2007 12:43 pm
Location: Oulu, Finland

Post by Ztane »

Tyler wrote:

Code: Select all

   xor   es,es ;Clears ES 
wtf?

Anyhow, you have to:

Code: Select all

  ; load ES with your DS selector register:
  mov bx, ds
  mov es, bx

  ; load the offset of Data:
  mov bx, Data
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post by Tyler »

Ztane wrote:
Tyler wrote:

Code: Select all

   xor   es,es ;Clears ES 
wtf?

Anyhow, you have to:

Code: Select all

  ; load ES with your DS selector register:
  mov bx, ds
  mov es, bx

  ; load the offset of Data:
  mov bx, Data
Given his own code, he is clearly doing this from system base, probably in a boot loader given the forum, and therefore the segments will be set to 0, XORing a register with itself is the fastest way todo this. If my assumptions are wrong he has a lot to learn about programming and assembly before he starts playing with bios interupts. Your comment was far from productive, next time you have a quarrel with my code, please attempt to articulate it.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post by inflater »

Code: Select all

xor   es,es ;Clears ES 
You can't access any segment register like that. ;)
You need to

Code: Select all

XOR AX,AX
MOV ES,AX
if you want to compile it.

In a bootloader or any simple kernel program that is in 16-bit real mode, it is better to have CS=DS=ES to prevent any problems with data. (IF the bootloader isn't .EXE, of course :D)

inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Post by Zenith »

Thank you all so much.

I finally got it to work!!! :D

P.S: Inflator is right. You can't write directly to a segment register.
Post Reply