flat assembler
Message board for the users of flat assembler.

Index > OS Construction > convert nasm boot cde to fasm

Author
Thread Post new topic Reply to topic
mns



Joined: 20 Dec 2007
Posts: 118
Location: Piliyandala,Sri lanka
mns
can some one tell me how to convert this nasm code(boot loder) to fasm? I tried to convert and could get bin file.but when I write it to flopy it didn't load fat12 system. Embarassed


Description:
Download
Filename: dd.asm
Filesize: 8.16 KB
Downloaded: 51 Time(s)

Post 20 Apr 2009, 18:14
View user's profile Send private message Send e-mail Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41
you joined in 07 and didn't post until now!? Well, second why is this in the DOS section , I did find this here funny:

boot loader wrote:

and dx, 0000111111111111b

I see mainly hex like
Code:
    and     dx, FFFh
    

Laughing

_________________
Want hosting for free for your asm project? You can PM me. (*.fasm4u.net)
Post 20 Apr 2009, 18:34
View user's profile Send private message Visit poster's website Reply with quote
mns



Joined: 20 Dec 2007
Posts: 118
Location: Piliyandala,Sri lanka
mns
I was doing my internship couldn't find lessure time.
(for 2nd why)sorry it's by mistake Laughing
Post 20 Apr 2009, 18:46
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Code:
;*************************************************************************
ORG 0x7C00
jmp     START

OEM_ID                db "QUASI-OS"
BytesPerSector        dw 0x0200
SectorsPerCluster     db 0x01
ReservedSectors       dw 0x0001
TotalFATs             db 0x02
MaxRootEntries        dw 0x00E0
TotalSectorsSmall     dw 0x0B40
MediaDescriptor       db 0xF0
SectorsPerFAT         dw 0x0009
SectorsPerTrack       dw 0x0012
NumHeads              dw 0x0002
HiddenSectors         dd 0x00000000
TotalSectorsLarge     dd 0x00000000
DriveNumber           db 0x00
Flags                 db 0x00
Signature             db 0x29
VolumeID              dd 0xFFFFFFFF
VolumeLabel           db "QUASI  BOOT"
SystemID              db "FAT12"

START:
; code located at 0000:7C00, adjust segment registers
     cli
     xor     ax, ax
     mov     ds, ax
     mov     es, ax
     mov     fs, ax
     mov     gs, ax
; create stack
     mov     ss, ax
     mov     sp, 0x7C00
     sti
; post message
     mov     si, msgLoading
     call    DisplayMessage
LOAD_ROOT:
; compute size of root directory and store in "cx"
     xor     cx, cx
     xor     dx, dx
     mov     ax, 0x0020                          ; 32 byte directory entry
     mul     WORD [MaxRootEntries]               ; total size of directory
     div     WORD [BytesPerSector]               ; sectors used by directory
     xchg    ax, cx
; compute location of root directory and store in "ax"
     mov     al, BYTE [TotalFATs]                ; number of FATs
     mul     WORD [SectorsPerFAT]                ; sectors used by FATs
     add     ax, WORD [ReservedSectors]          ; adjust for bootsector
     mov     WORD [datasector], ax               ; base of root directory
     add     WORD [datasector], cx
; read root directory into memory (7C00:0200)
     mov     bx, 0x0200                          ; copy root dir above bootcode
     call    ReadSectors
; browse root directory for binary image
     mov     cx, WORD [MaxRootEntries]           ; load loop counter
     mov     di, 0x0200                          ; locate first root entry
.LOOP:
     push    cx
     mov     cx, 0x000B                          ; eleven character name
     mov     si, ImageName                       ; image name to find
     push    di
rep  cmpsb                                       ; test for entry match
     pop     di
     je      LOAD_FAT
     pop     cx
     add     di, 0x0020                          ; queuenext directory entry:
     loop    .LOOP
     jmp     FAILURE
LOAD_FAT:
; save starting cluster of boot image
     mov     si, msgCRLF
     call    DisplayMessage
     mov     dx, WORD [di + 0x001A]
     mov     WORD [cluster], dx                  ; file's first cluster
; compute size of FAT and store in "cx"
     xor     ax, ax
     mov     al, BYTE [TotalFATs]                ; number of FATs
     mul     WORD [SectorsPerFAT]                ; sectors used by FATs
     mov     cx, ax
; compute location of FAT and store in "ax"
     mov     ax, WORD [ReservedSectors]          ; adjust for bootsector
; read FAT into memory (7C00:0200)
     mov     bx, 0x0200                          ; copy FAT above bootcode
     call    ReadSectors
; read image file into memory (0050:0000)
     mov     si, msgCRLF
     call    DisplayMessage
     mov     ax, 0x0050
     mov     es, ax                              ; destination for image
     mov     bx, 0x0000                          ; destination for image
     push    bx
LOAD_IMAGE:
     mov     ax, WORD [cluster]                  ; cluster to read
     pop     bx                                  ; buffer to read into
     call    ClusterLBA                          ; convert cluster to LBA
     xor     cx, cx
     mov     cl, BYTE [SectorsPerCluster]        ; sectors to read
     call    ReadSectors
     push    bx
; compute next cluster
     mov     ax, WORD [cluster]                  ; identify current cluster
     mov     cx, ax                              ; copy current cluster
     mov     dx, ax                              ; copy current cluster
     shr     dx, 0x0001                          ; divide by two
     add     cx, dx                              ; sum for (3/2)
     mov     bx, 0x0200                          ; location of FAT in memory
     add     bx, cx                              ; index into FAT
     mov     dx, WORD [bx]                       ; read two bytes from FAT
     test    ax, 0x0001
     jnz     .ODD_CLUSTER
.EVEN_CLUSTER:
     and     dx, 0000111111111111b               ; take low twelve bits
    jmp     .DONE
.ODD_CLUSTER:
     shr     dx, 0x0004                          ; take high twelve bits
.DONE:
     mov     WORD [cluster], dx                  ; store new cluster
     cmp     dx, 0x0FF0                          ; test for end of file
     jb      LOAD_IMAGE
DONE:
     mov     si, msgCRLF
     call    DisplayMessage
     push    WORD 0x0050
     push    WORD 0x0000
     retf
FAILURE:
     mov     si, msgFailure
     call    DisplayMessage
     mov     ah, 0x00
     int     0x16                                ; await keypress
     int     0x19                                ; warm boot computer

;*************************************************************************
; PROCEDURE DisplayMessage
; display ASCIIZ string at "ds:si" via BIOS
;*************************************************************************
DisplayMessage:
     lodsb                                       ; load next character
     or      al, al                              ; test for NUL character
     jz      .DONE
     mov     ah, 0x0E                            ; BIOS teletype
     mov     bh, 0x00                            ; display page 0
     mov     bl, 0x07                            ; text attribute
     int     0x10                                ; invoke BIOS
     jmp     DisplayMessage
.DONE:
     ret

;*************************************************************************
; PROCEDURE ReadSectors
; reads "cx" sectors from disk starting at "ax" into memory location "es:bx"
;*************************************************************************
ReadSectors:
.MAIN:
     mov     di, 0x0005                          ; five retries for error
.SECTORLOOP:
     push    ax
     push    bx
     push    cx
     call    LBACHS
     mov     ah, 0x02                            ; BIOS read sector
     mov     al, 0x01                            ; read one sector
     mov     ch, BYTE [absoluteTrack]            ; track
     mov     cl, BYTE [absoluteSector]           ; sector
     mov     dh, BYTE [absoluteHead]             ; head
     mov     dl, BYTE [DriveNumber]              ; drive
     int     0x13                                ; invoke BIOS
     jnc     .SUCCESS                            ; test for read error
     xor     ax, ax                              ; BIOS reset disk
     int     0x13                                ; invoke BIOS
     dec     di                                  ; decrement error counter
     pop     cx
     pop     bx
     pop     ax
     jnz     .SECTORLOOP                         ; attempt to read again
     int     0x18
.SUCCESS:
     mov     si, msgProgress
     call    DisplayMessage
     pop     cx
     pop     bx
     pop     ax
     add     bx, WORD [BytesPerSector]           ; queue next buffer
     inc     ax                                  ; queue next sector
     loop    .MAIN                               ; read next sector
     ret

;*************************************************************************
; PROCEDURE ClusterLBA
; convert FAT cluster into LBA addressing scheme
; LBA = (cluster - 2) * sectors per cluster
;*************************************************************************
ClusterLBA:
     sub     ax, 0x0002                          ; zero base cluster number
     xor     cx, cx
     mov     cl, BYTE [SectorsPerCluster]        ; convert byte to word
     mul     cx
     add     ax, WORD [datasector]               ; base data sector
     ret

;*************************************************************************
; PROCEDURE LBACHS
; convert "ax2; LBA addressing scheme to CHS addressing scheme
; absolute sector = (logical sector / sectors per track) + 1
; absolute head   = (logical sector / sectors per track) MOD number of heads
; absolute track  = logical sector / (sectors per track * number of heads)
;*************************************************************************
LBACHS:
     xor     dx, dx                              ; prepare dx:ax for operation
     div     WORD [SectorsPerTrack]              ; calculate
     inc     dl                                  ; adjust for sector 0
     mov     BYTE [absoluteSector], dl
     xor     dx, dx                              ; prepare dx:ax for operation
     div     WORD [NumHeads]                     ; calculate
     mov     BYTE [absoluteHead], dl
     mov     BYTE [absoluteTrack], al
     ret

absoluteSector db 0x00
absoluteHead   db 0x00
absoluteTrack  db 0x00

datasector  dw 0x0000
cluster     dw 0x0000
ImageName   db "LOADER  BIN"
msgLoading  db 0x0D, 0x0A, "Loading Boot Image ", 0x0D, 0x0A, 0x00
msgCRLF     db 0x0D, 0x0A, 0x00
msgProgress db ".", 0x00
msgFailure  db 0x0D, 0x0A, "ERROR : Press Any Key to Reboot", 0x00


; Padding before boot signature
rb 512 - 2 + $$ - $

dw $AA55
;*************************************************************************          


BTW, are you sure nasm version was working? The modifications I did will hardly solve the problem you comment. I tried replacing the first sector of a formatted floppy image with the binary and also had the problem of unrecognizable format (Windows asked me if I want to format the disk).
Post 20 Apr 2009, 18:49
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 118
Location: Piliyandala,Sri lanka
mns
yes nasm one worked well
Post 20 Apr 2009, 18:59
View user's profile Send private message Send e-mail Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41
locodelassembly wrote:

(Windows asked me if I want to format the disk).

that's sorta messed up :/
Post 20 Apr 2009, 19:12
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
But is it really at the original state? For example, I had to correct some labels that started with dot but missed the double-dot at the end, nasm really compiles that?
Post 20 Apr 2009, 19:16
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41
Post 20 Apr 2009, 19:22
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Coddy41, but I don't see there a line with only ".aLabel" but ".aLabel db ..." which of course ":" is not needed there. The labels I was referring to were those you jump to (like ".SUCCESS:" that was ".SUCCESS").
Post 20 Apr 2009, 19:34
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 118
Location: Piliyandala,Sri lanka
mns
sorry for the delay I'm attaching the BIN file(in rar file) I've got after compiling the code with nasm.it is working very well when it copied to 1st boot sector of a floppy


Description:
Download
Filename: boot.rar
Filesize: 559 Bytes
Downloaded: 46 Time(s)

Post 21 Apr 2009, 13:34
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
AAAAHH!!! Now I know what was happening. Add a NOP after the "jmp START" instruction or change it to "jmp near START".

BTW, sorry for still asking but did the original nasm source included "jmp START" or perhaps it had some specifier (like "near" for fasm) to make it 3 bytes long? I find strange that nasm didn't optimized the instruction to use the shortest encoding Confused
Post 21 Apr 2009, 15:50
View user's profile Send private message Reply with quote
mns



Joined: 20 Dec 2007
Posts: 118
Location: Piliyandala,Sri lanka
mns
yes, in original nasm source there is "jmp START"and no specifier.(I'm reattaching the original source)
And your solution working just fine Very Happy .
many many thanks Very Happy Very Happy


by the way,
these are 2 sites to a beginner to os development(like me Laughing )and from where I'v got that source
www.mohanraj.info/josh.jsp
www.geocities.com/mvea/bootstrap.htm


Description:
Download
Filename: dd.asm
Filesize: 8.16 KB
Downloaded: 35 Time(s)



Last edited by mns on 22 Apr 2009, 15:31; edited 3 times in total
Post 21 Apr 2009, 17:21
View user's profile Send private message Send e-mail Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41
I found this great when I wanted to start OS coding, uses asm, C, and fasm I think Neutral maybe some nasm here and there. http://www.osdever.net/tutorials.php?cat=0
Post 21 Apr 2009, 18:47
View user's profile Send private message Visit poster's website Reply with quote
mns



Joined: 20 Dec 2007
Posts: 118
Location: Piliyandala,Sri lanka
mns
thanks! Very Happy
Post 22 Apr 2009, 15:20
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.