Bona Fide OS Developer
View unanswered posts | View active topics It is currently Mon Jun 18, 2018 12:24 pm



Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
 Mixing 32 bit calculation in 16 bit program NASM 
Author Message

Joined: Tue Dec 28, 2010 2:17 pm
Posts: 11
Post Mixing 32 bit calculation in 16 bit program NASM
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 ??
Code:
;; 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_

Rest of my code is right coz the result (EAX after those instruction) 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.
The formula i am using is
LBA_addr = offset_from_partition_table + DataAreaStart + (cluster_no - 2) * sectors_per_cluster
In my code
offset_from_partition_table = LBAStart
DataAreaStart = DataAreaStart
cluster_no = AX
sectors_per_clusetr = SectorsPerCluster
Regards and thanx in advance.


Tue Dec 28, 2010 2:22 pm
Profile

Joined: Fri Aug 20, 2010 10:04 pm
Posts: 41
Post Re: Mixing 32 bit calculation in 16 bit program NASM
Did you calculate by hand? Are your inputs right? Why didn't you tell us the result of your code's calculations?

Laziness is the main problem. Go write a routine to display a hex value and call it, or use a debugger. What is the value of EAX? How could you not report that?


My guess is your inputs are not right.

Come to think of it... how do you expect to get such a big number after AND 0xFFFF?

Sectors per cluster is somewhere between 1 and 32.

65535*32=2,000,000

You didn't tell us your input values.

You probably listened to the retarded advice at http://www.osdev.org and are doing FAT16? Do FAT32. the reason I thought your number was too big is you probably want a block near the start of the partition, in all likelyhood... and SPC==32 is unusual. It's usually smaller.


Go write a routine to display a hex value. 0xB8000 is the start of screen text memory.

Think how easy this would be if you weren't lazy and wrote such a routine and called it after each step in the process of your calculations. I am truely astounded at how lazy all the people on this site and www.osdev.org are. Imagine you are being paid and your job depends on it and failure is not an option. Then, you won't be so lazy.


Tue Dec 28, 2010 3:14 pm
Profile

Joined: Tue Dec 28, 2010 2:17 pm
Posts: 11
Post Re: Mixing 32 bit calculation in 16 bit program NASM
Sorry for late reply sir, actually i am not that lazy. i wrote 4 routines to display values (of course they won't fit in 512 bytes), but none of them giving correct when it comes to this part. LBAStart, NoOfRootDirSectors, RootStart, DataAreaStart upto this all is well, then comes this weird problem, as my sector per cluster is 64. I couldn't provide actual data because i tried in so much way i lost track. and i am not using any VMs so starting and restarting is a tedious work. I recorded two values (for which process i don't remember). And my other purpose was to learn mixing 16 bit and 32 bit. i heard 0x66 was prefixied when 32 in 16 and in case of address 0x67, but how it is done and what actually prefixing is i don't know, and google wasn't much help. so i requested for help.
Any way here is what i have done so far, i think u can correct my problems : (yes i have spent past three days with this problem starting and restarting my pc to boot from a usb).
I agree with u sir we should work as if our life depends on it.
Code:

;COMMENT @
;   Boot parameter block Template
;   Author: dumb_terminal
;       boot from 4 gb transcend 2 partition each 2 GB
; nasm -o boot -f bin kernel.asm
;@

[CPU 686]
[BITS 16]
[ORG 0x7C00]



JMP SHORT OS_BOOT_
NOP

OEM               db "Dumb OS "
BytesPerSector    dw 512
SectorsPerCluster db 64
ReservedSectors   dw 1
NumberOfFAT       db 2
RootEntries       dw 512
SectorsUnder32    dw 0
MediaDescriptor   db 0xF8
SectorsPerFAT     dw 189
SectorsPerTrack   dw 63
NumberOfHeads     dw 255
HiddenSectors     dd 0
SectorsOver32     dd 3084417
BIOSDrive         db 0
Unused            db 0
ExBootSignature   db 0x29
VolumeSerial      db 0xF2, 0x6C, 0xCB, 0x50
VolumeLabel       db "Dumb OS    "
FileSystem        db "FAT16   "



OS_BOOT_:
   ;setting up segments for the whole purpose
   CLI
    ; bios places the current drive in DL
   MOV [BIOSDrive], DL
   MOV AX, CS
   MOV DS, AX
   MOV AX, 0x9000
   MOV SS, AX
   MOV SP, 0xFFFE
   STI
   
   ; Disk Address Packet is already been setup for reading MBR
   ; copy MBR to 0x1000:0000 our temp segmet
   CALL ReadSectorsLBA_

   ; Find FAT16 Partition
   MOV AX, [DAP + 6]
   MOV ES, AX
   MOV DI, 446
   MOV CX, 4

   CHECK_FAT16_:
      MOV AL, 0x06
      CMP AL, [ES:DI+4]
      JE FOUND_PARTITION_
      MOV AL, 0x0E
      CMP AL, [ES:DI+4]
      JE FOUND_PARTITION_
      ADD DI, 0x10
   LOOP CHECK_FAT16_
   
   MOV SI, errNoPart
   CALL PrintString
   
   FOUND_PARTITION_:
      MOV EAX, [ES:DI+8]
      MOV [LBAStart], EAX
   
   ; Find RootStart
   MOV AX, [SectorsPerFAT]
   MOV BL, [NumberOfFAT]
   MUL BL
   ADD AX, [ReservedSectors]
   MOV [RootStart], AX
   
   ; Find NoOfRootSector
   MOV AX, [RootEntries]
   SHL AX, 5
   MOV CX, [BytesPerSector]
   ADD AX, CX
   DEC AX
   XOR DX, DX
   DIV CX
   MOV [NoOfRootSector], AX
   
   ; Find Start of Data Area
   MOV AX, [RootStart]
   ADD AX, [NoOfRootSector]
   MOV [DataAreaStart], AX
   
        ; search root directory to obtain the begining cluster of second stage file
   MOV EDX, [LBAStart]
   ADD EDX, [RootStart]
   AND EDX, 0x0000FFFF     ; no prob here as it doesn't overflow, but not a standard way
   MOV [DAP + 8], EDX
   
   MOV AX, [NoOfRootSector]
   SEARCH_ROOT_:
      XOR DI, DI
      MOV BX, [BytesPerSector]
      CALL ReadSectorsLBA_
      SEARCH_SECTOR_:
         MOV CX, 11
         MOV SI, strKernel
         REPE CMPSB
         JE KERNEL_FOUND_
         ADD DI, CX
         ADD DI, 21
         SUB BX, 32
      JNZ SEARCH_SECTOR_
      DEC AX
      JZ KERNEL_NOT_FOUND_
      MOV EDX, [DAP + 8]
      INC EDX
      MOV [DAP + 8], EDX
   JMP SEARCH_ROOT_
      
   KERNEL_NOT_FOUND_:   
      MOV SI, strNotFound
      CALL PrintString
      MOV SI, strCRLF
      CALL PrintString
      JMP KEY_
      
   KERNEL_FOUND_:
      MOV SI, strFound
      CALL PrintString
      MOV SI, strCRLF
      CALL PrintString
      
      ; obtain first cluster of kernel
      MOV AX, [ES:DI+15];; i don't know why maybe due to an error of I/O it gave 0 once

      SUB AX, 2
      XOR DX, DX
      MOV BX, [SectorsPerCluster]
      MUL BX
      MOV [ClusterLBA], AX
      MOV [ClusterLBA + 2], DX
      MOV EAX, [ClusterLBA]
      ADD EAX, [LBAStart]
      ADD EAX, [DataAreaStart]
      ; EAX should hold the first cluster
       MOV DX, AX
       CALL OutputHex16_
       SHR EAX, 16
       MOV DX, AX
       CALL OutputHex16_
      ; our kernel will be located at 0x5000:0000
      MOV BX, 0x5000
      MOV ES, BX
      
      ; preparing the DAP for kernel transfer
      MOV [DAP + 6], BX ; transfer segment
      XOR BX, BX
      MOV BL, [SectorsPerCluster]
      MOV [DAP + 2], BX
      
      READ_CLUSTER_:
         ;MOV EAX, 969434 its the right value
         MOV [DAP + 8], EAX
         CALL ReadSectorsLBA_
      
      ;jmp 0x5000:0000   
   KEY_:
      MOV AH, 0x00
      INT 0x16
   REBOOT_OS_:
      JMP 0xFFFF:0000
      
      
   ; READ DISK IN LBA MODE
   ; INPUT EDX - SECTOR NO
   ; Reads a sector and puts it in 0x1000:0000
   ReadSectorsLBA_:
      PUSH AX
      PUSH DX
      PUSH SI
      
      MOV AX, 0x4200
      MOV SI, DAP
      MOV DL, [BIOSDrive]
      INT 0x13
      POP SI
      POP DX
      POP AX
      RET


   PrintCharacter:
      PUSH AX
      PUSH BX
      PUSH CX
      PUSH DX
      MOV AH, 0x0E   
      MOV BH, 0x00   
      MOV BL, 0x07
      INT 0x10
      POP DX
      POP CX
      POP BX
      POP AX   
      RET      
   PrintString:   
   next_character:   
      MOV AL, [SI]   
      INC SI      
      OR AL, AL   
      JZ exit_function
      CALL PrintCharacter
      JMP next_character   
   exit_function:   
      RET
   
   ; OUTPUT A 16 BIT NO IN HEX FORM
   ; INPUT DX - THE NUMBER
   OutputHex16_:
      PUSH AX
      PUSH BX
      PUSH CX
      PUSH DX
      MOV CX, 4
      OutputHex16_Shift_:
         MOV AL, DH
         SHL DX, 4
         SHR AL, 4
         ADD AL, 0x30
         CMP AL, 0x39
         JLE OutputHex16_Print_
         SUB AL, 0x30
         ADD AL, 0x37
         OutputHex16_Print_
         CALL PrintCharacter   
      LOOP OutputHex16_Shift_
      POP DX
      POP CX
      POP BX
      POP AX      
      RET


   errNoPart db 'NF16', 0
   strCRLF db 0x0D, 0x0A, 0
   strFound db 'Found', 0
   strNotFound db 'Found', 0
   
        DAP db 0x10
         db 0
         dw 1
         dw 0x0000
         dw 0x1000
              dd 0, 0
       
        RootStart dw 0
   NoOfRootSector dw 0
   DataAreaStart dw 0
   LBAStart dd 0
   strKernel db 'KERNEL  BIN'
   ClusterLBA dw 0, 0
   
        TIMES 510 - ($ - $$) db 0   
   DW 0xAA55         


Plz help a poor kid who is willing to learn but got stuck midway,
Regards.


Wed Dec 29, 2010 11:01 am
Profile

Joined: Fri Aug 20, 2010 10:04 pm
Posts: 41
Post Re: Mixing 32 bit calculation in 16 bit program NASM
The 66h and 67h prefixes should be automatically handled by your assembler. In a 16-bit section, it puts a 66h prefix on a 32-bit operand sized instruction or 67h on 32-bit addressing. In a 32-bit section, 66h cause 16-bit size operand.

Relax, 32-bit instructions in 16-bit sections are not unusual and work just fine. Uncertainty is one of the things which slow you down, isn't it? I can tell you not to be uncertain. You might generate a listing file and look for 66h or 67h prefixes, or look at the binary.


This should put BL on the screen.
Code:
    MOV AX,0B800h
    MOV ES,AX
    MOV BH,07h ; background black, forground grey
    MOV WORD ES:[0],BX
   


Here is a screen code chart. It's not very good. I googled it. Down below is 0x20-0xFF. Go search for a better one with 0x00-0x1f or make your own.:
http://www.google.com/imgres?imgurl=http://www.efg2.com/Lab/Library/Delphi/Strings/ScreenShowFont.jpg&imgrefurl=http://www.efg2.com/Lab/Library/Delphi/Strings/index.html&usg=__WqzrtQciJ8vGYwT5DxZC2tnEa0s=&h=399&w=377&sz=56&hl=en&start=0&zoom=1&tbnid=FTFiXcnbDwOwtM:&tbnh=141&tbnw=133&prev=/images%3Fq%3Dscreen%2Bcode%2Bchart%26um%3D1%26hl%3Den%26biw%3D1386%26bih%3D837%26tbs%3Disch:1&um=1&itbs=1&iact=hc&vpx=247&vpy=166&dur=281&hovh=231&hovw=218&tx=148&ty=115&ei=8ZAbTZa0EpGasAOlqqmaCg&oei=8ZAbTZa0EpGasAOlqqmaCg&esq=1&page=1&ndsp=26&ved=1t:429,r:1,s:0


I happen to be able to fetch the LBA address of my kernel file and can patch my boot loader with that value. My bootloader does not hassle with file system details. You probably don't know the LBA address of your second stage. A disadvantage of my technique is having to update my boot block if the file changes places. I have a script.


Here is my boot loader.
http://www.losethos.com/code/BootHD.html#l89

You can use it, no restrictions, but that's probably not fun.


Wed Dec 29, 2010 2:52 pm
Profile

Joined: Tue Dec 28, 2010 2:17 pm
Posts: 11
Post Re: Mixing 32 bit calculation in 16 bit program NASM
@losethos: Sorry for late reply sir, and sorry for late reply, thanx for the bootloader code (haven't tried yet but sounds like a jump patching like technique) but i am not going to use it for now, u r right it spoils the fun. But later on sure to have a look at the technique. So what u r saying is i don't know my kernel's LBA, hmm thats the part u are dead right. :? But working on it, will let you know. thanx again sir.
Regards.


Wed Dec 29, 2010 8:28 pm
Profile

Joined: Fri Aug 20, 2010 10:04 pm
Posts: 41
Post Re: Mixing 32 bit calculation in 16 bit program NASM
The odd jump is because my assembler doesn't support that instruction -- I wrote my own assembler and didn't do all the 16-bit stuff. The "patching" involves modifying the bootloader's binary output file by changing just the LBA of the kernel and the size of the kernel with another program.


Technically, it patches it in memory
here: http://www.losethos.com/code/InsBoot.html#l184


Thu Dec 30, 2010 8:18 am
Profile

Joined: Fri Aug 20, 2010 10:04 pm
Posts: 41
Post Re: Mixing 32 bit calculation in 16 bit program NASM
Don't listen to http://www.osdev.org. They sabatauge people with retarded advice. What the hell are you doing FAT16 for!!?

This is absolute lunacy http://forum.osdev.org/viewtopic.php?f=1&t=22926. Fear and misinformation and uncertainty will sabatauge you.

Why the hell are they talking about 486 computers and floppy drives! That is insanity! Design for 64-bit mode or better computers and don't waste time on obsolete hardware!


Thu Dec 30, 2010 4:42 pm
Profile

Joined: Tue Dec 28, 2010 2:17 pm
Posts: 11
Post Re: Mixing 32 bit calculation in 16 bit program NASM
losethos wrote:
Don't listen to http://www.osdev.org. They sabatauge people with retarded advice. What the hell are you doing FAT16 for!!?

This is absolute lunacy http://forum.osdev.org/viewtopic.php?f=1&t=22926. Fear and misinformation and uncertainty will sabatauge you.

Why the hell are they talking about 486 computers and floppy drives! That is insanity! Design for 64-bit mode or better computers and don't waste time on obsolete hardware!


well actually its a bit of experimental project (and academic too they said to do fat 16 and taught 8086 so let be it), and who says it for 486 and floppy drives ?? i wish to boot from an USB drive. But in near future i am sure to go beyond the academic barrier, i am keen to learn and OS is one of my core interests, but am still a n00b in implementing. Anyway thanx for your concern. Regards.


Thu Dec 30, 2010 4:59 pm
Profile

Joined: Fri Aug 20, 2010 10:04 pm
Posts: 41
Post Re: Mixing 32 bit calculation in 16 bit program NASM
Sorry, I was mostly ranting at OSDev. Did you read this http://forum.osdev.org/viewtopic.php?f=1&t=22926 (They banned me for talking sense.)

As for USB drives... unless you're going to limit yourself to 16-bit real mode, you don't know what you're in for. (If you change out of real mode, you have to do USB by yourself, a huge task.)

If you are doing just real mode (16-bit) and using the BIOS, I'd see if I could put FAT32 on it, so you can also use the code for the hard drive.


Look at this: http://forum.osdev.org/viewtopic.php?f=1&t=22932 CHS is insanity -- use LBA. CHS is ancient and limited in size. Sabatauge.


Thu Dec 30, 2010 5:09 pm
Profile

Joined: Tue Dec 28, 2010 2:17 pm
Posts: 11
Post Re: Mixing 32 bit calculation in 16 bit program NASM
losethos wrote:
Sorry, I was mostly ranting at OSDev. Did you read this http://forum.osdev.org/viewtopic.php?f=1&t=22926 (They banned me for talking sense.)

Well everyone has a personal opininion, so why ban peple when it comes to techincal talks ?? And why be sorry sir ?? u shouldn't.
losethos wrote:
As for USB drives... unless you're going to limit yourself to 16-bit real mode, you don't know what you're in for. (If you change out of real mode, you have to do USB by yourself, a huge task.)

If you are doing just real mode (16-bit) and using the BIOS, I'd see if I could put FAT32 on it, so you can also use the code for the hard drive.

Real mode is the plan for now.
losethos wrote:
Look at this: http://forum.osdev.org/viewtopic.php?f=1&t=22932 CHS is insanity -- use LBA. CHS is ancient and limited in size. Sabatauge.

LBA is the present (and maybe future) so why bother with CHS, setting up a DAP is not hard :shock:
Thanx for those advice sir and Regards.
P.S.: i have visited your http://www.losethos.com/ by the looks of it you have put a hell of a mind work.


Thu Dec 30, 2010 6:31 pm
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.