News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

more gas problems

Started by bobl, October 15, 2009, 09:00:34 PM

Previous topic - Next topic

bobl

Can someone put me straight re my nasm to gas conversion please


;my nasm makefile
#I'm assembling the following code with this
nasm simple.asm -o simple.bin
dd if=/dev/zero of=boot.img bs=512 count=2880
dd if=simple.bin of=boot.img conv=notrunc
qemu -fda boot.img -boot a




;my nasm code
push word 0b800h     
pop es
xor di, di
mov [es:di], word 441h
jmp $
times 510 - ($ - $$) db 0
db 55h
db 0aah



The above and all the other nasm examples I've got work fine
I'm trying to convert the above nasm example to gas...
but am failing miserably. Yes I know.....
but I want to see if I can step a gas built bootloader through qemu


#my attempted gas "makefile"
as -o simple.o simple.s
ld --oformat binary -o simple simple.o
dd if=/dev/zero of=boot.img bs=512 count=2880
dd if=simple of=boot.img conv=notrunc
qemu -fda boot.img -boot a




#my attempted gas code

#I added this
.code16
.text
.global _start
_start:


#intel2gas utility generated this
pushw $0xb800     
pop %es           
xorw %di,%di       
movw $0x441,es:di   
                   
#I added this
aloop:
   jmp aloop:         


#I added this
finish:
                              #300 isn't right and I was going to use a hex editor to find
                              #the right number to put in
%rep 300            #what's the syntax for (510 - (finish - _start))??
.byte 0x00
%endrep

#intel2gas generated this
.byte 0x55
.byte 0xaa


#Any help much appreciated

MichaelW

I think this is a reasonable translation:

.intel_syntax noprefix
.section .text
.code16
push 0xb800
pop es
xor di, di
mov WORD PTR es:[di], 0x441
0:
jmp 0b
.org 510
.word 0xaa55


And using the GNU assembler version 2.18.50 (i686-pc-mingw32) and this command line:

as -v -al -o gasboot.o gasboot.asm

It assembles without error, and the listing looks OK:

GAS LISTING gasboot.asm                         page 1


   1                    .intel_syntax noprefix
   2                    .section .text
   3                    .code16
   4 0000 6800B8        push 0xb800
   5 0003 07            pop es
   6 0004 31FF          xor di, di
   7 0006 26C70541      mov WORD PTR es:[di], 0x441
   7      04
   8                    0:
   9 000b EBFE          jmp 0b
  10 000d 00000000      .org 510
  10      00000000
  10      00000000
  10      00000000
  10      00000000
  11 01fe 55AA          .word 0xaa55
  12


But at this point I cannot find any way to make the Windows version of ld create a binary from the object module (even though objdump -i reports that the binary output format is supported).

According to this article:

http://susam.in/articles/boot-sector-code/

This command line should work:

ld --oformat binary -o gasboot.bin gasboot.o

But I get:

ld: cannot perform PE operations on non PE output file 'gasboot.bin'

And note that the information in the article was verified with GNU ld (GNU Binutils for Debian) 2.18.
eschew obfuscation

japheth

> But at this point I cannot find any way to make the Windows version of ld create a binary from the object module (even though objdump -i reports that the binary output format is supported).

Open Watcom WLink will digest it:

wlink format dos com file gasboot.o name gasboot.bin

bobl

#3
Michael
Thank for your time.
Your code works flawlessly with Linux's ld  (Thx for WLink japheth!)


.intel_syntax noprefix
.section .text
.code16
.global _start     # inform linker
_start:                 # default entrypoint known to ld
nop                     # not required but gdb likes a 1-byte instruction first, apparently??

push 0xb800
etc


Having failed to assemble gas myself I looked to see if nasm could provide the debug symbols gdb needs to control execution.
Apparently it can ie
nasm flags src.asm
where flags =
-f bin                                                    binary
-F stabs                                              stabs debug symbols
-g enable symbol insertion            self explanatory

Unfortunately -F needs a debug symbol format that is supported by -f's file type and
bin files dont't support debug symbols, apparently.
Additionally when typing 'nasm -f elf -y' it seems that only elf32/64 and not elf(16bit) support stabs debug symbols.

I was hoping to use -g in ld to get some symbols into the binary but when trying to assemble in elf format (that I thought ld would like)
nasm "forgot" what ORG meant ( I expect masm has an ORG equivalent).


#My version of original for qemu - works well
#dont use enth.bin cos used to make this. See how enth.bin got made
   nasm -g -f bin emakeimg.asm -o enth.fbin
   dd if=/dev/zero of=floppy.img bs=512 count=2880
   dd if=enth.fbin of=floppy.img conv=notrunc
   qemu -fda floppy.img -boot a


#this makes emakimg image that doesnt even need dd to run in qemu. WOW!
#ld not for 16bit relocatable but gnu has extensions ie -w+gnu-elf-extensions
#'file emakeimg' reports 'emakeimg' resulting as an 'x86 bootsector'
   #nasm -g -w+gnu-elf-extensions emakeimg.asm
   #qemu -fda emakeimg -boot a


I can use either of the above to get the application working but I can't work out the assemble and ld command lines to get the two to talk so I can use ld -g (in case there's any nasm experts looking in)

I have to go out now but will try putting the symbols into gasboot.bin tomorrow with ld.
Other than this route getting symbols that gdb likes into 16 bit binaries seems difficult & I wonder if this is generally the case in non-DOS os'es
or if it's just me as a beginner??
Suggestions re any other ways to step through a bootloader would be much appreciated.











MichaelW

QuoteOpen Watcom WLink will digest it

It did, thanks.

Open Watcom Linker Version 1.2
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See http://www.openwatcom.org/ for details.
loading object files
Warning! W1080: file gasboot.o is a 32-bit object file
Warning! W1023: no starting address found, using 0000:0000
creating a DOS .COM executable


0B04:0000 6800B8            PUSH    WORD B800
0B04:0003 07                POP     ES
0B04:0004 31FF              XOR     DI,DI
0B04:0006 26C7054104        MOV     WORD PTR ES:[DI],0441
0B04:000B EBFE              JMP     000B
0B04:000D 0000              ADD     [BX+SI],AL
. . .
0B04:01FB 0000              ADD     [BX+SI],AL
0B04:01FD 0055AA            ADD     [DI-56],DL

eschew obfuscation

bobl

Thanks for the link
I've never really given openwatcom much thought but its ability to link gas under windows where ld can't and fact that it's ported to linux and bsd suggests that the people behind it have got their act together.
japheth has drawn this collection to people's attention before i.e.
http://www.masm32.com/board/index.php?topic=9300.msg67489
In that thread some one asked what the references were for that weren't for Win32 executables.
Here's an example of why you'd want a .com file i.e. the above code tests the setup for writing a bootloader
Thank's once again japheth

BlackVortex

I couldn't care less about gas, but do you guys know that recently japheth improved/fixed Wlink ?

Information here :
http://www.masm32.com/board/index.php?topic=12301.0