flat assembler
Message board for the users of flat assembler.

Index > DOS > Pls help me to convert the following code to FASM

Author
Thread Post new topic Reply to topic
prana



Joined: 28 Aug 2003
Posts: 51
prana
Pls help me to find out how the following code should be written in FASM:
This is to understand the syntax and directives better.

***********************************
Code:
 format MZ

; section '.data' data readable writeable

segment rom_bios_data at 40h

         org 1Ah
         head dw        ?
         tail dw        ?
         buffer         rw      16
        label           buffer_end        word

segment main
        org 100h
        use16
readem         proc
        mov     bx,rom_bios_data
        mov     ds,bx
 get_char:
        mov     bx,head
        cmp     bx,tail
        je      get_char
        mov     dx,[bx]
        mov     [tail],bx

        cmp     dl,'q'
        je      bye
        cmp     dl,'Q'
        je      bye
        mov     ah,2
        int     21h
        jmp     get_char

 bye:   int     20h
readem         endp
    


**********************************
It should be .com file.


The original code written for MASM was:
***********************************
Code:
.MODEL SMALL
ROM_BIOS_DATA SEGMENT at 40h
        org 1Ah

        head        dw        ?
        tail        dw        ?
        buffer      dw        16
        buffer_end  label     word
ROM_BIOS_DATA ENDS

code    SEGMENT

        ORG 100h

_readem   proc near
         assume ds:ROM_BIOS_DATA
         mov  bx, ROM_BIOS_DATA
         mov  ds, bx

get_char:
         mov  bx, head
         cmp  bx, tail
         je   get_char
         mov  dx, bx
         mov  tail, bx

         cmp  dl, 'q'
         je   bye
         cmp  dl, 'Q'
         je   bye
         mov  ah, 2
         int  21h
         jmp  get_char

bye:     int  20h
_readem   endp
code     ends
         end  _readem             
    


*****************************************
My general questions are on the use of proc,segment,at,org directives (or macros?). Could someone explain them a little bit in context of FASM.
Thanks for any help.
.
Post 05 Sep 2003, 04:10
View user's profile Send private message Reply with quote
scientica
Retired moderator


Joined: 16 Jun 2003
Posts: 689
Location: Linköping, Sweden
scientica
I'm soon of to schools so I'll just (try to) explain the org directive.
In short it sets the address origin (where it begins) to the number after it, for instance:

Code:
label1:
org 100h
label2:    

label1 has the address/offset 0, where as label 2 has the address/offset 100h.
Org is used mostly in .COM files (org 100h), and boot loaders (org 7500h, iirc)
Sorry if my explanation is too short, I hope you at least.

_________________
... a professor saying: "use this proprietary software to learn computer science" is the same as English professor handing you a copy of Shakespeare and saying: "use this book to learn Shakespeare without opening the book itself.
- Bradley Kuhn
Post 05 Sep 2003, 04:51
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Thanks Scientica. Smile

My original intention was to ask how to manage those two ORG s in the code.
Neither NASM nor FASM supports this kind of organisation.

A restructuring of that code would be more helpful.

Thanks.
Post 05 Sep 2003, 05:17
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7410
Location: Kraków, Poland
Tomasz Grysztar
For such purpose you should use "virtual", not "org".
Also, as you probably want to make .com file (as you are using "org 100h" and "int 20h" to exit), you should use MZ format for it. Here's how this whole program should be written for FASM:
Code:
label rom_bios_data at 40h

virtual at 1Ah
  head dw ?
  tail dw ?
  buffer rw 16
  label buffer_end word
end virtual

        org     100h

        mov     bx,rom_bios_data
        mov     ds,bx
      get_char:
        mov     bx,[head]
        cmp     bx,[tail]
        je      get_char
        mov     dx,[bx]
        mov     [tail],bx
        cmp     dl,'q'
        je      bye
        cmp     dl,'Q'
        je      bye
        mov     ah,2
        int     21h
        jmp     get_char
      bye:
        int     20h    
Post 05 Sep 2003, 09:34
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Great! Very Happy

Just one more question for now, does FASM uses proc/endp directives, or r they macros? If so, when their use is advisable. Also could you throw some light on segment? Does it use nasm style segment name use16/use32 etc?


I know you are very busy and also writing a tutorial for us Very Happy but knowing these small details would help me a lot to get started.

Thanks.
Post 05 Sep 2003, 10:37
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7410
Location: Kraków, Poland
Tomasz Grysztar
proc/endp are macros, and for DOS programming I would not recommend using them at all, unless you need to operate with some HLL routines.
segment directive is valid for MZ format only, and it labels a segment in the memory block of your program, other segments can be defined just as a labels, absolute or relative to your segments. For example, here's how you can make MZ program, which works the same way as standard .com (with PSP being common segment for all, so you have to use "org 100h" and can exit with "int 20h"):
Code:
format MZ
entry PSP:100h

segment main use16
PSP = main - 10h

        org     100h

        mov     ah,9
        mov     dx,_message
        int     21h

        int     20h

_message db '.com emulated with MZ format',24h    

More examples of MZ programs you can find in the DOS distrubution of fasm.


Last edited by Tomasz Grysztar on 05 Sep 2003, 11:06; edited 1 time in total
Post 05 Sep 2003, 10:48
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Ok, so here PSP is the label at 100h, but why it is equal to main-10h?

Another thing is when I compile the first example without a format MZ the resulting com size is 39bytes, but when compiled with format MZ the size is 71 bytes. Why so? Can I avoid writing the format directive in my program like this?

Thanks.
Post 05 Sep 2003, 11:02
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7410
Location: Kraków, Poland
Tomasz Grysztar
.com files are simple binary programs, while MZ programs have special headers, defining more settings for program, so you can use more segments, define entry point, stack, etc.
For programs that don't need more that 64 kilobytes of memory, binary format is enough.
Post 05 Sep 2003, 11:05
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Ok, thanks.
Post 05 Sep 2003, 11:14
View user's profile Send private message Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Privalov, is the following a valid COFF format file under FASM?

Code:
format COFF
        public _start
section '.data'
hello db 'Hello world!',24h
section '.code'
_start:
        mov     ah,9
        mov     dx,word ptr hello
        int     21h
        mov     ah,1ch
        int     21h
        ret                  
    


Though it compiled and linked, I see no output Confused

I must be doing something wrong, what's that? And how in general a COFF template should look like. I also didn't yet understand the placement of Prog Seg Prefix in your last post.
Could u pls help?
Post 05 Sep 2003, 12:45
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7410
Location: Kraków, Poland
Tomasz Grysztar
What linker are you using? COFF format is designed for the flat protected mode programs, and is not very usable for 16-bit DOS programming.
"mov dx,word ptr hello" is definitely wrong here, as it is the same as "mov dx,word [hello]". Use "mov edx,hello" instead
Post 05 Sep 2003, 12:53
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
I am using LINK. I changed the mov dx, word ptr hello to mov edx, hello, but somehow confused btw the two. A little explanation would be good. Also nothing gets printed. I want to know that whether the segment directives are ok here.

And in a previous example you showed something
segment main use16
PSP = main - 10h
What purpose it solved? Could u explain a bit.

Thanks.
Post 05 Sep 2003, 13:03
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7410
Location: Kraków, Poland
Tomasz Grysztar
LINK of what package? What kind of executables it creates?

And for your second question - when your program is loaded into memory, it is preceded by the so-called Program Segment Prefix (PSP in short), which is 100h bytes in size. Then the data of your program follows. In case of simple binary program everything is located in one segment, so PSP is at address 0 in that segment, and your program just after it, at address 100h - that's why you have to use "org 100h" there. In case of MZ programs, you can have multiple segments, and your first segment is defined to be the segment just after the PSP. As the number of segment is the count of paragraphs from the beginning of memory, and one paragraph is 10h bytes, the difference of 100h bytes in addresses is difference of 10h in paragraphs, so to get your PSP segment number, you have to substract 10h from the number of first segment of your code. Also, for all DOS programs DS is initially set to the number of PSP segment, so you can just put the "org 100h" at the beginning of your first segment and do everything the same way as in case of .com program. The entry point is defined in the PSP segment, too - so CS is also equal to it, and you can use "int 20h" function (which needs your CS to be the number of your PSP segment).
Post 05 Sep 2003, 13:35
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Thanks a lot for the explanation Privalov. I worked earlier with PSP, but forgot many details now.

Thanks again for your time.
And for your other question, I'm using the LINK comes with VC.
Post 05 Sep 2003, 13:41
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7410
Location: Kraków, Poland
Tomasz Grysztar
So it generates Win32 programs, not DOS ones - that's why interrupt functions are not working for you. But there are also some linkers, that can link COFF into programs for protected mode DOS with extenders.
Post 05 Sep 2003, 13:45
View user's profile Send private message Visit poster's website Reply with quote
prana



Joined: 28 Aug 2003
Posts: 51
prana
Ok! I have alink, should I try that one? I compiled some program long ago with it int 33h I believe, forgot a lot though.
Post 05 Sep 2003, 13:50
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


Copyright © 1999-2019, Tomasz Grysztar.

Powered by rwasa.