Author Topic: small hello world again  (Read 13980 times)

sinsi

  • Guest
Re: small hello world again
« Reply #15 on: May 08, 2015, 07:39:00 PM »
jj, the macros in fasm are much more complex.
Code: [Select]
; example of simplified Windows programming using complex macro features
include 'win32ax.inc' ; you can simply switch between win32ax, win32wx, win64ax and win64wx here
.code
  start:
invoke MessageBox,HWND_DESKTOP,"Hi! I'm the example program!",invoke GetCommandLine,MB_OK
invoke ExitProcess,0
.end start
Here's how to import functions the raw way
Code: [Select]
section '.idata' import data readable writeable

  dd 0,0,0,RVA kernel_name,RVA kernel_table
  dd 0,0,0,RVA user_name,RVA user_table
  dd 0,0,0,0,0

  kernel_table:
    ExitProcess dd RVA _ExitProcess
    dd 0
  user_table:
    MessageBoxA dd RVA _MessageBoxA
    dd 0

  kernel_name db 'KERNEL32.DLL',0
  user_name db 'USER32.DLL',0

  _ExitProcess dw 0
    db 'ExitProcess',0
  _MessageBoxA dw 0
    db 'MessageBoxA',0
and the easy way
Code: [Select]
; import data in any section
data import
 library kernel32,'KERNEL32.DLL',\
user32,'USER32.DLL',\
winmm,'WINMM.DLL'
 import kernel32,\
ExitProcess,'ExitProcess'
 import user32,\
MessageBoxA,'MessageBoxA'
 import winmm,\
mciSendString,'mciSendStringA'
end data

No need for a linker.

jj2007

  • Member
  • *****
  • Posts: 11529
  • Assembler is fun ;-)
    • MasmBasic
Re: small hello world again
« Reply #16 on: May 08, 2015, 07:44:33 PM »
jj, the macros in fasm are much more complex.

So a simple mov ecx, len(esi) is not possible? I.e. the function form of macros doesn't exist in FASM?

Mikl__

  • Member
  • *****
  • Posts: 1124
Re: small hello world again
« Reply #17 on: May 08, 2015, 07:51:14 PM »
jj2007,
and how you can know  the length of the string pointed to by the register ESI before compilation?

jj2007

  • Member
  • *****
  • Posts: 11529
  • Assembler is fun ;-)
    • MasmBasic
Re: small hello world again
« Reply #18 on: May 08, 2015, 08:54:46 PM »
jj2007,
and how you can know  the length of the string pointed to by the register ESI before compilation?

Mysteries of MASM :P

Run this with a debugger:
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
  mov esi, chr$("Hello World")
  MsgBox 0, str$(len(esi)), "Bytes:", MB_OK
  exit
end start

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8440
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: small hello world again
« Reply #19 on: May 08, 2015, 08:59:58 PM »
Mikl__,

Fixed length string lengths are known at assembly time, they do not necessarily need runtime support, even though runtime support will deliver the same answer. The pre-processor in MASM is a bad mannered old pig but it is powerful.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

StarsInTheSky

  • Regular Member
  • *
  • Posts: 26
Re: small hello world again
« Reply #20 on: May 09, 2015, 06:50:07 AM »
Just an update on my struggle to understand #2 question...

First this Quickedit is a wonderful, I love the way you easily can change all menues :D

So I am using the makeit.bat option, inside my makeit.bat I put the following:
Code: [Select]
set PATH=%PATH%;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE;C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin
Set filepath=%1
For %%A in (%filepath%) do (
    Set Folder=%%~dpA
    Set fileName=%%~nxA
)

nmake Name=%filename%
pause

So I am getting the filename from Quickedit and I pass it to nmake. In my MAKEFILE I have:
Code: [Select]
$(NAME).exe: $(NAME).obj
        "\masm32\bin\poLink.exe /SUBSYSTEM:WINDOWS /LIBPATH:c:\masm32\lib /MERGE:.rdata=.text /MERGE:.data=.text /OPT:REF /OPT:NOWIN98 $(NAME).obj
$(NAME).obj: $(NAME).asm
        "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\ml.exe" /c /coff /Cp $(NAME).asm

But the libpath is not working for my code. The calls to MessageBoxA@16 and ExitProcess@4 gives me errors.
So not been succesful in replacing the /defaultlib:C:\masm32\lib\kernel32.lib /defaultlib:C:\masm32\lib\user32.lib yet.
I'm missing something...

EDIT: I got #2 working. Just by including the inc files and the libs, instead of the external declarations, the calls worked. i don't need to write it with the invoke syntax.  :icon_mrgreen: In fact Mikl has given me hints about this with the codes. I can use my external declarations with the libs and omit the includes if I like to. Thanks again Mikl  :t
« Last Edit: May 09, 2015, 09:13:31 AM by StarsInTheSky »

StarsInTheSky

  • Regular Member
  • *
  • Posts: 26
Re: small hello world again
« Reply #21 on: May 13, 2015, 08:36:47 AM »
For my particular question about #1, I have only found others having problem with the /ALIGN argument as well and no solution. Abandoning Link for polink, I got my hello world to 1K, and I've read that 1 K (1024 bytes) is the lowest you can get a working exe file on win 7 x64. Could someone confirm? Is there a theory behind that? Is it related to that the memory manager handles memory at 1K chunks in x64? I tried to run the upx packer on my 1K messagebox, and indeed it throws this error: upx: messageboxa2e.exe: NotCompressibleException

For learning PE I found this thread:
http://board.flatassembler.net/topic.php?t=1309

Is there a MASM syntax equivalent?


jj2007

  • Member
  • *****
  • Posts: 11529
  • Assembler is fun ;-)
    • MasmBasic
Re: small hello world again
« Reply #22 on: May 13, 2015, 09:25:42 AM »
I've read that 1 K (1024 bytes) is the lowest you can get a working exe file on win 7 x64. Could someone confirm?

You should not believe all that stuff that people post on the Internet. 929 bytes is possible  8)

StarsInTheSky

  • Regular Member
  • *
  • Posts: 26
Re: small hello world again
« Reply #23 on: May 13, 2015, 10:15:39 AM »
Hehe dos is dead, long live windows. the dos stub has become the data section  :greenclp: Thanks JJ


K_F

  • Member
  • *****
  • Posts: 1669
  • Anybody out there?
Re: small hello world again
« Reply #25 on: May 14, 2015, 08:38:46 AM »
Hey Stars.. sh.tload of stuff to go through...  :biggrin:

Don't worry, we all had to do the same.. takes a bit of time but you'll get there.
.. and when it happens.. there's no looking back !!  :t
'Sire, Sire!... the peasants are Revolting !!!'
'Yes, they are.. aren't they....'