Page 1 of 1

Register Question

Posted: Tue Dec 23, 2008 6:04 pm
by renovatio
I wrote this function to get the value of a cluster from fat. All you need is to put the number of cluster in AX. After calling the function, the value is stored in AX. But for example if the cluster number is 1 (zero based = 2nd) and the value at that position is 2, the result is: ah is 2 and al is
16. Shouldn't AX be only 2 (AH = 0, AL = 2).

Code: Select all

getcluster: mov [.byte], ax
	    mov dx, 0
	    div word [.two]
	    add [.byte], ax
	    mov bx, fat
	    add bx, [.byte]
	    mov ax, [bx] 

	    cmp dx, 0
	    je .odd
	    and ax, 0000111111111111b
	    ret
.odd:	    shr ax, 4
	    ret
.byte:   dw 0
.two:	    dw 2

fat: db 00000000b, 00010000b
     db 00000010b, 00000000b
     db 00110000b, 00000100b
Lots of thanks :)

Re: Register Question

Posted: Wed Dec 24, 2008 6:26 am
by CodeCat
Let's see what happens when you set ax = 1 beforehand:

Code: Select all

                                (ax = 1)

.byte = ax                      (.byte = 1)
dx = 0                          (dx = 0)
ax = dx:ax / 2; dx = dx:ax % 2  (ax = 0, dx = 1)
.byte += ax                     (.byte = 1)
bx = fat                        (bx = fat)
bx += .byte                     (bx = fat + 1)
ax = [bx]                       (ax = [fat + 1] = 00000010 00010000 b)

compare dx, 0                   (not equal)
jump to .odd if equal           (don't jump)
ax = ax & 00001111 11111111 b   (ax = 00000010 00010000 b)
return
See the problem yet? I do. ;)

Re: Register Question

Posted: Wed Dec 24, 2008 6:30 am
by hailstorm
Well, you should be able to understand and follow your code... Apparently, you are unable to do that. That is a shame! [-X Just kiddin' :mrgreen:
Let's see what your code does:

Code: Select all

getcluster: 
       mov [.byte], ax    ; Should ax be 1, .byte contains 1.
       mov dx, 0          ; Clear dx for division
       div word [.two]    ; Divide ax by 2; ax contains 0, dx contains 1.
       add [.byte], ax    ; .byte remains 1
       mov bx, fat        ; Put offset of fat table in bx
       add bx, [.byte]    ; Add 1 to bx, since that's the value of .byte
       mov ax, [bx]       ; ax gets the value of [fat + 1] 
; which means: 00010000b is placed in al, 00000010b is placed in ah.
; AX is equal to 0x0210
       cmp dx, 0          ; dx does not equal zero
       je .odd            ; So this jump does not occur
       and ax, 0000111111111111b  ; 0x0210 & 0x0fff = 0x0210 (And there you have your answer.)
       ret
.odd:       shr ax, 4
       ret
.byte:   dw 0
.two:       dw 2

fat: db 00000000b, 00010000b
     db 00000010b, 00000000b
     db 00110000b, 00000100b
Good luck with figuring out what to do about this problem... I have the feeling that your fat table is incorrectly defined...

Re: Register Question

Posted: Wed Dec 24, 2008 12:06 pm
by renovatio
Thanks for your replies. Are these definitions the same?

Code: Select all

fat1: db 00000000b, 00010000b
     db 00000010b, 00000000b
     db 00110000b, 00000100b

fat2: dw 0000000000010000b
     dw 0000001000000000b
     dw 0011000000000100b

Re: Register Question

Posted: Wed Dec 24, 2008 1:09 pm
by ru2aqare
renovatio wrote:Thanks for your replies. Are these definitions the same?
No, they are not the same. The latter one has its bytes reversed due to the x86 architecture being little-endian (least significant bits go first).

Code: Select all

fat1: db 00000000b, 00010000b
     db 00000010b, 00000000b
     db 00110000b, 00000100b

fat2: dw 0001000000000000b
     dw 0000000000000010b
     dw 0000010000110000b
These are the same.