News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

small hello world again

Started by StarsInTheSky, May 08, 2015, 12:34:45 PM

Previous topic - Next topic

sinsi

jj, the macros in fasm are much more complex.

; 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

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

; 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

Quote from: sinsi on May 08, 2015, 07:39:00 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__

jj2007,
and how you can know  the length of the string pointed to by the register ESI before compilation?

jj2007

Quote from: Mikl__ 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?

Mysteries of MASM :P

Run this with a debugger:
include \masm32\include\masm32rt.inc

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

hutch--

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.

StarsInTheSky

#20
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:

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:

$(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

StarsInTheSky

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

Quote from: StarsInTheSky on May 13, 2015, 08:36:47 AMI'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

Hehe dos is dead, long live windows. the dos stub has become the data section  :greenclp: Thanks JJ

Mikl__

#24
Hi, StarsInTheSky !
For learning PE see
  • http://www.cyberforum.ru/post3945127.html
  • http://www.cyberforum.ru/post3946211.html
  • http://www.cyberforum.ru/post4075465.html
Or Iczelion's PE Tutorials
or Microsoft Portable Executable and Common Object File Format Specification
or PE a windows executable walkthrough

K_F

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....'