# Mixing 32 bit calculation in 16 bit program NASM



## dumb_terminal (Dec 28, 2010)

Hello all, i am new here and this is my first post. I am a n00b in this case, and really don't know how to do it. so it would be great if u kindly show me the right way to do it.
For instance the following instructions provides wrong result, how to make it right ??

```
;; just to remind you [BITS 16]
AND EAX, 0x0000FFFF
SUB EAX, 2
XOR ECX, ECX
MOV CL, [SectorsPerCluster]
MUL ECX
ADD EAX, DWORD [DataAreaStart]
ADD EAX, [LBAStart]
;MOV EAX, 969434
MOV [DAP + 8], EAX
CALL ReadSectorsLBA_
```
My code is right coz the result should be 969434 and if i put it directly it gives desired result. But the formula isn't wrong either, plz help me understand mixing up 32 Bit calculation in 16 bit program. Regards and thanx in advance.


----------



## graudeejs (Dec 28, 2010)

How do you expect anyone to answer without telling what are you trying to calculate, and giving examples of input and output data of what you expect and what you actually get?

BTW: This is probably wrong forum (should ask on some asm forum instead of FreeBSD forum)
And it's defiantly wrong section


----------



## dumb_terminal (Dec 28, 2010)

umm sorry, i am trying to get the starting LBA of a cluster.
The formula i am using is 
starting_LBA = offset_from_partition_table + data_area_start + (cluster_no - 2) * sector_per_cluster

in my code:
offset_from_partition_table = LBAStart
data_area_start = DataAreaStart
cluster_no = AX
sector_per_cluseter = SectorsPerCluster

and in my code DAP is the DiskAddressPacket for LBA read (int 0x13 ah = 0x42).
and plz don't suggest using 32 bit code, as i have to have to do it in 16 bit code.
And the EAX comes in play as the file i am trying to find may be situated long distance from start (big LBA). Plz help. And Thanx for replying.


----------



## dumb_terminal (Dec 28, 2010)

And yeah LBAStart is a DWORD value and so is the sector number field of DAP, so i have no choice but resort to EAX.
P.S. : I am not sure, which value i am getting as i am starting and restarting my pc each time to test instead of VMS. But its certainly not 969434 (where the kernel is). Elemeneting the top half of EAX i get is 346, (printed in binary form).


----------



## graudeejs (Dec 28, 2010)

You run it on read PC?
I have suggestion for you:
1) User virtual machine
2) user qmeu

qemu has building debugger, so once you master it (I haven't used qemu debugger), you can actually see what is going on with your code.
Qemu downside is low speed, so installing Vista would take forever

Also for which os are you doing this (not important, but still curious)


----------



## dumb_terminal (Dec 28, 2010)

Yeah i could do that but the point is i don't understand how the mixing up of 32 bit register with 16 bit code is done actually (heard of prefixing with 0x66 in case of address 0x67 etc but no real sample to learn from). which OS, (under win xp sp2 the development is going on), and my OS is nothing but a desire to learn. I dont use VMS because i don't know how to configure it to boot from USB.  regards and plz help regarding the actual topic sir.


----------



## graudeejs (Dec 28, 2010)

The problem with your code is still, many unknown variables.... 
You really need to use debugger


----------



## dumb_terminal (Dec 28, 2010)

Yeah i get your point. But I came here step by step, all other values are tested by printing integer, yeah i could afford that coz then the bootloader size was really small. But yeah, i guess i do need a debugger. Thanx for ur help . But still u could have tought me how to mix 32 bit calculation within 16 bit program though. Regards and thanx.


----------



## graudeejs (Dec 28, 2010)

http://flatassembler.net/examples/phboot.zip << FAT12 bootloader in fasm

I haven't been writing 16bit code for 5 years
As far as I remember, there were no limitations, as long as CPU supports 32 bit reg....
The only difference is memory space

(I might be wrong... like I said, I haven't coded 16bits for long time)


----------



## dumb_terminal (Dec 28, 2010)

umm thanx, but i am on fat16, but you tried to help. Thanx again.


----------



## DutchDaemon (Dec 28, 2010)

If no one can come up with any relevance to the FreeBSD operating system I will delete this topic.


----------

