flat assembler
Message board for the users of flat assembler.

Index > Programming Language Design > [FASMG] Minimal Intel Windows

Author
Thread Post new topic Reply to topic
codestar



Joined: 25 Dec 2014
Posts: 254
codestar 15 May 2015, 07:35
Standalone .EXE example. 1K.

SYSTEM.INC
Code:
; SYSTEM.INC - INTEL, WINDOWS

macro align n
  db ((n-1)-($+n-1) mod n) dup(0)
end macro

;;;;;;;;;;;;;;;;;;;;;; CPU ;;;;;;;;;;;;;;;;;;;;;;;

eax=0
ecx=1
edx=2
ebx=3

macro int3
  db $CC
end macro

macro push_r r
  db $50+r
end macro

macro push_i i
  db $68
  dd i
end macro

macro mov_ri r, i
  db $B8+r
  dd i
end macro

macro call_p a
  db $FF, $15
  dd a
end macro

;;;;;;;;;;;;;;;;;;;;; SYSTEM ;;;;;;;;;;;;;;;;;;;;;

@rva equ ($1000-$200)
@base equ $400000+@rva

@entry=$1000+(@main-@section)
@section_size=@end_section-@section
@image_size =\
  ((($1000+@section_size) shr 12)+1) shl 12
@import_size=@end_import-@section

macro write_exe_header
  dw 'MZ'
  db $3A dup(0)
  dd $40
  db 'PE', 0, 0
  dd $1014C, 0, 0, 0, $10F00E0, $10B, 0, 0, 0
  dd @entry, 0, 0, $400000, $1000, $200
  dd 1, 0, 4, 0, @image_size, $200, 0, 2
  dd $1000, $1000, $1000, 0, 0, 16
  dd 0, 0, $1000, @import_size
  dq 14 dup(0)
end macro

macro write_section_header
  dq '.one'
  dd @end_program-@end_section, $1000
  dd @end_program-$200, $200, 0, 0, 0
  dd $0E0000020
end macro

macro library p&     ; import library
  irp dll, p
    dd 0, 0, 0
    dd @rva+dll#_name, @rva+dll#_table
  end irp
  dd 0, 0, 0, 0, 0
end macro

macro import dll, p& ; import functions
  dll#_name db \     ; 'name.dll'
    `dll, '.DLL', 0
  dll#_table:        ; table
  irp name, p        ; import rvas
    label name \
      at @base+$
    dd @rva+_#name
  end irp
  dd 0
  irp name, p        ; import names
    _#name dw 0      ; ordinal=0
    db `name, 0      ; 'name'
  end irp
end macro

write_exe_header
write_section_header

align $200
@section:
  library KERNEL32, USER32
  import KERNEL32, ExitProcess
  import USER32, MessageBoxA
@end_import:
align 16

macro main
  align 16
  @main:
end macro

postpone
  align 16
  @end_section:
    align $200
  @end_program:
end postpone

;;;;;;;;;;;;;;;;;;; LANGUAGE ;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;; CONSTANTS ;;;;;;;;;;;;;;;;;;;;

; numeric BLACK, WHITE=1, RED, GREEN, BLUE

macro numeric p&
  local i
  i=0
  irp q, p
    match name==value, q
      i=value
      name=i
    else match name, q
      name=i
    end match
    i=i+1
  end irp
end macro

;;;;;;;;;;;;;;;;;;;; VARIABLE ;;;;;;;;;;;;;;;;;;;;

; int i=7, n='Z', j=$CAFEBABE

scope=0

macro variable type, p&
  irp q, p
    match name==value, q
      label name at @base+$
      type value
    else match name, q
      label name at @base+$
      type 0
    end match
  end irp
end macro

macro byte p&
  variable db, p
end macro

macro int p&
  irp q, p
    match name[]==values, q
      match {v}, values
        label name at @base+$
        dd v
      else
        err 'Use: ={...}'
      end match
    else match name[size], q
      label name at @base+$
      dd size dup(0)
    else
      variable dd, q
    end match
  end irp
end macro

macro integer p&
  int p
end macro

macro void p&
  int p
end macro

macro text p&
  irp q, p
    match name==value, q
      label name at @base+$
      db value, 0
    else match name[size], q
      label name at @base+$
      db size dup(0)
    else
      err ''
    end match
  end irp
end macro

;;;;;;;;;;;;;;;;;;;;; CLASS ;;;;;;;;;;;;;;;;;;;;;;

macro class type
  esc macro type name
    scope='c'
    name:
    namespace name
      ; ...
end macro

macro endc!
      ; ...
    end namespace
    scope=0
  esc end macro
end macro

; EXAMPLE:

; class POINT
;   int x, y
; endc

; POINT point

; 2-DO: single-line classes:

; class RGB   = byte a, r, g, b
; class POINT = int x, y
; class LINE  = POINT a, b
; class BOX   = int x, y, w, h
; class RECT  = int l, t, r, b    
EXAMPLE.ASM
Code:
; EXAMPLE.ASM > .EXE. ONLY 1K

include 'system.inc'

text title='Hello, FASMG!'
text caption='Minimal Win32 Example'

main
  push_i 0
  mov_ri eax, title
  push_r eax
  mov_ri eax, caption
  push_r eax
  push_i 0
  call_p MessageBoxA
  push_i 0
  call_p ExitProcess    


Description:
Download
Filename: windowsg.zip
Filesize: 2.55 KB
Downloaded: 812 Time(s)

Post 15 May 2015, 07:35
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.