The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: bobl on October 07, 2009, 09:23:12 PM

Title: converting Fasm to Gas with .intel_syntax noprefix
Post by: bobl on October 07, 2009, 09:23:12 PM
Heres some Fasm code.

section ".text"
   mov ebx, label_1
section ".data"
   label_1 db "some_string", 0

Could someone please show me how you would convert this to "standard" intel syntax.
Thx in anticipation
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: nathanpc on October 07, 2009, 09:55:10 PM
Welcome!
As i can see, this is Fasm syntax and this is not about Masm, then the correct place to post this is in the Fasm board.

Thanks!

nathan,

Admin handles problems like this if they arise, leave it to us.
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: dedndave on October 07, 2009, 10:00:52 PM
i am not so sure you really want intel syntax - lol
i have not seen the intel assembler in use since about 1985
for masm (more or less "the" standard), it would look like this...

        .code

        mov     ebx,label_1

        .data

label_1 db "some_string",0
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: sinsi on October 07, 2009, 10:17:29 PM

.code
  mov ebx,OFFSET label_1
.data
label_1 db "some_string", 0

FASM doesn't use OFFSET.

dave, your masm code in fasm would be "mov ebx,[label_1]"
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: qWord on October 07, 2009, 10:19:39 PM
my suggestionfor masm:    .data
    label_1 db "some_string", 0
    .code 
    mov ebx,OFFSET label_1


PS: masm doesn't support forward referencing

Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: dedndave on October 08, 2009, 03:12:40 AM
it will forward reference a label - but i did miss the offset - lol
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: GregL on October 08, 2009, 03:52:52 AM
bobl,

I fiddled around with as (Gas) for a short while. It would be something like this


.intel_syntax noprefix
.arch pentium
.global _start

.data

    label_1: .asciz "some_string"

.text

    mov ebx, OFFSET label_1

.end




Get the manual, it's included in the GNU binutils download and it's decent.  I could post some of the simple programs I wrote with it if you would like.


Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: bobl on October 08, 2009, 09:28:53 AM
Nathan
I did ask on the Fasm forum ie http://board.flatassembler.net/topic.php?p=102970#102970
I think the Gas forum would've been a good place to go but couldn't see one last night.

Dedndave and Sinsi
Thx for the masm conversion and I only asked here cos I thought Microsoft and Intel were thick together and so masm would be more likely to follow intel syntax.

Greg
Thx for the conversion, the binutils documentation and offer of some sample programs.
I'd very much like to take you up on that offer if you don't mind as all the documentation I looked at last night didn't have intel syntax.

Overall I'm extremely impressed with your helpfulness on this forum so thanks very much indeed.
I use Powerbasic at every opportunity so you will be seeing me on this site more when I've finished this g++ program.
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: hutch-- on October 08, 2009, 10:36:18 AM
bobl,

MASM is the main Intel notation assemblr and much of its lower level notation can be used with little change in the GNU "AS.EXE" assembler. GAS is in fact a very good tool once you get the swing of it and from memory it had a reasonable macro engine in it as well so with a little work you should be reasonably productive with it. Here is a seperate module I could find written in a version of GAS of about 2005.


/* ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« */

   /*************************
    assemble this module only
    *************************/


    .intel_syntax noprefix
    .global szcopy

/* ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« */

    .text

  .align 8
  szcopy:

    mov ecx, [esp+4]
    mov edx, [esp+8]

    push ebx
    xor ebx, ebx

  cpy:
  .rept 3
    mov al, [ecx+ebx]
    mov [edx+ebx], al
    add ebx, 0x01
    test al, al
    jz cpout
  .endr

    mov al, [ecx+ebx]
    mov [edx+ebx], al
    add ebx, 0x01
    test al, al
    jnz cpy

  cpout:

    pop ebx

    ret 8

/*«««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««*/


This is the batch file I built it with back then.


@echo off

if exist module.exe del module.exe
if exist module.obj del module.obj

\gas\bin\as -o module.obj module.as
\gas\bin\ld -e_entry -subsystem windows -L\gas\lib -o module.exe module.obj szcopy.obj --library=masm32 --library=user32 --library=kernel32 --library=gdi32 --library=Comctl32 --library=comdlg32 --library=shell32 --library=oleaut32 --library=ole32 --library=msvcrt -s

dir module.*

pause
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: bobl on October 08, 2009, 01:24:53 PM
Hutch
Re masm's similarity and gas' functionality, that's very reassuring.
Your intel syntax example is just what I need at the moment cos I couldn't find one on google last night.
Hopefully the binutils documentation Greg referred to might have a few.
Intel syntax seems more straight forward than AT&T hence my wish to stick with it if I can.
Thanks very much for your help
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: nathanpc on October 08, 2009, 04:10:44 PM
Hi bobl,
If you want to use C, you can do like this:
gcc -S hello.c
And gcc will convert your C source code in Gas source code.

Take a look at:
Hope I'm Helping,
Nathan Paulino Campos
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: bobl on October 08, 2009, 05:04:12 PM
Yes Nathan your post is very helpful i.e. that second link about using gcc -S is helpful re understanding how to call a gas function from g++.
I've been using the extern "C" command cos name-mangling in c++ is not consistent across different compilers.
gas looks similar to the other assemblers re this in that it seems only to require the function name in a global statement and as a label + ret.
Thanks for your help.
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: bobl on October 08, 2009, 05:12:21 PM
BTW I didn't get a manual with my binutils and looking at the sources it seems to come in a weird format but I found it here in nice html.
http://sourceware.org/binutils/docs-2.19/as/
It's certainly detailed but seems more of a reference for a seasoned asm man than a how-to for a beginner.
I'm sure it will be of use as a reference however once I get up to speed.

Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: MichaelW on October 08, 2009, 06:06:23 PM
I posted a set of invoke macros for GAS, along two example apps, here:

http://www.masm32.com/board/index.php?topic=8689.0

Note that the macros require a relatively recent version of GAS (2.18.5+).

I recall seeing in a GAS source file recently a comment indicating that the Intel syntax was based on the MASM BNF Grammar that Microsoft included in the programmer's guide. This would explain why the basic syntax is so close to MASM (although the GAS macro system is very different than the MASM macro system and, by comparison, very weak).
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: bobl on October 08, 2009, 07:35:19 PM
Thanks very much Michael. I've got 2.19.
Wow I nearly missed the examples link.
That's great!
I see there's some DOS ones in there too.
Thx
Shame about the macros being weak cos some of my higher level code is dependent upon them and this may well be the case with asm.
Title: Re: converting Fasm to Gas with .intel_syntax noprefix
Post by: GregL on October 09, 2009, 01:50:54 AM
bobl,

Sorry, I thought the manual was included in the download. It's titled "Using as" and is a PDF file.

Here is a "Hello World" type program written in GAS.


.intel_syntax noprefix
.arch pentium
.global _start

.include "D:\\ASM\\GAS\\Include\\windows.inc"
.include "D:\\ASM\\GAS\\Include\\kernel32.inc"
.include "D:\\ASM\\GAS\\Include\\user32.inc"
.include "D:\\ASM\\GAS\\Include\\msvcrt.inc"

.macro Exit uExitCode
    push \uExitCode
    call ExitProcess
.endm


.data

    # StdOut variables
    hStdOut:      .long  0
    dwNumWritten: .long  0
    dwNumToWrite: .long  0
   
    # main variables
    pszPrompt:    .long  0
    szMsg:        .asciz "GNU Assembler, it's a GAS!\r\n"
    szPause:      .asciz "\r\nPress any key to continue ... "
    szExit:       .asciz "\r\nPress any key to exit ... "
   
.text

  main:

    push OFFSET szMsg
    call StdOut
   
    push OFFSET szExit
    call WaitKey
   
    Exit 0
     
#------------------------------------------     
StdOut:
    push ebp
    mov ebp, esp   
    mov eax, [ebp+8]
    cmp eax, NULL  # is the pointer null?
    je L1
    mov pszPrompt, eax   
    push STD_OUTPUT_HANDLE
    call GetStdHandle
    mov  hStdOut, eax
    push pszPrompt
    call lstrlen
    mov dwNumToWrite, eax
    push NULL
    push OFFSET dwNumWritten
    push dwNumToWrite
    push pszPrompt
    push hStdOut
    call WriteFile
  L1:
    mov esp, ebp
    pop ebp
    ret
#------------------------------------------
WaitKey:
    push ebp
    mov ebp, esp
    mov eax, [ebp+8]
    cmp eax, NULL  # is the pointer null
    jne L2
    mov eax, OFFSET szPause
  L2: 
    push eax
    call StdOut
    call getch
    cmp eax, 0
    je L3
    cmp eax, 0x0E
    je L3
    jmp L4
  L3: 
    call getch
  L4:
    mov esp, ebp
    pop ebp
    ret
#------------------------------------------     
.end


The batch file (I used the VC 6.0 import libraries).

@echo off
if exist %1.o del %1.o
if exist %1.exe del %1.exe

C:\binutils\bin\as -o %1.o %1.s
if errorlevel 1 goto error
C:\binutils\bin\ld -emain -subsystem console -L"C:\Program Files\Microsoft Visual Studio\VC98\Lib" -o %1.exe %1.o --library user32  --library kernel32 --library msvcrt -s
if errorlevel 1 goto error

if exist %1.o del %1.o
goto done

:error
pause

:done


The include files, I was adding to these as needed so there is not much to them.

windows.inc

NULL = 0
MB_OK = 0
STD_OUTPUT_HANDLE = -11


kernel32.inc

// kernel32.dll functions
lstrlen = _lstrlenA@4
lstrcat = _lstrcatA@8
GetStdHandle = _GetStdHandle@4
WriteFile = _WriteFile@20
MessageBox = _MessageBoxA@16
ExitProcess = _ExitProcess@4


user32.inc

// user32.dll functions
wsprintf = _wsprintfA


msvcrt.inc

// msvcrt.dll functions
getch = __getch
sprintf = _sprintf
printf = _printf