flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > OS Construction > Converting IBM PC BIOS to FASM syntax

Author
Thread Post new topic Reply to topic
nkeck72



Joined: 28 May 2015
Posts: 78
Location: 0000:7C00
Converting IBM PC BIOS to FASM syntax
Hi all, I am happy to say that I have successfully found the source code for the original IBM PC BIOS code at https://sites.google.com/site/pcdosretro/ibmpcbios. The source code was written in another assembler's syntax (Looks to me like NASM or MASM) and wish to convert it to a more understandable FASM syntax for two reasons:

1.) I wish to understand more about how the BIOS worked and how it tested all the different hardware and

2.) I wish to see if it compiles, and if it does I want to see how their source code was written to get all the data structures in their exact places.

I also want to convert this to FASM syntax as I am starting a BIOS project and want to see what structure I should generally follow. However, the syntax is obviously from another assembler and I cannot seem to make any sense of it. I need help converting this code to a format FASM can understand, seeing as I will be using FASM to construct my project. I will definitely need help with this, and will set up a GitHub page for those who wish to contribute.

In the mean time, I will post up the source code in the old syntax. The one thing that has been giving me the most trouble are the "* SEGMENT AT *" statements, which FASM doesn't seem to have support for in anything but MZ format.

Cheers!


Description: The original IBM PC BIOS source code.
Download
Filename: PCBIOS.ASM
Filesize: 175.27 KB
Downloaded: 40 Time(s)


_________________
It may look hard, but it won't take long if you take it one byte at a time.

NOS: www.github.com/nkeck720/nos


Code:

org 100h
mov ah09h
mov dxmessage
int 21h
int 20h

message db "Hello FASM world!"0Dh0Ah"$"


Post 12 Dec 2015, 21:43
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14672
Location: Origae-6
IIRC "segment at" is merely an alias to tell the assembler that the CS/DS/ES/FS/GS/SS register should be that value when referring to labels within the segment.
Post 13 Dec 2015, 04:41
View user's profile Send private message Visit poster's website Reply with quote
nkeck72



Joined: 28 May 2015
Posts: 78
Location: 0000:7C00
So what of this:

Code:

ABS0 SEGMENT AT 0




How would this translate? And what register is it setting?
Post 13 Dec 2015, 04:43
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14672
Location: Origae-6
No register is being set. The label ABS0 is allocated to address 0:0
Post 13 Dec 2015, 04:52
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6303
Location: Kraków, Poland
To convert this to fasm syntax you'd have to drop all the ASSUME statement, because fasm does not track segment registers (the "flat" in its name originally referred to exactly that). Unfortunately that means you then have to be aware of how the label you use relates to the contents of segment registers at given moment - because of that writing segment code in NASM/fasm syntax is a bit harder.

And this also means that in the conversion the SEGMENT AT syntax becomes meaningless. You can rewrite this part:

Code:
ABS0    SEGMENT AT 0
STG_LOC0        LABEL   BYTE
        ORG     2*4
NMI_PTR         LABEL   WORD
        ORG     5*4
INT5_PTR        LABEL   WORD
        ORG     8*4
INT_ADDR        LABEL   WORD
INT_PTR         LABEL   DWORD
        ORG     10H*4
VIDEO_INT       LABEL   WORD
        ORG     1DH*4
PARM_PTR        LABEL   DWORD           ; POINTER TO VIDEO PARMS
        ORG     01EH*4                  ; INTERRUPT 1EH
DISK_POINTER    LABEL   DWORD
        ORG     01FH*4                  ; LOCATION OF POINTER
EXT_PTR LABEL   DWORD           ; POINTER TO EXTENSION
        ORG     7C00H
BOOT_LOCN       LABEL   FAR
ABS0    ENDS

this way:

Code:
; Note: these labels refer to offsets in segment 0
LABEL   STG_LOC0        BYTE AT 0
LABEL   NMI_PTR         WORD AT 2*4
LABEL   INT5_PTR        WORD AT 5*4
LABEL   INT_ADDR        WORD AT 8*4
LABEL   INT_PTR         DWORD AT 8*4
LABEL   VIDEO_INT       WORD AT 10H*4
LABEL   PARM_PTR        DWORD AT 1DH*4
LABEL   DISK_POINTER    DWORD AT 01EH*4
LABEL   EXT_PTR         DWORD AT 01FH*4
LABEL   BOOT_LOCN       AT 7C00H


And then this one:

Code:
DATA    SEGMENT AT 40H
RS232_BASE      DW      4 DUP(?)        ; ADDRESSES OF RS232 ADAPTERS
PRINTER_BASE    DW      4 DUP(?)        ; ADDRESSES OF PRINTERS
EQUIP_FLAG      DW      ?               ; INSTALLED HARDWARE
MFG_TST         DB      ?  
; ...
DATA    ENDS

like this:

Code:
VIRTUAL AT 0
; Note: these labels refer to offsets in segment 40h
RS232_BASE      DW      4 DUP(?)        ; ADDRESSES OF RS232 ADAPTERS
PRINTER_BASE    DW      4 DUP(?)        ; ADDRESSES OF PRINTERS
EQUIP_FLAG      DW      ?               ; INSTALLED HARDWARE
MFG_TST         DB      ?
; ...
END VIRTUAL



When looking at this thread I had a thought that I could perhaps create a variant of my x86 macros for fasm g that would handle this syntax, and then you could reassemble this entire source with fasm g after only adding a single INCLUDE line on top. I think it would be quite easy to do (with ELEMENT-based segmented addresses the bracket-less memory operand would be distinguished from an absolute value and the right segment prefix could be chosen after extracting the segment number from ELEMENT metadata). But when I look at this syntax I also recall all the reasons I had for hating it, so I'm not jumping to it. Wink
Post 13 Dec 2015, 12:39
View user's profile Send private message Visit poster's website Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 196
First of all I suggest you read Bios Disassembly Ninjutsu book http://www.amazon.com/BIOS-Disassembly-Ninjutsu-Uncovered/dp/1931769605

The author released some time ago PDF version of it.

Secondly the assembler used is old version of MASM. You may download old MASM versions here for example: https://winworldpc.com/

It can't be in NASM syntax due to its age.

Secondly - I know it it quite perverse method since you will either need to compile the source code or use BIOS ROM dump - but you can either try to use IDA or Sourcer Bios Preprocessor (take a look in here: https://corexor.wordpress.com/2015/12/09/sourcer-and-windows-source/) and select TASM as output assembler. This will be as close as you can get for FASM syntax.
Post 13 Dec 2015, 14:42
View user's profile Send private message Reply with quote
nkeck72



Joined: 28 May 2015
Posts: 78
Location: 0000:7C00
Thanks much, I will download MASM and see if what ACP mentioned works.

Tomasz, I am assuming that your post means there is no way to explicitly specify to FASM what segments are being referred to in the SEGMENT AT statements? Also, yeah, I hate this syntax as well, it looks like a mess to me Very Happy
Post 13 Dec 2015, 15:22
View user's profile Send private message Visit poster's website Reply with quote
Hayden



Joined: 06 Oct 2005
Posts: 132
BIOS
All BIOS code is coded for the motherboard its embedded on. data structures and code will vary with different manufuacture's. If you would really like to know how the BIOS operates and its services you might want to take a look it the following attachment.


Description: complete EBIOS developers guide
Download
Filename: EBIOS-UM.PDF
Filesize: 294.56 KB
Downloaded: 40 Time(s)


_________________
New User.. Hayden McKay.
Post 17 Dec 2015, 17:16
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 196
IMHO looking at coreboot + SeaBIOS sources and documentation is a better idea, but your mileage may vary.

http://coreboot.org

The one big advantage is that coreboot + SeaBIOS can be run using QEMU while not every other BIOS will work correctly.
Post 17 Dec 2015, 17:52
View user's profile Send private message Reply with quote
nkeck72



Joined: 28 May 2015
Posts: 78
Location: 0000:7C00
Hayden - The IBM PC became a standard in pretty much everything, right down to the I/O ports and the structures used. I think that looking through this BIOS would help, since it is essentially the standard for any PC with a BIOS (none of that UEFI rubbish). Thanks for the links, though, will do a bit of reading on those!

_________________
It may look hard, but it won't take long if you take it one byte at a time.

NOS: www.github.com/nkeck720/nos


Code:

org 100h
mov ah09h
mov dxmessage
int 21h
int 20h

message db "Hello FASM world!"0Dh0Ah"$"


Post 21 Dec 2015, 17:41
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14672
Location: Origae-6

nkeck72 wrote:
Hayden - The IBM PC became a standard in pretty much everything, right down to the I/O ports and the structures used. I think that looking through this BIOS would help, since it is essentially the standard for any PC with a BIOS (none of that UEFI rubbish).

Sure, if you want FDD, DMA, parallel ports and serial ports and all the old peripheral interfaces. The current PCs you can buy today have changed considerably. For example ACPI, PCI bus, USB, south/north bridge, HDMI, touchpad, GPU, sound, etc. None of those things existed in the original PC.
Post 21 Dec 2015, 18:04
View user's profile Send private message Visit poster's website Reply with quote
nkeck72



Joined: 28 May 2015
Posts: 78
Location: 0000:7C00
Notice I said that it will help - I am not going to base the entire BIOS off of the IBM PC. The only reason I am interested in it is that it is a standard - the I/O ports and memory layout aren' gonna change anytime soon.
Post 21 Dec 2015, 18:11
View user's profile Send private message Visit poster's website Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 196

revolution wrote:
Sure, if you want FDD, DMA, parallel ports and serial ports and all the old peripheral interfaces. The current PCs you can buy today have changed considerably. For example ACPI, PCI bus, USB, south/north bridge, HDMI, touchpad, GPU, sound, etc. None of those things existed in the original PC.



True, but coreboot does support most of newest technology sometimes in few different forms. The potential drawback is that it is using 3rd party binary blobs for some things but this is only due to the fact that manufacturer never released documentation or sources or there are restricted NDAs in place. Still it is great resource to learn about modern legacy BIOS thanks to SeaBIOS and other options includign UEFI.
Post 22 Dec 2015, 20:30
View user's profile Send private message 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.