Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

equivalent GAS code for some NASM code

617 views
Skip to first unread message

bilsch

unread,
Mar 21, 2016, 3:39:31 AM3/21/16
to
1) NASM code: times 510-($-$$) db 0
fills the space between the the last byte of the program and byte number
510 of the sector. Fills it with zeros.

what is GAS equivalent of: times 510-($-$$) db 0

2) does GAS have equivalent assembler directive to: bits 16

TIA. Bill S.

Frank Kotler

unread,
Mar 21, 2016, 9:09:04 AM3/21/16
to
bilsch wrote:
> 1) NASM code: times 510-($-$$) db 0
> fills the space between the the last byte of the program and byte number
> 510 of the sector. Fills it with zeros.
>
> what is GAS equivalent of: times 510-($-$$) db 0

.org 510

on my system, actually .org 497 and a few odds and ends of variables
plus the boot sig.

> 2) does GAS have equivalent assembler directive to: bits 16

.code16

Best,
Frank



>
> TIA. Bill S.

Rod Pemberton

unread,
Mar 21, 2016, 10:09:15 AM3/21/16
to
On Mon, 21 Mar 2016 09:01:54 -0400
Frank Kotler <fbko...@nospicedham.myfairpoint.net> wrote:

> bilsch wrote:

> > 1) NASM code: times 510-($-$$) db 0
> > fills the space between the the last byte of the program and byte
> > number 510 of the sector. Fills it with zeros.
> >
> > what is GAS equivalent of: times 510-($-$$) db 0
>
> .org 510
>
> on my system, actually .org 497 and a few odds and ends of variables
> plus the boot sig.

Didn't the OP ask for a fill too, or is it unnecessary here?

.fill 0x1fe - (. - start) , 1, 0
.org 510


In the .fill command, the '0' is the value to fill. The '1' is
the size of the fill value in bytes, i.e., one byte, not two, four,
or eight. The '.' in the .fill command represents the current assembly
location, like '$' in NASM. The 'start' is like '$$' in NASM, i.e., the
start of the code being assembled. The 0x1fe (510) minus the offset to
the current location is the repeat count for the fill.

See the GAS directives here:
https://ftp.gnu.org/pub/old-gnu/Manuals/gas-2.9.1/html_chapter/as_7.html

You'll need the 55h AAh signature after offset 510 for any boot
sector code, except for floppies:

GAS
.word 0xAA55

NASM
dw 0xAA55

> > 2) does GAS have equivalent assembler directive to: bits 16
>
> .code16

He needs either a .text or .data segment with GAS too, yes?

.code16
.text


Rod Pemberton

Mike Gonta

unread,
Mar 21, 2016, 12:25:18 PM3/21/16
to
"Rod Pemberton" wrote:
> Frank Kotler wrote:
>
>> bilsch wrote:
>
>> > 1) NASM code: times 510-($-$$) db 0
>> > fills the space between the the last byte of the program and byte
>> > number 510 of the sector. Fills it with zeros.
>> >
>> > what is GAS equivalent of: times 510-($-$$) db 0
>>
>> .org 510
>>
>> on my system, actually .org 497 and a few odds and ends of variables
>> plus the boot sig.
>
> Didn't the OP ask for a fill too, or is it unnecessary here?
>
> .fill 0x1fe - (. - start) , 1, 0
> .org 510

In this case the ".org" directive is redundant since it does the
same thing as the ".fill"

> In the .fill command, the '0' is the value to fill. The '1' is
> the size of the fill value in bytes, i.e., one byte, not two, four,
> or eight.

The default in ".fill" is to fill the specified count with zero bytes,
so in this case the parameters are redundant. ".org" fills from the
current location counter to the specified offset in the same way
and also has a byte fill value parameter available.

> The '.' in the .fill command represents the current assembly
> location, like '$' in NASM.

Good news is that "as" also recognizes the "$" in the exact same way
as nasm/fasm.
Too bad for no "$$".

>> > 2) does GAS have equivalent assembler directive to: bits 16
>>
>> .code16
>
> He needs either a .text or .data segment with GAS too, yes?
>
> .code16
> .text

".text" is the default so it's not required in this case.

GAS ("as") outputs an object file and cannot directly output a
binary file (like nasm/fasm). The object file can be converted
to a binary with "objcopy" (but will require simple runtime linking
like masm) or it can be linked with "ld" which produces the same
binary as the nasm/fasm "org 0x7C00".

as yourfile.s -o yourfile.o
ld --oformat binary -Ttext 0x7C00 yourfile.o -o yourfile.bin

Using a capital "S" ("s" is the same as "asm" for nasm/fasm) allows
the file to be processed by "cpp" (the c preprocessor) by passing
it to gcc.

<code>
.intel_syntax noprefix # no yucky AT&T
.code16 # .text section by default
.global _start # so "ld" (the linker) doesn't issue a warning
# otherwise not needed
_start: # hash sign used for single line comments
/ but forward slash is also used for single line comments
/ BEWARE - because of this "as" and even "cpp" don't recognize it as
division
.fill 510-($-_start) / the "$" is also recognized by "as" - sames as
nasm/fasm
.word 0xAA55
</code>


Mike Gonta
look and see - many look but few see

http://mikegonta.com


Mike Gonta

unread,
Mar 21, 2016, 5:56:59 PM3/21/16
to
"Mike Gonta" wrote:

> GAS ("as") outputs an object file and cannot directly output a
> binary file (like nasm/fasm). The object file can be converted
> to a binary with "objcopy" (but will require simple runtime linking
> like masm) or it can be linked with "ld" which produces the same
> binary as the nasm/fasm "org 0x7C00".
>
> as yourfile.s -o yourfile.o
> ld --oformat binary -Ttext 0x7C00 yourfile.o -o yourfile.bin

On Windows, a cross compiler with elf output would be used.

ld -melf_i386 --oformat binary -Ttext 0x7C00 yourfile.o -o yourfile.bin

> Using a capital "S" ("s" is the same as "asm" for nasm/fasm) allows
> the file to be processed by "cpp" (the c preprocessor) by passing
> it to gcc.

On Windows, file names are case insensitive, however the capital "S" is
required for gcc to pass the assembly file to the c preprocessor (cpp)
before it is passed to (as). Doing so permits the use of "c" style #define
macros and "c++" style (also modern "c") multi line comments (/* */).

Rod Pemberton

unread,
Mar 22, 2016, 9:28:11 AM3/22/16
to
On Mon, 21 Mar 2016 12:21:50 -0400
"Mike Gonta" <mike...@nospicedham.gmail.com> wrote:

> "Rod Pemberton" wrote:
> > Frank Kotler wrote:
> >> bilsch wrote:

> >> > 1) NASM code: times 510-($-$$) db 0
> >> > fills the space between the the last byte of the program and byte
> >> > number 510 of the sector. Fills it with zeros.
> >> >
> >> > what is GAS equivalent of: times 510-($-$$) db 0
> >>
> >> .org 510
> >>
> >> on my system, actually .org 497 and a few odds and ends of
> >> variables plus the boot sig.
> >
> > Didn't the OP ask for a fill too, or is it unnecessary here?
> >
> > .fill 0x1fe - (. - start) , 1, 0
> > .org 510
>
> In this case the ".org" directive is redundant since it does the
> same thing as the ".fill"

Thanks Mike. I learned something new. I hadn't even remotely
considered that GAS .org would do a .fill, but it does. See
.fill in the link I posted ... GAS .org even has an optional
value parameter to fill with. That would dramatically reduce
the need for ever using a .fill. If NASM also does a fill
for ORG, I don't see it mentioned in the ORG section of the
documentation. I've not used MASM much. Does anyone know if
MASM fills for an ORG?


Rod Pemberton

Mike Gonta

unread,
Mar 22, 2016, 11:59:07 AM3/22/16
to
Hi Rod,
Gas/masm "org" is different than nasm/fasm "org"
Gas/masm "org" moves the program location counter ahead (but not back)
and has to fill the gap with something.
Nasm "org" effectively links the code to the org address, there
is no movement of the program location counter.
Fasm "org" does the same, however fasm allows multiple "orgs" in
the same code.

Steve

unread,
Mar 23, 2016, 8:03:02 AM3/23/16
to
Rod Pemberton <NoHave...@nospicedham.bcczxcfre.cmm> writes:

> ... I've not used MASM much. Does anyone know if
>MASM fills for an ORG?

Hi Rod,

As for as I know it need not. When making a *.COM program they
always start with an ORG 100H. If they filled the first 256 bytes it
would destroy the PSP. However when I misused an ORG, it required
something to exist in the code to move the pointer, and the area was
zeroed out.

Regards,

Steve N.
0 new messages