News:

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

converting Fasm to Gas with .intel_syntax noprefix

Started by bobl, October 07, 2009, 09:23:12 PM

Previous topic - Next topic

GregL

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