Page 1 of 1

Help in writing to a sector

Posted: Sun Apr 15, 2007 7:56 pm
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

Posted: Sun Apr 15, 2007 8:34 pm
by Adan
mov bx,[Data]
I think it should be:

Code: Select all

  mov bx, Data 
Watch out also your segment selector.

Good luck.

Posted: Tue Apr 17, 2007 9:03 pm
by Zenith
Sorry, but it doesn't seem to work.

Posted: Tue Apr 17, 2007 9:23 pm
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 

Posted: Tue Apr 17, 2007 11:45 pm
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

Posted: Wed Apr 18, 2007 8:56 am
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.

Posted: Wed Apr 18, 2007 10:26 am
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

Posted: Thu Apr 19, 2007 9:32 pm
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.